Skip to content

Commit 45d3020

Browse files
committed
Add HaveAnyParameters and NotHaveAnyParameters conditions for MethodMembers
- Add HaveAnyParameters() condition to check if methods have parameters - Add NotHaveAnyParameters() condition to check if methods have no parameters - Add corresponding methods to IMethodMemberConditions interface - Add comprehensive test coverage for both conditions - Test both regular methods and constructors - Verify failure descriptions in violation cases - Use correct IL method names (.ctor) for constructor tests - Remove redundant test cases that provided no additional value The new conditions enable architectural rules to enforce parameter requirements on methods and constructors, complementing existing method member conditions. Signed-off-by: Ahmed Mohamed El Ahmar <[email protected]>
1 parent c87381f commit 45d3020

File tree

9 files changed

+290
-173
lines changed

9 files changed

+290
-173
lines changed

ArchUnitNET/Fluent/Syntax/Elements/Members/MethodMembers/IMethodMemberConditions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ public interface IMethodMemberConditions<out TReturnType, out TRuleType>
2828
TReturnType HaveReturnType(IObjectProvider<IType> types);
2929
TReturnType HaveReturnType(Type type, params Type[] moreTypes);
3030
TReturnType HaveReturnType(IEnumerable<Type> types);
31+
TReturnType HaveAnyParameters();
3132

3233
//Negations
3334

3435
TReturnType BeNoConstructor();
3536
TReturnType NotBeVirtual();
37+
TReturnType NotHaveAnyParameters();
3638

3739
TReturnType NotBeCalledBy(IType firstType, params IType[] moreTypes);
3840
TReturnType NotBeCalledBy(Type type, params Type[] moreTypes);

ArchUnitNET/Fluent/Syntax/Elements/Members/MethodMembers/MethodMemberConditionsDefinition.cs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Architecture architecture
159159
//ignore, can't have a dependency anyways
160160
}
161161
}
162+
162163
var methodMemberList = methodMembers.ToList();
163164
var passedObjects = methodMemberList
164165
.Where(methodMember =>
@@ -300,8 +301,8 @@ IEnumerable<ConditionResult> Condition(IEnumerable<MethodMember> methodMembers)
300301
.Distinct()
301302
.Aggregate(
302303
"does not have dependencies in method body to \""
303-
+ firstType.FullName
304-
+ "\"",
304+
+ firstType.FullName
305+
+ "\"",
305306
(current, type) => current + " or \"" + type.FullName + "\""
306307
);
307308
}
@@ -359,6 +360,7 @@ Architecture architecture
359360
//ignore, can't have a dependency anyways
360361
}
361362
}
363+
362364
var methodMemberList = methodMembers.ToList();
363365
var passedObjects = methodMemberList
364366
.Where(methodMember =>
@@ -382,8 +384,8 @@ Architecture architecture
382384
.Distinct()
383385
.Aggregate(
384386
"does not have dependencies in method body to \""
385-
+ firstType.FullName
386-
+ "\"",
387+
+ firstType.FullName
388+
+ "\"",
387389
(current, type) => current + " or \"" + type.FullName + "\""
388390
);
389391
}
@@ -657,6 +659,7 @@ Architecture architecture
657659
//ignore, can't have a dependency anyways
658660
}
659661
}
662+
660663
var methodMemberList = methodMembers.ToList();
661664
var failedObjects = methodMemberList
662665
.Where(methodMember =>
@@ -799,8 +802,8 @@ IEnumerable<ConditionResult> Condition(IEnumerable<MethodMember> methodMembers)
799802
.Distinct()
800803
.Aggregate(
801804
"does have dependencies in method body to \""
802-
+ firstType.FullName
803-
+ "\"",
805+
+ firstType.FullName
806+
+ "\"",
804807
(current, type) => current + " or \"" + type.FullName + "\""
805808
);
806809
}
@@ -861,6 +864,7 @@ Architecture architecture
861864
//ignore, can't have a dependency anyways
862865
}
863866
}
867+
864868
var methodMemberList = methodMembers.ToList();
865869
var failedObjects = methodMemberList
866870
.Where(methodMember =>
@@ -884,8 +888,8 @@ Architecture architecture
884888
.Distinct()
885889
.Aggregate(
886890
"does have dependencies in method body to \""
887-
+ firstType.FullName
888-
+ "\"",
891+
+ firstType.FullName
892+
+ "\"",
889893
(current, type) => current + " or \"" + type.FullName + "\""
890894
);
891895
}
@@ -1011,5 +1015,31 @@ bool Condition(MethodMember member)
10111015
description
10121016
);
10131017
}
1018+
1019+
/// <summary>
1020+
/// Selects method members that have any parameters
1021+
/// </summary>
1022+
/// <returns>A condition that can be applied to method members</returns>
1023+
public static ICondition<MethodMember> HaveAnyParameters()
1024+
{
1025+
return new SimpleCondition<MethodMember>(
1026+
method => method.Parameters.Any(),
1027+
"have any parameters",
1028+
"does not have any parameters"
1029+
);
1030+
}
1031+
1032+
/// <summary>
1033+
/// Selects method members that do not have any parameters (parameterless)
1034+
/// </summary>
1035+
/// <returns>A condition that can be applied to method members</returns>
1036+
public static ICondition<MethodMember> NotHaveAnyParameters()
1037+
{
1038+
return new SimpleCondition<MethodMember>(
1039+
method => !method.Parameters.Any(),
1040+
"not have any parameters",
1041+
"has parameters"
1042+
);
1043+
}
10141044
}
1015-
}
1045+
}

