Skip to content

Commit ec7788c

Browse files
committed
add Func<TypeDefinition, bool> version of MeetCustomRule
1 parent 97da451 commit ec7788c

4 files changed

Lines changed: 34 additions & 3 deletions

File tree

src/NetArchTest.Rules/Condition.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
2-
using NetArchTest.Assemblies;
32
using System.Collections.Generic;
3+
using Mono.Cecil;
4+
using NetArchTest.Assemblies;
45
using NetArchTest.Functions;
56
using NetArchTest.RuleEngine;
67

@@ -204,5 +205,14 @@ public ConditionList MeetCustomRule(ICustomRule rule)
204205
AddFunctionCall(x => FunctionDelegates.MeetCustomRule(x, rule, true));
205206
return CreateConditionList();
206207
}
208+
/// <summary>
209+
/// Selects types that meet a custom rule.
210+
/// </summary>
211+
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
212+
public ConditionList MeetCustomRule(Func<TypeDefinition, bool> rule)
213+
{
214+
AddFunctionCall(x => FunctionDelegates.MeetCustomRule(x, rule, true));
215+
return CreateConditionList();
216+
}
207217
}
208218
}

src/NetArchTest.Rules/Functions/FunctionDelegates.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,16 @@ internal static IEnumerable<TypeSpec> MeetCustomRule(IEnumerable<TypeSpec> input
7979
return input.Where(t => !rule.MeetsRule(t.Definition));
8080
}
8181
}
82+
internal static IEnumerable<TypeSpec> MeetCustomRule(IEnumerable<TypeSpec> input, Func<TypeDefinition, bool> rule, bool condition)
83+
{
84+
if (condition)
85+
{
86+
return input.Where(t => rule(t.Definition));
87+
}
88+
else
89+
{
90+
return input.Where(t => !rule(t.Definition));
91+
}
92+
}
8293
}
8394
}

src/NetArchTest.Rules/Predicate.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using Mono.Cecil;
34
using NetArchTest.Assemblies;
45
using NetArchTest.Functions;
56
using NetArchTest.RuleEngine;
@@ -201,5 +202,14 @@ public PredicateList MeetCustomRule(ICustomRule rule)
201202
AddFunctionCall(x => FunctionDelegates.MeetCustomRule(x, rule, true));
202203
return CreatePredicateList();
203204
}
205+
/// <summary>
206+
/// Selects types that meet a custom rule.
207+
/// </summary>
208+
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
209+
public PredicateList MeetCustomRule(Func<TypeDefinition, bool> rule)
210+
{
211+
AddFunctionCall(x => FunctionDelegates.MeetCustomRule(x, rule, true));
212+
return CreatePredicateList();
213+
}
204214
}
205215
}

test/NetArchTest.Rules.UnitTests/ConditionTests_Special.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public void BeImmutableExternally()
5858
}
5959

6060

61-
[Fact(DisplayName = "AreStateless")]
62-
public void AreStateless()
61+
[Fact(DisplayName = "BeStateless")]
62+
public void BeStateless()
6363
{
6464
var result = Types
6565
.InAssembly(Assembly.GetAssembly(typeof(StatelessClass_StaticField)))

0 commit comments

Comments
 (0)