Skip to content

Commit 0644a4e

Browse files
committed
refactoring
1 parent 203797d commit 0644a4e

20 files changed

Lines changed: 559 additions & 426 deletions

src/NetArchTest.Rules/Condition.cs

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ private void AddFunctionCall(Func<FunctionSequenceExecutionContext, IEnumerable<
4141

4242

4343

44-
45-
4644
/// <summary>
4745
/// Selects types are decorated with a specific custom attribut.
4846
/// </summary>
@@ -53,6 +51,14 @@ public ConditionList HaveCustomAttribute(Type attribute)
5351
AddFunctionCall(x => FunctionDelegates.HaveCustomAttribute(x, attribute, true));
5452
return CreateConditionList();
5553
}
54+
/// <summary>
55+
/// Selects types are decorated with a specific custom attribut.
56+
/// </summary>
57+
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
58+
public ConditionList HaveCustomAttribute<T>()
59+
{
60+
return HaveCustomAttribute(typeof(T));
61+
}
5662

5763
/// <summary>
5864
/// Selects types that are not decorated with a specific custom attribute.
@@ -64,6 +70,14 @@ public ConditionList NotHaveCustomAttribute(Type attribute)
6470
AddFunctionCall(x => FunctionDelegates.HaveCustomAttribute(x, attribute, false));
6571
return CreateConditionList();
6672
}
73+
/// <summary>
74+
/// Selects types that are not decorated with a specific custom attribute.
75+
/// </summary>
76+
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
77+
public ConditionList NotHaveCustomAttribute<T>()
78+
{
79+
return NotHaveCustomAttribute(typeof(T));
80+
}
6781

6882
/// <summary>
6983
/// Selects types that are decorated with a specific custom attribute or derived one.
@@ -75,6 +89,14 @@ public ConditionList HaveCustomAttributeOrInherit(Type attribute)
7589
AddFunctionCall(x => FunctionDelegates.HaveCustomAttributeOrInherit(x, attribute, true));
7690
return CreateConditionList();
7791
}
92+
/// <summary>
93+
/// Selects types that are decorated with a specific custom attribute or derived one.
94+
/// </summary>
95+
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
96+
public ConditionList HaveCustomAttributeOrInherit<T>()
97+
{
98+
return HaveCustomAttributeOrInherit(typeof(T));
99+
}
78100

79101
/// <summary>
80102
/// Selects types are not decorated with a specific custom attribute or derived one.
@@ -86,6 +108,14 @@ public ConditionList NotHaveCustomAttributeOrInherit(Type attribute)
86108
AddFunctionCall(x => FunctionDelegates.HaveCustomAttributeOrInherit(x, attribute, false));
87109
return CreateConditionList();
88110
}
111+
/// <summary>
112+
/// Selects types are not decorated with a specific custom attribute or derived one.
113+
/// </summary>
114+
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
115+
public ConditionList NotHaveCustomAttributeOrInherit<T>()
116+
{
117+
return NotHaveCustomAttributeOrInherit(typeof(T));
118+
}
89119

90120
/// <summary>
91121
/// Selects types that inherit a particular type.
@@ -94,9 +124,17 @@ public ConditionList NotHaveCustomAttributeOrInherit(Type attribute)
94124
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
95125
public ConditionList Inherit(Type type)
96126
{
97-
AddFunctionCall(x => FunctionDelegates.Inherits(x, type, true));
127+
AddFunctionCall(x => FunctionDelegates.Inherit(x, type, true));
98128
return CreateConditionList();
99129
}
130+
/// <summary>
131+
/// Selects types that inherit a particular type.
132+
/// </summary>
133+
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
134+
public ConditionList Inherit<T>()
135+
{
136+
return Inherit(typeof(T));
137+
}
100138

101139
/// <summary>
102140
/// Selects types that do not inherit a particular type.
@@ -105,9 +143,17 @@ public ConditionList Inherit(Type type)
105143
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
106144
public ConditionList NotInherit(Type type)
107145
{
108-
AddFunctionCall(x => FunctionDelegates.Inherits(x, type, false));
146+
AddFunctionCall(x => FunctionDelegates.Inherit(x, type, false));
109147
return CreateConditionList();
110148
}
149+
/// <summary>
150+
/// Selects types that do not inherit a particular type.
151+
/// </summary>
152+
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
153+
public ConditionList NotInherit<T>()
154+
{
155+
return NotInherit(typeof(T));
156+
}
111157

112158
/// <summary>
113159
/// Selects types that implement a particular interface.
@@ -116,9 +162,17 @@ public ConditionList NotInherit(Type type)
116162
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
117163
public ConditionList ImplementInterface(Type interfaceType)
118164
{
119-
AddFunctionCall(x => FunctionDelegates.ImplementsInterface(x, interfaceType, true));
165+
AddFunctionCall(x => FunctionDelegates.ImplementInterface(x, interfaceType, true));
120166
return CreateConditionList();
121167
}
168+
/// <summary>
169+
/// Selects types that implement a particular interface.
170+
/// </summary>
171+
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
172+
public ConditionList ImplementInterface<T>()
173+
{
174+
return ImplementInterface(typeof(T));
175+
}
122176

