Skip to content

Commit

Permalink
Use targeted methods instead of LINQ
Browse files Browse the repository at this point in the history
  • Loading branch information
jnyrup committed Oct 12, 2023
1 parent 3f250d8 commit 1ff6be6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
4 changes: 1 addition & 3 deletions Src/FluentAssertions/Common/TypeExtensions.cs
Expand Up @@ -351,9 +351,7 @@ private static bool IsImplementationOfOpenGeneric(this Type type, Type definitio

// check subject's interfaces against definition
return type.GetInterfaces()
.Where(i => i.IsGenericType)
.Select(i => i.GetGenericTypeDefinition())
.Contains(definition);
.Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == definition);
}

public static bool IsDerivedFromOpenGeneric(this Type type, Type definition)
Expand Down
4 changes: 2 additions & 2 deletions Src/FluentAssertions/Types/TypeAssertions.cs
Expand Up @@ -462,7 +462,7 @@ public AndConstraint<TypeAssertions> Implement(Type interfaceType, string becaus
{
Guard.ThrowIfArgumentIsNull(interfaceType);

bool containsInterface = Subject.GetInterfaces().Contains(interfaceType);
bool containsInterface = interfaceType.IsAssignableFrom(Subject) && interfaceType != Subject;

Execute.Assertion
.BecauseOf(because, becauseArgs)
Expand Down Expand Up @@ -511,7 +511,7 @@ public AndConstraint<TypeAssertions> NotImplement(Type interfaceType, string bec
{
Guard.ThrowIfArgumentIsNull(interfaceType);

bool containsInterface = Subject.GetInterfaces().Contains(interfaceType);
bool containsInterface = interfaceType.IsAssignableFrom(Subject) && interfaceType != Subject;

Execute.Assertion
.BecauseOf(because, becauseArgs)
Expand Down
24 changes: 24 additions & 0 deletions Tests/FluentAssertions.Specs/Types/TypeAssertionSpecs.Implement.cs
Expand Up @@ -73,6 +73,20 @@ public void When_asserting_a_type_to_implement_null_it_should_throw()
act.Should().ThrowExactly<ArgumentNullException>()
.WithParameterName("interfaceType");
}

[Fact]
public void An_interface_does_not_implement_itself()
{
// Arrange
var type = typeof(IDummyInterface);

// Act
Action act = () =>
type.Should().Implement(typeof(IDummyInterface));

// Assert
act.Should().Throw<XunitException>();
}
}

public class ImplementOfT
Expand Down Expand Up @@ -156,6 +170,16 @@ public void When_asserting_a_type_not_to_implement_null_it_should_throw()
act.Should().ThrowExactly<ArgumentNullException>()
.WithParameterName("interfaceType");
}

[Fact]
public void An_interface_does_not_implement_itself()
{
// Arrange
var type = typeof(IDummyInterface);

// Act / Assert
type.Should().NotImplement(typeof(IDummyInterface));
}
}

public class NotImplementOfT
Expand Down

0 comments on commit 1ff6be6

Please sign in to comment.