Skip to content

Commit

Permalink
BenMorris/NetArchTest#126 - Add support for structs, enums and delegates
Browse files Browse the repository at this point in the history
  • Loading branch information
NeVeSpl committed Nov 20, 2023
1 parent 1a99cd4 commit d2b14ed
Show file tree
Hide file tree
Showing 19 changed files with 553 additions and 229 deletions.
60 changes: 60 additions & 0 deletions src/NetArchTest.Rules/Condition.Traits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,66 @@ public ConditionList NotBeClasses()
return CreateConditionList();
}

/// <summary>
/// Selects types that are structures.
/// </summary>
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList BeStructures()
{
AddFunctionCall(x => FunctionDelegates.BeStruct(x, true));
return CreateConditionList();
}

/// <summary>
/// Selects types that are not structures.
/// </summary>
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList NotBeStructures()
{
AddFunctionCall(x => FunctionDelegates.BeStruct(x, false));
return CreateConditionList();
}

/// <summary>
/// Selects types that are enums.
/// </summary>
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList BeEnums()
{
AddFunctionCall(x => FunctionDelegates.BeEnum(x, true));
return CreateConditionList();
}

/// <summary>
/// Selects types that are not enums.
/// </summary>
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList NotBeEnums()
{
AddFunctionCall(x => FunctionDelegates.BeEnum(x, false));
return CreateConditionList();
}

/// <summary>
/// Selects types that are delegates.
/// </summary>
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList BeDelegates()
{
AddFunctionCall(x => FunctionDelegates.BeDelegate(x, true));
return CreateConditionList();
}

/// <summary>
/// Selects types that are not delegates.
/// </summary>
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList NotBeDelegates()
{
AddFunctionCall(x => FunctionDelegates.BeDelegate(x, false));
return CreateConditionList();
}

/// <summary>
/// Selects types that have generic parameters.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,16 @@ public static string GetNamespace(this TypeDefinition typeDefinition)
}
return typeDefinition.Namespace;
}



