Skip to content

Commit

Permalink
Mark Substitute.For<T> method as Pure (#844)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzliera authored Nov 30, 2024
1 parent 116db20 commit 72418ad
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/NSubstitute/Substitute.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.Contracts;
using NSubstitute.Core;

// Disable nullability for client API, so it does not affect clients.
Expand All @@ -6,18 +7,19 @@
namespace NSubstitute;

/// <summary>
/// Create a substitute for one or more types. For example: <c>Substitute.For&lt;ISomeType&gt;()</c>
/// Create a substitute for one or more types. For example: <c>Substitute.For&lt;ISomeType&gt;()</c>
/// </summary>
public static class Substitute
{
/// <summary>
/// Substitute for an interface or class.
/// <para>Be careful when specifying a class, as all non-virtual members will actually be executed. Only virtual members
/// <para>Be careful when specifying a class, as all non-virtual members will actually be executed. Only virtual members
/// can be recorded or have return values specified.</para>
/// </summary>
/// <typeparam name="T">The type of interface or class to substitute.</typeparam>
/// <param name="constructorArguments">Arguments required to construct a class being substituted. Not required for interfaces or classes with default constructors.</param>
/// <returns>A substitute for the interface or class.</returns>
[Pure]
public static T For<T>(params object[] constructorArguments)
where T : class
{
Expand All @@ -26,13 +28,14 @@ public static T For<T>(params object[] constructorArguments)

/// <summary>
/// <para>Substitute for multiple interfaces or a class that implements an interface. At most one class can be specified.</para>
/// <para>Be careful when specifying a class, as all non-virtual members will actually be executed. Only virtual members
/// <para>Be careful when specifying a class, as all non-virtual members will actually be executed. Only virtual members
/// can be recorded or have return values specified.</para>
/// </summary>
/// <typeparam name="T1">The type of interface or class to substitute.</typeparam>
/// <typeparam name="T2">An additional interface or class (maximum of one class) the substitute should implement.</typeparam>
/// <param name="constructorArguments">Arguments required to construct a class being substituted. Not required for interfaces or classes with default constructors.</param>
/// <returns>A substitute of type T1, that also implements T2.</returns>
[Pure]
public static T1 For<T1, T2>(params object[] constructorArguments)
where T1 : class
where T2 : class
Expand All @@ -43,14 +46,15 @@ public static T1 For<T1, T2>(params object[] constructorArguments)
/// <summary>
/// <para>Substitute for multiple interfaces or a class that implements multiple interfaces. At most one class can be specified.</para>
/// If additional interfaces are required use the <see cref="For(System.Type[], System.Object[])" /> overload.
/// <para>Be careful when specifying a class, as all non-virtual members will actually be executed. Only virtual members
/// <para>Be careful when specifying a class, as all non-virtual members will actually be executed. Only virtual members
/// can be recorded or have return values specified.</para>
/// </summary>
/// <typeparam name="T1">The type of interface or class to substitute.</typeparam>
/// <typeparam name="T2">An additional interface or class (maximum of one class) the substitute should implement.</typeparam>
/// <typeparam name="T3">An additional interface or class (maximum of one class) the substitute should implement.</typeparam>
/// <param name="constructorArguments">Arguments required to construct a class being substituted. Not required for interfaces or classes with default constructors.</param>
/// <returns>A substitute of type T1, that also implements T2 and T3.</returns>
[Pure]
public static T1 For<T1, T2, T3>(params object[] constructorArguments)
where T1 : class
where T2 : class
Expand All @@ -61,12 +65,13 @@ public static T1 For<T1, T2, T3>(params object[] constructorArguments)

/// <summary>
/// <para>Substitute for multiple interfaces or a class that implements multiple interfaces. At most one class can be specified.</para>
/// <para>Be careful when specifying a class, as all non-virtual members will actually be executed. Only virtual members
/// <para>Be careful when specifying a class, as all non-virtual members will actually be executed. Only virtual members
/// can be recorded or have return values specified.</para>
/// </summary>
/// <param name="typesToProxy">The types of interfaces or a type of class and multiple interfaces the substitute should implement.</param>
/// <param name="constructorArguments">Arguments required to construct a class being substituted. Not required for interfaces or classes with default constructors.</param>
/// <returns>A substitute implementing the specified types.</returns>
[Pure]
public static object For(Type[] typesToProxy, object[] constructorArguments)
{
var substituteFactory = SubstitutionContext.Current.SubstituteFactory;
Expand All @@ -81,8 +86,9 @@ public static object For(Type[] typesToProxy, object[] constructorArguments)
/// </summary>
/// <typeparam name="T">The type to substitute for parts of. Must be a class; not a delegate or interface.</typeparam>
/// <param name="constructorArguments"></param>
/// <returns>An instance of the class that will execute real methods when called, but allows parts to be selectively
/// <returns>An instance of the class that will execute real methods when called, but allows parts to be selectively
/// overridden via `Returns` and `When..DoNotCallBase`.</returns>
[Pure]
public static T ForPartsOf<T>(params object[] constructorArguments)
where T : class
{
Expand All @@ -93,16 +99,17 @@ public static T ForPartsOf<T>(params object[] constructorArguments)
/// <summary>
/// Creates a proxy for a class that implements an interface, forwarding methods and properties to an instance of the class, effectively mimicking a real instance.
/// Both the interface and the class must be provided as parameters.
/// The proxy will log calls made to the interface members and delegate them to an instance of the class. Specific members can be substituted
/// The proxy will log calls made to the interface members and delegate them to an instance of the class. Specific members can be substituted
/// by using <see cref="WhenCalled{T}.DoNotCallBase()">When(() => call).DoNotCallBase()</see> or by
/// <see cref="SubstituteExtensions.Returns{T}(T,T,T[])">setting a value to return value</see> for that member.
/// This extension supports sealed classes and non-virtual members, with some limitations. Since the substituted method is non-virtual, internal calls within the object will invoke the original implementation and will not be logged.
/// </summary>
/// <typeparam name="TInterface">The interface the substitute will implement.</typeparam>
/// <typeparam name="TClass">The class type implementing the interface. Must be a class; not a delegate or interface. </typeparam>
/// <param name="constructorArguments"></param>
/// <returns>An object implementing the selected interface. Calls will be forwarded to the actuall methods, but allows parts to be selectively
/// <returns>An object implementing the selected interface. Calls will be forwarded to the actuall methods, but allows parts to be selectively
/// overridden via `Returns` and `When..DoNotCallBase`.</returns>
[Pure]
public static TInterface ForTypeForwardingTo<TInterface, TClass>(params object[] constructorArguments)
where TInterface : class
{
Expand Down

0 comments on commit 72418ad

Please sign in to comment.