ArchUnitNET/Fluent/Syntax/Elements/Members/MethodMembers/MethodMembersShould.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ public class MethodMembersShould
99
IComplexMethodMemberConditions
1010
{
1111
public MethodMembersShould(IArchRuleCreator<MethodMember> ruleCreator)
12-
: base(ruleCreator) { }
12+
: base(ruleCreator)
13+
{
14+
}
1315

1416
public ShouldRelateToMethodMembersThat<
1517
MethodMembersShouldConjunction,
@@ -297,5 +299,18 @@ public MethodMembersShouldConjunction NotHaveReturnType(IEnumerable<Type> types)
297299
_ruleCreator.AddCondition(MethodMemberConditionsDefinition.NotHaveReturnType(types));
298300
return new MethodMembersShouldConjunction(_ruleCreator);
299301
}
302+
303+
304+
public MethodMembersShouldConjunction HaveAnyParameters()
305+
{
306+
_ruleCreator.AddCondition(MethodMemberConditionsDefinition.HaveAnyParameters());
307+
return new MethodMembersShouldConjunction(_ruleCreator);
308+
}
309+
310+
public MethodMembersShouldConjunction NotHaveAnyParameters()
311+
{
312+
_ruleCreator.AddCondition(MethodMemberConditionsDefinition.NotHaveAnyParameters());
313+
return new MethodMembersShouldConjunction(_ruleCreator);
314+
}
300315
}
301-
}
316+
}

ArchUnitNET/Fluent/Syntax/Elements/Types/Classes/ClassConditionsDefinition.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Linq;
22
using ArchUnitNET.Domain;
3-
using ArchUnitNET.Domain.Extensions;
43
using ArchUnitNET.Fluent.Conditions;
54

65
namespace ArchUnitNET.Fluent.Syntax.Elements.Types.Classes
@@ -44,16 +43,6 @@ public static ICondition<Class> BeImmutable()
4443
"is not immutable"
4544
);
4645
}
47-
48-
public static ICondition<Class> HavePrivateParameterlessConstructor()
49-
{
50-
return new SimpleCondition<Class>(
51-
cls => (cls.IsAbstract.HasValue && cls.IsAbstract.Value) || cls.GetConstructors().Any(c =>
52-
c.Visibility == Visibility.Private && !c.Parameters.Any()),
53-
"have private parameterless constructor",
54-
"does not have private parameterless constructor"
55-
);
56-
}
5746

5847
//Negations
5948

@@ -92,15 +81,5 @@ public static ICondition<Class> NotBeImmutable()
9281
"is immutable"
9382
);
9483
}
95-
96-
public static ICondition<Class> NotHavePrivateParameterlessConstructor()
97-
{
98-
return new SimpleCondition<Class>(
99-
cls => (cls.IsAbstract.HasValue && cls.IsAbstract.Value) || !cls.GetConstructors().Any(c =>
100-
c.Visibility == Visibility.Private && !c.Parameters.Any()),
101-
"not have private parameterless constructor",
102-
"has private parameterless constructor"
103-
);
104-
}
10584
}
10685
}

ArchUnitNET/Fluent/Syntax/Elements/Types/Classes/ClassesShould.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ public ClassesShouldConjunction BeImmutable()
3333
return new ClassesShouldConjunction(_ruleCreator);
3434
}
3535

36-
public ClassesShouldConjunction HavePrivateParameterlessConstructor()
37-
{
38-
_ruleCreator.AddCondition(ClassConditionsDefinition.HavePrivateParameterlessConstructor());
39-
return new ClassesShouldConjunction(_ruleCreator);
40-
}
41-
4236
//Negations
4337

4438
public ClassesShouldConjunction NotBeAbstract()
@@ -64,11 +58,5 @@ public ClassesShouldConjunction NotBeImmutable()
6458
_ruleCreator.AddCondition(ClassConditionsDefinition.NotBeImmutable());
6559
return new ClassesShouldConjunction(_ruleCreator);
6660
}
67-
68-
public ClassesShouldConjunction NotHavePrivateParameterlessConstructor()
69-
{
70-
_ruleCreator.AddCondition(ClassConditionsDefinition.NotHavePrivateParameterlessConstructor());
71-
return new ClassesShouldConjunction(_ruleCreator);
72-
}
7361
}
7462
}

ArchUnitNETTests/Fluent/Syntax/Elements/ClassPrivateConstructorConditionTests.cs

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)