Skip to content

Commit

Permalink
Adding ThatAreAsync() and ThatAreNotAsync() to `MethodInfoSelecto…
Browse files Browse the repository at this point in the history
…r` (#1725)

Adds ThatAreAsync and ThatAreNotAsync to MethodInfoSelector
  • Loading branch information
chvollm committed Nov 8, 2021
1 parent 313ee24 commit 31fb3cf
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Src/FluentAssertions/Types/MethodInfoSelector.cs
Expand Up @@ -133,6 +133,24 @@ public MethodInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
return this;
}

/// <summary>
/// Only return methods that are async.
/// </summary>
public MethodInfoSelector ThatAreAsync()
{
selectedMethods = selectedMethods.Where(method => method.IsAsync());
return this;
}

/// <summary>
/// Only return methods that are not async.
/// </summary>
public MethodInfoSelector ThatAreNotAsync()
{
selectedMethods = selectedMethods.Where(method => !method.IsAsync());
return this;
}

/// <summary>
/// Select return types of the methods
/// </summary>
Expand Down
Expand Up @@ -2341,10 +2341,12 @@ 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 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 ThatAreNotAsync() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWith<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
Expand Down
Expand Up @@ -2343,10 +2343,12 @@ 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 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 ThatAreNotAsync() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWith<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
Expand Down
Expand Up @@ -2343,10 +2343,12 @@ 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 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 ThatAreNotAsync() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWith<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
Expand Down
Expand Up @@ -2294,10 +2294,12 @@ 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 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 ThatAreNotAsync() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWith<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
Expand Down
Expand Up @@ -2343,10 +2343,12 @@ 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 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 ThatAreNotAsync() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWith<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
Expand Down
39 changes: 39 additions & 0 deletions Tests/FluentAssertions.Specs/Types/MethodInfoSelectorSpecs.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using FluentAssertions.Types;
using Internal.Main.Test;
using Xunit;
Expand Down Expand Up @@ -272,6 +273,34 @@ public void When_selecting_methods_not_decorated_with_a_noninheritable_attribute
methods.Should().ContainSingle();
}

[Fact]
public void When_selecting_methods_that_are_async_it_should_only_return_the_applicable_methods()
{
// Arrange
Type type = typeof(TestClassForMethodSelectorWithAsyncAndNonAsyncMethod);

// Act
MethodInfo[] methods = type.Methods().ThatAreAsync().ToArray();

// Assert
methods.Should().ContainSingle()
.Which.Name.Should().Be("PublicVoidAsyncMethod");
}

[Fact]
public void When_selecting_methods_that_are_not_async_it_should_only_return_the_applicable_methods()
{
// Arrange
Type type = typeof(TestClassForMethodSelectorWithAsyncAndNonAsyncMethod);

// Act
MethodInfo[] methods = type.Methods().ThatAreNotAsync().ToArray();

// Assert
methods.Should().ContainSingle()
.Which.Name.Should().Be("PublicVoidNotAsyncMethod");
}

[Fact]
public void When_selecting_methods_not_decorated_with_or_inheriting_a_noninheritable_attribute_it_should_only_return_the_applicable_methods()
{
Expand Down Expand Up @@ -367,6 +396,16 @@ internal class TestClassForMethodSelectorWithNonInheritableAttributeDerived : Te
public override void PublicVirtualVoidMethodWithAttribute() { }
}

internal class TestClassForMethodSelectorWithAsyncAndNonAsyncMethod
{
public async void PublicVoidAsyncMethod()
{
await Task.Yield();
}

public void PublicVoidNotAsyncMethod() { }
}

internal class TestClassForMethodReturnTypesSelector
{
public void SomeMethod() { }
Expand Down
1 change: 1 addition & 0 deletions docs/_pages/releases.md
Expand Up @@ -10,6 +10,7 @@ sidebar:
## Unreleased

### What's New
* Adding `ThatAreAsync()` and `ThatAreNotAsync()` for filtering in method assertions - [#1725](https://github.com/fluentassertions/fluentassertions/pull/1725)

### Fixes

Expand Down

0 comments on commit 31fb3cf

Please sign in to comment.