Skip to content

Commit a4d6d5b

Browse files
committed
introduce Options
1 parent fa3b718 commit a4d6d5b

23 files changed

+123
-198
lines changed

src/NetArchTest.Rules/Condition.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal Condition(RuleContext rule)
2222
}
2323
internal Condition(RuleContext rule, bool should) : this(rule)
2424
{
25-
rule.ConditionContext.Should= should;
25+
rule.ConditionContext.Should = should;
2626
}
2727

2828

@@ -34,11 +34,14 @@ private void AddFunctionCall(Func<IEnumerable<TypeSpec>, IEnumerable<TypeSpec>>
3434
{
3535
context.Sequence.AddFunctionCall(func);
3636
}
37+
private void AddFunctionCall(Func<FunctionSequenceExecutionContext, IEnumerable<TypeSpec>, IEnumerable<TypeSpec>> func)
38+
{
39+
context.Sequence.AddFunctionCall(func);
40+
}
41+
3742

3843

39-
4044

41-
4245

4346
/// <summary>
4447
/// Selects types are decorated with a specific custom attribut.

src/NetArchTest.Rules/ConditionList.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,24 @@ public Condition Or()
4444
/// Returns an indication of whether all the selected types satisfy the conditions.
4545
/// </summary>
4646
/// <returns>An indication of whether the conditions are true, along with a list of types failing the check if they are not.</returns>
47-
public TestResult GetResult()
47+
public TestResult GetResult(Options options = null)
4848
{
49-
return rule.GetResult();
49+
return rule.GetResult(options);
5050
}
5151

5252
/// <summary>
5353
/// Returns the list of types that satisfy the conditions.
5454
/// </summary>
5555
/// <returns>A list of types.</returns>
56-
public IEnumerable<IType> GetTypes()
56+
public IEnumerable<IType> GetTypes(Options options = null)
5757
{
58-
return rule.GetTypes();
58+
return rule.GetTypes(options);
5959
}
6060

6161

62-
internal IEnumerable<Type> GetReflectionTypes()
62+
internal IEnumerable<Type> GetReflectionTypes(Options options = null)
6363
{
64-
return rule.GetReflectionTypes();
64+
return rule.GetReflectionTypes(options);
6565
}
6666
}
6767
}

src/NetArchTest.Rules/Condition_Dependencies.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public sealed partial class Condition
1111
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
1212
public ConditionList HaveDependencyOnAny(params string[] dependencies)
1313
{
14-
context.Sequence.AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveDependencyOnAny(context, inputTypes, dependencies, true));
14+
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveDependencyOnAny(context, inputTypes, dependencies, true));
1515
return CreateConditionList();
1616
}
1717

@@ -22,7 +22,7 @@ public ConditionList HaveDependencyOnAny(params string[] dependencies)
2222
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
2323
public ConditionList HaveDependencyOnAll(params string[] dependencies)
2424
{
25-
context.Sequence.AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveDependencyOnAll(context, inputTypes, dependencies, true));
25+
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveDependencyOnAll(context, inputTypes, dependencies, true));
2626
return CreateConditionList();
2727
}
2828

@@ -33,7 +33,7 @@ public ConditionList HaveDependencyOnAll(params string[] dependencies)
3333
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
3434
public ConditionList OnlyHaveDependencyOn(params string[] dependencies)
3535
{
36-
context.Sequence.AddFunctionCall((context, inputTypes) => FunctionDelegates.OnlyHaveDependenciesOnAnyOrNone(context, inputTypes, dependencies, true));
36+
AddFunctionCall((context, inputTypes) => FunctionDelegates.OnlyHaveDependenciesOnAnyOrNone(context, inputTypes, dependencies, true));
3737
return CreateConditionList();
3838
}
3939

@@ -44,7 +44,7 @@ public ConditionList OnlyHaveDependencyOn(params string[] dependencies)
4444
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
4545
public ConditionList NotHaveDependencyOnAny(params string[] dependencies)
4646
{
47-
context.Sequence.AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveDependencyOnAny(context, inputTypes, dependencies, false));
47+
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveDependencyOnAny(context, inputTypes, dependencies, false));
4848
return CreateConditionList();
4949
}
5050

