Skip to content

Commit

Permalink
Add ThatAre[Not]Abstract, ThatAre[Not]Overridable to MethodInfoSelect…
Browse files Browse the repository at this point in the history
…or (as mentioned in More Assertions on types fluentassertions#645)
  • Loading branch information
94sedighi committed Dec 2, 2022
1 parent 0a04100 commit 1616ca1
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 1 deletion.
36 changes: 36 additions & 0 deletions Src/FluentAssertions/Types/MethodInfoSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,42 @@ public MethodInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
return this;
}

/// <summary>
/// Only return methods that are overridable
/// </summary>
/// <returns></returns>
public MethodInfoSelector ThatAreOverridable()
{
selectedMethods = selectedMethods.Where(method => !method.IsFinal && method.IsVirtual);
return this;
}

/// <summary>
/// Only return methods that are not overridable
/// </summary>
/// <returns></returns>
public MethodInfoSelector ThatAreNotOverridable()
{
selectedMethods = selectedMethods.Where(method => method.IsFinal || !method.IsVirtual);
return this;
}

/// <summary>
/// Only return methods that are abstract
/// </summary>
/// <returns></returns>
public MethodInfoSelector ThatAreAbstract()
{
selectedMethods = selectedMethods.Where(method => method.IsAbstract);
return this;
}

public MethodInfoSelector ThatAreNotAbstract()
{
selectedMethods = selectedMethods.Where(method => !method.IsAbstract);
return this;
}

/// <summary>
/// Only return methods that are async.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2466,18 +2466,22 @@ namespace FluentAssertions.Types
public FluentAssertions.Types.MethodInfoSelector ThatReturnVoid { get; }
public System.Collections.Generic.IEnumerator<System.Reflection.MethodInfo> GetEnumerator() { }
public FluentAssertions.Types.TypeSelector ReturnTypes() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreAbstract() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreAsync() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreDecoratedWith<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreDecoratedWithOrInherit<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotAbstract() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotAsync() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWith<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotOverridable() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreOverridable() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatDoNotReturn<TReturn>() { }
Expand Down Expand Up @@ -2765,4 +2769,4 @@ namespace FluentAssertions.Xml
public bool CanHandle(object value) { }
public void Format(object value, FluentAssertions.Formatting.FormattedObjectGraph formattedGraph, FluentAssertions.Formatting.FormattingContext context, FluentAssertions.Formatting.FormatChild formatChild) { }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2596,18 +2596,22 @@ namespace FluentAssertions.Types
public FluentAssertions.Types.MethodInfoSelector ThatReturnVoid { get; }
public System.Collections.Generic.IEnumerator<System.Reflection.MethodInfo> GetEnumerator() { }
public FluentAssertions.Types.TypeSelector ReturnTypes() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreAbstract() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreAsync() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreDecoratedWith<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreDecoratedWithOrInherit<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotAbstract() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotAsync() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWith<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotOverridable() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreOverridable() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatDoNotReturn<TReturn>() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2468,18 +2468,22 @@ namespace FluentAssertions.Types
public FluentAssertions.Types.MethodInfoSelector ThatReturnVoid { get; }
public System.Collections.Generic.IEnumerator<System.Reflection.MethodInfo> GetEnumerator() { }
public FluentAssertions.Types.TypeSelector ReturnTypes() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreAbstract() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreAsync() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreDecoratedWith<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreDecoratedWithOrInherit<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotAbstract() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotAsync() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWith<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotOverridable() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreOverridable() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatDoNotReturn<TReturn>() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2468,18 +2468,22 @@ namespace FluentAssertions.Types
public FluentAssertions.Types.MethodInfoSelector ThatReturnVoid { get; }
public System.Collections.Generic.IEnumerator<System.Reflection.MethodInfo> GetEnumerator() { }
public FluentAssertions.Types.TypeSelector ReturnTypes() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreAbstract() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreAsync() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreDecoratedWith<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreDecoratedWithOrInherit<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotAbstract() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotAsync() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWith<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotOverridable() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreOverridable() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatDoNotReturn<TReturn>() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2417,18 +2417,22 @@ namespace FluentAssertions.Types
public FluentAssertions.Types.MethodInfoSelector ThatReturnVoid { get; }
public System.Collections.Generic.IEnumerator<System.Reflection.MethodInfo> GetEnumerator() { }
public FluentAssertions.Types.TypeSelector ReturnTypes() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreAbstract() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreAsync() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreDecoratedWith<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreDecoratedWithOrInherit<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotAbstract() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotAsync() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWith<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotOverridable() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreOverridable() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatDoNotReturn<TReturn>() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2468,18 +2468,22 @@ namespace FluentAssertions.Types
public FluentAssertions.Types.MethodInfoSelector ThatReturnVoid { get; }
public System.Collections.Generic.IEnumerator<System.Reflection.MethodInfo> GetEnumerator() { }
public FluentAssertions.Types.TypeSelector ReturnTypes() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreAbstract() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreAsync() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreDecoratedWith<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreDecoratedWithOrInherit<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotAbstract() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotAsync() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWith<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotOverridable() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreOverridable() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatDoNotReturn<TReturn>() { }
Expand Down
79 changes: 79 additions & 0 deletions Tests/FluentAssertions.Specs/Types/MethodInfoSelectorSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,62 @@ public void When_selecting_methods_not_decorated_with_a_noninheritable_attribute
methods.Should().ContainSingle();
}

