Skip to content

Commit

Permalink
Merge pull request #114 from brandhuf/fix-attribute-argument-bug
Browse files Browse the repository at this point in the history
Fix attribute argument bug
  • Loading branch information
fgather authored Sep 20, 2021
2 parents a3355d3 + 296d512 commit 0deaa8e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
16 changes: 8 additions & 8 deletions ArchUnitNET/Fluent/Syntax/Elements/ObjectConditionsDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ public static ICondition<TRuleType> HaveAttributeWithNamedArguments(Type attribu

public static ICondition<TRuleType> HaveAnyAttributesWithArguments(IEnumerable<object> argumentValues)
{
var argumentValueList = argumentValues.ToList();
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
string description;
Func<TRuleType, Architecture, string> failDescription;
if (argumentValueList.IsNullOrEmpty())
Expand Down Expand Up @@ -1124,7 +1124,7 @@ public static ICondition<TRuleType> HaveAttributeWithArguments([NotNull] string
IEnumerable<object> argumentValues)
{
string description, failDescription;
var argumentValueList = argumentValues.ToList();
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
if (argumentValueList.IsNullOrEmpty())
{
description = "have attribute \"" + attribute + "\"";
Expand Down Expand Up @@ -1182,7 +1182,7 @@ public static ICondition<TRuleType> HaveAttributeWithArguments([NotNull] Attribu
IEnumerable<object> argumentValues)
{
string description, failDescription;
var argumentValueList = argumentValues.ToList();
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
if (argumentValueList.IsNullOrEmpty())
{
description = "have attribute \"" + attribute.FullName + "\"";
Expand Down Expand Up @@ -1240,7 +1240,7 @@ public static ICondition<TRuleType> HaveAttributeWithArguments([NotNull] Type at
IEnumerable<object> argumentValues)
{
string description, failDescription;
var argumentValueList = argumentValues.ToList();
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
if (argumentValueList.IsNullOrEmpty())
{
description = "have attribute \"" + attribute.FullName + "\"";
Expand Down Expand Up @@ -2371,7 +2371,7 @@ public static ICondition<TRuleType> NotHaveAttributeWithNamedArguments(Type attr

public static ICondition<TRuleType> NotHaveAnyAttributesWithArguments(IEnumerable<object> argumentValues)
{
var argumentValueList = argumentValues.ToList();
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
string description;
Func<TRuleType, Architecture, string> failDescription;
if (argumentValueList.IsNullOrEmpty())
Expand Down Expand Up @@ -2435,7 +2435,7 @@ public static ICondition<TRuleType> NotHaveAttributeWithArguments([NotNull] stri
IEnumerable<object> argumentValues)
{
string description, failDescription;
var argumentValueList = argumentValues.ToList();
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
if (argumentValueList.IsNullOrEmpty())
{
description = "not have attribute \"" + attribute + "\"";
Expand Down Expand Up @@ -2493,7 +2493,7 @@ public static ICondition<TRuleType> NotHaveAttributeWithArguments([NotNull] Attr
IEnumerable<object> argumentValues)
{
string description, failDescription;
var argumentValueList = argumentValues.ToList();
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
if (argumentValueList.IsNullOrEmpty())
{
description = "not have attribute \"" + attribute.FullName + "\"";
Expand Down Expand Up @@ -2551,7 +2551,7 @@ public static ICondition<TRuleType> NotHaveAttributeWithArguments([NotNull] Type
IEnumerable<object> argumentValues)
{
string description, failDescription;
var argumentValueList = argumentValues.ToList();
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
if (argumentValueList.IsNullOrEmpty())
{
description = "not have attribute \"" + attribute.FullName + "\"";
Expand Down
16 changes: 8 additions & 8 deletions ArchUnitNET/Fluent/Syntax/Elements/ObjectPredicatesDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ public static IPredicate<T> HaveAttributeWithNamedArguments(Type attribute,

public static IPredicate<T> HaveAnyAttributesWithArguments(IEnumerable<object> argumentValues)
{
var argumentValueList = argumentValues.ToList();
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
string description;
if (argumentValueList.IsNullOrEmpty())
{
Expand Down Expand Up @@ -718,7 +718,7 @@ public static IPredicate<T> HaveAttributeWithArguments([NotNull] string attribut
IEnumerable<object> argumentValues)
{
string description;
var argumentValueList = argumentValues.ToList();
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
if (argumentValueList.IsNullOrEmpty())
{
description = "have attribute \"" + attribute + "\"";
Expand Down Expand Up @@ -772,7 +772,7 @@ public static IPredicate<T> HaveAttributeWithArguments([NotNull] Attribute attri
IEnumerable<object> argumentValues)
{
string description;
var argumentValueList = argumentValues.ToList();
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
if (argumentValueList.IsNullOrEmpty())
{
description = "have attribute \"" + attribute.FullName + "\"";
Expand Down Expand Up @@ -826,7 +826,7 @@ public static IPredicate<T> HaveAttributeWithArguments([NotNull] Type attribute,
IEnumerable<object> argumentValues)
{
string description;
var argumentValueList = argumentValues.ToList();
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
if (argumentValueList.IsNullOrEmpty())
{
description = "have attribute \"" + attribute.FullName + "\"";
Expand Down Expand Up @@ -1620,7 +1620,7 @@ public static IPredicate<T> DoNotHaveAttributeWithNamedArguments(Type attribute,

public static IPredicate<T> DoNotHaveAnyAttributesWithArguments(IEnumerable<object> argumentValues)
{
var argumentValueList = argumentValues.ToList();
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
string description;
if (argumentValueList.IsNullOrEmpty())
{
Expand Down Expand Up @@ -1665,7 +1665,7 @@ public static IPredicate<T> DoNotHaveAttributeWithArguments([NotNull] string att
IEnumerable<object> argumentValues)
{
string description;
var argumentValueList = argumentValues.ToList();
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
if (argumentValueList.IsNullOrEmpty())
{
description = "do not have attribute \"" + attribute + "\"";
Expand Down Expand Up @@ -1719,7 +1719,7 @@ public static IPredicate<T> DoNotHaveAttributeWithArguments([NotNull] Attribute
IEnumerable<object> argumentValues)
{
string description;
var argumentValueList = argumentValues.ToList();
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
if (argumentValueList.IsNullOrEmpty())
{
description = "do not have attribute \"" + attribute.FullName + "\"";
Expand Down Expand Up @@ -1773,7 +1773,7 @@ public static IPredicate<T> DoNotHaveAttributeWithArguments([NotNull] Type attri
IEnumerable<object> argumentValues)
{
string description;
var argumentValueList = argumentValues.ToList();
var argumentValueList = argumentValues?.ToList() ?? new List<object> {null};
if (argumentValueList.IsNullOrEmpty())
{
description = "do not have attribute \"" + attribute.FullName + "\"";
Expand Down
8 changes: 4 additions & 4 deletions ArchUnitNET/Loader/MonoCecilAttributeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ private static void HandleAttributeArgument(CustomAttributeArgument argument, Ty

type = typeFactory.GetOrCreateStubTypeInstanceFromTypeReference(argument.Type);

if (type.IsArray)
if (argument.Value is IEnumerable<CustomAttributeArgument> attArgEnumerable)
{
value = (from arrayMember in (CustomAttributeArgument[]) argument.Value
select arrayMember.Value is TypeReference tr
value = (from attArg in attArgEnumerable
select attArg.Value is TypeReference tr
? typeFactory.GetOrCreateStubTypeInstanceFromTypeReference(tr)
: arrayMember.Value)
: attArg.Value)
.ToArray();
}
else
Expand Down
17 changes: 10 additions & 7 deletions ArchUnitNETTests/Domain/AttributeArgumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void FluentPredicatesTest()
.HaveAnyAttributesWithNamedArguments(("Parameter2", "param2_1"), ("Parameter3", "param3_2")).Should()
.Exist().Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
.HaveAnyAttributesWithArguments(1).Should()
.HaveAnyAttributesWithArguments(null).Should()
.Exist().Check(Architecture);
Types().That().Are(typeof(ClassWithTypeParameterAttribute)).And()
.HaveAnyAttributesWithArguments(typeof(ClassWithArrayParameterAttribute)).Should()
Expand Down Expand Up @@ -151,7 +151,7 @@ public void FluentPredicatesTest()
.Should()
.NotExist().Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
.DoNotHaveAnyAttributesWithArguments(1).Should()
.DoNotHaveAnyAttributesWithArguments(null).Should()
.NotExist().Check(Architecture);
Types().That().Are(typeof(ClassWithTypeParameterAttribute)).And()
.DoNotHaveAnyAttributesWithArguments(typeof(ClassWithArrayParameterAttribute)).Should()
Expand Down Expand Up @@ -188,7 +188,7 @@ public void FluentConditionsTest()
.HaveAnyAttributesWithNamedArguments(("Parameter2", "param2_1"), ("Parameter3", "param3_2"))
.Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
.HaveAnyAttributesWithArguments(1).Check(Architecture);
.HaveAnyAttributesWithArguments(null).Check(Architecture);
Types().That().Are(typeof(ClassWithTypeParameterAttribute)).Should()
.HaveAnyAttributesWithArguments(typeof(ClassWithArrayParameterAttribute)).Check(Architecture);
Types().That().Are(typeof(ClassWithTypeParameterAttribute)).Should()
Expand Down Expand Up @@ -227,7 +227,7 @@ public void FluentConditionsTest()
.Check(Architecture));
Assert.Throws<FailedArchRuleException>(() => Types().That()
.Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
.NotHaveAnyAttributesWithArguments(1).Check(Architecture));
.NotHaveAnyAttributesWithArguments(null).Check(Architecture));
Assert.Throws<FailedArchRuleException>(() => Types().That().Are(typeof(ClassWithTypeParameterAttribute))
.Should()
.NotHaveAnyAttributesWithArguments(typeof(ClassWithArrayParameterAttribute)).Check(Architecture));
Expand Down Expand Up @@ -270,9 +270,12 @@ public AttributeWithStringParameters(string parameter1, string parameter2)

internal class AttributeWithObjectParameter : Attribute
{
public AttributeWithObjectParameter(object type)
public AttributeWithObjectParameter(params object[] arguments)
{
}

public AttributeWithObjectParameter(object arg)
{
Type = type;
}

public object Type { get; }
Expand All @@ -283,7 +286,7 @@ public AttributeWithObjectParameter(object type)
[AttributeWithStringParameters("param1_0")]
[AttributeWithStringParameters("param1_1", Parameter2 = "param2_1")]
[AttributeWithStringParameters("param1_2", "param2_2", Parameter3 = "param3_2")]
[AttributeWithObjectParameter(1)]
[AttributeWithObjectParameter(null)]
internal class ClassWithMultipleAttributesWithParameters
{
}
Expand Down

0 comments on commit 0deaa8e

Please sign in to comment.