Skip to content
Merged
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
10 changes: 8 additions & 2 deletions src/NSubstitute/Core/CallInfo.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using NSubstitute.Exceptions;
using System.Diagnostics.CodeAnalysis;
using NSubstitute.Exceptions;

namespace NSubstitute.Core;

public class CallInfo(Argument[] callArguments)
public class CallInfo(Argument[] callArguments, Type[] genericArguments)
{

/// <summary>
Expand Down Expand Up @@ -35,6 +35,12 @@ private void EnsureArgIsSettable(Argument argument, int index, object? value)
}
}

/// <summary>
/// Gets the generic type arguments in case of a generic method call or an empty array otherwise.
/// </summary>
/// <returns>Array of types of the generic type arguments for this method call</returns>
public Type[] GenericArgs() => genericArguments;

/// <summary>
/// Get the arguments passed to this call.
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion src/NSubstitute/Core/CallInfoFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ public class CallInfoFactory : ICallInfoFactory
public CallInfo Create(ICall call)
{
var arguments = GetArgumentsFromCall(call);
return new CallInfo(arguments);
var genericArguments = call.GetMethodInfo().GetGenericArguments();
return new CallInfo(arguments, genericArguments);
}

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 @@ -12,6 +13,7 @@ public interface ISomethingWithGenerics
void SomeAction<TState>(int level, TState state);
string SomeFunction<TState>(int level, TState state);
ICollection<TState> SomeFunction<TState>(TState state);
ICollection<TState> SomeFunctionWithoutGenericMethodParameter<TState>(int level);
bool SomeFunctionWithOut<TState>(out IEnumerable<TState> state);
bool SomeFunctionWithRef<TState>(ref IEnumerable<TState> state);
void SomeActionWithGenericConstraints<TState>(int level, TState state) where TState : IEnumerable<int>;
Expand Down Expand Up @@ -177,4 +179,35 @@ public void Returns_works_with_AnyType_for_ref_parameter_with_AnyType_generic_ar

Assert.That(result, Is.True);
}

[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
.SomeFunctionWithoutGenericMethodParameter<Arg.AnyType>(Arg.Any<int>())
.Returns(x =>
{
Type argumentType = x.GenericArgs()[0];
MethodInfo method = methodInfo.MakeGenericMethod(argumentType);
return method.Invoke(null, [x.Arg<int>()]);
});

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

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