@@ -55,7 +55,7 @@ public ConditionList NotHaveDependencyOnAny(params string[] dependencies)
5555
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
5656
public ConditionList NotHaveDependencyOnAll(params string[] dependencies)
5757
{
58-
context.Sequence.AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveDependencyOnAll(context, inputTypes, dependencies, false));
58+
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveDependencyOnAll(context, inputTypes, dependencies, false));
5959
return CreateConditionList();
6060
}
6161

@@ -66,7 +66,7 @@ public ConditionList NotHaveDependencyOnAll(params string[] dependencies)
6666
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
6767
public ConditionList HaveDependencyOtherThan(params string[] dependencies)
6868
{
69-
context.Sequence.AddFunctionCall((context, inputTypes) => FunctionDelegates.OnlyHaveDependenciesOnAnyOrNone(context, inputTypes, dependencies, false));
69+
AddFunctionCall((context, inputTypes) => FunctionDelegates.OnlyHaveDependenciesOnAnyOrNone(context, inputTypes, dependencies, false));
7070
return CreateConditionList();
7171
}
7272
}

src/NetArchTest.Rules/Condition_Names.cs

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public sealed partial class Condition
1212
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
1313
public ConditionList HaveName(string name)
1414
{
15-
AddFunctionCall(x => FunctionDelegates.HaveName(x, name, true));
15+
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveName(context, inputTypes, name, true));
1616
return CreateConditionList();
1717
}
1818

@@ -23,7 +23,7 @@ public ConditionList HaveName(string name)
2323
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
2424
public ConditionList NotHaveName(string name)
2525
{
26-
AddFunctionCall(x => FunctionDelegates.HaveName(x, name, false));
26+
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveName(context, inputTypes, name, false));
2727
return CreateConditionList();
2828
}
2929

@@ -56,7 +56,7 @@ public ConditionList NotHaveNameMatching(string pattern)
5656
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
5757
public ConditionList HaveNameStartingWith(string start)
5858
{
59-
AddFunctionCall(x => FunctionDelegates.HaveNameStartingWith(x, start, true));
59+
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveNameStartingWith(context, inputTypes, start, true));
6060
return CreateConditionList();
6161
}
6262

@@ -67,33 +67,9 @@ public ConditionList HaveNameStartingWith(string start)
6767
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
6868
public ConditionList NotHaveNameStartingWith(string start)
6969
{
70-
AddFunctionCall(x => FunctionDelegates.HaveNameStartingWith(x, start, false));
70+
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveNameStartingWith(context, inputTypes, start, false));
7171
return CreateConditionList();
72-
}
73-
74-
/// <summary>
75-
/// Selects types whose names start with the specified text.
76-
/// </summary>
77-
/// <param name="start">The text to match against.</param>
78-
/// <param name="comparer">The string comparer.</param>
79-
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
80-
public ConditionList HaveNameStartingWith(string start, StringComparison comparer)
81-
{
82-
AddFunctionCall(x => FunctionDelegates.HaveNameStartingWith(x, start, true, comparer));
83-
return CreateConditionList();
84-
}
85-
86-
/// <summary>
87-
/// Selects types whose names do not start with the specified text.
88-
/// </summary>
89-
/// <param name="start">The text to match against.</param>
90-
/// <param name="comparer">The string comparer.</param>
91-
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
92-
public ConditionList NotHaveNameStartingWith(string start, StringComparison comparer)
93-
{
94-
AddFunctionCall(x => FunctionDelegates.HaveNameStartingWith(x, start, false, comparer));
95-
return CreateConditionList();
96-
}
72+
}
9773

9874
/// <summary>
9975
/// Selects types whose names do not end with the specified text.
@@ -102,7 +78,7 @@ public ConditionList NotHaveNameStartingWith(string start, StringComparison comp
10278
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
10379
public ConditionList HaveNameEndingWith(string end)
10480
{
105-
AddFunctionCall(x => FunctionDelegates.HaveNameEndingWith(x, end, true));
81+
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveNameEndingWith(context, inputTypes, end, true));
10682
return CreateConditionList();
10783
}
10884