public static bool IsDelegate(this TypeDefinition typeDefinition)
{
return typeDefinition.IsClass && typeDefinition.BaseType?.FullName == "System.MulticastDelegate";
}
public static bool IsStruct(this TypeDefinition typeDefinition)
{
return typeDefinition.IsValueType && typeDefinition.BaseType?.FullName == "System.ValueType";
}
}
}
105 changes: 70 additions & 35 deletions src/NetArchTest.Rules/Functions/FunctionDelegates.Traits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,59 @@
namespace NetArchTest.Functions
{
internal static partial class FunctionDelegates
{
/// <summary> Function for finding abstract classes. </summary>
internal static IEnumerable<TypeSpec> BeAbstract(IEnumerable<TypeSpec> input, bool condition)
{
// Types

internal static IEnumerable<TypeSpec> BeClass(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
{
return input.Where(c => c.Definition.IsAbstract);
return input.Where(c => IsClass(c.Definition));
}
else
{
return input.Where(c => !c.Definition.IsAbstract);
return input.Where(c => !IsClass(c.Definition));
}

bool IsClass(TypeDefinition c) => c.IsClass && !c.IsValueType && !c.IsDelegate();
}

/// <summary> Function for finding classes. </summary>
internal static IEnumerable<TypeSpec> BeClass(IEnumerable<TypeSpec> input, bool condition)
internal static IEnumerable<TypeSpec> BeStruct(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
{
return input.Where(c => c.Definition.IsClass);
return input.Where(c => c.Definition.IsStruct());
}
else
{
return input.Where(c => !c.Definition.IsClass);
return input.Where(c => !c.Definition.IsStruct());
}
}

internal static IEnumerable<TypeSpec> BeEnum(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
{
return input.Where(c => c.Definition.IsEnum);
}
else
{
return input.Where(c => !c.Definition.IsEnum);
}
}

internal static IEnumerable<TypeSpec> BeDelegate(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
{
return input.Where(c => c.Definition.IsDelegate());
}
else
{
return input.Where(c => !c.Definition.IsDelegate());
}
}

/// <summary> Function for finding interfaces. </summary>
internal static IEnumerable<TypeSpec> BeInterface(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
Expand All @@ -47,7 +72,20 @@ internal static IEnumerable<TypeSpec> BeInterface(IEnumerable<TypeSpec> input, b
}
}

/// <summary> Function for finding static classes. </summary>
// Modifiers

internal static IEnumerable<TypeSpec> BeAbstract(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
{
return input.Where(c => c.Definition.IsAbstract);
}
else
{
return input.Where(c => !c.Definition.IsAbstract);
}
}

internal static IEnumerable<TypeSpec> BeStatic(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
Expand All @@ -62,7 +100,20 @@ internal static IEnumerable<TypeSpec> BeStatic(IEnumerable<TypeSpec> input, bool
bool ClassIsStatic(TypeSpec c) => c.Definition.IsAbstract && c.Definition.IsSealed && !c.Definition.IsInterface && !c.Definition.GetConstructors().Any(m => m.IsPublic);
}

/// <summary> Function for finding types with generic parameters. </summary>
internal static IEnumerable<TypeSpec> BeSealed(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
{
return input.Where(c => c.Definition.IsSealed);
}
else
{
return input.Where(c => !c.Definition.IsSealed);
}
}

// Generic

internal static IEnumerable<TypeSpec> BeGeneric(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
Expand All @@ -75,8 +126,8 @@ internal static IEnumerable<TypeSpec> BeGeneric(IEnumerable<TypeSpec> input, boo
}
}


/// <summary> Function for finding nested classes. </summary>
// Nested
internal static IEnumerable<TypeSpec> BeNested(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
Expand All @@ -89,7 +140,6 @@ internal static IEnumerable<TypeSpec> BeNested(IEnumerable<TypeSpec> input, bool
}
}

/// <summary> Function for finding nested public classes. </summary>
internal static IEnumerable<TypeSpec> BeNestedPublic(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
Expand All @@ -102,7 +152,6 @@ internal static IEnumerable<TypeSpec> BeNestedPublic(IEnumerable<TypeSpec> input
}
}

/// <summary> Function for finding nested private classes. </summary>
internal static IEnumerable<TypeSpec> BeNestedPrivate(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
Expand All @@ -115,8 +164,8 @@ internal static IEnumerable<TypeSpec> BeNestedPrivate(IEnumerable<TypeSpec> inpu
}
}

// Access Modifiers

/// <summary> Function for finding public classes. </summary>
internal static IEnumerable<TypeSpec> BePublic(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
Expand All @@ -128,21 +177,9 @@ internal static IEnumerable<TypeSpec> BePublic(IEnumerable<TypeSpec> input, bool
return input.Where(c => c.Definition.IsNested ? !c.Definition.IsNestedPublic : c.Definition.IsNotPublic);
}
}

/// <summary> Function for finding sealed classes. </summary>
internal static IEnumerable<TypeSpec> BeSealed(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
{
return input.Where(c => c.Definition.IsSealed);
}
else
{
return input.Where(c => !c.Definition.IsSealed);
}
}

/// <summary> Function for finding immutable classes. </summary>

//

internal static IEnumerable<TypeSpec> BeImmutable(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
Expand All @@ -154,9 +191,7 @@ internal static IEnumerable<TypeSpec> BeImmutable(IEnumerable<TypeSpec> input, b
return input.Where(c => !c.Definition.IsImmutable());
}
}


/// <summary> Function for finding nullable classes. </summary>

internal static IEnumerable<TypeSpec> HasNullableMembers(IEnumerable<TypeSpec> input, bool condition)
{
if (condition)
Expand Down
60 changes: 60 additions & 0 deletions src/NetArchTest.Rules/Predicate.Traits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,66 @@ public PredicateList AreNotClasses()
return CreatePredicateList();
}

/// <summary>
/// Selects types that are structures.
/// </summary>
/// <returns>An updated set of predicates that can be applied to a list of types.</returns>
public PredicateList AreStructures()
{
AddFunctionCall(x => FunctionDelegates.BeStruct(x, true));
return CreatePredicateList();
}

/// <summary>
/// Selects types that are not structures.
/// </summary>
/// <returns>An updated set of predicates that can be applied to a list of types.</returns>
public PredicateList AreNotStructures()
{
AddFunctionCall(x => FunctionDelegates.BeStruct(x, false));
return CreatePredicateList();
}

/// <summary>
/// Selects types that are delegates.
/// </summary>
/// <returns>An updated set of predicates that can be applied to a list of types.</returns>
public PredicateList AreDelegates()
{
AddFunctionCall(x => FunctionDelegates.BeDelegate(x, true));
return CreatePredicateList();
}

/// <summary>
/// Selects types that are not delegates.
/// </summary>
/// <returns>An updated set of predicates that can be applied to a list of types.</returns>
public PredicateList AreNotDelegates()
{
AddFunctionCall(x => FunctionDelegates.BeDelegate(x, false));
return CreatePredicateList();
}

/// <summary>
/// Selects types that are enums.
/// </summary>
/// <returns>An updated set of predicates that can be applied to a list of types.</returns>
public PredicateList AreEnums()
{
AddFunctionCall(x => FunctionDelegates.BeEnum(x, true));
return CreatePredicateList();
}

/// <summary>
/// Selects types that are not enums.
/// </summary>
/// <returns>An updated set of predicates that can be applied to a list of types.</returns>
public PredicateList AreNotEnums()
{
AddFunctionCall(x => FunctionDelegates.BeEnum(x, false));
return CreatePredicateList();
}

/// <summary>
/// Selects types that have generic parameters.
/// </summary>
Expand Down
Loading

0 comments on commit d2b14ed

Please sign in to comment.