diff --git a/src/NSubstitute/Arg.cs b/src/NSubstitute/Arg.cs index c0313bb2..f03b636c 100644 --- a/src/NSubstitute/Arg.cs +++ b/src/NSubstitute/Arg.cs @@ -1,11 +1,11 @@ using System; using System.Linq.Expressions; -using NSubstitute.Core; using NSubstitute.Core.Arguments; -using NSubstitute.Exceptions; // Disable nullability for client API, so it does not affect clients. #nullable disable annotations +#pragma warning disable CS1574 +#pragma warning disable CS0419 namespace NSubstitute { @@ -14,7 +14,9 @@ namespace NSubstitute /// public static class Arg { - public class AnyType { } + public class AnyType + { + } /// /// Match any argument value compatible with type . @@ -96,20 +98,14 @@ public static ref TDelegate InvokeDelegate(params object[] arguments) /// public static ref T Do(Action useArgument) { - if (typeof(T) == typeof(AnyType)) - { - SubstitutionContext.Current.ThreadContext.DequeueAllArgumentSpecifications(); - throw new DoAnyTypeException(); - } - return ref ArgumentMatcher.Enqueue(new AnyArgumentMatcher(typeof(T)), x => useArgument((T) x!)); } /// - /// Capture any argument and use it to call the function + /// 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 AnyType DoForAny(Action useArgument) + public static ref AnyType Do(Action useArgument) where T : AnyType { return ref ArgumentMatcher.Enqueue(new AnyArgumentMatcher(typeof(AnyType)), x => useArgument(x!)); } @@ -194,15 +190,13 @@ public static class Compat /// This is provided for compatibility with older compilers -- /// if possible use instead. /// - public static T Do(Action useArgument) => Arg.Do(useArgument); + public static T Do(Action useArgument) => Arg.Do(useArgument); /// - /// Capture any argument and use it to call the function + /// 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 AnyType DoForAny(Action useArgument) => Arg.DoForAny(useArgument); + public static AnyType Do(Action useArgument) where T : AnyType => Arg.Do(useArgument); } private static Action InvokeDelegateAction(params object[] arguments) diff --git a/src/NSubstitute/Compatibility/CompatArg.cs b/src/NSubstitute/Compatibility/CompatArg.cs index 4bf88a23..c1bc0577 100644 --- a/src/NSubstitute/Compatibility/CompatArg.cs +++ b/src/NSubstitute/Compatibility/CompatArg.cs @@ -3,6 +3,8 @@ // Disable nullability for client API, so it does not affect clients. #nullable disable annotations +#pragma warning disable CS1574 +#pragma warning disable CS0419 namespace NSubstitute.Compatibility { @@ -100,11 +102,9 @@ private CompatArg() { } public T Do(Action useArgument) => Arg.Do(useArgument); /// - /// Capture any argument and use it to call the function + /// 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 Arg.AnyType DoForAny(Action useArgument) => Arg.DoForAny(useArgument); + public static Arg.AnyType Do(Action useArgument) where T : Arg.AnyType => Arg.Do(useArgument); } } diff --git a/tests/NSubstitute.Acceptance.Specs/GenericArguments.cs b/tests/NSubstitute.Acceptance.Specs/GenericArguments.cs index a1f07593..85fd7df7 100644 --- a/tests/NSubstitute.Acceptance.Specs/GenericArguments.cs +++ b/tests/NSubstitute.Acceptance.Specs/GenericArguments.cs @@ -1,4 +1,6 @@ -using NSubstitute.Exceptions; +using System; +using System.Collections.Generic; +using NSubstitute.Exceptions; using NUnit.Framework; namespace NSubstitute.Acceptance.Specs; @@ -17,9 +19,8 @@ public void Return_result_for_any_argument() string argDoResult = null; int? whenDoResult = null; bool whenDoCalled = false; - ISomethingWithGenerics something = Substitute.For(); - something.Log(Arg.Any(), Arg.DoForAny(a => argDoResult = a.ToString())); + something.Log(Arg.Any(), Arg.Do(a => argDoResult = ">>" + ((int)a).ToString("P"))); something .When(substitute => substitute.Log(Arg.Any(), Arg.Any())) .Do(info => @@ -33,16 +34,7 @@ public void Return_result_for_any_argument() something.Received().Log(Arg.Any(), Arg.Any()); something.Received().Log(7, 3409); Assert.That(whenDoCalled, Is.True); - Assert.That(argDoResult, Is.EqualTo("3409")); + Assert.That(argDoResult, Is.EqualTo(">>340 900.00 %")); Assert.That(whenDoResult, Is.EqualTo(3409)); } - - [Test] - public void Throw_with_Do_AnyType() - { - ISomethingWithGenerics something = Substitute.For(); - - Assert.Throws(() => - something.Log(Arg.Any(), Arg.Do(a => _ = a.ToString()))); - } } \ No newline at end of file