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

Added enumeration predicates and conditions #140

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/NetArchTest.Rules/Conditions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -717,5 +717,25 @@ public ConditionList MeetCustomRule(ICustomRule rule)
_sequence.AddFunctionCall(FunctionDelegates.MeetCustomRule, rule, true);
return new ConditionList(_types, _should, _sequence);
}

/// <summary>
/// Selects types that are enumerations.
/// </summary>
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList BeEnums()
{
_sequence.AddFunctionCall(FunctionDelegates.BeEnum, true, true);
return new ConditionList(_types, _should, _sequence);
}

/// <summary>
/// Selects types that are not enumerations.
/// </summary>
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
public ConditionList NotBeEnums()
{
_sequence.AddFunctionCall(FunctionDelegates.BeEnum, true, false);
return new ConditionList(_types, _should, _sequence);
}
}
}
9 changes: 9 additions & 0 deletions src/NetArchTest.Rules/FunctionDelegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,5 +322,14 @@ bool ClassIsStatic(TypeDefinition c) =>
{
return input.Where(t => rule.MeetsRule(t) == condition);
};

/// <summary>
/// Function for finding enumerations.
/// </summary>
internal static readonly FunctionDelegate<bool> BeEnum =
delegate (IEnumerable<TypeDefinition> input, bool dummy, bool condition)
{
return input.Where(c => c.IsEnum == condition);
};
}
}
20 changes: 20 additions & 0 deletions src/NetArchTest.Rules/Predicates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -734,5 +734,25 @@ public PredicateList MeetCustomRule(ICustomRule rule)
_sequence.AddFunctionCall(FunctionDelegates.MeetCustomRule, rule, true);
return new PredicateList(_types, _sequence);
}

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

/// <summary>
/// Selects types that are not enumerations.
/// </summary>
/// <returns>An updated set of predicates that can be applied to a list of types.</returns>
public PredicateList AreNotEnums()
{
_sequence.AddFunctionCall(FunctionDelegates.BeEnum, true, false);
return new PredicateList(_types, _sequence);
}
}
}
30 changes: 30 additions & 0 deletions test/NetArchTest.Rules.UnitTests/ConditionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
30 changes: 30 additions & 0 deletions test/NetArchTest.Rules.UnitTests/PredicateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<Type>(typeof(ExampleEnum), result);
Assert.Contains<Type>(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<Type>(typeof(NoEnum), result);
}
}
}
6 changes: 6 additions & 0 deletions test/NetArchTest.TestStructure/Enumerations/ExampleEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace NetArchTest.TestStructure.Enumerations
{
public enum ExampleEnum
{
}
}
9 changes: 9 additions & 0 deletions test/NetArchTest.TestStructure/Enumerations/FlagEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace NetArchTest.TestStructure.Enumerations
{
[Flags]
public enum FlagEnum
{
}
}
6 changes: 6 additions & 0 deletions test/NetArchTest.TestStructure/Enumerations/NoEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace NetArchTest.TestStructure.Enumerations
{
public class NoEnum
{
}
}