123177
/// <summary>
124178
/// Selects types that do not implement a particular interface.
@@ -127,11 +181,19 @@ public ConditionList ImplementInterface(Type interfaceType)
127181
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
128182
public ConditionList NotImplementInterface(Type interfaceType)
129183
{
130-
AddFunctionCall(x => FunctionDelegates.ImplementsInterface(x, interfaceType, false));
184+
AddFunctionCall(x => FunctionDelegates.ImplementInterface(x, interfaceType, false));
131185
return CreateConditionList();
132186
}
133-
134-
187+
/// <summary>
188+
/// Selects types that do not implement a particular interface.
189+
/// </summary>
190+
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
191+
public ConditionList NotImplementInterface<T>()
192+
{
193+
return NotImplementInterface(typeof(T));
194+
}
195+
196+
135197

136198
/// <summary>
137199
/// Selects types that meet a custom rule.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using NetArchTest.Functions;
2+
3+
namespace NetArchTest.Rules
4+
{
5+
public sealed partial class Condition
6+
{
7+
8+
/// <summary>
9+
/// Selects types that are immutable.
10+
/// </summary>
11+
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
12+
public ConditionList BeImmutable()
13+
{
14+
AddFunctionCall(x => FunctionDelegates.BeImmutable(x, true));
15+
return CreateConditionList();
16+
}
17+
18+
/// <summary>
19+
/// Selects types that are mutable.
20+
/// </summary>
21+
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
22+
public ConditionList BeMutable()
23+
{
24+
AddFunctionCall(x => FunctionDelegates.BeImmutable(x, false));
25+
return CreateConditionList();
26+
}
27+
28+
/// <summary>
29+
/// Selects types according to whether they have nullable members.
30+
/// </summary>
31+
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
32+
public ConditionList OnlyHaveNullableMembers()
33+
{
34+
AddFunctionCall(x => FunctionDelegates.HasNullableMembers(x, true));
35+
return CreateConditionList();
36+
}
37+
38+
/// <summary>
39+
/// Selects types according to whether they have nullable members.
40+
/// </summary>
41+
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
42+
public ConditionList HaveSomeNonNullableMembers()
43+
{
44+
AddFunctionCall(x => FunctionDelegates.HasNullableMembers(x, false));
45+
return CreateConditionList();
46+
}
47+
}
48+
}

src/NetArchTest.Rules/Condition_Traits.cs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -83,47 +83,5 @@ public ConditionList NotBeStatic()
8383
AddFunctionCall(x => FunctionDelegates.BeStatic(x, false));
8484
return CreateConditionList();
8585
}
86-
87-
88-
89-
/// <summary>
90-
/// Selects types that are immutable.
91-
/// </summary>
92-
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
93-
public ConditionList BeImmutable()
94-
{
95-
AddFunctionCall(x => FunctionDelegates.BeImmutable(x, true));
96-
return CreateConditionList();
97-
}
98-
99-
/// <summary>
100-
/// Selects types that are mutable.
101-
/// </summary>
102-
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
103-
public ConditionList BeMutable()
104-
{
105-
AddFunctionCall(x => FunctionDelegates.BeImmutable(x, false));
106-
return CreateConditionList();
107-
}
108-
109-
/// <summary>
110-
/// Selects types according to whether they have nullable members.
111-
/// </summary>
112-
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
113-
public ConditionList OnlyHaveNullableMembers()
114-
{
115-
AddFunctionCall(x => FunctionDelegates.HasNullableMembers(x, true));
116-
return CreateConditionList();
117-
}
118-
119-
/// <summary>
120-
/// Selects types according to whether they have nullable members.
121-
/// </summary>
122-
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
123-
public ConditionList HaveSomeNonNullableMembers()
124-
{
125-
AddFunctionCall(x => FunctionDelegates.HasNullableMembers(x, false));
126-
return CreateConditionList();
127-
}
12886
}
12987
}

src/NetArchTest.Rules/Functions/FunctionDelegates.cs

