Skip to content

Commit

Permalink
write tests for fluent implementations
Browse files Browse the repository at this point in the history
Signed-off-by: Fritz Brandhuber <[email protected]>
  • Loading branch information
brandhuf committed Jul 12, 2021
1 parent 3af9c31 commit 118caf3
Showing 1 changed file with 154 additions and 0 deletions.
154 changes: 154 additions & 0 deletions ArchUnitNETTests/Domain/AttributeArgumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
using ArchUnitNET.Domain;
using ArchUnitNET.Domain.Extensions;
using ArchUnitNET.Loader;
using ArchUnitNET.xUnit;
using Xunit;
using static ArchUnitNET.Fluent.ArchRuleDefinition;
using Attribute = System.Attribute;

namespace ArchUnitNETTests.Domain
Expand Down Expand Up @@ -93,6 +95,158 @@ public void AssignNamesToAttributeParameters()
Assert.Contains(namedArguments, arg => arg.Name == "Parameter2" && (string) arg.Value == "param2_1");
Assert.Contains(namedArguments, arg => arg.Name == "Parameter3" && (string) arg.Value == "param3_2");
}

[Fact]
public void FluentPredicatesTest()
{
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
.HaveAttributeWithArguments(_attributeWithStringParameters, "param1_1").Should().Exist()
.Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
.HaveAnyAttributesWithArguments("param1_0").Should()
.Exist().Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
.HaveAnyAttributesWithNamedArguments(("Parameter2", "param2_1")).Should()
.Exist().Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
.HaveAnyAttributesWithNamedArguments(("Parameter2", "param2_1"), ("Parameter3", "param3_2")).Should()
.Exist().Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
.HaveAnyAttributesWithArguments(1).Should()
.Exist().Check(Architecture);
Types().That().Are(typeof(ClassWithTypeParameterAttribute)).And()
.HaveAnyAttributesWithArguments(typeof(ClassWithArrayParameterAttribute)).Should()
.Exist().Check(Architecture);
Types().That().Are(typeof(ClassWithTypeParameterAttribute)).And()
.HaveAnyAttributesWithNamedArguments(("Type2", typeof(ClassWithArrayParameterAttribute))).Should()
.Exist().Check(Architecture);


Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
.HaveAnyAttributesWithNamedArguments(("Parameter2", "param1_1")).Should()
.NotExist().Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
.HaveAttributeWithArguments(_attributeWithStringParameters, "non_existent").Should()
.NotExist().Check(Architecture);
Types().That().Are(typeof(ClassWithTypeParameterAttribute)).And()
.HaveAnyAttributesWithNamedArguments(("Type3", typeof(ClassWithArrayParameterAttribute))).Should()
.NotExist().Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
.HaveAnyAttributesWithArguments("1").Should()
.NotExist().Check(Architecture);

//Negations

Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
.DoNotHaveAttributeWithArguments(_attributeWithStringParameters, "param1_1").Should().NotExist()
.Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
.DoNotHaveAnyAttributesWithArguments("param1_0").Should()
.NotExist().Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
.DoNotHaveAnyAttributesWithNamedArguments(("Parameter2", "param2_1")).Should()
.NotExist().Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
.DoNotHaveAnyAttributesWithNamedArguments(("Parameter2", "param2_1"), ("Parameter3", "param3_2"))
.Should()
.NotExist().Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
.DoNotHaveAnyAttributesWithArguments(1).Should()
.NotExist().Check(Architecture);
Types().That().Are(typeof(ClassWithTypeParameterAttribute)).And()
.DoNotHaveAnyAttributesWithArguments(typeof(ClassWithArrayParameterAttribute)).Should()
.NotExist().Check(Architecture);
Types().That().Are(typeof(ClassWithTypeParameterAttribute)).And()
.DoNotHaveAnyAttributesWithNamedArguments(("Type2", typeof(ClassWithArrayParameterAttribute))).Should()
.NotExist().Check(Architecture);


Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
.DoNotHaveAnyAttributesWithNamedArguments(("Parameter2", "param1_1")).Should()
.Exist().Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
.DoNotHaveAttributeWithArguments(_attributeWithStringParameters, "non_existent").Should()
.Exist().Check(Architecture);
Types().That().Are(typeof(ClassWithTypeParameterAttribute)).And()
.DoNotHaveAnyAttributesWithNamedArguments(("Type3", typeof(ClassWithArrayParameterAttribute))).Should()
.Exist().Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).And()
.DoNotHaveAnyAttributesWithArguments("1").Should()
.Exist().Check(Architecture);
}