[Fact]
public void When_selecting_methods_that_are_overridable_it_should_only_return_the_applicable_methods()
{
// Arrange
Type type = typeof(TestClassForMethodSelectorWithAbstractAndVirtualMethods);

// Act
IEnumerable<MethodInfo> methods = type.Methods().ThatAreOverridable().ToArray();

// Assert
int overridableMethodsCount = 6;
methods.Should().HaveCount(overridableMethodsCount);
}

[Fact]
public void When_selecting_methods_that_are_not_overridable_it_should_only_return_the_applicable_methods()
{
// Arrange
Type type = typeof(TestClassForMethodSelectorWithAbstractAndVirtualMethods);

// Act
IEnumerable<MethodInfo> methods = type.Methods().ThatAreNotOverridable();

// Assert
int notOverridableCount = 4;
methods.Should().HaveCount(notOverridableCount);
}

[Fact]
public void When_selecting_methods_that_are_abstract_it_should_only_return_the_applicable_methods()
{
// Arrange
Type type = typeof(TestClassForMethodSelectorWithAbstractAndVirtualMethods);

// Act
IEnumerable<MethodInfo> methods = type.Methods().ThatAreAbstract().ToArray();

// Assert
int abstractMethodsCount = 3;
methods.Should().HaveCount(abstractMethodsCount);
}

[Fact]
public void When_selecting_methods_that_are_not_abstract_it_should_only_return_the_applicable_methods()
{
// Arrange
Type type = typeof(TestClassForMethodSelectorWithAbstractAndVirtualMethods);

// Act
IEnumerable<MethodInfo> methods = type.Methods().ThatAreNotAbstract().ToArray();

// Assert
int notAbstractMethodsCount = 7;
methods.Should().HaveCount(notAbstractMethodsCount);
}

[Fact]
public void When_selecting_methods_that_are_async_it_should_only_return_the_applicable_methods()
{
Expand Down Expand Up @@ -483,6 +539,29 @@ internal class TestClassForMethodSelectorWithStaticAndNonStaticMethod
public void PublicNonStaticMethod() { }
}

internal abstract class TestClassForMethodSelectorWithAbstractAndVirtualMethods
{
public abstract void PublicAbstractMethod();

protected abstract void ProtectedAbstractMethod();

internal abstract void InternalAbstractMethod();

public virtual void PublicVirtualMethod() { }

protected virtual void ProptectedVirtualMethod() { }

internal virtual void InternalVirtualMethod() { }

public void PublicNotAbstractMethod() { }

protected void ProtectedNotAbstractMethod() { }

internal void InternalNotAbstractMethod() { }

private void PrivateAbstractMethod() { }
}

internal class TestClassForMethodReturnTypesSelector
{
public void SomeMethod() { }
Expand Down

0 comments on commit 1616ca1

Please sign in to comment.