@@ -113,7 +89,7 @@ public ConditionList HaveNameEndingWith(string end)
11389
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
11490
public ConditionList NotHaveNameEndingWith(string end)
11591
{
116-
AddFunctionCall(x => FunctionDelegates.HaveNameEndingWith(x, end, false));
92+
AddFunctionCall((context, inputTypes) => FunctionDelegates.HaveNameEndingWith(context, inputTypes, end, false));
11793
return CreateConditionList();
11894
}
11995

@@ -123,23 +99,7 @@ public ConditionList NotHaveNameEndingWith(string end)
12399
/// <param name="end">The text to match against.</param>
124100
/// <param name="comparer">The string comparer.</param>
125101
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
126-
public ConditionList HaveNameEndingWith(string end, StringComparison comparer)
127-
{
128-
AddFunctionCall(x => FunctionDelegates.HaveNameEndingWith(x, end, true, comparer));
129-
return CreateConditionList();
130-
}
131102

132-
/// <summary>
133-
/// Selects types whose names do not end with the specified text.
134-
/// </summary>
135-
/// <param name="end">The text to match against.</param>
136-
/// <param name="comparer">The string comparer.</param>
137-
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
138-
public ConditionList NotHaveNameEndingWith(string end, StringComparison comparer)
139-
{
140-
AddFunctionCall(x => FunctionDelegates.HaveNameEndingWith(x, end, false, comparer));
141-
return CreateConditionList();
142-
}
143103

144104

145105