[Fact]
public void FluentConditionsTest()
{
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
.HaveAttributeWithArguments(_attributeWithStringParameters, "param1_1").Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
.HaveAnyAttributesWithArguments("param1_0").Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
.HaveAnyAttributesWithNamedArguments(("Parameter2", "param2_1")).Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
.HaveAnyAttributesWithNamedArguments(("Parameter2", "param2_1"), ("Parameter3", "param3_2"))
.Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
.HaveAnyAttributesWithArguments(1).Check(Architecture);
Types().That().Are(typeof(ClassWithTypeParameterAttribute)).Should()
.HaveAnyAttributesWithArguments(typeof(ClassWithArrayParameterAttribute)).Check(Architecture);
Types().That().Are(typeof(ClassWithTypeParameterAttribute)).Should()
.HaveAnyAttributesWithNamedArguments(("Type2", typeof(ClassWithArrayParameterAttribute)))
.Check(Architecture);


Assert.Throws<FailedArchRuleException>(() => Types().That()
.Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
.HaveAnyAttributesWithNamedArguments(("Parameter2", "param1_1")).Check(Architecture));
Assert.Throws<FailedArchRuleException>(() => Types().That()
.Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
.HaveAttributeWithArguments(_attributeWithStringParameters, "non_existent").Check(Architecture));
Assert.Throws<FailedArchRuleException>(() => Types().That().Are(typeof(ClassWithTypeParameterAttribute))
.Should()
.HaveAnyAttributesWithNamedArguments(("Type3", typeof(ClassWithArrayParameterAttribute)))
.Check(Architecture));
Assert.Throws<FailedArchRuleException>(() => Types().That()
.Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
.HaveAnyAttributesWithArguments("1").Check(Architecture));

//Negations

Assert.Throws<FailedArchRuleException>(() => Types().That()
.Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
.NotHaveAttributeWithArguments(_attributeWithStringParameters, "param1_1").Check(Architecture));
Assert.Throws<FailedArchRuleException>(() => Types().That()
.Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
.NotHaveAnyAttributesWithArguments("param1_0").Check(Architecture));
Assert.Throws<FailedArchRuleException>(() => Types().That()
.Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
.NotHaveAnyAttributesWithNamedArguments(("Parameter2", "param2_1")).Check(Architecture));
Assert.Throws<FailedArchRuleException>(() => Types().That()
.Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
.NotHaveAnyAttributesWithNamedArguments(("Parameter2", "param2_1"), ("Parameter3", "param3_2"))
.Check(Architecture));
Assert.Throws<FailedArchRuleException>(() => Types().That()
.Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
.NotHaveAnyAttributesWithArguments(1).Check(Architecture));
Assert.Throws<FailedArchRuleException>(() => Types().That().Are(typeof(ClassWithTypeParameterAttribute))
.Should()
.NotHaveAnyAttributesWithArguments(typeof(ClassWithArrayParameterAttribute)).Check(Architecture));
Assert.Throws<FailedArchRuleException>(() => Types().That().Are(typeof(ClassWithTypeParameterAttribute))
.Should()
.NotHaveAnyAttributesWithNamedArguments(("Type2", typeof(ClassWithArrayParameterAttribute)))
.Check(Architecture));


Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
.NotHaveAnyAttributesWithNamedArguments(("Parameter2", "param1_1")).Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
.NotHaveAttributeWithArguments(_attributeWithStringParameters, "non_existent").Check(Architecture);
Types().That().Are(typeof(ClassWithTypeParameterAttribute)).Should()
.NotHaveAnyAttributesWithNamedArguments(("Type3", typeof(ClassWithArrayParameterAttribute)))
.Check(Architecture);
Types().That().Are(typeof(ClassWithMultipleAttributesWithParameters)).Should()
.NotHaveAnyAttributesWithArguments("1").Check(Architecture);
}
}

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
Expand Down

0 comments on commit 118caf3

Please sign in to comment.