Skip to content

Commit

Permalink
nsubstitute#853 Fix matching with multiple generic arguments of the …
Browse files Browse the repository at this point in the history
…same type
  • Loading branch information
rholek-IDG committed Jan 6, 2025
1 parent d0ff096 commit 712fc43
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/NSubstitute/Core/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public static bool IsCompatibleWith(this object? instance, Type type)
aCompatibleInstanceType.GetGenericTypeDefinition() == genericTypeDefinition)
{
// both are the same generic type. If their GenericTypeArguments match then they are equivalent
return CallSpecification.TypesAreAllEquivalent(
aCompatibleInstanceType.GenericTypeArguments, type.GenericTypeArguments);
var typesAreAllEquivalent = CallSpecification.TypesAreAllEquivalent(aCompatibleInstanceType.GenericTypeArguments, type.GenericTypeArguments);
if (typesAreAllEquivalent)
return true;
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/NSubstitute.Acceptance.Specs/ArgumentMatching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,24 @@ public void Supports_matching_custom_class_with_derived_class_argument()
service.Received().MyMethod(Arg.Any<MySampleClassArgument>());
}

[Test]
public void Supports_matching_custom_with_multiple_generic_arguments_string()
{
var service = Substitute.For<IMyService>();
var argument = new MyStringIntArgument();

service.MyMethodWithReturnValue<string>().Returns(argument);
}

[Test]
public void Supports_matching_custom_with_multiple_generic_arguments_int()
{
var service = Substitute.For<IMyService>();
var argument = new MyStringIntArgument();

service.MyMethodWithReturnValue<int>().Returns(argument);
}

[Test]
public void Supports_matching_generic_interface_bound_type_ArgAnyType_with_class_argument()
{
Expand Down Expand Up @@ -837,6 +855,9 @@ public void SetUp()
public interface IMyService
{
void MyMethod<T>(IMyArgument<T> argument);

IMyArgument<T> MyMethodWithReturnValue<T>();

}

public interface IMyArgument<T> { }
Expand All @@ -845,6 +866,7 @@ public class MyStringArgument : IMyArgument<string> { }
public class MyOtherStringArgument : IMyArgument<string> { }
public class MySampleClassArgument : IMyArgument<SampleClass> { }
public class MyOtherSampleClassArgument : IMyArgument<SampleClass> { }
public class MyStringIntArgument : IMyArgument<string>, IMyArgument<int> { }
public class MySampleDerivedClassArgument : MySampleClassArgument { }

[Test]
Expand Down

0 comments on commit 712fc43

Please sign in to comment.