Lines changed: 6 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@ namespace NetArchTest.Functions
99
{
1010
internal static partial class FunctionDelegates
1111
{
12-
13-
14-
15-
16-
17-
18-
19-
20-
21-
2212
internal static IEnumerable<TypeSpec> HaveCustomAttribute(IEnumerable<TypeSpec> input, Type attribute, bool condition)
2313
{
2414
if (condition)
@@ -30,7 +20,6 @@ internal static IEnumerable<TypeSpec> HaveCustomAttribute(IEnumerable<TypeSpec>
3020
return input.Where(c => !c.Definition.CustomAttributes.Any(a => attribute.FullName.Equals(a.AttributeType.FullName, StringComparison.InvariantCultureIgnoreCase)));
3121
}
3222
}
33-
3423

3524
internal static IEnumerable<TypeSpec> HaveCustomAttributeOrInherit(IEnumerable<TypeSpec> input, Type attribute, bool condition)
3625
{
@@ -45,10 +34,8 @@ internal static IEnumerable<TypeSpec> HaveCustomAttributeOrInherit(IEnumerable<T
4534
return input.Where(c => !c.Definition.CustomAttributes.Any(a => a.AttributeType.Resolve().IsSubclassOf(target) || attribute.FullName.Equals(a.AttributeType.FullName, StringComparison.InvariantCultureIgnoreCase)));
4635
}
4736
}
48-
49-
5037

51-
internal static IEnumerable<TypeSpec> Inherits(IEnumerable<TypeSpec> input, Type type, bool condition)
38+
internal static IEnumerable<TypeSpec> Inherit(IEnumerable<TypeSpec> input, Type type, bool condition)
5239
{
5340
// Convert the incoming type to a definition
5441
var target = type.ToTypeDefinition();
@@ -61,41 +48,26 @@ internal static IEnumerable<TypeSpec> Inherits(IEnumerable<TypeSpec> input, Type
6148
return input.Where(c => !c.Definition.IsSubclassOf(target));
6249
}
6350
}
64-
6551

66-
internal static IEnumerable<TypeSpec> ImplementsInterface(IEnumerable<TypeSpec> input, Type typeInterface, bool condition)
52+
internal static IEnumerable<TypeSpec> ImplementInterface(IEnumerable<TypeSpec> input, Type typeInterface, bool condition)
6753
{
6854
if (!typeInterface.IsInterface)
6955
{
7056
throw new ArgumentException($"The type {typeInterface.FullName} is not an interface.");
7157
}
7258

73-
var target = typeInterface.FullName;
74-
var found = new List<TypeSpec>();
75-
76-
foreach (var type in input)
77-
{
78-
if (type.Definition.Interfaces.Any(t => t.InterfaceType.Resolve().FullName.Equals(target, StringComparison.InvariantCultureIgnoreCase)))
79-
{
80-
found.Add(type);
81-
}
82-
}
83-
8459
if (condition)
8560
{
86-
return found;
61+
return input.Where(c => Implements(c.Definition));
8762
}
8863
else
8964
{
90-
return input.Where(c => !found.Contains(c));
65+
return input.Where(c => !Implements(c.Definition));
9166
}
92-
}
93-
9467

95-
68+
bool Implements(TypeDefinition c) => c.Interfaces.Any(t => t.InterfaceType.FullName.Equals(typeInterface.FullName, StringComparison.InvariantCultureIgnoreCase));
69+
}
9670

97-
98-
9971
internal static IEnumerable<TypeSpec> MeetCustomRule(IEnumerable<TypeSpec> input, ICustomRule rule, bool condition)
10072
{
10173
if (condition)
@@ -107,32 +79,5 @@ internal static IEnumerable<TypeSpec> MeetCustomRule(IEnumerable<TypeSpec> input
10779
return input.Where(t => !rule.MeetsRule(t.Definition));
10880
}
10981
}
110-
111-
112-
113-
114-
internal static IEnumerable<TypeSpec> BeImmutable(IEnumerable<TypeSpec> input, bool condition)
115-
{
116-
if (condition)
117-
{
118-
return input.Where(c => c.Definition.IsImmutable());
119-
}
120-
else
121-
{
122-
return input.Where(c => !c.Definition.IsImmutable());
123-
}
124-
}
125-
126-
internal static IEnumerable<TypeSpec> HasNullableMembers(IEnumerable<TypeSpec> input, bool condition)
127-
{
128-
if (condition)
129-
{
130-
return input.Where(c => c.Definition.HasNullableMembers());
131-
}
132-
else
133-
{
134-
return input.Where(c => !c.Definition.HasNullableMembers());
135-
}
136-
}
13782
}
13883
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Mono.Cecil;
4+
using NetArchTest.Assemblies;
5+
6+
namespace NetArchTest.Functions
7+
{
8+
internal static partial class FunctionDelegates
9+
{
10+
internal static IEnumerable<TypeSpec> BeImmutable(IEnumerable<TypeSpec> input, bool condition)
11+
{
12+
if (condition)
13+
{
14+
return input.Where(c => c.Definition.IsImmutable());
15+
}
16+
else
17+
{
18+
return input.Where(c => !c.Definition.IsImmutable());
19+
}
20+
}
21+
22+
internal static IEnumerable<TypeSpec> HasNullableMembers(IEnumerable<TypeSpec> input, bool condition)
23+
{
24+
if (condition)
25+
{
26+
return input.Where(c => c.Definition.HasNullableMembers());
27+
}
28+
else
29+
{
30+
return input.Where(c => !c.Definition.HasNullableMembers());
31+
}
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)