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

Consider types assignable to open generics (#954) #955

Merged
merged 7 commits into from Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
103 changes: 99 additions & 4 deletions Src/FluentAssertions/Types/TypeAssertions.cs
Expand Up @@ -81,8 +81,18 @@ public new AndConstraint<TypeAssertions> BeAssignableTo<T>(string because = "",
/// <returns>An <see cref="AndConstraint{T}"/> which can be used to chain assertions.</returns>
public new AndConstraint<TypeAssertions> BeAssignableTo(Type type, string because = "", params object[] becauseArgs)
{
bool isAssignable;
if (type.GetTypeInfo().IsGenericTypeDefinition)
{
isAssignable = IsAssignableToOpenGeneric(type);
}
else
{
isAssignable = type.IsAssignableFrom(Subject);
}

Execute.Assertion
.ForCondition(type.IsAssignableFrom(Subject))
.ForCondition(isAssignable)
.BecauseOf(because, becauseArgs)
.FailWith(
"Expected {context:" + Identifier + "} {0} to be assignable to {1}{reason}, but it is not.",
Expand All @@ -92,6 +102,61 @@ public new AndConstraint<TypeAssertions> BeAssignableTo(Type type, string becaus
return new AndConstraint<TypeAssertions>(this);
}

bool IsAssignableToOpenGeneric(Type definition)
mdonoughe marked this conversation as resolved.
Show resolved Hide resolved
{
// The CLR type system does not consider anything to be assignable to an open generic type.
// For the purposes of test assertions, the user probably means that the subject type is
// assignable to any generic type based on the given generic type definition.

if (definition.GetTypeInfo().IsInterface)
{
return IsImplementationOfOpenGeneric(definition);
}
else
{
return Subject.IsSameOrEqualTo(definition) || IsDerivedFromOpenGeneric(definition);
}
}

bool IsImplementationOfOpenGeneric(Type definition)
{
// check subject against definition
TypeInfo subjectInfo = Subject.GetTypeInfo();
if (subjectInfo.IsInterface && subjectInfo.IsGenericType &&
subjectInfo.GetGenericTypeDefinition().IsSameOrEqualTo(definition))
{
return true;
}

// check subject's interfaces against definition
return subjectInfo.ImplementedInterfaces
.Select(i => i.GetTypeInfo())
.Where(i => i.IsGenericType)
.Select(i => i.GetGenericTypeDefinition())
.Any(d => d.IsSameOrEqualTo(definition));
}

bool IsDerivedFromOpenGeneric(Type definition)
{
if (Subject.IsSameOrEqualTo(definition))
{
// do not consider a type to be derived from itself
return false;
}

// check subject and its base types against definition
for (TypeInfo baseType = Subject.GetTypeInfo(); baseType != null;
baseType = baseType.BaseType?.GetTypeInfo())
{
if (baseType.IsGenericType && baseType.GetGenericTypeDefinition().IsSameOrEqualTo(definition))
{
return true;
}
}

return false;
}

/// <summary>
/// Asserts than an instance of the subject type is not assignable variable of type <typeparamref name="T"/>.
/// </summary>
Expand All @@ -113,8 +178,18 @@ public new AndConstraint<TypeAssertions> NotBeAssignableTo<T>(string because = "
/// <returns>An <see cref="AndConstraint{T}"/> which can be used to chain assertions.</returns>
public new AndConstraint<TypeAssertions> NotBeAssignableTo(Type type, string because = "", params object[] becauseArgs)
{
bool isNotAssignable;
mdonoughe marked this conversation as resolved.
Show resolved Hide resolved
if (type.GetTypeInfo().IsGenericTypeDefinition)
{
isNotAssignable = !IsAssignableToOpenGeneric(type);
}
else
{
isNotAssignable = !type.IsAssignableFrom(Subject);
}

Execute.Assertion
.ForCondition(!type.IsAssignableFrom(Subject))
.ForCondition(isNotAssignable)
.BecauseOf(because, becauseArgs)
.FailWith(
"Expected {context:" + Identifier + "} {0} to not be assignable to {1}{reason}, but it is.",
Expand Down Expand Up @@ -471,7 +546,17 @@ public AndConstraint<TypeAssertions> BeDerivedFrom(Type baseType, string because
throw new ArgumentException("Must not be an interface Type.", nameof(baseType));
}

Execute.Assertion.ForCondition(Subject.GetTypeInfo().IsSubclassOf(baseType))
bool isDerivedFrom;
if (baseType.GetTypeInfo().IsGenericTypeDefinition)
{
isDerivedFrom = IsDerivedFromOpenGeneric(baseType);
}
else
{
isDerivedFrom = Subject.GetTypeInfo().IsSubclassOf(baseType);
}

Execute.Assertion.ForCondition(isDerivedFrom)
.BecauseOf(because, becauseArgs)
.FailWith("Expected type {0} to be derived from {1}{reason}, but it is not.", Subject, baseType);

Expand Down Expand Up @@ -505,8 +590,18 @@ public AndConstraint<TypeAssertions> NotBeDerivedFrom(Type baseType, string beca
throw new ArgumentException("Must not be an interface Type.", nameof(baseType));
}

bool isNotDerivedFrom;
mdonoughe marked this conversation as resolved.
Show resolved Hide resolved
if (baseType.GetTypeInfo().IsGenericTypeDefinition)
{
isNotDerivedFrom = !IsDerivedFromOpenGeneric(baseType);
}
else
{
isNotDerivedFrom = !Subject.GetTypeInfo().IsSubclassOf(baseType);
}

Execute.Assertion
.ForCondition(!Subject.GetTypeInfo().IsSubclassOf(baseType))
.ForCondition(isNotDerivedFrom)
.BecauseOf(because, becauseArgs)
.FailWith("Expected type {0} not to be derived from {1}{reason}, but it is.", Subject, baseType);

Expand Down