@@ -192,7 +152,7 @@ public ConditionList NotResideInNamespaceMatching(string pattern)
192152
/// </summary>
193153
/// <param name="name">The namespace part to match against.</param>
194154
/// <returns>An updated set of predicates that can be applied to a list of types.</returns>
195-
public ConditionList ResideInNamespaceStartingWith(string name)
155+
internal ConditionList ResideInNamespaceStartingWith(string name)
196156
{
197157
AddFunctionCall(x => FunctionDelegates.ResideInNamespaceMatching(x, $"^{name}", true));
198158
return CreateConditionList();
@@ -203,7 +163,7 @@ public ConditionList ResideInNamespaceStartingWith(string name)
203163
/// </summary>
204164
/// <param name="name">The namespace part to match against.</param>
205165
/// <returns>An updated set of predicates that can be applied to a list of types.</returns>
206-
public ConditionList NotResideInNamespaceStartingWith(string name)
166+
internal ConditionList NotResideInNamespaceStartingWith(string name)
207167
{
208168
AddFunctionCall(x => FunctionDelegates.ResideInNamespaceMatching(x, $"^{name}", false));
209169
return CreateConditionList();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
namespace System.Runtime.CompilerServices
2+
{
3+
internal static class IsExternalInit { }
4+
}

src/NetArchTest.Rules/Functions/FunctionDelegates_Names.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@
44
using System.Text.RegularExpressions;
55
using Mono.Cecil;
66
using NetArchTest.Assemblies;
7+
using NetArchTest.RuleEngine;
78

89
namespace NetArchTest.Functions
910
{
1011
internal static partial class FunctionDelegates
1112
{
1213
// Name & Namespace
1314

14-
internal static IEnumerable<TypeSpec> HaveName(IEnumerable<TypeSpec> input, string name, bool condition)
15+
internal static IEnumerable<TypeSpec> HaveName(FunctionSequenceExecutionContext context, IEnumerable<TypeSpec> input, string name, bool condition)
1516
{
1617
var plainName = name.RemoveGenericPart();
1718
if (condition)
1819
{
19-
return input.Where(c => c.Definition.GetName().Equals(plainName, StringComparison.InvariantCultureIgnoreCase));
20+
return input.Where(c => c.Definition.GetName().Equals(plainName, context.UserOptions.Comparer));
2021
}
2122
else
2223
{
23-
return input.Where(c => !c.Definition.GetName().Equals(plainName, StringComparison.InvariantCultureIgnoreCase));
24+
return input.Where(c => !c.Definition.GetName().Equals(plainName, context.UserOptions.Comparer));
2425
}
2526
}
2627

@@ -37,27 +38,27 @@ internal static IEnumerable<TypeSpec> HaveNameMatching(IEnumerable<TypeSpec> inp
3738
}
3839
}
3940

40-
internal static IEnumerable<TypeSpec> HaveNameStartingWith(IEnumerable<TypeSpec> input, string start, bool condition, StringComparison comparer = StringComparison.InvariantCultureIgnoreCase)
41+
internal static IEnumerable<TypeSpec> HaveNameStartingWith(FunctionSequenceExecutionContext context, IEnumerable<TypeSpec> input, string start, bool condition)
4142
{
4243
if (condition)
4344
{
44-
return input.Where(c => c.Definition.GetName().StartsWith(start, comparer));
45+
return input.Where(c => c.Definition.GetName().StartsWith(start, context.UserOptions.Comparer));
4546
}
4647
else
4748
{
48-
return input.Where(c => !c.Definition.GetName().StartsWith(start, comparer));
49+
return input.Where(c => !c.Definition.GetName().StartsWith(start, context.UserOptions.Comparer));
4950
}
5051
}
5152

52-
internal static IEnumerable<TypeSpec> HaveNameEndingWith(IEnumerable<TypeSpec> input, string end, bool condition, StringComparison comparer = StringComparison.InvariantCultureIgnoreCase)
53+
internal static IEnumerable<TypeSpec> HaveNameEndingWith(FunctionSequenceExecutionContext context, IEnumerable<TypeSpec> input, string end, bool condition)
5354
{
5455
if (condition)
5556
{
56-
return input.Where(c => c.Definition.GetName().EndsWith(end, comparer));
57+
return input.Where(c => c.Definition.GetName().EndsWith(end, context.UserOptions.Comparer));
5758
}
5859
else
5960
{
60-
return input.Where(c => !c.Definition.GetName().EndsWith(end, comparer));
61+
return input.Where(c => !c.Definition.GetName().EndsWith(end, context.UserOptions.Comparer));
6162
}
6263
}
6364

src/NetArchTest.Rules/NetArchTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<FileVersion>1.3.7.0</FileVersion>
1919
<PackageId>NetArchTest.eNhancedEdition </PackageId>
2020
<PackageLicenseExpression>MIT</PackageLicenseExpression>
21+
<LangVersion>10</LangVersion>
2122
</PropertyGroup>
2223

2324
<ItemGroup>

src/NetArchTest.Rules/Options.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace NetArchTest
4+
{
5+
public record class Options
6+
{
7+
public static readonly Options Default = new Options();
8+
9+
10+
public StringComparison Comparer { get; init; } = StringComparison.InvariantCultureIgnoreCase;
11+
12+
13+
}
14+
}

src/NetArchTest.Rules/Predicate.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ private void AddFunctionCall(Func<IEnumerable<TypeSpec>, IEnumerable<TypeSpec>>
3030
{
3131
context.Sequence.AddFunctionCall(func);
3232
}
33+
private void AddFunctionCall(Func<FunctionSequenceExecutionContext, IEnumerable<TypeSpec>, IEnumerable<TypeSpec>> func)
34+
{
35+
context.Sequence.AddFunctionCall(func);
36+
}
3337

3438

35-
3639

3740
/// <summary>
3841
/// Selects types that are decorated with a specific custom attribute.

src/NetArchTest.Rules/PredicateList.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,19 @@ public Predicate Or()
7373
/// Returns the types returned by these predicates.
7474
/// </summary>
7575
/// <returns>A list of types.</returns>
76-
public IEnumerable<IType> GetTypes()
76+
public IEnumerable<IType> GetTypes(Options options = null)
7777
{
78-
return rule.GetTypes();
78+
return rule.GetTypes(options);
7979
}
8080

8181

82-
internal IEnumerable<TypeSpec> GetTypeSpecifications()
82+
internal IEnumerable<TypeSpec> GetTypeSpecifications(Options options = null)
8383
{
84-
return rule.Execute();
84+
return rule.Execute(options);
8585
}
86-
internal IEnumerable<Type> GetReflectionTypes()
86+
internal IEnumerable<Type> GetReflectionTypes(Options options = null)
8787
{
88-
return rule.GetReflectionTypes();
88+
return rule.GetReflectionTypes(options);
8989
}
9090
}
9191
}

0 commit comments

Comments
 (0)