diff --git a/.editorconfig b/.editorconfig
index 0df0c455c..84059808e 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -17,6 +17,7 @@ dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion
+csharp_style_namespace_declarations=file_scoped:warning
# ReSharper properties
resharper_int_align_switch_expressions = true
diff --git a/src/NSubstitute/Arg.cs b/src/NSubstitute/Arg.cs
index 63716ca34..458499bf3 100644
--- a/src/NSubstitute/Arg.cs
+++ b/src/NSubstitute/Arg.cs
@@ -6,225 +6,224 @@
#pragma warning disable CS1574
#pragma warning disable CS0419
-namespace NSubstitute
+namespace NSubstitute;
+
+///
+/// Argument matchers used for specifying calls to substitutes.
+///
+public static class Arg
{
///
- /// Argument matchers used for specifying calls to substitutes.
+ /// This type can be used with any matcher to match a generic type parameter.
///
- public static class Arg
+ ///
+ /// If the generic type parameter has constraints, you will have to create a derived class/struct that
+ /// implements those constraints.
+ ///
+ public interface AnyType
{
- ///
- /// This type can be used with any matcher to match a generic type parameter.
- ///
- ///
- /// If the generic type parameter has constraints, you will have to create a derived class/struct that
- /// implements those constraints.
- ///
- public interface AnyType
- {
- }
+ }
+
+ ///
+ /// Match any argument value compatible with type .
+ ///
+ public static ref T Any()
+ {
+ return ref ArgumentMatcher.Enqueue(new AnyArgumentMatcher(typeof(T)));
+ }
+
+ ///
+ /// Match argument that is equal to .
+ ///
+ public static ref T Is(T value)
+ {
+ return ref ArgumentMatcher.Enqueue(new EqualsArgumentMatcher(value));
+ }
+
+ ///
+ /// Match argument that satisfies .
+ /// If the throws an exception for an argument it will be treated as non-matching.
+ ///
+ public static ref T Is(Expression> predicate)
+ {
+ return ref ArgumentMatcher.Enqueue(new ExpressionArgumentMatcher(predicate));
+ }
+
+ ///
+ /// Match argument that satisfies .
+ /// If the throws an exception for an argument it will be treated as non-matching.
+ ///
+ public static ref T Is(Expression> predicate) where T : AnyType
+ {
+ return ref ArgumentMatcher.Enqueue(new ExpressionArgumentMatcher(predicate));
+ }
+
+ ///
+ /// Invoke any argument whenever a matching call is made to the substitute.
+ ///
+ public static ref Action Invoke()
+ {
+ return ref ArgumentMatcher.Enqueue(new AnyArgumentMatcher(typeof(Action)), InvokeDelegateAction());
+ }
+ ///
+ /// Invoke any argument with specified argument whenever a matching call is made to the substitute.
+ ///
+ public static ref Action Invoke(T arg)
+ {
+ return ref ArgumentMatcher.Enqueue>(new AnyArgumentMatcher(typeof(Action)), InvokeDelegateAction(arg));
+ }
+
+ ///
+ /// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
+ ///
+ public static ref Action Invoke(T1 arg1, T2 arg2)
+ {
+ return ref ArgumentMatcher.Enqueue>(new AnyArgumentMatcher(typeof(Action)), InvokeDelegateAction(arg1, arg2));
+ }
+
+ ///
+ /// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
+ ///
+ public static ref Action Invoke(T1 arg1, T2 arg2, T3 arg3)
+ {
+ return ref ArgumentMatcher.Enqueue>(new AnyArgumentMatcher(typeof(Action)), InvokeDelegateAction(arg1, arg2, arg3));
+ }
+
+ ///
+ /// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
+ ///
+ public static ref Action Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
+ {
+ return ref ArgumentMatcher.Enqueue>(new AnyArgumentMatcher(typeof(Action)), InvokeDelegateAction(arg1, arg2, arg3, arg4));
+ }
+
+ ///
+ /// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
+ ///
+ /// Arguments to pass to delegate.
+ public static ref TDelegate InvokeDelegate(params object[] arguments)
+ {
+ return ref ArgumentMatcher.Enqueue(new AnyArgumentMatcher(typeof(TDelegate)), InvokeDelegateAction(arguments));
+ }
+
+ ///
+ /// Capture any argument compatible with type and use it to call the function
+ /// whenever a matching call is made to the substitute.
+ ///
+ public static ref T Do(Action useArgument)
+ {
+ return ref ArgumentMatcher.Enqueue(new AnyArgumentMatcher(typeof(T)), x => useArgument((T)x!));
+ }
+
+ ///
+ /// Capture any argument compatible with type and use it to call the function
+ /// whenever a matching call is made to the substitute.
+ ///
+ public static ref T Do(Action useArgument) where T : AnyType
+ {
+ return ref ArgumentMatcher.Enqueue(new AnyArgumentMatcher(typeof(AnyType)), x => useArgument(x!));
+ }
+
+ ///
+ /// Alternate version of matchers for compatibility with pre-C#7 compilers
+ /// which do not support ref return types. Do not use unless you are unable to use .
+ ///
+ /// For more information see Compatibility Argument
+ /// Matchers in the NSubstitute documentation.
+ ///
+ public static class Compat
+ {
///
/// Match any argument value compatible with type .
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
///
- public static ref T Any()
- {
- return ref ArgumentMatcher.Enqueue(new AnyArgumentMatcher(typeof(T)));
- }
+ public static T Any() => Arg.Any();
///
/// Match argument that is equal to .
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
///
- public static ref T Is(T value)
- {
- return ref ArgumentMatcher.Enqueue(new EqualsArgumentMatcher(value));
- }
+ public static T Is(T value) => Arg.Is(value);
///
/// Match argument that satisfies .
/// If the throws an exception for an argument it will be treated as non-matching.
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
///
- public static ref T Is(Expression> predicate)
- {
- return ref ArgumentMatcher.Enqueue(new ExpressionArgumentMatcher(predicate));
- }
+ public static T Is(Expression> predicate) => Arg.Is(predicate);
///
/// Match argument that satisfies .
/// If the throws an exception for an argument it will be treated as non-matching.
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
///
- public static ref T Is(Expression> predicate) where T : AnyType
- {
- return ref ArgumentMatcher.Enqueue(new ExpressionArgumentMatcher(predicate));
- }
+ public static AnyType Is(Expression> predicate) where T : AnyType => Arg.Is(predicate);
///
/// Invoke any argument whenever a matching call is made to the substitute.
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
///
- public static ref Action Invoke()
- {
- return ref ArgumentMatcher.Enqueue(new AnyArgumentMatcher(typeof(Action)), InvokeDelegateAction());
- }
+ public static Action Invoke() => Arg.Invoke();
///
/// Invoke any argument with specified argument whenever a matching call is made to the substitute.
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
///
- public static ref Action Invoke(T arg)
- {
- return ref ArgumentMatcher.Enqueue>(new AnyArgumentMatcher(typeof(Action)), InvokeDelegateAction(arg));
- }
+ public static Action Invoke(T arg) => Arg.Invoke(arg);
///
/// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
///
- public static ref Action Invoke(T1 arg1, T2 arg2)
- {
- return ref ArgumentMatcher.Enqueue>(new AnyArgumentMatcher(typeof(Action)), InvokeDelegateAction(arg1, arg2));
- }
+ public static Action Invoke(T1 arg1, T2 arg2) => Arg.Invoke(arg1, arg2);
///
/// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
///
- public static ref Action Invoke(T1 arg1, T2 arg2, T3 arg3)
- {
- return ref ArgumentMatcher.Enqueue>(new AnyArgumentMatcher(typeof(Action)), InvokeDelegateAction(arg1, arg2, arg3));
- }
+ public static Action Invoke(T1 arg1, T2 arg2, T3 arg3) => Arg.Invoke(arg1, arg2, arg3);
///
/// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
///
- public static ref Action Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
- {
- return ref ArgumentMatcher.Enqueue>(new AnyArgumentMatcher(typeof(Action)), InvokeDelegateAction(arg1, arg2, arg3, arg4));
- }
+ public static Action Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4) => Arg.Invoke(arg1, arg2, arg3, arg4);
///
/// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
///
/// Arguments to pass to delegate.
- public static ref TDelegate InvokeDelegate(params object[] arguments)
- {
- return ref ArgumentMatcher.Enqueue(new AnyArgumentMatcher(typeof(TDelegate)), InvokeDelegateAction(arguments));
- }
+ public static TDelegate InvokeDelegate(params object[] arguments) => Arg.InvokeDelegate(arguments);
///
/// Capture any argument compatible with type and use it to call the function
/// whenever a matching call is made to the substitute.
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
///
- public static ref T Do(Action useArgument)
- {
- return ref ArgumentMatcher.Enqueue(new AnyArgumentMatcher(typeof(T)), x => useArgument((T)x!));
- }
+ public static T Do(Action useArgument) => Arg.Do(useArgument);
///
/// Capture any argument compatible with type and use it to call the function
/// whenever a matching call is made to the substitute.
///
- public static ref T Do(Action useArgument) where T : AnyType
- {
- return ref ArgumentMatcher.Enqueue(new AnyArgumentMatcher(typeof(AnyType)), x => useArgument(x!));
- }
+ public static AnyType Do(Action useArgument) where T : AnyType => Arg.Do(useArgument);
+ }
- ///
- /// Alternate version of matchers for compatibility with pre-C#7 compilers
- /// which do not support ref return types. Do not use unless you are unable to use .
- ///
- /// For more information see Compatibility Argument
- /// Matchers in the NSubstitute documentation.
- ///
- public static class Compat
- {
- ///
- /// Match any argument value compatible with type .
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public static T Any() => Arg.Any();
-
- ///
- /// Match argument that is equal to .
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public static T Is(T value) => Arg.Is(value);
-
- ///
- /// Match argument that satisfies .
- /// If the throws an exception for an argument it will be treated as non-matching.
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public static T Is(Expression> predicate) => Arg.Is(predicate);
-
- ///
- /// Match argument that satisfies .
- /// If the throws an exception for an argument it will be treated as non-matching.
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public static AnyType Is(Expression> predicate) where T : AnyType => Arg.Is(predicate);
-
- ///
- /// Invoke any argument whenever a matching call is made to the substitute.
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public static Action Invoke() => Arg.Invoke();
-
- ///
- /// Invoke any argument with specified argument whenever a matching call is made to the substitute.
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public static Action Invoke(T arg) => Arg.Invoke(arg);
-
- ///
- /// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public static Action Invoke(T1 arg1, T2 arg2) => Arg.Invoke(arg1, arg2);
-
- ///
- /// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public static Action Invoke(T1 arg1, T2 arg2, T3 arg3) => Arg.Invoke(arg1, arg2, arg3);
-
- ///
- /// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public static Action Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4) => Arg.Invoke(arg1, arg2, arg3, arg4);
-
- ///
- /// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- /// Arguments to pass to delegate.
- public static TDelegate InvokeDelegate(params object[] arguments) => Arg.InvokeDelegate(arguments);
-
- ///
- /// Capture any argument compatible with type and use it to call the function
- /// whenever a matching call is made to the substitute.
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public static T Do(Action useArgument) => Arg.Do(useArgument);
-
- ///
- /// Capture any argument compatible with type and use it to call the function
- /// whenever a matching call is made to the substitute.
- ///
- public static AnyType Do(Action useArgument) where T : AnyType => Arg.Do(useArgument);
- }
-
- private static Action InvokeDelegateAction(params object[] arguments)
- {
- return x => ((Delegate)x).DynamicInvoke(arguments);
- }
+ private static Action InvokeDelegateAction(params object[] arguments)
+ {
+ return x => ((Delegate)x).DynamicInvoke(arguments);
}
}
diff --git a/src/NSubstitute/Callback.cs b/src/NSubstitute/Callback.cs
index b7d5e0cc6..22a08901c 100644
--- a/src/NSubstitute/Callback.cs
+++ b/src/NSubstitute/Callback.cs
@@ -1,127 +1,126 @@
-using System.Collections.Concurrent;
-using NSubstitute.Callbacks;
+using NSubstitute.Callbacks;
using NSubstitute.Core;
+using System.Collections.Concurrent;
// Disable nullability for client API, so it does not affect clients.
#nullable disable annotations
-namespace NSubstitute
+namespace NSubstitute;
+
+///
+/// Perform this chain of callbacks and/or always callback when called.
+///
+public class Callback
{
///
- /// Perform this chain of callbacks and/or always callback when called.
+ /// Perform as first in chain of callback when called.
///
- public class Callback
+ ///
+ ///
+ public static ConfiguredCallback First(Action doThis)
{
- ///
- /// Perform as first in chain of callback when called.
- ///
- ///
- ///
- public static ConfiguredCallback First(Action doThis)
- {
- return new ConfiguredCallback().Then(doThis);
- }
+ return new ConfiguredCallback().Then(doThis);
+ }
- ///
- /// Perform this action always when callback is called.
- ///
- ///
- ///
- public static Callback Always(Action doThis)
- {
- return new ConfiguredCallback().AndAlways(doThis);
- }
+ ///
+ /// Perform this action always when callback is called.
+ ///
+ ///
+ ///
+ public static Callback Always(Action doThis)
+ {
+ return new ConfiguredCallback().AndAlways(doThis);
+ }
- ///
- /// Throw exception returned by function as first callback in chain of callback when called.
- ///
- ///
- ///
- public static ConfiguredCallback FirstThrow(Func throwThis) where TException : Exception
- {
- return new ConfiguredCallback().ThenThrow(throwThis);
- }
+ ///
+ /// Throw exception returned by function as first callback in chain of callback when called.
+ ///
+ ///
+ ///
+ public static ConfiguredCallback FirstThrow(Func throwThis) where TException : Exception
+ {
+ return new ConfiguredCallback().ThenThrow(throwThis);
+ }
- ///
- /// Throw this exception as first callback in chain of callback when called.
- ///
- ///
- ///
- public static ConfiguredCallback FirstThrow(TException exception) where TException : Exception
- {
- return new ConfiguredCallback().ThenThrow(info => exception);
- }
+ ///
+ /// Throw this exception as first callback in chain of callback when called.
+ ///
+ ///
+ ///
+ public static ConfiguredCallback FirstThrow(TException exception) where TException : Exception
+ {
+ return new ConfiguredCallback().ThenThrow(info => exception);
+ }
- ///
- /// Throw exception returned by function always when callback is called.
- ///
- /// The type of the exception.
- /// The throw this.
- ///
- public static Callback AlwaysThrow(Func throwThis) where TException : Exception
- {
- return new ConfiguredCallback().AndAlways(ToCallback(throwThis));
- }
+ ///
+ /// Throw exception returned by function always when callback is called.
+ ///
+ /// The type of the exception.
+ /// The throw this.
+ ///
+ public static Callback AlwaysThrow(Func throwThis) where TException : Exception
+ {
+ return new ConfiguredCallback().AndAlways(ToCallback(throwThis));
+ }
- ///
- /// Throw this exception always when callback is called.
- ///
- /// The type of the exception.
- /// The exception.
- ///
- public static Callback AlwaysThrow(TException exception) where TException : Exception
- {
- return AlwaysThrow(_ => exception);
- }
+ ///
+ /// Throw this exception always when callback is called.
+ ///
+ /// The type of the exception.
+ /// The exception.
+ ///
+ public static Callback AlwaysThrow(TException exception) where TException : Exception
+ {
+ return AlwaysThrow(_ => exception);
+ }
- protected static Action ToCallback(Func throwThis)
- where TException : notnull, Exception
- {
- return ci => { if (throwThis != null) throw throwThis(ci); };
- }
+ protected static Action ToCallback(Func throwThis)
+ where TException : notnull, Exception
+ {
+ return ci => { if (throwThis != null) throw throwThis(ci); };
+ }
- internal Callback() { }
- private readonly ConcurrentQueue> callbackQueue = new ConcurrentQueue>();
- private Action alwaysDo = x => { };
- private Action keepDoing = x => { };
+ internal Callback() { }
+ private readonly ConcurrentQueue> callbackQueue = new ConcurrentQueue>();
+ private Action alwaysDo = x => { };
+ private Action keepDoing = x => { };
- protected void AddCallback(Action doThis)
- {
- callbackQueue.Enqueue(doThis);
- }
+ protected void AddCallback(Action doThis)
+ {
+ callbackQueue.Enqueue(doThis);
+ }
- protected void SetAlwaysDo(Action always)
+ protected void SetAlwaysDo(Action always)
+ {
+ alwaysDo = always ?? (_ => { });
+ }
+
+ protected void SetKeepDoing(Action keep)
+ {
+ keepDoing = keep ?? (_ => { });
+ }
+
+ public void Call(CallInfo callInfo)
+ {
+ try
{
- alwaysDo = always ?? (_ => { });
+ CallFromStack(callInfo);
}
-
- protected void SetKeepDoing(Action keep)
+ finally
{
- keepDoing = keep ?? (_ => { });
+ alwaysDo(callInfo);
}
+ }
- public void Call(CallInfo callInfo)
+ private void CallFromStack(CallInfo callInfo)
+ {
+ if (callbackQueue.TryDequeue(out var callback))
{
- try
- {
- CallFromStack(callInfo);
- }
- finally
- {
- alwaysDo(callInfo);
- }
+ callback(callInfo);
}
-
- private void CallFromStack(CallInfo callInfo)
+ else
{
- if (callbackQueue.TryDequeue(out var callback))
- {
- callback(callInfo);
- }
- else
- {
- keepDoing(callInfo);
- }
+ keepDoing(callInfo);
}
}
}
\ No newline at end of file
diff --git a/src/NSubstitute/Callbacks/ConfiguredCallback.cs b/src/NSubstitute/Callbacks/ConfiguredCallback.cs
index 2ae2c0019..b017af2d8 100644
--- a/src/NSubstitute/Callbacks/ConfiguredCallback.cs
+++ b/src/NSubstitute/Callbacks/ConfiguredCallback.cs
@@ -3,74 +3,73 @@
// Disable nullability for client API, so it does not affect clients.
#nullable disable annotations
-namespace NSubstitute.Callbacks
+namespace NSubstitute.Callbacks;
+
+public class ConfiguredCallback : EndCallbackChain
{
- public class ConfiguredCallback : EndCallbackChain
+ internal ConfiguredCallback() { }
+
+ ///
+ /// Perform this action once in chain of called callbacks.
+ ///
+ public ConfiguredCallback Then(Action doThis)
{
- internal ConfiguredCallback() { }
+ AddCallback(doThis);
+ return this;
+ }
- ///
- /// Perform this action once in chain of called callbacks.
- ///
- public ConfiguredCallback Then(Action doThis)
- {
- AddCallback(doThis);
- return this;
- }
+ ///
+ /// Keep doing this action after the other callbacks have run.
+ ///
+ public EndCallbackChain ThenKeepDoing(Action doThis)
+ {
+ SetKeepDoing(doThis);
+ return this;
+ }
- ///
- /// Keep doing this action after the other callbacks have run.
- ///
- public EndCallbackChain ThenKeepDoing(Action doThis)
- {
- SetKeepDoing(doThis);
- return this;
- }
+ ///
+ /// Keep throwing this exception after the other callbacks have run.
+ ///
+ public EndCallbackChain ThenKeepThrowing(Func throwThis) where TException : Exception =>
+ ThenKeepDoing(ToCallback(throwThis));
- ///
- /// Keep throwing this exception after the other callbacks have run.
- ///
- public EndCallbackChain ThenKeepThrowing(Func throwThis) where TException : Exception =>
- ThenKeepDoing(ToCallback(throwThis));
+ ///
+ /// Keep throwing this exception after the other callbacks have run.
+ ///
+ public EndCallbackChain ThenKeepThrowing(TException throwThis) where TException : Exception =>
+ ThenKeepThrowing(info => throwThis);
- ///
- /// Keep throwing this exception after the other callbacks have run.
- ///
- public EndCallbackChain ThenKeepThrowing(TException throwThis) where TException : Exception =>
- ThenKeepThrowing(info => throwThis);
+ ///
+ /// Throw exception returned by function once when called in a chain of callbacks.
+ ///
+ /// The type of the exception
+ /// Produce the exception to throw for a CallInfo
+ public ConfiguredCallback ThenThrow(Func throwThis) where TException : Exception
+ {
+ AddCallback(ToCallback(throwThis));
+ return this;
+ }
- ///
- /// Throw exception returned by function once when called in a chain of callbacks.
- ///
- /// The type of the exception
- /// Produce the exception to throw for a CallInfo
- public ConfiguredCallback ThenThrow(Func throwThis) where TException : Exception
- {
- AddCallback(ToCallback(throwThis));
- return this;
- }
+ ///
+ /// Throw this exception once when called in a chain of callbacks.
+ ///
+ /// The type of the exception
+ /// The exception to throw
+ public ConfiguredCallback ThenThrow(TException exception) where TException : Exception =>
+ ThenThrow(_ => exception);
+}
- ///
- /// Throw this exception once when called in a chain of callbacks.
- ///
- /// The type of the exception
- /// The exception to throw
- public ConfiguredCallback ThenThrow(TException exception) where TException : Exception =>
- ThenThrow(_ => exception);
- }
+public class EndCallbackChain : Callback
+{
+ internal EndCallbackChain() { }
- public class EndCallbackChain : Callback
+ ///
+ /// Perform the given action for every call.
+ ///
+ /// The action to perform for every call
+ public Callback AndAlways(Action doThis)
{
- internal EndCallbackChain() { }
-
- ///
- /// Perform the given action for every call.
- ///
- /// The action to perform for every call
- public Callback AndAlways(Action doThis)
- {
- SetAlwaysDo(doThis);
- return this;
- }
+ SetAlwaysDo(doThis);
+ return this;
}
}
\ No newline at end of file
diff --git a/src/NSubstitute/ClearOptions.cs b/src/NSubstitute/ClearOptions.cs
index 3bc1e7ac8..55cd3b397 100644
--- a/src/NSubstitute/ClearOptions.cs
+++ b/src/NSubstitute/ClearOptions.cs
@@ -1,26 +1,25 @@
-namespace NSubstitute
+namespace NSubstitute;
+
+[Flags]
+public enum ClearOptions
{
- [Flags]
- public enum ClearOptions
- {
- ///
- /// Clear all the received calls
- ///
- ReceivedCalls = 1,
+ ///
+ /// Clear all the received calls
+ ///
+ ReceivedCalls = 1,
- ///
- /// Clear all configured return results (including auto-substituted values).
- ///
- ReturnValues = 2,
+ ///
+ /// Clear all configured return results (including auto-substituted values).
+ ///
+ ReturnValues = 2,
- ///
- /// Clear all call actions configured for this substitute (via When..Do, Arg.Invoke, and Arg.Do)
- ///
- CallActions = 4,
+ ///
+ /// Clear all call actions configured for this substitute (via When..Do, Arg.Invoke, and Arg.Do)
+ ///
+ CallActions = 4,
- ///
- /// Clears all received calls and configured return values and callbacks.
- ///
- All = ReceivedCalls | ReturnValues | CallActions
- }
+ ///
+ /// Clears all received calls and configured return values and callbacks.
+ ///
+ All = ReceivedCalls | ReturnValues | CallActions
}
\ No newline at end of file
diff --git a/src/NSubstitute/Compatibility/CompatArg.cs b/src/NSubstitute/Compatibility/CompatArg.cs
index 4ea2cc272..7d1a84f41 100644
--- a/src/NSubstitute/Compatibility/CompatArg.cs
+++ b/src/NSubstitute/Compatibility/CompatArg.cs
@@ -5,113 +5,112 @@
#pragma warning disable CS1574
#pragma warning disable CS0419
-namespace NSubstitute.Compatibility
+namespace NSubstitute.Compatibility;
+
+///
+/// Alternate version of matchers for compatibility with pre-C#7 compilers
+/// which do not support ref return types. Do not use unless you are unable to use .
+///
+/// provides a non-static version of , which can make it easier
+/// to use from an abstract base class. You can get a reference to this instance using the static
+/// field.
+///
+/// For more information see Compatibility Argument
+/// Matchers in the NSubstitute documentation.
+///
+public class CompatArg
{
+ private CompatArg() { }
+
+ ///
+ /// Get the CompatArg instance.
+ ///
+ public static readonly CompatArg Instance = new CompatArg();
+
+ ///
+ /// Match any argument value compatible with type .
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
+ ///
+ public T Any() => Arg.Any();
+
+ ///
+ /// Match argument that is equal to .
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
+ ///
+ public T Is(T value) => Arg.Is(value);
+
+ ///
+ /// Match argument that satisfies .
+ /// If the throws an exception for an argument it will be treated as non-matching.
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
+ ///
+ public T Is(Expression> predicate) => Arg.Is(predicate);
+
+ ///
+ /// Match argument that satisfies .
+ /// If the throws an exception for an argument it will be treated as non-matching.
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
+ ///
+ public Arg.AnyType Is(Expression> predicate) where T : Arg.AnyType => Arg.Is(predicate);
+
+ ///
+ /// Invoke any argument whenever a matching call is made to the substitute.
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
+ ///
+ public Action Invoke() => Arg.Invoke();
+
+ ///
+ /// Invoke any argument with specified argument whenever a matching call is made to the substitute.
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
+ ///
+ public Action Invoke(T arg) => Arg.Invoke(arg);
+
+ ///
+ /// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
+ ///
+ public Action Invoke(T1 arg1, T2 arg2) => Arg.Invoke(arg1, arg2);
+
+ ///
+ /// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
+ ///
+ public Action Invoke(T1 arg1, T2 arg2, T3 arg3) => Arg.Invoke(arg1, arg2, arg3);
+
+ ///
+ /// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
+ ///
+ public Action Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4) => Arg.Invoke(arg1, arg2, arg3, arg4);
+
+ ///
+ /// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
+ ///
+ /// Arguments to pass to delegate.
+ public TDelegate InvokeDelegate(params object[] arguments) => Arg.InvokeDelegate(arguments);
+
+ ///
+ /// Capture any argument compatible with type and use it to call the function
+ /// whenever a matching call is made to the substitute.
+ /// This is provided for compatibility with older compilers --
+ /// if possible use instead.
+ ///
+ public T Do(Action useArgument) => Arg.Do(useArgument);
+
///
- /// Alternate version of matchers for compatibility with pre-C#7 compilers
- /// which do not support ref return types. Do not use unless you are unable to use .
- ///
- /// provides a non-static version of , which can make it easier
- /// to use from an abstract base class. You can get a reference to this instance using the static
- /// field.
- ///
- /// For more information see Compatibility Argument
- /// Matchers in the NSubstitute documentation.
+ /// Capture any argument compatible with type and use it to call the function
+ /// whenever a matching call is made to the substitute.
///
- public class CompatArg
- {
- private CompatArg() { }
-
- ///
- /// Get the CompatArg instance.
- ///
- public static readonly CompatArg Instance = new CompatArg();
-
- ///
- /// Match any argument value compatible with type .
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public T Any() => Arg.Any();
-
- ///
- /// Match argument that is equal to .
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public T Is(T value) => Arg.Is(value);
-
- ///
- /// Match argument that satisfies .
- /// If the throws an exception for an argument it will be treated as non-matching.
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public T Is(Expression> predicate) => Arg.Is(predicate);
-
- ///
- /// Match argument that satisfies .
- /// If the throws an exception for an argument it will be treated as non-matching.
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public Arg.AnyType Is(Expression> predicate) where T : Arg.AnyType => Arg.Is(predicate);
-
- ///
- /// Invoke any argument whenever a matching call is made to the substitute.
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public Action Invoke() => Arg.Invoke();
-
- ///
- /// Invoke any argument with specified argument whenever a matching call is made to the substitute.
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public Action Invoke(T arg) => Arg.Invoke(arg);
-
- ///
- /// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public Action Invoke(T1 arg1, T2 arg2) => Arg.Invoke(arg1, arg2);
-
- ///
- /// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public Action Invoke(T1 arg1, T2 arg2, T3 arg3) => Arg.Invoke(arg1, arg2, arg3);
-
- ///
- /// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public Action Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4) => Arg.Invoke(arg1, arg2, arg3, arg4);
-
- ///
- /// Invoke any argument with specified arguments whenever a matching call is made to the substitute.
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- /// Arguments to pass to delegate.
- public TDelegate InvokeDelegate(params object[] arguments) => Arg.InvokeDelegate(arguments);
-
- ///
- /// Capture any argument compatible with type and use it to call the function
- /// whenever a matching call is made to the substitute.
- /// This is provided for compatibility with older compilers --
- /// if possible use instead.
- ///
- public T Do(Action useArgument) => Arg.Do(useArgument);
-
- ///
- /// Capture any argument compatible with type and use it to call the function
- /// whenever a matching call is made to the substitute.
- ///
- public static Arg.AnyType Do(Action useArgument) where T : Arg.AnyType => Arg.Do(useArgument);
- }
+ public static Arg.AnyType Do(Action useArgument) where T : Arg.AnyType => Arg.Do(useArgument);
}
diff --git a/src/NSubstitute/Compatibility/DiagnosticsNullabilityAttributes.cs b/src/NSubstitute/Compatibility/DiagnosticsNullabilityAttributes.cs
index fbe8ff36a..a016b1f35 100644
--- a/src/NSubstitute/Compatibility/DiagnosticsNullabilityAttributes.cs
+++ b/src/NSubstitute/Compatibility/DiagnosticsNullabilityAttributes.cs
@@ -2,141 +2,140 @@
// This was copied from https://github.com/dotnet/runtime/blob/39b9607807f29e48cae4652cd74735182b31182e/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs
// and updated to have the scope of the attributes be internal.
-namespace System.Diagnostics.CodeAnalysis
-{
+namespace System.Diagnostics.CodeAnalysis;
- /// Specifies that null is allowed as an input even if the corresponding type disallows it.
- [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
- internal sealed class AllowNullAttribute : Attribute { }
- /// Specifies that null is disallowed as an input even if the corresponding type allows it.
- [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
- internal sealed class DisallowNullAttribute : Attribute { }
+/// Specifies that null is allowed as an input even if the corresponding type disallows it.
+[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
+internal sealed class AllowNullAttribute : Attribute { }
- /// Specifies that an output may be null even if the corresponding type disallows it.
- [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
- internal sealed class MaybeNullAttribute : Attribute { }
+/// Specifies that null is disallowed as an input even if the corresponding type allows it.
+[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
+internal sealed class DisallowNullAttribute : Attribute { }
- /// Specifies that an output will not be null even if the corresponding type allows it.
- [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
- internal sealed class NotNullAttribute : Attribute { }
+/// Specifies that an output may be null even if the corresponding type disallows it.
+[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
+internal sealed class MaybeNullAttribute : Attribute { }
- /// Specifies that when a method returns , the parameter may be null even if the corresponding type disallows it.
- [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
- internal sealed class MaybeNullWhenAttribute : Attribute
- {
- /// Initializes the attribute with the specified return value condition.
- ///
- /// The return value condition. If the method returns this value, the associated parameter may be null.
- ///
- public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
-
- /// Gets the return value condition.
- public bool ReturnValue { get; }
- }
+/// Specifies that an output will not be null even if the corresponding type allows it.
+[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
+internal sealed class NotNullAttribute : Attribute { }
- /// Specifies that when a method returns , the parameter will not be null even if the corresponding type allows it.
- [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
- internal sealed class NotNullWhenAttribute : Attribute
- {
- /// Initializes the attribute with the specified return value condition.
- ///
- /// The return value condition. If the method returns this value, the associated parameter will not be null.
- ///
- public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
-
- /// Gets the return value condition.
- public bool ReturnValue { get; }
- }
+/// Specifies that when a method returns , the parameter may be null even if the corresponding type disallows it.
+[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
+internal sealed class MaybeNullWhenAttribute : Attribute
+{
+ /// Initializes the attribute with the specified return value condition.
+ ///
+ /// The return value condition. If the method returns this value, the associated parameter may be null.
+ ///
+ public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
+
+ /// Gets the return value condition.
+ public bool ReturnValue { get; }
+}
- /// Specifies that the output will be non-null if the named parameter is non-null.
- [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
- internal sealed class NotNullIfNotNullAttribute : Attribute
- {
- /// Initializes the attribute with the associated parameter name.
- ///
- /// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.
- ///
- public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName;
-
- /// Gets the associated parameter name.
- public string ParameterName { get; }
- }
+/// Specifies that when a method returns , the parameter will not be null even if the corresponding type allows it.
+[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
+internal sealed class NotNullWhenAttribute : Attribute
+{
+ /// Initializes the attribute with the specified return value condition.
+ ///
+ /// The return value condition. If the method returns this value, the associated parameter will not be null.
+ ///
+ public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
+
+ /// Gets the return value condition.
+ public bool ReturnValue { get; }
+}
+
+/// Specifies that the output will be non-null if the named parameter is non-null.
+[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
+internal sealed class NotNullIfNotNullAttribute : Attribute
+{
+ /// Initializes the attribute with the associated parameter name.
+ ///
+ /// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.
+ ///
+ public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName;
+
+ /// Gets the associated parameter name.
+ public string ParameterName { get; }
+}
- /// Applied to a method that will never return under any circumstance.
- [AttributeUsage(AttributeTargets.Method, Inherited = false)]
- internal sealed class DoesNotReturnAttribute : Attribute { }
+/// Applied to a method that will never return under any circumstance.
+[AttributeUsage(AttributeTargets.Method, Inherited = false)]
+internal sealed class DoesNotReturnAttribute : Attribute { }
- /// Specifies that the method will not return if the associated Boolean parameter is passed the specified value.
- [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
- internal sealed class DoesNotReturnIfAttribute : Attribute
- {
- /// Initializes the attribute with the specified parameter value.
- ///
- /// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to
- /// the associated parameter matches this value.
- ///
- public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue;
-
- /// Gets the condition parameter value.
- public bool ParameterValue { get; }
- }
+/// Specifies that the method will not return if the associated Boolean parameter is passed the specified value.
+[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
+internal sealed class DoesNotReturnIfAttribute : Attribute
+{
+ /// Initializes the attribute with the specified parameter value.
+ ///
+ /// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to
+ /// the associated parameter matches this value.
+ ///
+ public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue;
+
+ /// Gets the condition parameter value.
+ public bool ParameterValue { get; }
+}
- /// Specifies that the method or property will ensure that the listed field and property members have not-null values.
- [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
- internal sealed class MemberNotNullAttribute : Attribute
+/// Specifies that the method or property will ensure that the listed field and property members have not-null values.
+[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
+internal sealed class MemberNotNullAttribute : Attribute
+{
+ /// Initializes the attribute with a field or property member.
+ ///
+ /// The field or property member that is promised to be not-null.
+ ///
+ public MemberNotNullAttribute(string member) => Members = new[] { member };
+
+ /// Initializes the attribute with the list of field and property members.
+ ///
+ /// The list of field and property members that are promised to be not-null.
+ ///
+ public MemberNotNullAttribute(params string[] members) => Members = members;
+
+ /// Gets field or property member names.
+ public string[] Members { get; }
+}
+
+/// Specifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition.
+[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
+internal sealed class MemberNotNullWhenAttribute : Attribute
+{
+ /// Initializes the attribute with the specified return value condition and a field or property member.
+ ///
+ /// The return value condition. If the method returns this value, the associated parameter will not be null.
+ ///
+ ///
+ /// The field or property member that is promised to be not-null.
+ ///
+ public MemberNotNullWhenAttribute(bool returnValue, string member)
{
- /// Initializes the attribute with a field or property member.
- ///
- /// The field or property member that is promised to be not-null.
- ///
- public MemberNotNullAttribute(string member) => Members = new[] { member };
-
- /// Initializes the attribute with the list of field and property members.
- ///
- /// The list of field and property members that are promised to be not-null.
- ///
- public MemberNotNullAttribute(params string[] members) => Members = members;
-
- /// Gets field or property member names.
- public string[] Members { get; }
+ ReturnValue = returnValue;
+ Members = new[] { member };
}
- /// Specifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition.
- [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
- internal sealed class MemberNotNullWhenAttribute : Attribute
+ /// Initializes the attribute with the specified return value condition and list of field and property members.
+ ///
+ /// The return value condition. If the method returns this value, the associated parameter will not be null.
+ ///
+ ///
+ /// The list of field and property members that are promised to be not-null.
+ ///
+ public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
{
- /// Initializes the attribute with the specified return value condition and a field or property member.
- ///
- /// The return value condition. If the method returns this value, the associated parameter will not be null.
- ///
- ///
- /// The field or property member that is promised to be not-null.
- ///
- public MemberNotNullWhenAttribute(bool returnValue, string member)
- {
- ReturnValue = returnValue;
- Members = new[] { member };
- }
-
- /// Initializes the attribute with the specified return value condition and list of field and property members.
- ///
- /// The return value condition. If the method returns this value, the associated parameter will not be null.
- ///
- ///
- /// The list of field and property members that are promised to be not-null.
- ///
- public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
- {
- ReturnValue = returnValue;
- Members = members;
- }
-
- /// Gets the return value condition.
- public bool ReturnValue { get; }
-
- /// Gets field or property member names.
- public string[] Members { get; }
+ ReturnValue = returnValue;
+ Members = members;
}
+
+ /// Gets the return value condition.
+ public bool ReturnValue { get; }
+
+ /// Gets field or property member names.
+ public string[] Members { get; }
}
#endif
diff --git a/src/NSubstitute/Core/Argument.cs b/src/NSubstitute/Core/Argument.cs
index b8d594cc5..a2709736f 100644
--- a/src/NSubstitute/Core/Argument.cs
+++ b/src/NSubstitute/Core/Argument.cs
@@ -1,60 +1,59 @@
-namespace NSubstitute.Core
+namespace NSubstitute.Core;
+
+public class Argument
{
- public class Argument
- {
- private readonly ICall? _call;
- private readonly int _argIndex;
+ private readonly ICall? _call;
+ private readonly int _argIndex;
- private readonly Type? _declaredType;
- private readonly Func? _getValue;
- private readonly Action? _setValue;
+ private readonly Type? _declaredType;
+ private readonly Func? _getValue;
+ private readonly Action? _setValue;
- [Obsolete("This constructor overload is deprecated and will be removed in the next version.")]
- public Argument(Type declaredType, Func getValue, Action setValue)
- {
- _declaredType = declaredType;
- _getValue = getValue;
- _setValue = setValue;
- }
+ [Obsolete("This constructor overload is deprecated and will be removed in the next version.")]
+ public Argument(Type declaredType, Func getValue, Action setValue)
+ {
+ _declaredType = declaredType;
+ _getValue = getValue;
+ _setValue = setValue;
+ }
- public Argument(ICall call, int argIndex)
- {
- _call = call;
- _argIndex = argIndex;
- }
+ public Argument(ICall call, int argIndex)
+ {
+ _call = call;
+ _argIndex = argIndex;
+ }
- public object? Value
+ public object? Value
+ {
+ get => _getValue != null ? _getValue() : _call!.GetArguments()[_argIndex];
+ set
{
- get => _getValue != null ? _getValue() : _call!.GetArguments()[_argIndex];
- set
+ if (_setValue != null)
{
- if (_setValue != null)
- {
- _setValue(value);
- }
- else
- {
- _call!.GetArguments()[_argIndex] = value;
- }
+ _setValue(value);
+ }
+ else
+ {
+ _call!.GetArguments()[_argIndex] = value;
}
}
+ }
- public bool IsByRef => DeclaredType.IsByRef;
+ public bool IsByRef => DeclaredType.IsByRef;
- public Type DeclaredType => _declaredType ?? _call!.GetParameterInfos()[_argIndex].ParameterType;
+ public Type DeclaredType => _declaredType ?? _call!.GetParameterInfos()[_argIndex].ParameterType;
- public Type ActualType => Value == null ? DeclaredType : Value.GetType();
+ public Type ActualType => Value == null ? DeclaredType : Value.GetType();
- public bool IsDeclaredTypeEqualToOrByRefVersionOf(Type type) =>
- AsNonByRefType(DeclaredType) == type;
+ public bool IsDeclaredTypeEqualToOrByRefVersionOf(Type type) =>
+ AsNonByRefType(DeclaredType) == type;
- public bool IsValueAssignableTo(Type type) =>
- type.IsAssignableFrom(AsNonByRefType(ActualType));
+ public bool IsValueAssignableTo(Type type) =>
+ type.IsAssignableFrom(AsNonByRefType(ActualType));
- public bool CanSetValueWithInstanceOf(Type type) =>
- AsNonByRefType(DeclaredType).IsAssignableFrom(type);
+ public bool CanSetValueWithInstanceOf(Type type) =>
+ AsNonByRefType(DeclaredType).IsAssignableFrom(type);
- private static Type AsNonByRefType(Type type) =>
- type.IsByRef ? type.GetElementType()! : type;
- }
+ private static Type AsNonByRefType(Type type) =>
+ type.IsByRef ? type.GetElementType()! : type;
}
\ No newline at end of file
diff --git a/src/NSubstitute/Core/ArgumentSpecificationDequeue.cs b/src/NSubstitute/Core/ArgumentSpecificationDequeue.cs
index 4ce507c64..0c251d6d3 100644
--- a/src/NSubstitute/Core/ArgumentSpecificationDequeue.cs
+++ b/src/NSubstitute/Core/ArgumentSpecificationDequeue.cs
@@ -1,36 +1,35 @@
using System.Reflection;
using NSubstitute.Core.Arguments;
-namespace NSubstitute.Core
+namespace NSubstitute.Core;
+
+public class ArgumentSpecificationDequeue : IArgumentSpecificationDequeue
{
- public class ArgumentSpecificationDequeue : IArgumentSpecificationDequeue
- {
- private static readonly IArgumentSpecification[] EmptySpecifications = new IArgumentSpecification[0];
+ private static readonly IArgumentSpecification[] EmptySpecifications = new IArgumentSpecification[0];
- private readonly Func> _dequeueAllQueuedArgSpecs;
+ private readonly Func> _dequeueAllQueuedArgSpecs;
- public ArgumentSpecificationDequeue(Func> dequeueAllQueuedArgSpecs)
- {
- _dequeueAllQueuedArgSpecs = dequeueAllQueuedArgSpecs;
- }
+ public ArgumentSpecificationDequeue(Func> dequeueAllQueuedArgSpecs)
+ {
+ _dequeueAllQueuedArgSpecs = dequeueAllQueuedArgSpecs;
+ }
- public IList DequeueAllArgumentSpecificationsForMethod(int parametersCount)
+ public IList DequeueAllArgumentSpecificationsForMethod(int parametersCount)
+ {
+ if (parametersCount == 0)
{
- if (parametersCount == 0)
- {
- // We violate public contract, as mutable list was expected as result.
- // However, in reality we never expect value to be mutated, so this optimization is fine.
- // We are not allowed to change public contract due to SemVer, so keeping that as it is.
- return EmptySpecifications;
- }
-
- var queuedArgSpecifications = _dequeueAllQueuedArgSpecs.Invoke();
- return queuedArgSpecifications;
+ // We violate public contract, as mutable list was expected as result.
+ // However, in reality we never expect value to be mutated, so this optimization is fine.
+ // We are not allowed to change public contract due to SemVer, so keeping that as it is.
+ return EmptySpecifications;
}
- public IList DequeueAllArgumentSpecificationsForMethod(MethodInfo methodInfo)
- {
- return DequeueAllArgumentSpecificationsForMethod(methodInfo.GetParameters().Length);
- }
+ var queuedArgSpecifications = _dequeueAllQueuedArgSpecs.Invoke();
+ return queuedArgSpecifications;
+ }
+
+ public IList DequeueAllArgumentSpecificationsForMethod(MethodInfo methodInfo)
+ {
+ return DequeueAllArgumentSpecificationsForMethod(methodInfo.GetParameters().Length);
}
}
\ No newline at end of file
diff --git a/src/NSubstitute/Core/Arguments/AnyArgumentMatcher.cs b/src/NSubstitute/Core/Arguments/AnyArgumentMatcher.cs
index 903331327..2f46784c2 100644
--- a/src/NSubstitute/Core/Arguments/AnyArgumentMatcher.cs
+++ b/src/NSubstitute/Core/Arguments/AnyArgumentMatcher.cs
@@ -1,16 +1,15 @@
-namespace NSubstitute.Core.Arguments
+namespace NSubstitute.Core.Arguments;
+
+public class AnyArgumentMatcher : IArgumentMatcher
{
- public class AnyArgumentMatcher : IArgumentMatcher
- {
- private readonly Type _typeArgMustBeCompatibleWith;
+ private readonly Type _typeArgMustBeCompatibleWith;
- public AnyArgumentMatcher(Type typeArgMustBeCompatibleWith)
- {
- _typeArgMustBeCompatibleWith = typeArgMustBeCompatibleWith;
- }
+ public AnyArgumentMatcher(Type typeArgMustBeCompatibleWith)
+ {
+ _typeArgMustBeCompatibleWith = typeArgMustBeCompatibleWith;
+ }
- public override string ToString() => "any " + _typeArgMustBeCompatibleWith.GetNonMangledTypeName();
+ public override string ToString() => "any " + _typeArgMustBeCompatibleWith.GetNonMangledTypeName();
- public bool IsSatisfiedBy(object? argument) => argument.IsCompatibleWith(_typeArgMustBeCompatibleWith);
- }
+ public bool IsSatisfiedBy(object? argument) => argument.IsCompatibleWith(_typeArgMustBeCompatibleWith);
}
\ No newline at end of file
diff --git a/src/NSubstitute/Core/Arguments/ArgumentFormatter.cs b/src/NSubstitute/Core/Arguments/ArgumentFormatter.cs
index cf1491d67..1bfb5ff7b 100644
--- a/src/NSubstitute/Core/Arguments/ArgumentFormatter.cs
+++ b/src/NSubstitute/Core/Arguments/ArgumentFormatter.cs
@@ -1,27 +1,26 @@
-namespace NSubstitute.Core.Arguments
+namespace NSubstitute.Core.Arguments;
+
+public class ArgumentFormatter : IArgumentFormatter
{
- public class ArgumentFormatter : IArgumentFormatter
- {
- internal static IArgumentFormatter Default { get; } = new ArgumentFormatter();
+ internal static IArgumentFormatter Default { get; } = new ArgumentFormatter();
- public string Format(object? argument, bool highlight)
- {
- var formatted = Format(argument);
- return highlight ? "*" + formatted + "*" : formatted;
- }
+ public string Format(object? argument, bool highlight)
+ {
+ var formatted = Format(argument);
+ return highlight ? "*" + formatted + "*" : formatted;
+ }
- private string Format(object? arg)
+ private string Format(object? arg)
+ {
+ return arg switch
{
- return arg switch
- {
- null => "",
- string str => $"\"{str}\"",
- { } obj when HasDefaultToString(obj) => arg.GetType().GetNonMangledTypeName(),
- _ => arg.ToString() ?? string.Empty
- };
+ null => "",
+ string str => $"\"{str}\"",
+ { } obj when HasDefaultToString(obj) => arg.GetType().GetNonMangledTypeName(),
+ _ => arg.ToString() ?? string.Empty
+ };
- static bool HasDefaultToString(object obj)
- => obj.GetType().GetMethod(nameof(ToString), Type.EmptyTypes)!.DeclaringType == typeof(object);
- }
+ static bool HasDefaultToString(object obj)
+ => obj.GetType().GetMethod(nameof(ToString), Type.EmptyTypes)!.DeclaringType == typeof(object);
}
}
\ No newline at end of file
diff --git a/src/NSubstitute/Core/Arguments/ArgumentMatchInfo.cs b/src/NSubstitute/Core/Arguments/ArgumentMatchInfo.cs
index e60e5fc65..fc2d2303c 100644
--- a/src/NSubstitute/Core/Arguments/ArgumentMatchInfo.cs
+++ b/src/NSubstitute/Core/Arguments/ArgumentMatchInfo.cs
@@ -1,52 +1,51 @@
-namespace NSubstitute.Core.Arguments
+namespace NSubstitute.Core.Arguments;
+
+public class ArgumentMatchInfo
{
- public class ArgumentMatchInfo
+ public ArgumentMatchInfo(int index, object? argument, IArgumentSpecification specification)
{
- public ArgumentMatchInfo(int index, object? argument, IArgumentSpecification specification)
- {
- Index = index;
- _argument = argument;
- _specification = specification;
- }
+ Index = index;
+ _argument = argument;
+ _specification = specification;
+ }
- private readonly object? _argument;
- private readonly IArgumentSpecification _specification;
- public int Index { get; }
+ private readonly object? _argument;
+ private readonly IArgumentSpecification _specification;
+ public int Index { get; }
- public bool IsMatch => _specification.IsSatisfiedBy(_argument);
+ public bool IsMatch => _specification.IsSatisfiedBy(_argument);
- public string DescribeNonMatch()
- {
- var describeNonMatch = _specification.DescribeNonMatch(_argument);
- if (string.IsNullOrEmpty(describeNonMatch)) return string.Empty;
- var argIndexPrefix = "arg[" + Index + "]: ";
- return string.Format("{0}{1}", argIndexPrefix, describeNonMatch.Replace("\n", "\n".PadRight(argIndexPrefix.Length + 1)));
- }
+ public string DescribeNonMatch()
+ {
+ var describeNonMatch = _specification.DescribeNonMatch(_argument);
+ if (string.IsNullOrEmpty(describeNonMatch)) return string.Empty;
+ var argIndexPrefix = "arg[" + Index + "]: ";
+ return string.Format("{0}{1}", argIndexPrefix, describeNonMatch.Replace("\n", "\n".PadRight(argIndexPrefix.Length + 1)));
+ }
- public bool Equals(ArgumentMatchInfo? other)
- {
- if (ReferenceEquals(null, other)) return false;
- if (ReferenceEquals(this, other)) return true;
- return other.Index == Index && Equals(other._argument, _argument) && Equals(other._specification, _specification);
- }
+ public bool Equals(ArgumentMatchInfo? other)
+ {
+ if (ReferenceEquals(null, other)) return false;
+ if (ReferenceEquals(this, other)) return true;
+ return other.Index == Index && Equals(other._argument, _argument) && Equals(other._specification, _specification);
+ }
- public override bool Equals(object? obj)
- {
- if (ReferenceEquals(null, obj)) return false;
- if (ReferenceEquals(this, obj)) return true;
- if (obj.GetType() != typeof(ArgumentMatchInfo)) return false;
- return Equals((ArgumentMatchInfo)obj);
- }
+ public override bool Equals(object? obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ if (ReferenceEquals(this, obj)) return true;
+ if (obj.GetType() != typeof(ArgumentMatchInfo)) return false;
+ return Equals((ArgumentMatchInfo)obj);
+ }
- public override int GetHashCode()
+ public override int GetHashCode()
+ {
+ unchecked
{
- unchecked
- {
- int result = Index;
- result = (result * 397) ^ (_argument != null ? _argument.GetHashCode() : 0);
- result = (result * 397) ^ _specification.GetHashCode();
- return result;
- }
+ int result = Index;
+ result = (result * 397) ^ (_argument != null ? _argument.GetHashCode() : 0);
+ result = (result * 397) ^ _specification.GetHashCode();
+ return result;
}
}
}
\ No newline at end of file
diff --git a/src/NSubstitute/Core/Arguments/ArgumentMatcher.cs b/src/NSubstitute/Core/Arguments/ArgumentMatcher.cs
index a84168103..f4d4bbeec 100644
--- a/src/NSubstitute/Core/Arguments/ArgumentMatcher.cs
+++ b/src/NSubstitute/Core/Arguments/ArgumentMatcher.cs
@@ -1,63 +1,62 @@
using NSubstitute.Exceptions;
-namespace NSubstitute.Core.Arguments
+namespace NSubstitute.Core.Arguments;
+
+public static class ArgumentMatcher
{
- public static class ArgumentMatcher
+ ///
+ /// Enqueues a matcher for the method argument in current position and returns the value which should be
+ /// passed back to the method you invoke.
+ ///
+ public static ref T? Enqueue(IArgumentMatcher argumentMatcher)
{
- ///
- /// Enqueues a matcher for the method argument in current position and returns the value which should be
- /// passed back to the method you invoke.
- ///
- public static ref T? Enqueue(IArgumentMatcher argumentMatcher)
+ if (argumentMatcher == null) throw new ArgumentNullException(nameof(argumentMatcher));
+
+ IArgumentMatcher nonGenericMatcher = argumentMatcher switch
{
- if (argumentMatcher == null) throw new ArgumentNullException(nameof(argumentMatcher));
+ IDescribeNonMatches => new GenericToNonGenericMatcherProxyWithDescribe(argumentMatcher),
+ _ => new GenericToNonGenericMatcherProxy(argumentMatcher)
+ };
- IArgumentMatcher nonGenericMatcher = argumentMatcher switch
- {
- IDescribeNonMatches => new GenericToNonGenericMatcherProxyWithDescribe(argumentMatcher),
- _ => new GenericToNonGenericMatcherProxy(argumentMatcher)
- };
+ return ref EnqueueArgSpecification(new ArgumentSpecification(typeof(T), nonGenericMatcher));
+ }
- return ref EnqueueArgSpecification(new ArgumentSpecification(typeof(T), nonGenericMatcher));
- }
+ internal static ref T? Enqueue(IArgumentMatcher argumentMatcher) =>
+ ref EnqueueArgSpecification(new ArgumentSpecification(typeof(T), argumentMatcher));
- internal static ref T? Enqueue