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

Adding ThatAreAsync() and ThatAreNotAsync() to MethodInfoSelector #1725

Merged
merged 8 commits into from Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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()
chvollm marked this conversation as resolved.
Show resolved Hide resolved
{
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