diff --git a/src/NetArchTest.Rules/Conditions.cs b/src/NetArchTest.Rules/Conditions.cs index 0d36634..db7ebea 100644 --- a/src/NetArchTest.Rules/Conditions.cs +++ b/src/NetArchTest.Rules/Conditions.cs @@ -717,5 +717,25 @@ public ConditionList MeetCustomRule(ICustomRule rule) _sequence.AddFunctionCall(FunctionDelegates.MeetCustomRule, rule, true); return new ConditionList(_types, _should, _sequence); } + + /// + /// Selects types that are enumerations. + /// + /// An updated set of conditions that can be applied to a list of types. + public ConditionList BeEnums() + { + _sequence.AddFunctionCall(FunctionDelegates.BeEnum, true, true); + return new ConditionList(_types, _should, _sequence); + } + + /// + /// Selects types that are not enumerations. + /// + /// An updated set of conditions that can be applied to a list of types. + public ConditionList NotBeEnums() + { + _sequence.AddFunctionCall(FunctionDelegates.BeEnum, true, false); + return new ConditionList(_types, _should, _sequence); + } } } diff --git a/src/NetArchTest.Rules/FunctionDelegates.cs b/src/NetArchTest.Rules/FunctionDelegates.cs index 9bbe775..72e54c7 100644 --- a/src/NetArchTest.Rules/FunctionDelegates.cs +++ b/src/NetArchTest.Rules/FunctionDelegates.cs @@ -322,5 +322,14 @@ bool ClassIsStatic(TypeDefinition c) => { return input.Where(t => rule.MeetsRule(t) == condition); }; + + /// + /// Function for finding enumerations. + /// + internal static readonly FunctionDelegate BeEnum = + delegate (IEnumerable input, bool dummy, bool condition) + { + return input.Where(c => c.IsEnum == condition); + }; } } \ No newline at end of file diff --git a/src/NetArchTest.Rules/Predicates.cs b/src/NetArchTest.Rules/Predicates.cs index 5d53570..5bb2b1e 100644 --- a/src/NetArchTest.Rules/Predicates.cs +++ b/src/NetArchTest.Rules/Predicates.cs @@ -734,5 +734,25 @@ public PredicateList MeetCustomRule(ICustomRule rule) _sequence.AddFunctionCall(FunctionDelegates.MeetCustomRule, rule, true); return new PredicateList(_types, _sequence); } + + /// + /// Selects types that are enumerations. + /// + /// An updated set of predicates that can be applied to a list of types. + public PredicateList AreEnums() + { + _sequence.AddFunctionCall(FunctionDelegates.BeEnum, true, true); + return new PredicateList(_types, _sequence); + } + + /// + /// Selects types that are not enumerations. + /// + /// An updated set of predicates that can be applied to a list of types. + public PredicateList AreNotEnums() + { + _sequence.AddFunctionCall(FunctionDelegates.BeEnum, true, false); + return new PredicateList(_types, _sequence); + } } } diff --git a/test/NetArchTest.Rules.UnitTests/ConditionTests.cs b/test/NetArchTest.Rules.UnitTests/ConditionTests.cs index 77bbad5..295c204 100644 --- a/test/NetArchTest.Rules.UnitTests/ConditionTests.cs +++ b/test/NetArchTest.Rules.UnitTests/ConditionTests.cs @@ -984,5 +984,35 @@ public void MeetCustomRule_MatchesFound_ClassSelected() // The custom rule was executed at least once Assert.True(rule.TestMethodCalled); } + + [Fact(DisplayName = "Types can be selected if they are enumerations.")] + public void AreEnums_MatchesFound_EnumsSelected() + { + var result = Types + .InAssembly(Assembly.GetAssembly(typeof(ClassA1))) + .That() + .ResideInNamespace("NetArchTest.TestStructure.Enumerations") + .And() + .DoNotHaveName("NoEnum") + .Should() + .BeEnums().GetResult(); + + Assert.True(result.IsSuccessful); + } + + [Fact(DisplayName = "Types can be selected if they are not enumerations.")] + public void AreNotEnums_MatchesFound_EnumsSelected() + { + var result = Types + .InAssembly(Assembly.GetAssembly(typeof(ClassA1))) + .That() + .ResideInNamespace("NetArchTest.TestStructure.Enumerations") + .And() + .HaveName("NoEnum") + .Should() + .NotBeEnums().GetResult(); + + Assert.True(result.IsSuccessful); + } } } diff --git a/test/NetArchTest.Rules.UnitTests/PredicateTests.cs b/test/NetArchTest.Rules.UnitTests/PredicateTests.cs index 6ccd57a..a5feb97 100644 --- a/test/NetArchTest.Rules.UnitTests/PredicateTests.cs +++ b/test/NetArchTest.Rules.UnitTests/PredicateTests.cs @@ -29,6 +29,7 @@ namespace NetArchTest.Rules.UnitTests using Xunit; using NetArchTest.TestStructure.Nullable; using NetArchTest.TestStructure.Dependencies.Examples; + using NetArchTest.TestStructure.Enumerations; public class PredicateTests { @@ -1096,5 +1097,34 @@ public void GetResult_Doesnt_Evaluate_Twice() Assert.True(customRule.TimesTimesCalled == 1); } + + [Fact(DisplayName = "Types can be selected if they are enumerations.")] + public void AreEnums_MatchesFound_EnumsSelected() + { + var result = Types + .InAssembly(Assembly.GetAssembly(typeof(ClassA1))) + .That() + .ResideInNamespace("NetArchTest.TestStructure.Enumerations") + .And() + .AreEnums().GetTypes(); + + Assert.Equal(2, result.Count()); // Two types found + Assert.Contains(typeof(ExampleEnum), result); + Assert.Contains(typeof(FlagEnum), result); + } + + [Fact(DisplayName = "Types can be selected if they are not enumerations.")] + public void AreNotEnums_MatchesFound_EnumsSelected() + { + var result = Types + .InAssembly(Assembly.GetAssembly(typeof(ClassA1))) + .That() + .ResideInNamespace("NetArchTest.TestStructure.Enumerations") + .And() + .AreNotEnums().GetTypes(); + + Assert.Single(result); // One type found + Assert.Contains(typeof(NoEnum), result); + } } } diff --git a/test/NetArchTest.TestStructure/Enumerations/ExampleEnum.cs b/test/NetArchTest.TestStructure/Enumerations/ExampleEnum.cs new file mode 100644 index 0000000..d8c2a8d --- /dev/null +++ b/test/NetArchTest.TestStructure/Enumerations/ExampleEnum.cs @@ -0,0 +1,6 @@ +namespace NetArchTest.TestStructure.Enumerations +{ + public enum ExampleEnum + { + } +} diff --git a/test/NetArchTest.TestStructure/Enumerations/FlagEnum.cs b/test/NetArchTest.TestStructure/Enumerations/FlagEnum.cs new file mode 100644 index 0000000..e34b182 --- /dev/null +++ b/test/NetArchTest.TestStructure/Enumerations/FlagEnum.cs @@ -0,0 +1,9 @@ +using System; + +namespace NetArchTest.TestStructure.Enumerations +{ + [Flags] + public enum FlagEnum + { + } +} diff --git a/test/NetArchTest.TestStructure/Enumerations/NoEnum.cs b/test/NetArchTest.TestStructure/Enumerations/NoEnum.cs new file mode 100644 index 0000000..b09682d --- /dev/null +++ b/test/NetArchTest.TestStructure/Enumerations/NoEnum.cs @@ -0,0 +1,6 @@ +namespace NetArchTest.TestStructure.Enumerations +{ + public class NoEnum + { + } +}