Skip to content

Commit 8fe125f

Browse files
committed
add rules : AreStaticless, BeStaticless
1 parent 55ceaf4 commit 8fe125f

5 files changed

Lines changed: 65 additions & 2 deletions

File tree

sources/NetArchTest/Condition_Special.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public ConditionList BeStateless()
4747
return CreateConditionList();
4848
}
4949

50+
/// <summary>
51+
/// Selects types that are staticless, they do not have static state
52+
/// </summary>
53+
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
54+
public ConditionList BeStaticless()
55+
{
56+
AddFunctionCall(x => FunctionDelegates.BeStaticless(x, true));
57+
return CreateConditionList();
58+
}
59+
5060
/// <summary>
5161
/// Selects types according to whether they have nullable members.
5262
/// </summary>

sources/NetArchTest/Extensions/Mono.Cecil/TypeDefinitionExtensions.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ public static string GetFilePath(this TypeDefinition typeDefinition)
167167
}
168168

169169
public static bool IsStateless(this TypeDefinition type)
170-
{
171-
// Check if the type has any instance fields
170+
{
172171
if (type.HasFields)
173172
{
174173
foreach (var field in type.Fields)
@@ -182,5 +181,19 @@ public static bool IsStateless(this TypeDefinition type)
182181
}
183182
return true;
184183
}
184+
public static bool IsStaticless(this TypeDefinition type)
185+
{
186+
if (type.HasFields)
187+
{
188+
foreach (var field in type.Fields)
189+
{
190+
if (field.IsStatic && field.HasConstant == false)
191+
{
192+
return false;
193+
}
194+
}
195+
}
196+
return true;
197+
}
185198
}
186199
}

sources/NetArchTest/Functions/FunctionDelegates_Special.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ internal static IEnumerable<TypeSpec> BeStateless(IEnumerable<TypeSpec> input, b
7070
return input.Where(c => !c.Definition.IsStateless());
7171
}
7272
}
73+
internal static IEnumerable<TypeSpec> BeStaticless(IEnumerable<TypeSpec> input, bool condition)
74+
{
75+
if (condition)
76+
{
77+
return input.Where(c => c.Definition.IsStaticless());
78+
}
79+
else
80+
{
81+
return input.Where(c => !c.Definition.IsStaticless());
82+
}
83+
}
7384

7485
internal static IEnumerable<TypeSpec> HaveFileNameMatchingTypeName(FunctionSequenceExecutionContext context, IEnumerable<TypeSpec> input, bool condition)
7586
{

sources/NetArchTest/Predicate_Special.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ public PredicateList AreStateless()
4444
return CreatePredicateList();
4545
}
4646

47+
/// <summary>
48+
/// Selects types that are staticless, they do not have static state
49+
/// </summary>
50+
/// <returns>An updated set of conditions that can be applied to a list of types.</returns>
51+
public PredicateList AreStaticless()
52+
{
53+
AddFunctionCall(x => FunctionDelegates.BeStaticless(x, true));
54+
return CreatePredicateList();
55+
}
56+
4757
/// <summary>
4858
/// Selects types that have only nullable members.
4959
/// </summary>

tests/NetArchTest.Rules.UnitTests/PredicateTests_Special.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,25 @@ public void AreStateless()
100100
Assert.Contains<Type>(typeof(StatelessClass_Prop), result);
101101
}
102102

103+
[Fact(DisplayName = "AreStaticless")]
104+
public void AreStaticless()
105+
{
106+
var result = fixture.Types
107+
.That()
108+
.ResideInNamespace(namespaceof<StatelessClass_StaticField>())
109+
.And()
110+
.AreStaticless().GetReflectionTypes();
111+
112+
Assert.Equal(6, result.Count());
113+
114+
Assert.Contains<Type>(typeof(StatelessClass_ConstField), result);
115+
Assert.Contains<Type>(typeof(StatefulClass_Field), result);
116+
Assert.Contains<Type>(typeof(StatefulRecordClass), result);
117+
Assert.Contains<Type>(typeof(StatefulRecordStruct), result);
118+
Assert.Contains<Type>(typeof(StatefulClass_Prop), result);
119+
Assert.Contains<Type>(typeof(StatefulClass_ReadonlyField), result);
120+
}
121+
103122
[Fact(DisplayName = "OnlyHaveNullableMembers")]
104123
public void OnlyHaveNullableMembers()
105124
{

0 commit comments

Comments
 (0)