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

Add ThatAre[Not]Structs in TypeSelector.cs #2056

Closed
26 changes: 26 additions & 0 deletions Src/FluentAssertions/Types/TypeSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,32 @@ public TypeSelector ThatAreNotClasses()
return this;
}

/// <summary>
/// Determines wheter the type is a struct (a value type but not an Enum)
jnyrup marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <returns></returns>
public TypeSelector ThatAreStruct()
jnyrup marked this conversation as resolved.
Show resolved Hide resolved
{
// A Type is a struct when it is a valueType, but Enums and primitives are also
// valueTypes so they must be excluded
types = types.Where(t => t.IsValueType && !t.IsEnum
&& !t.IsPrimitive && !t.IsEquivalentTo(typeof(decimal)))
.ToList();
return this;
}

/// <summary>
/// Determines wheter the type is not a struct
/// </summary>
/// <returns></returns>
public TypeSelector ThatAreNotStruct()
{
types = types.Where(t => !t.IsValueType || t.IsEnum
|| t.IsPrimitive || t.IsEquivalentTo(typeof(decimal)))
.ToList();
return this;
}

/// <summary>
/// Determines whether the type is static
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2654,8 +2654,10 @@ namespace FluentAssertions.Types
where TAttribute : System.Attribute { }
public FluentAssertions.Types.TypeSelector ThatAreNotInNamespace(string @namespace) { }
public FluentAssertions.Types.TypeSelector ThatAreNotStatic() { }
public FluentAssertions.Types.TypeSelector ThatAreNotStruct() { }
public FluentAssertions.Types.TypeSelector ThatAreNotUnderNamespace(string @namespace) { }
public FluentAssertions.Types.TypeSelector ThatAreStatic() { }
public FluentAssertions.Types.TypeSelector ThatAreStruct() { }
public FluentAssertions.Types.TypeSelector ThatAreUnderNamespace(string @namespace) { }
public FluentAssertions.Types.TypeSelector ThatDeriveFrom<TBase>() { }
public FluentAssertions.Types.TypeSelector ThatDoNotDeriveFrom<TBase>() { }
Expand Down Expand Up @@ -2765,4 +2767,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 @@ -2784,8 +2784,10 @@ namespace FluentAssertions.Types
where TAttribute : System.Attribute { }
public FluentAssertions.Types.TypeSelector ThatAreNotInNamespace(string @namespace) { }
public FluentAssertions.Types.TypeSelector ThatAreNotStatic() { }
public FluentAssertions.Types.TypeSelector ThatAreNotStruct() { }
public FluentAssertions.Types.TypeSelector ThatAreNotUnderNamespace(string @namespace) { }
public FluentAssertions.Types.TypeSelector ThatAreStatic() { }
public FluentAssertions.Types.TypeSelector ThatAreStruct() { }
public FluentAssertions.Types.TypeSelector ThatAreUnderNamespace(string @namespace) { }
public FluentAssertions.Types.TypeSelector ThatDeriveFrom<TBase>() { }
public FluentAssertions.Types.TypeSelector ThatDoNotDeriveFrom<TBase>() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2656,8 +2656,10 @@ namespace FluentAssertions.Types
where TAttribute : System.Attribute { }
public FluentAssertions.Types.TypeSelector ThatAreNotInNamespace(string @namespace) { }
public FluentAssertions.Types.TypeSelector ThatAreNotStatic() { }
public FluentAssertions.Types.TypeSelector ThatAreNotStruct() { }
public FluentAssertions.Types.TypeSelector ThatAreNotUnderNamespace(string @namespace) { }
public FluentAssertions.Types.TypeSelector ThatAreStatic() { }
public FluentAssertions.Types.TypeSelector ThatAreStruct() { }
public FluentAssertions.Types.TypeSelector ThatAreUnderNamespace(string @namespace) { }
public FluentAssertions.Types.TypeSelector ThatDeriveFrom<TBase>() { }
public FluentAssertions.Types.TypeSelector ThatDoNotDeriveFrom<TBase>() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2656,8 +2656,10 @@ namespace FluentAssertions.Types
where TAttribute : System.Attribute { }
public FluentAssertions.Types.TypeSelector ThatAreNotInNamespace(string @namespace) { }
public FluentAssertions.Types.TypeSelector ThatAreNotStatic() { }
public FluentAssertions.Types.TypeSelector ThatAreNotStruct() { }
public FluentAssertions.Types.TypeSelector ThatAreNotUnderNamespace(string @namespace) { }
public FluentAssertions.Types.TypeSelector ThatAreStatic() { }
public FluentAssertions.Types.TypeSelector ThatAreStruct() { }
public FluentAssertions.Types.TypeSelector ThatAreUnderNamespace(string @namespace) { }
public FluentAssertions.Types.TypeSelector ThatDeriveFrom<TBase>() { }
public FluentAssertions.Types.TypeSelector ThatDoNotDeriveFrom<TBase>() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2605,8 +2605,10 @@ namespace FluentAssertions.Types
where TAttribute : System.Attribute { }
public FluentAssertions.Types.TypeSelector ThatAreNotInNamespace(string @namespace) { }
public FluentAssertions.Types.TypeSelector ThatAreNotStatic() { }
public FluentAssertions.Types.TypeSelector ThatAreNotStruct() { }
public FluentAssertions.Types.TypeSelector ThatAreNotUnderNamespace(string @namespace) { }
public FluentAssertions.Types.TypeSelector ThatAreStatic() { }
public FluentAssertions.Types.TypeSelector ThatAreStruct() { }
public FluentAssertions.Types.TypeSelector ThatAreUnderNamespace(string @namespace) { }
public FluentAssertions.Types.TypeSelector ThatDeriveFrom<TBase>() { }
public FluentAssertions.Types.TypeSelector ThatDoNotDeriveFrom<TBase>() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2656,8 +2656,10 @@ namespace FluentAssertions.Types
where TAttribute : System.Attribute { }
public FluentAssertions.Types.TypeSelector ThatAreNotInNamespace(string @namespace) { }
public FluentAssertions.Types.TypeSelector ThatAreNotStatic() { }
public FluentAssertions.Types.TypeSelector ThatAreNotStruct() { }
public FluentAssertions.Types.TypeSelector ThatAreNotUnderNamespace(string @namespace) { }
public FluentAssertions.Types.TypeSelector ThatAreStatic() { }
public FluentAssertions.Types.TypeSelector ThatAreStruct() { }
public FluentAssertions.Types.TypeSelector ThatAreUnderNamespace(string @namespace) { }
public FluentAssertions.Types.TypeSelector ThatDeriveFrom<TBase>() { }
public FluentAssertions.Types.TypeSelector ThatDoNotDeriveFrom<TBase>() { }
Expand Down
56 changes: 56 additions & 0 deletions Tests/FluentAssertions.Specs/Types/TypeSelectorSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Internal.Other.Test;
using Internal.Other.Test.Common;
using Internal.StaticAndNonStaticClasses.Test;
using Internal.StructsAndNotStructs.Test;
using Internal.UnwrapSelectorTestTypes.Test;
using Xunit;
using ISomeInterface = Internal.Main.Test.ISomeInterface;
Expand Down Expand Up @@ -495,6 +496,42 @@ public void When_selecting_types_that_are_not_classes_it_should_return_the_corre
.And.Contain(typeof(NotOnlyClassesEnumeration));
}

