Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: allow interception of any generic method call when using Arg.AnyType #855

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/NSubstitute/Core/CallInfo.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using NSubstitute.Exceptions;

// Disable nullability for entry-point API
#nullable disable annotations

namespace NSubstitute.Core;

public class CallInfo(Argument[] callArguments)
public class CallInfo(Argument[] callArguments, MethodInfo methodInfo)
{

/// <summary>
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/NSubstitute/Core/CallInfoFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions tests/NSubstitute.Acceptance.Specs/GenericArguments.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Globalization;
using System.Reflection;
using NUnit.Framework;

namespace NSubstitute.Acceptance.Specs;
Expand All @@ -11,6 +12,7 @@ public interface ISomethingWithGenerics
{
void SomeAction<TState>(int level, TState state);
string SomeFunction<TState>(int level, TState state);
ICollection<TState> SomeFunction<TState>(int level);
void SomeActionWithGenericConstraints<TState>(int level, TState state) where TState : IEnumerable<int>;
string SomeFunctionWithGenericConstraints<TState>(int level, TState state) where TState : IEnumerable<int>;
}
Expand Down Expand Up @@ -131,4 +133,35 @@ public void Is_matcher_works_with_AnyType_and_constraints()

Assert.That(result, Is.EqualTo("matched"));
}

[Test]
public void Callback_allows_access_to_method_call()
{
static ICollection<T> CreateSubstitute<T>(int count)
{
ICollection<T> substitute = Substitute.For<ICollection<T>>();
substitute.Count.Returns(count);
return substitute;
}

MethodInfo methodInfo = typeof(GenericArguments)
.GetMethods(BindingFlags.Static | BindingFlags.NonPublic)
.Single(x
=> x.Name.Contains(nameof(CreateSubstitute))
&& x.Name.Contains(nameof(Callback_allows_access_to_method_call)));

ISomethingWithGenerics something = Substitute.For<ISomethingWithGenerics>();
something
.SomeFunction<Arg.AnyType>(Arg.Any<int>())
.Returns(x =>
{
Type argumentType = x.MethodInfo.GetGenericArguments()[0];
MethodInfo method = methodInfo.MakeGenericMethod(argumentType);
return method.Invoke(null, [x.Arg<int>()]);
});

ICollection<int> result = something.SomeFunction<int>(7);

Assert.That(result.Count, Is.EqualTo(7));
}
}