Skip to content

Commit 0deaa8e

Browse files
authored
Merge pull request #114 from brandhuf/fix-attribute-argument-bug
Fix attribute argument bug
2 parents a3355d3 + 296d512 commit 0deaa8e

File tree

4 files changed

+30
-27
lines changed

4 files changed

+30
-27
lines changed

ArchUnitNET/Fluent/Syntax/Elements/ObjectConditionsDefinition.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ public static ICondition<TRuleType> HaveAttributeWithNamedArguments(Type attribu
10591059

10601060
public static ICondition<TRuleType> HaveAnyAttributesWithArguments(IEnumerable<object> argumentValues)
10611061
{
1062-
var argumentValueList = argumentValues.ToList();
1062+
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
10631063
string description;
10641064
Func<TRuleType, Architecture, string> failDescription;
10651065
if (argumentValueList.IsNullOrEmpty())
@@ -1124,7 +1124,7 @@ public static ICondition<TRuleType> HaveAttributeWithArguments([NotNull] string
11241124
IEnumerable<object> argumentValues)
11251125
{
11261126
string description, failDescription;
1127-
var argumentValueList = argumentValues.ToList();
1127+
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
11281128
if (argumentValueList.IsNullOrEmpty())
11291129
{
11301130
description = "have attribute \"" + attribute + "\"";
@@ -1182,7 +1182,7 @@ public static ICondition<TRuleType> HaveAttributeWithArguments([NotNull] Attribu
11821182
IEnumerable<object> argumentValues)
11831183
{
11841184
string description, failDescription;
1185-
var argumentValueList = argumentValues.ToList();
1185+
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
11861186
if (argumentValueList.IsNullOrEmpty())
11871187
{
11881188
description = "have attribute \"" + attribute.FullName + "\"";
@@ -1240,7 +1240,7 @@ public static ICondition<TRuleType> HaveAttributeWithArguments([NotNull] Type at
12401240
IEnumerable<object> argumentValues)
12411241
{
12421242
string description, failDescription;
1243-
var argumentValueList = argumentValues.ToList();
1243+
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
12441244
if (argumentValueList.IsNullOrEmpty())
12451245
{
12461246
description = "have attribute \"" + attribute.FullName + "\"";
@@ -2371,7 +2371,7 @@ public static ICondition<TRuleType> NotHaveAttributeWithNamedArguments(Type attr
23712371

23722372
public static ICondition<TRuleType> NotHaveAnyAttributesWithArguments(IEnumerable<object> argumentValues)
23732373
{
2374-
var argumentValueList = argumentValues.ToList();
2374+
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
23752375
string description;
23762376
Func<TRuleType, Architecture, string> failDescription;
23772377
if (argumentValueList.IsNullOrEmpty())
@@ -2435,7 +2435,7 @@ public static ICondition<TRuleType> NotHaveAttributeWithArguments([NotNull] stri
24352435
IEnumerable<object> argumentValues)
24362436
{
24372437
string description, failDescription;
2438-
var argumentValueList = argumentValues.ToList();
2438+
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
24392439
if (argumentValueList.IsNullOrEmpty())
24402440
{
24412441
description = "not have attribute \"" + attribute + "\"";
@@ -2493,7 +2493,7 @@ public static ICondition<TRuleType> NotHaveAttributeWithArguments([NotNull] Attr
24932493
IEnumerable<object> argumentValues)
24942494
{
24952495
string description, failDescription;
2496-
var argumentValueList = argumentValues.ToList();
2496+
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
24972497
if (argumentValueList.IsNullOrEmpty())
24982498
{
24992499
description = "not have attribute \"" + attribute.FullName + "\"";
@@ -2551,7 +2551,7 @@ public static ICondition<TRuleType> NotHaveAttributeWithArguments([NotNull] Type
25512551
IEnumerable<object> argumentValues)
25522552
{
25532553
string description, failDescription;
2554-
var argumentValueList = argumentValues.ToList();
2554+
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
25552555
if (argumentValueList.IsNullOrEmpty())
25562556
{
25572557
description = "not have attribute \"" + attribute.FullName + "\"";

ArchUnitNET/Fluent/Syntax/Elements/ObjectPredicatesDefinition.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ public static IPredicate<T> HaveAttributeWithNamedArguments(Type attribute,
673673

674674
public static IPredicate<T> HaveAnyAttributesWithArguments(IEnumerable<object> argumentValues)
675675
{
676-
var argumentValueList = argumentValues.ToList();
676+
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
677677
string description;
678678
if (argumentValueList.IsNullOrEmpty())
679679
{
@@ -718,7 +718,7 @@ public static IPredicate<T> HaveAttributeWithArguments([NotNull] string attribut
718718
IEnumerable<object> argumentValues)
719719
{
720720
string description;
721-
var argumentValueList = argumentValues.ToList();
721+
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
722722
if (argumentValueList.IsNullOrEmpty())
723723
{
724724
description = "have attribute \"" + attribute + "\"";
@@ -772,7 +772,7 @@ public static IPredicate<T> HaveAttributeWithArguments([NotNull] Attribute attri
772772
IEnumerable<object> argumentValues)
773773
{
774774
string description;
775-
var argumentValueList = argumentValues.ToList();
775+
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
776776
if (argumentValueList.IsNullOrEmpty())
777777
{
778778
description = "have attribute \"" + attribute.FullName + "\"";
@@ -826,7 +826,7 @@ public static IPredicate<T> HaveAttributeWithArguments([NotNull] Type attribute,
826826
IEnumerable<object> argumentValues)
827827
{
828828
string description;
829-
var argumentValueList = argumentValues.ToList();
829+
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
830830
if (argumentValueList.IsNullOrEmpty())
831831
{
832832
description = "have attribute \"" + attribute.FullName + "\"";
@@ -1620,7 +1620,7 @@ public static IPredicate<T> DoNotHaveAttributeWithNamedArguments(Type attribute,
16201620

16211621
public static IPredicate<T> DoNotHaveAnyAttributesWithArguments(IEnumerable<object> argumentValues)
16221622
{
1623-
var argumentValueList = argumentValues.ToList();
1623+
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
16241624
string description;
16251625
if (argumentValueList.IsNullOrEmpty())
16261626
{
@@ -1665,7 +1665,7 @@ public static IPredicate<T> DoNotHaveAttributeWithArguments([NotNull] string att
16651665
IEnumerable<object> argumentValues)
16661666
{
16671667
string description;
1668-
var argumentValueList = argumentValues.ToList();
1668+
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
16691669
if (argumentValueList.IsNullOrEmpty())
16701670
{
16711671
description = "do not have attribute \"" + attribute + "\"";
@@ -1719,7 +1719,7 @@ public static IPredicate<T> DoNotHaveAttributeWithArguments([NotNull] Attribute
17191719
IEnumerable<object> argumentValues)
17201720
{
17211721
string description;
1722-
var argumentValueList = argumentValues.ToList();
1722+
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
17231723
if (argumentValueList.IsNullOrEmpty())
17241724
{
17251725
description = "do not have attribute \"" + attribute.FullName + "\"";
@@ -1773,7 +1773,7 @@ public static IPredicate<T> DoNotHaveAttributeWithArguments([NotNull] Type attri
17731773
IEnumerable<object> argumentValues)
17741774
{
17751775
string description;
1776-
var argumentValueList = argumentValues.ToList();
1776+
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
17771777
if (argumentValueList.IsNullOrEmpty())
17781778
{
17791779
description = "do not have attribute \"" + attribute.FullName + "\"";

ArchUnitNET/Loader/MonoCecilAttributeExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ private static void HandleAttributeArgument(CustomAttributeArgument argument, Ty
5353

5454
type = typeFactory.GetOrCreateStubTypeInstanceFromTypeReference(argument.Type);
5555

56-
if (type.IsArray)
56+
if (argument.Value is IEnumerable<CustomAttributeArgument> attArgEnumerable)
5757
{
58-
value = (from arrayMember in (CustomAttributeArgument[]) argument.Value
59-
select arrayMember.Value is TypeReference tr
58+
value = (from attArg in attArgEnumerable
59+
select attArg.Value is TypeReference tr
6060
? typeFactory.GetOrCreateStubTypeInstanceFromTypeReference(tr)
61-
: arrayMember.Value)
61+
: attArg.Value)
6262
.ToArray();
6363
}
6464
else

ArchUnitNETTests/Domain/AttributeArgumentTests.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void FluentPredicatesTest()
112112
.HaveAnyAttributesWithNamedArguments(("Parameter2", "param2_1"), ("Parameter3", "param3_2")).Should()
113113
.Exist().Check(Architecture);
114114
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
115-
.HaveAnyAttributesWithArguments(1).Should()
115+
.HaveAnyAttributesWithArguments(null).Should()
116116
.Exist().Check(Architecture);
117117
Types().That().Are(typeof(ClassWithTypeParameterAttribute)).And()
118118
.HaveAnyAttributesWithArguments(typeof(ClassWithArrayParameterAttribute)).Should()
@@ -151,7 +151,7 @@ public void FluentPredicatesTest()
151151
.Should()
152152
.NotExist().Check(Architecture);
153153
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
154-
.DoNotHaveAnyAttributesWithArguments(1).Should()
154+
.DoNotHaveAnyAttributesWithArguments(null).Should()
155155
.NotExist().Check(Architecture);
156156
Types().That().Are(typeof(ClassWithTypeParameterAttribute)).And()
157157
.DoNotHaveAnyAttributesWithArguments(typeof(ClassWithArrayParameterAttribute)).Should()
@@ -188,7 +188,7 @@ public void FluentConditionsTest()
188188
.HaveAnyAttributesWithNamedArguments(("Parameter2", "param2_1"), ("Parameter3", "param3_2"))
189189
.Check(Architecture);
190190
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
191-
.HaveAnyAttributesWithArguments(1).Check(Architecture);
191+
.HaveAnyAttributesWithArguments(null).Check(Architecture);
192192
Types().That().Are(typeof(ClassWithTypeParameterAttribute)).Should()
193193
.HaveAnyAttributesWithArguments(typeof(ClassWithArrayParameterAttribute)).Check(Architecture);
194194
Types().That().Are(typeof(ClassWithTypeParameterAttribute)).Should()
@@ -227,7 +227,7 @@ public void FluentConditionsTest()
227227
.Check(Architecture));
228228
Assert.Throws<FailedArchRuleException>(() => Types().That()
229229
.Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
230-
.NotHaveAnyAttributesWithArguments(1).Check(Architecture));
230+
.NotHaveAnyAttributesWithArguments(null).Check(Architecture));
231231
Assert.Throws<FailedArchRuleException>(() => Types().That().Are(typeof(ClassWithTypeParameterAttribute))
232232
.Should()
233233
.NotHaveAnyAttributesWithArguments(typeof(ClassWithArrayParameterAttribute)).Check(Architecture));
@@ -270,9 +270,12 @@ public AttributeWithStringParameters(string parameter1, string parameter2)
270270

271271
internal class AttributeWithObjectParameter : Attribute
272272
{
273-
public AttributeWithObjectParameter(object type)
273+
public AttributeWithObjectParameter(params object[] arguments)
274+
{
275+
}
276+
277+
public AttributeWithObjectParameter(object arg)
274278
{
275-
Type = type;
276279
}
277280

278281
public object Type { get; }
@@ -283,7 +286,7 @@ public AttributeWithObjectParameter(object type)
283286
[AttributeWithStringParameters("param1_0")]
284287
[AttributeWithStringParameters("param1_1", Parameter2 = "param2_1")]
285288
[AttributeWithStringParameters("param1_2", "param2_2", Parameter3 = "param3_2")]
286-
[AttributeWithObjectParameter(1)]
289+
[AttributeWithObjectParameter(null)]
287290
internal class ClassWithMultipleAttributesWithParameters
288291
{
289292
}

0 commit comments

Comments
 (0)