From 118caf30b8533aa85c742744d18294e6a2df5527 Mon Sep 17 00:00:00 2001 From: Fritz Brandhuber Date: Mon, 12 Jul 2021 19:02:27 +0200 Subject: [PATCH] write tests for fluent implementations Signed-off-by: Fritz Brandhuber --- .../Domain/AttributeArgumentTests.cs | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) diff --git a/ArchUnitNETTests/Domain/AttributeArgumentTests.cs b/ArchUnitNETTests/Domain/AttributeArgumentTests.cs index 79f5c310f..646ca7f29 100644 --- a/ArchUnitNETTests/Domain/AttributeArgumentTests.cs +++ b/ArchUnitNETTests/Domain/AttributeArgumentTests.cs @@ -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 @@ -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(() => Types().That() + .Are(typeof(ClassWithMultipleAttributesWithParameters)).Should() + .HaveAnyAttributesWithNamedArguments(("Parameter2", "param1_1")).Check(Architecture)); + Assert.Throws(() => Types().That() + .Are(typeof(ClassWithMultipleAttributesWithParameters)).Should() + .HaveAttributeWithArguments(_attributeWithStringParameters, "non_existent").Check(Architecture)); + Assert.Throws(() => Types().That().Are(typeof(ClassWithTypeParameterAttribute)) + .Should() + .HaveAnyAttributesWithNamedArguments(("Type3", typeof(ClassWithArrayParameterAttribute))) + .Check(Architecture)); + Assert.Throws(() => Types().That() + .Are(typeof(ClassWithMultipleAttributesWithParameters)).Should() + .HaveAnyAttributesWithArguments("1").Check(Architecture)); + + //Negations + + Assert.Throws(() => Types().That() + .Are(typeof(ClassWithMultipleAttributesWithParameters)).Should() + .NotHaveAttributeWithArguments(_attributeWithStringParameters, "param1_1").Check(Architecture)); + Assert.Throws(() => Types().That() + .Are(typeof(ClassWithMultipleAttributesWithParameters)).Should() + .NotHaveAnyAttributesWithArguments("param1_0").Check(Architecture)); + Assert.Throws(() => Types().That() + .Are(typeof(ClassWithMultipleAttributesWithParameters)).Should() + .NotHaveAnyAttributesWithNamedArguments(("Parameter2", "param2_1")).Check(Architecture)); + Assert.Throws(() => Types().That() + .Are(typeof(ClassWithMultipleAttributesWithParameters)).Should() + .NotHaveAnyAttributesWithNamedArguments(("Parameter2", "param2_1"), ("Parameter3", "param3_2")) + .Check(Architecture)); + Assert.Throws(() => Types().That() + .Are(typeof(ClassWithMultipleAttributesWithParameters)).Should() + .NotHaveAnyAttributesWithArguments(1).Check(Architecture)); + Assert.Throws(() => Types().That().Are(typeof(ClassWithTypeParameterAttribute)) + .Should() + .NotHaveAnyAttributesWithArguments(typeof(ClassWithArrayParameterAttribute)).Check(Architecture)); + Assert.Throws(() => 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)]