Skip to content

Commit

Permalink
fix for issue fluentassertions#458; Check whether expectedType is a g…
Browse files Browse the repository at this point in the history
…eneric type definition and if it is verify that the subject type is a generic type for that generic type definition (fluentassertions#476)
  • Loading branch information
ben-m-lucas authored and dennisdoomen committed Sep 5, 2016
1 parent 5c89e71 commit 77be654
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Src/Core/Primitives/ReferenceTypeAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,15 @@ public AndConstraint<TAssertions> BeOfType(Type expectedType, string because = "
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:type} to be {0}{reason}, but found <null>.", expectedType);

Subject.GetType().Should().Be(expectedType, because, becauseArgs);
Type subjectType = Subject.GetType();
if (expectedType.IsGenericTypeDefinition() && subjectType.IsGenericType())
{
subjectType.GetGenericTypeDefinition().Should().Be(expectedType, because, becauseArgs);
}
else
{
subjectType.Should().Be(expectedType, because, becauseArgs);
}

return new AndConstraint<TAssertions>((TAssertions)this);
}
Expand Down
5 changes: 5 additions & 0 deletions Src/FluentAssertions.Core.Dotnet/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public static Type[] GetGenericArguments(this Type type)
return type.GetTypeInfo().GenericTypeArguments;
}

public static bool IsGenericTypeDefinition(this Type type)
{
return type.GetTypeInfo().IsGenericTypeDefinition;
}

}

}
4 changes: 4 additions & 0 deletions Src/FluentAssertions.Core/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public static bool IsInterface(this Type type)
return type.IsInterface;
}

public static bool IsGenericTypeDefinition(this Type type)
{
return type.IsGenericTypeDefinition;
}

public static Type GetTypeInfo(this Type type)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,25 @@ public void When_object_is_of_the_expected_type_it_should_not_throw()
action.ShouldNotThrow();
}

[TestMethod]
public void When_object_is_an_open_generic_of_the_expected_type_it_should_not_throw()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var openGeneric = new System.Collections.Generic.List<string>();

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action action = () => openGeneric.Should().BeOfType(typeof(System.Collections.Generic.List<>));

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
action.ShouldNotThrow();
}

[TestMethod]
public void When_object_is_null_it_should_throw()
{
Expand Down

0 comments on commit 77be654

Please sign in to comment.