[Fact]
public void When_selecting_types_that_are_structs_it_should_return_the_correct_types()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 What about record structs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will extend the tests for record structs and i think it should work fine with record structs as well.

{
// Arrange
Assembly assembly = typeof(InternalStructType).GetTypeInfo().Assembly;

// Act
IEnumerable<Type> types = AllTypes.From(assembly)
.ThatAreInNamespace("Internal.StructsAndNotStructs.Test")
.ThatAreStruct();

// Assert
types.Should()
.ContainSingle()
.Which.Should().Be(typeof(InternalStructType));
}

[Fact]
public void When_selecting_types_that_are_not_structs_it_should_return_the_correct_types()
{
// Arrange
Assembly assembly = typeof(InternalClassAndNotStruct).GetTypeInfo().Assembly;

// Act
IEnumerable<Type> types = AllTypes.From(assembly)
.ThatAreInNamespace("Internal.StructsAndNotStructs.Test")
.ThatAreNotStruct();

// Assert
types.Should()
.HaveCount(3)
.And.Contain(typeof(InternalClassAndNotStruct))
.And.Contain(typeof(InternalEnumAndNotStruct))
.And.Contain(typeof(InternalInterfaceAndNotStruct));
}

[Fact]
public void When_selecting_types_that_are_static_classes_it_should_return_the_correct_types()
{
Expand Down Expand Up @@ -695,6 +732,25 @@ internal class NotAStaticClass
}
}

namespace Internal.StructsAndNotStructs.Test
{
internal struct InternalStructType
{
}

internal enum InternalEnumAndNotStruct
{
}

internal interface InternalInterfaceAndNotStruct
{
}

internal class InternalClassAndNotStruct
{
}
}

namespace Internal.UnwrapSelectorTestTypes.Test
{
internal class ClassToExploreUnwrappedTaskTypes
Expand Down
1 change: 1 addition & 0 deletions docs/_pages/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ sidebar:
## Unreleased

### What's new
* Added `ThatAre[Not]Struct method for Types Filter - [#2056](https://github.com/fluentassertions/fluentassertions/pull/2056)
94sedighi marked this conversation as resolved.
Show resolved Hide resolved
* Added `BeOneOf` methods for object comparisons and `IComparable`s - [#2028](https://github.com/fluentassertions/fluentassertions/pull/2028)
* Added `BeCloseTo` and `NotBeCloseTo` to `TimeOnly` - [#2030](https://github.com/fluentassertions/fluentassertions/pull/2030)

Expand Down
11 changes: 11 additions & 0 deletions docs/_pages/typesandmethods.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ var properties = types.Properties().ThatArePublicOrInternal;
properties.Should().BeVirtual();
```

You can also filter types that are Struct

```csharp
AllTypes.From(assembly)
.ThatAreStruct();

AllTypes.From(assembly)
.ThatAreNotStruct();
```


Alternatively you can use this more fluent syntax instead.

```csharp
Expand Down