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

#853 Fix matching with multiple generic arguments of the same type #858

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: 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