Skip to content

Commit 38d7e45

Browse files
Refactor attribute with arguments fluent syntax methods
Signed-off-by: Alexander Linne <[email protected]>
1 parent bb4b5d6 commit 38d7e45

21 files changed

+1754
-4649
lines changed

ArchUnitNET/Domain/Extensions/AttributeExtensions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34

45
namespace ArchUnitNET.Domain.Extensions
@@ -26,5 +27,28 @@ public static bool OnlyHasAttributesMatching(this IHasAttributes a, string patte
2627
return a.Attributes.IsNullOrEmpty()
2728
|| a.Attributes.All(attribute => attribute.FullNameMatches(pattern));
2829
}
30+
31+
internal static IEnumerable<object> GetAllAttributeArgumentValues(
32+
this AttributeInstance instance
33+
) =>
34+
instance
35+
.AttributeArguments.Select(arg => arg.Value)
36+
.Select(value =>
37+
value is ITypeInstance<IType> typeInstance ? typeInstance.Type : value
38+
);
39+
40+
internal static IEnumerable<(string, object)> GetAllNamedAttributeArgumentTuples(
41+
this AttributeInstance instance
42+
) =>
43+
instance
44+
.AttributeArguments.OfType<AttributeNamedArgument>()
45+
.Select(arg =>
46+
(
47+
arg.Name,
48+
arg.Value is ITypeInstance<IType> typeInstance
49+
? typeInstance.Type
50+
: arg.Value
51+
)
52+
);
2953
}
3054
}

ArchUnitNET/Domain/Extensions/EnumerableExtensions.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,25 @@ public static string FormatDescription<T>(
4242
return $"{multipleDescription} {string.Join(" and ", list.Select(elementDescription))}";
4343
}
4444
}
45+
46+
internal static IEnumerable<object> ResolveAttributeArguments(
47+
this IEnumerable<object> objects,
48+
Architecture architecture
49+
)
50+
{
51+
return objects.Select(obj =>
52+
obj is Type type ? architecture.GetITypeOfType(type) : obj
53+
);
54+
}
55+
56+
internal static IEnumerable<(string, object)> ResolveNamedAttributeArgumentTuples(
57+
this IEnumerable<(string, object)> namedArguments,
58+
Architecture architecture
59+
)
60+
{
61+
return namedArguments.Select(arg =>
62+
(arg.Item1, arg.Item2 is Type type ? architecture.GetITypeOfType(type) : arg.Item2)
63+
);
64+
}
4565
}
4666
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace ArchUnitNET.Domain.Extensions
6+
{
7+
internal static class ICanBeAnalyzedExtensions
8+
{
9+
internal static IEnumerable<object> GetAllAttributeArgumentValues(
10+
this ICanBeAnalyzed obj,
11+
Attribute attribute = null
12+
)
13+
{
14+
var attributeInstances =
15+
attribute == null
16+
? obj.AttributeInstances
17+
: obj.AttributeInstances.Where(instance => instance.Type.Equals(attribute));
18+
return attributeInstances
19+
.SelectMany(instance => instance.AttributeArguments.Select(arg => arg.Value))
20+
.Select(value =>
21+
value is ITypeInstance<IType> typeInstance ? typeInstance.Type : value
22+
);
23+
}
24+
25+
internal static IEnumerable<(string, object)> GetAllNamedAttributeArgumentTuples(
26+
this ICanBeAnalyzed obj,
27+
Attribute attribute = null
28+
)
29+
{
30+
var attributeInstances =
31+
attribute == null
32+
? obj.AttributeInstances
33+
: obj.AttributeInstances.Where(instance => instance.Type.Equals(attribute));
34+
return attributeInstances.SelectMany(instance =>
35+
instance
36+
.AttributeArguments.OfType<AttributeNamedArgument>()
37+
.Select(arg =>
38+
(
39+
arg.Name,
40+
arg.Value is ITypeInstance<IType> typeInstance
41+
? typeInstance.Type
42+
: arg.Value
43+
)
44+
)
45+
);
46+
}
47+
}
48+
}

ArchUnitNET/Fluent/Syntax/Elements/GivenObjectsThat.cs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using ArchUnitNET.Domain;
5+
using ArchUnitNET.Domain.Extensions;
56
using ArchUnitNET.Fluent.Predicates;
67
using static ArchUnitNET.Fluent.Syntax.ConjunctionFactory;
78
using Attribute = ArchUnitNET.Domain.Attribute;
@@ -59,18 +60,16 @@ protected GivenObjectsThat(IArchRuleCreator<TRuleType> ruleCreator)
5960
public TGivenRuleTypeConjunction HaveAnyAttributesWithArguments(IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAnyAttributesWithArguments(argumentValues));
6061
public TGivenRuleTypeConjunction HaveAnyAttributesWithArguments(object firstArgumentValue, params object[] moreArgumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAnyAttributesWithArguments(new[] { firstArgumentValue }.Concat(moreArgumentValues)));
6162

