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

Adds ThatAreStatic and ThatAreNotStatic to MethodInfoSelector #1740

Merged
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
20 changes: 19 additions & 1 deletion Src/FluentAssertions/Types/MethodInfoSelector.cs
Expand Up @@ -35,7 +35,7 @@ public MethodInfoSelector(IEnumerable<Type> types)
Guard.ThrowIfArgumentContainsNull(types, nameof(types));

selectedMethods = types.SelectMany(t => t
.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
.Where(method => !HasSpecialName(method)));
}

Expand Down Expand Up @@ -151,6 +151,24 @@ public MethodInfoSelector ThatAreNotAsync()
return this;
}

/// <summary>
/// Only return methods that are static.
/// </summary>
public MethodInfoSelector ThatAreStatic()
{
selectedMethods = selectedMethods.Where(method => method.IsStatic);
return this;
}

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

/// <summary>
/// Only return methods that are virtual.
/// </summary>
Expand Down
Expand Up @@ -2363,7 +2363,9 @@ namespace FluentAssertions.Types
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatDoNotReturn<TReturn>() { }
public FluentAssertions.Types.MethodInfoSelector ThatReturn<TReturn>() { }
Expand Down
Expand Up @@ -2365,7 +2365,9 @@ namespace FluentAssertions.Types
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatDoNotReturn<TReturn>() { }
public FluentAssertions.Types.MethodInfoSelector ThatReturn<TReturn>() { }
Expand Down
Expand Up @@ -2365,7 +2365,9 @@ namespace FluentAssertions.Types
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatDoNotReturn<TReturn>() { }
public FluentAssertions.Types.MethodInfoSelector ThatReturn<TReturn>() { }
Expand Down
Expand Up @@ -2316,7 +2316,9 @@ namespace FluentAssertions.Types
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatDoNotReturn<TReturn>() { }
public FluentAssertions.Types.MethodInfoSelector ThatReturn<TReturn>() { }
Expand Down
Expand Up @@ -2365,7 +2365,9 @@ namespace FluentAssertions.Types
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotDecoratedWithOrInherit<TAttribute>()
where TAttribute : System.Attribute { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreNotVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreStatic() { }
public FluentAssertions.Types.MethodInfoSelector ThatAreVirtual() { }
public FluentAssertions.Types.MethodInfoSelector ThatDoNotReturn<TReturn>() { }
public FluentAssertions.Types.MethodInfoSelector ThatReturn<TReturn>() { }
Expand Down
35 changes: 35 additions & 0 deletions Tests/FluentAssertions.Specs/Types/MethodInfoSelectorSpecs.cs
Expand Up @@ -332,6 +332,34 @@ public void When_selecting_methods_that_are_not_virtual_it_should_only_return_th
.And.NotContain(m => m.Name == "PublicVirtualVoidMethodWithAttribute")
.And.NotContain(m => m.Name == "ProtectedVirtualVoidMethodWithAttribute");
}

[Fact]
public void When_selecting_methods_that_are_static_it_should_only_return_the_applicable_methods()
{
// Arrange
Type type = typeof(TestClassForMethodSelectorWithStaticAndNonStaticMethod);

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

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

[Fact]
public void When_selecting_methods_that_are_not_static_it_should_only_return_the_applicable_methods()
{
// Arrange
Type type = typeof(TestClassForMethodSelectorWithStaticAndNonStaticMethod);

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

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

[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 @@ -435,6 +463,13 @@ internal class TestClassForMethodSelectorWithAsyncAndNonAsyncMethod
public Task PublicNonAsyncMethod() => Task.CompletedTask;
}

internal class TestClassForMethodSelectorWithStaticAndNonStaticMethod
{
public static void PublicStaticMethod() { }

public void PublicNonStaticMethod() { }
}

internal class TestClassForMethodReturnTypesSelector
{
public void SomeMethod() { }
Expand Down
3 changes: 2 additions & 1 deletion docs/_pages/releases.md
Expand Up @@ -18,10 +18,11 @@ sidebar:
### What's New
* Adding `ThatAreAsync()` and `ThatAreNotAsync()` for filtering in method assertions - [#1725](https://github.com/fluentassertions/fluentassertions/pull/1725)
* Adding `ThatAreVirtual()` and `ThatAreNotVirtual()` for filtering in method assertions - [#1744](https://github.com/fluentassertions/fluentassertions/pull/1744)
* Adding `ThatAreStatic()` and `ThatAreNotStatic()` for filtering in method assertions - [#1740](https://github.com/fluentassertions/fluentassertions/pull/1740)
* Adding collection content to assertion messages for `HaveCountGreaterThan()`, `HaveCountGreaterThanOrEqualTo()`, `HaveCountLessThan()` and `HaveCountLessThanOrEqualTo()` - [#1760](https://github.com/fluentassertions/fluentassertions/pull/1760)

### Fixes
* Prevent multiple enumeration of `IEnumerable`s in parameter-less `ContainSingle()` - [#1753](https://github.com/fluentassertions/fluentassertions/pull/1753)
* Querying methods on classes, e.g. `typeof(MyController).Methods()`, now also includes static methods - [#1740](https://github.com/fluentassertions/fluentassertions/pull/1740)
* Change `HaveCount()` assertion message order to state expected and actual collection count before dumping its content` - [#1760](https://github.com/fluentassertions/fluentassertions/pull/1760)
* `CompleteWithinAsync` did not take initial sync computation into account when measuring execution time - [1762](https://github.com/fluentassertions/fluentassertions/pull/1762).

Expand Down