From 1fcbcde3debf7b066a71527db03529a070cd02ca Mon Sep 17 00:00:00 2001 From: Joost Molenkamp Date: Fri, 13 Dec 2024 20:46:36 +0100 Subject: [PATCH] Feature: allow interception of any generic method call when using Arg.AnyType --- src/NSubstitute/Core/CallInfo.cs | 7 +++++-- src/NSubstitute/Core/CallInfoFactory.cs | 2 +- src/NSubstitute/Core/CallSpecification.cs | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/NSubstitute/Core/CallInfo.cs b/src/NSubstitute/Core/CallInfo.cs index 63ac178e..8e5adae2 100644 --- a/src/NSubstitute/Core/CallInfo.cs +++ b/src/NSubstitute/Core/CallInfo.cs @@ -1,12 +1,13 @@ -using System.Diagnostics.CodeAnalysis; using NSubstitute.Exceptions; +using System.Diagnostics.CodeAnalysis; +using System.Reflection; // Disable nullability for entry-point API #nullable disable annotations namespace NSubstitute.Core; -public class CallInfo(Argument[] callArguments) +public class CallInfo(Argument[] callArguments, MethodInfo methodInfo) { /// @@ -25,6 +26,8 @@ public object this[int index] } } + public MethodInfo MethodInfo => methodInfo; + private void EnsureArgIsSettable(Argument argument, int index, object value) { if (!argument.IsByRef) diff --git a/src/NSubstitute/Core/CallInfoFactory.cs b/src/NSubstitute/Core/CallInfoFactory.cs index 983c652e..fe14d426 100644 --- a/src/NSubstitute/Core/CallInfoFactory.cs +++ b/src/NSubstitute/Core/CallInfoFactory.cs @@ -5,7 +5,7 @@ public class CallInfoFactory : ICallInfoFactory public CallInfo Create(ICall call) { var arguments = GetArgumentsFromCall(call); - return new CallInfo(arguments); + return new CallInfo(arguments, call.GetMethodInfo()); } private static Argument[] GetArgumentsFromCall(ICall call) diff --git a/src/NSubstitute/Core/CallSpecification.cs b/src/NSubstitute/Core/CallSpecification.cs index 7b099a94..3f39b1e9 100644 --- a/src/NSubstitute/Core/CallSpecification.cs +++ b/src/NSubstitute/Core/CallSpecification.cs @@ -1,5 +1,5 @@ -using System.Reflection; using NSubstitute.Core.Arguments; +using System.Reflection; namespace NSubstitute.Core; @@ -93,7 +93,7 @@ internal static bool TypesAreAllEquivalent(Type[] aArgs, Type[] bArgs) private static bool AreEquivalentDefinitions(MethodInfo a, MethodInfo b) { return a.IsGenericMethod == b.IsGenericMethod - && a.ReturnType == b.ReturnType + && TypesAreAllEquivalent([a.ReturnType], [b.ReturnType]) && a.Name.Equals(b.Name, StringComparison.Ordinal); }