62-
public TGivenRuleTypeConjunction HaveAttributeWithArguments(Attribute attribute, IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithArguments(attribute, argumentValues));
63-
public TGivenRuleTypeConjunction HaveAttributeWithArguments(Attribute attribute, object firstArgumentValue, params object[] moreArgumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithArguments(attribute, new[] { firstArgumentValue }.Concat(moreArgumentValues)));
64-
public TGivenRuleTypeConjunction HaveAttributeWithArguments(Type attribute, IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithArguments(attribute, argumentValues));
65-
public TGivenRuleTypeConjunction HaveAttributeWithArguments(Type attribute, object firstArgumentValue, params object[] moreArgumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithArguments(attribute, new[] { firstArgumentValue }.Concat(moreArgumentValues)));
63+
public TGivenRuleTypeConjunction HaveAttributeWithArguments(Attribute attribute, IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithArguments(attribute.FullName, _ => attribute, argumentValues));
64+
public TGivenRuleTypeConjunction HaveAttributeWithArguments(Type attribute, IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithArguments(attribute.FullName, architecture => architecture.GetAttributeOfType(attribute), argumentValues));
6665

6766
public TGivenRuleTypeConjunction HaveAnyAttributesWithNamedArguments(IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAnyAttributesWithNamedArguments(attributeArguments));
68-
public TGivenRuleTypeConjunction HaveAnyAttributesWithNamedArguments((string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments) => HaveAnyAttributesWithNamedArguments(new[] { firstAttributeArgument }.Concat(moreAttributeArguments));
67+
public TGivenRuleTypeConjunction HaveAnyAttributesWithNamedArguments(params (string, object)[] attributeArguments) => HaveAnyAttributesWithNamedArguments((IEnumerable<(string, object)>)attributeArguments);
6968

70-
public TGivenRuleTypeConjunction HaveAttributeWithNamedArguments(Attribute attribute, IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithNamedArguments(attribute, attributeArguments));
71-
public TGivenRuleTypeConjunction HaveAttributeWithNamedArguments(Attribute attribute, (string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments) => HaveAttributeWithNamedArguments(attribute, new[] { firstAttributeArgument }.Concat(moreAttributeArguments));
72-
public TGivenRuleTypeConjunction HaveAttributeWithNamedArguments(Type attribute, IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithNamedArguments(attribute, attributeArguments));
73-
public TGivenRuleTypeConjunction HaveAttributeWithNamedArguments(Type attribute, (string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments) => HaveAttributeWithNamedArguments(attribute, new[] { firstAttributeArgument }.Concat(moreAttributeArguments));
69+
public TGivenRuleTypeConjunction HaveAttributeWithNamedArguments(Attribute attribute, IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithNamedArguments(attribute.FullName, _ => attribute, attributeArguments));
70+
public TGivenRuleTypeConjunction HaveAttributeWithNamedArguments(Attribute attribute, params (string, object)[] attributeArguments) => HaveAttributeWithNamedArguments(attribute, (IEnumerable<(string, object)>)attributeArguments);
71+
public TGivenRuleTypeConjunction HaveAttributeWithNamedArguments(Type attribute, IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveAttributeWithNamedArguments(attribute.FullName, architecture => architecture.GetAttributeOfType(attribute), attributeArguments));
72+
public TGivenRuleTypeConjunction HaveAttributeWithNamedArguments(Type attribute, params (string, object)[] attributeArguments) => HaveAttributeWithNamedArguments(attribute, (IEnumerable<(string, object)>)attributeArguments);
7473

7574
public TGivenRuleTypeConjunction HaveName(string name) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveName(name));
7675
public TGivenRuleTypeConjunction HaveNameMatching(string pattern) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.HaveNameMatching(pattern));
@@ -122,20 +121,17 @@ protected GivenObjectsThat(IArchRuleCreator<TRuleType> ruleCreator)
122121
public TGivenRuleTypeConjunction DoNotHaveAnyAttributes(IEnumerable<Type> attributes) => DoNotHaveAnyAttributes(new SystemTypeObjectProvider<Attribute>(attributes));
123122

124123
public TGivenRuleTypeConjunction DoNotHaveAnyAttributesWithArguments(IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAnyAttributesWithArguments(argumentValues));
125-
public TGivenRuleTypeConjunction DoNotHaveAnyAttributesWithArguments(object firstArgumentValue, params object[] moreArgumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAnyAttributesWithArguments(new[] { firstArgumentValue }.Concat(moreArgumentValues)));
126124

127-
public TGivenRuleTypeConjunction DoNotHaveAttributeWithArguments(Attribute attribute, IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithArguments(attribute, argumentValues));
128-
public TGivenRuleTypeConjunction DoNotHaveAttributeWithArguments(Attribute attribute, object firstArgumentValue, params object[] moreArgumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithArguments(attribute, new[] { firstArgumentValue }.Concat(moreArgumentValues)));
129-
public TGivenRuleTypeConjunction DoNotHaveAttributeWithArguments(Type attribute, IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithArguments(attribute, argumentValues));
130-
public TGivenRuleTypeConjunction DoNotHaveAttributeWithArguments(Type attribute, object firstArgumentValue, params object[] moreArgumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithArguments(attribute, new[] { firstArgumentValue }.Concat(moreArgumentValues)));
125+
public TGivenRuleTypeConjunction DoNotHaveAttributeWithArguments(Attribute attribute, IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithArguments(attribute.FullName, _ => attribute, argumentValues));
126+
public TGivenRuleTypeConjunction DoNotHaveAttributeWithArguments(Type attribute, IEnumerable<object> argumentValues) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithArguments(attribute.FullName, architecture => architecture.GetAttributeOfType(attribute), argumentValues));
131127

