Skip to content

Commit

Permalink
Add ThatAre[Not]Struct in TypeSelector.cs as mentioned in Issue fluen…
Browse files Browse the repository at this point in the history
  • Loading branch information
94sedighi committed Dec 5, 2022
1 parent 0a04100 commit b1162dd
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 1 deletion.
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)
/// </summary>
/// <returns></returns>
public TypeSelector ThatAreStruct()
{
// 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()
{
// 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

0 comments on commit b1162dd

Please sign in to comment.