132128
public TGivenRuleTypeConjunction DoNotHaveAnyAttributesWithNamedArguments(IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAnyAttributesWithNamedArguments(attributeArguments));
133-
public TGivenRuleTypeConjunction DoNotHaveAnyAttributesWithNamedArguments((string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments) => DoNotHaveAnyAttributesWithNamedArguments(new[] { firstAttributeArgument }.Concat(moreAttributeArguments));
129+
public TGivenRuleTypeConjunction DoNotHaveAnyAttributesWithNamedArguments(params (string, object)[] attributeArguments) => DoNotHaveAnyAttributesWithNamedArguments((IEnumerable<(string, object)>)attributeArguments);
134130

135-
public TGivenRuleTypeConjunction DoNotHaveAttributeWithNamedArguments(Attribute attribute, IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithNamedArguments(attribute, attributeArguments));
136-
public TGivenRuleTypeConjunction DoNotHaveAttributeWithNamedArguments(Attribute attribute, (string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments) => DoNotHaveAttributeWithNamedArguments(attribute, new[] { firstAttributeArgument }.Concat(moreAttributeArguments));
137-
public TGivenRuleTypeConjunction DoNotHaveAttributeWithNamedArguments(Type attribute, IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithNamedArguments(attribute, attributeArguments));
138-
public TGivenRuleTypeConjunction DoNotHaveAttributeWithNamedArguments(Type attribute, (string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments) => DoNotHaveAttributeWithNamedArguments(attribute, new[] { firstAttributeArgument }.Concat(moreAttributeArguments));
131+
public TGivenRuleTypeConjunction DoNotHaveAttributeWithNamedArguments(Attribute attribute, IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithNamedArguments(attribute.FullName, _ => attribute, attributeArguments));
132+
public TGivenRuleTypeConjunction DoNotHaveAttributeWithNamedArguments(Attribute attribute, params (string, object)[] attributeArguments) => DoNotHaveAttributeWithNamedArguments(attribute, (IEnumerable<(string, object)>)attributeArguments);
133+
public TGivenRuleTypeConjunction DoNotHaveAttributeWithNamedArguments(Type attribute, IEnumerable<(string, object)> attributeArguments) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveAttributeWithNamedArguments(attribute.FullName, architecture => architecture.GetAttributeOfType(attribute), attributeArguments));
134+
public TGivenRuleTypeConjunction DoNotHaveAttributeWithNamedArguments(Type attribute, params (string, object)[] attributeArguments) => DoNotHaveAttributeWithNamedArguments(attribute, (IEnumerable<(string, object)>)attributeArguments);
139135

140136
public TGivenRuleTypeConjunction DoNotHaveName(string name) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveName(name));
141137
public TGivenRuleTypeConjunction DoNotHaveNameMatching(string pattern) => AddPredicate(ObjectPredicatesDefinition<TRuleType>.DoNotHaveNameMatching(pattern));

ArchUnitNET/Fluent/Syntax/Elements/IObjectConditions.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,19 @@ public interface IObjectConditions<out TReturnType, out TRuleType>
5252
TReturnType OnlyHaveAttributes(IObjectProvider<Attribute> attributes);
5353
TReturnType OnlyHaveAttributes(IEnumerable<Attribute> attributes);
5454
TReturnType OnlyHaveAttributes(IEnumerable<Type> attributes);
55-
55+
5656
TReturnType HaveAnyAttributesWithArguments(IEnumerable<object> argumentValues);
57-
TReturnType HaveAnyAttributesWithArguments(object firstArgumentValue, params object[] moreArgumentValues);
5857

5958
TReturnType HaveAttributeWithArguments(Attribute attribute, IEnumerable<object> argumentValues);
60-
TReturnType HaveAttributeWithArguments(Attribute attribute, object firstArgumentValue, params object[] moreArgumentValues);
6159
TReturnType HaveAttributeWithArguments(Type attribute, IEnumerable<object> argumentValues);
62-
TReturnType HaveAttributeWithArguments(Type attribute, object firstArgumentValue, params object[] moreArgumentValues);
6360

6461
TReturnType HaveAnyAttributesWithNamedArguments(IEnumerable<(string, object)> attributeArguments);
65-
TReturnType HaveAnyAttributesWithNamedArguments((string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments);
62+
TReturnType HaveAnyAttributesWithNamedArguments(params (string, object)[] attributeArguments);
6663

6764
TReturnType HaveAttributeWithNamedArguments(Attribute attribute, IEnumerable<(string, object)> attributeArguments);
68-
TReturnType HaveAttributeWithNamedArguments(Attribute attribute, (string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments);
65+
TReturnType HaveAttributeWithNamedArguments(Attribute attribute, params (string, object)[] attributeArguments);
6966
TReturnType HaveAttributeWithNamedArguments(Type attribute, IEnumerable<(string, object)> attributeArguments);
70-
TReturnType HaveAttributeWithNamedArguments(Type attribute, (string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments);
67+
TReturnType HaveAttributeWithNamedArguments(Type attribute, params (string, object)[] attributeArguments);
7168

7269
TReturnType HaveName(string name);
7370
TReturnType HaveNameMatching(string pattern);
@@ -121,21 +118,17 @@ public interface IObjectConditions<out TReturnType, out TRuleType>
121118
TReturnType NotHaveAnyAttributes(IEnumerable<Type> attributes);
122119

123120
TReturnType NotHaveAnyAttributesWithArguments(IEnumerable<object> argumentValues);
124-
TReturnType NotHaveAnyAttributesWithArguments(object firstArgumentValue, params object[] moreArgumentValues);
125121

126122
TReturnType NotHaveAttributeWithArguments(Attribute attribute, IEnumerable<object> argumentValues);
127-
TReturnType NotHaveAttributeWithArguments(Attribute attribute, object firstArgumentValue, params object[] moreArgumentValues);
128123
TReturnType NotHaveAttributeWithArguments(Type attribute, IEnumerable<object> argumentValues);
129-
TReturnType NotHaveAttributeWithArguments(Type attribute, object firstArgumentValue, params object[] moreArgumentValues);
130124

131125
TReturnType NotHaveAnyAttributesWithNamedArguments(IEnumerable<(string, object)> attributeArguments);
132-
133-
TReturnType NotHaveAnyAttributesWithNamedArguments((string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments);
126+
TReturnType NotHaveAnyAttributesWithNamedArguments(params (string, object)[] attributeArguments);
134127

135128
TReturnType NotHaveAttributeWithNamedArguments(Attribute attribute, IEnumerable<(string, object)> attributeArguments);
136-
TReturnType NotHaveAttributeWithNamedArguments(Attribute attribute, (string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments);
129+
TReturnType NotHaveAttributeWithNamedArguments(Attribute attribute, params (string, object)[] attributeArguments);
137130
TReturnType NotHaveAttributeWithNamedArguments(Type attribute, IEnumerable<(string, object)> attributeArguments);
138-
TReturnType NotHaveAttributeWithNamedArguments(Type attribute, (string, object) firstAttributeArgument, params (string, object)[] moreAttributeArguments);
131+
TReturnType NotHaveAttributeWithNamedArguments(Type attribute, params (string, object)[] attributeArguments);
139132

140133
TReturnType NotHaveName(string name);
141134
TReturnType NotHaveNameMatching(string pattern);

0 commit comments

Comments
 (0)