diff --git a/ArchUnitNETTests/AssemblyTestHelper/AttributeAssemblyTestHelper.cs b/ArchUnitNETTests/AssemblyTestHelper/AttributeAssemblyTestHelper.cs index a26305f25..21a8fdaec 100644 --- a/ArchUnitNETTests/AssemblyTestHelper/AttributeAssemblyTestHelper.cs +++ b/ArchUnitNETTests/AssemblyTestHelper/AttributeAssemblyTestHelper.cs @@ -86,6 +86,24 @@ public class AttributeAssemblyTestHelpers : AssemblyTestHelper public System.Type ClassWithThreeAttributesWithNamedArgumentsSystemType = typeof(ClassWithThreeAttributesWithNamedArguments); + public Attribute RegularAttribute; + public System.Type RegularAttributeSystemType = typeof(RegularAttribute); + + public Attribute OtherRegularAttribute; + public System.Type OtherRegularAttributeSystemType = typeof(OtherRegularAttribute); + + public Attribute AbstractAttribute; + public System.Type AbstractAttributeSystemType = typeof(AbstractAttribute); + + public Attribute OtherAbstractAttribute; + public System.Type OtherAbstractAttributeSystemType = typeof(OtherAbstractAttribute); + + public Attribute SealedAttribute; + public System.Type SealedAttributeSystemType = typeof(SealedAttribute); + + public Attribute OtherSealedAttribute; + public System.Type OtherSealedAttributeSystemType = typeof(OtherSealedAttribute); + public AttributeAssemblyTestHelpers() { Attribute1 = Architecture.GetAttributeOfType(typeof(Attribute1)); @@ -122,5 +140,11 @@ public AttributeAssemblyTestHelpers() ClassWithThreeAttributesWithNamedArguments = Architecture.GetClassOfType( typeof(ClassWithThreeAttributesWithNamedArguments) ); + RegularAttribute = Architecture.GetAttributeOfType(typeof(RegularAttribute)); + OtherRegularAttribute = Architecture.GetAttributeOfType(typeof(OtherRegularAttribute)); + AbstractAttribute = Architecture.GetAttributeOfType(typeof(AbstractAttribute)); + OtherAbstractAttribute = Architecture.GetAttributeOfType(typeof(OtherAbstractAttribute)); + SealedAttribute = Architecture.GetAttributeOfType(typeof(SealedAttribute)); + OtherSealedAttribute = Architecture.GetAttributeOfType(typeof(OtherSealedAttribute)); } } diff --git a/ArchUnitNETTests/Fluent/Syntax/Elements/AttributeSyntaxElementsTests.cs b/ArchUnitNETTests/Fluent/Syntax/Elements/AttributeSyntaxElementsTests.cs index 0507876bd..1e9e3b4c7 100644 --- a/ArchUnitNETTests/Fluent/Syntax/Elements/AttributeSyntaxElementsTests.cs +++ b/ArchUnitNETTests/Fluent/Syntax/Elements/AttributeSyntaxElementsTests.cs @@ -1,158 +1,131 @@ -using System.Collections.Generic; -using ArchUnitNET.Domain; +using System.Threading.Tasks; +using ArchUnitNETTests.AssemblyTestHelper; using Xunit; using static ArchUnitNET.Fluent.ArchRuleDefinition; namespace ArchUnitNETTests.Fluent.Syntax.Elements { + // csharpier-ignore public class AttributeSyntaxElementsTests { - public AttributeSyntaxElementsTests() + [Fact] + public async Task BeAbstractTest() { - _attributes = Architecture.Attributes; + var helper = new AttributeAssemblyTestHelpers(); + + helper.AddSnapshotHeader("No Violations"); + var should = Attributes().That().Are(helper.AbstractAttribute).Should(); + + helper.AddSnapshotSubHeader("Conditions"); + should.BeAbstract().AssertNoViolations(helper); + + helper.AddSnapshotSubHeader("Predicates"); + should.Be(Attributes().That().AreAbstract()).AssertNoViolations(helper); + + helper.AddSnapshotHeader("Violations"); + should = Attributes().That().Are(helper.RegularAttribute).Should(); + + helper.AddSnapshotSubHeader("Conditions"); + should.BeAbstract().AssertOnlyViolations(helper); + + helper.AddSnapshotSubHeader("Predicates"); + should.Be(Attributes().That().AreAbstract()).AssertOnlyViolations(helper); + + helper.AddSnapshotHeader("Multiple inputs"); + Attributes().That().Are(helper.AbstractAttribute, helper.OtherAbstractAttribute).Should().BeAbstract().AssertNoViolations(helper); + Attributes().That().Are(helper.AbstractAttribute, helper.RegularAttribute).Should().BeAbstract().AssertAnyViolations(helper); + + await helper.AssertSnapshotMatches(); } - private static readonly Architecture Architecture = - StaticTestArchitectures.ArchUnitNETTestArchitecture; - private readonly IEnumerable _attributes; + [Fact] + public async Task NotBeAbstractTest() + { + var helper = new AttributeAssemblyTestHelpers(); + + helper.AddSnapshotHeader("No Violations"); + var should = Attributes().That().Are(helper.RegularAttribute).Should(); + + helper.AddSnapshotSubHeader("Conditions"); + should.NotBeAbstract().AssertNoViolations(helper); + + helper.AddSnapshotSubHeader("Predicates"); + should.Be(Attributes().That().AreNotAbstract()).AssertNoViolations(helper); + + helper.AddSnapshotHeader("Violations"); + should = Attributes().That().Are(helper.AbstractAttribute).Should(); + + helper.AddSnapshotSubHeader("Conditions"); + should.NotBeAbstract().AssertOnlyViolations(helper); + + helper.AddSnapshotSubHeader("Predicates"); + should.Be(Attributes().That().AreNotAbstract()).AssertOnlyViolations(helper); + + helper.AddSnapshotHeader("Multiple inputs"); + Attributes().That().Are(helper.RegularAttribute, helper.OtherRegularAttribute).Should().NotBeAbstract().AssertNoViolations(helper); + Attributes().That().Are(helper.RegularAttribute, helper.AbstractAttribute).Should().NotBeAbstract().AssertAnyViolations(helper); + + await helper.AssertSnapshotMatches(); + } [Fact] - public void AreAbstractTest() + public async Task BeSealedTest() { - foreach (var attribute in _attributes) - { - var attributeIsAbstract = Attributes().That().Are(attribute).Should().BeAbstract(); - var attributeIsNotAbstract = Attributes() - .That() - .Are(attribute) - .Should() - .NotBeAbstract(); - var abstractAttributesDoNotIncludeType = Attributes() - .That() - .AreAbstract() - .Should() - .NotBe(attribute) - .OrShould() - .NotExist(); - var notAbstractAttributesDoNotIncludeType = Attributes() - .That() - .AreNotAbstract() - .Should() - .NotBe(attribute) - .AndShould() - .Exist(); - - Assert.Equal( - attribute.IsAbstract, - attributeIsAbstract.HasNoViolations(Architecture) - ); - Assert.Equal( - !attribute.IsAbstract, - attributeIsNotAbstract.HasNoViolations(Architecture) - ); - Assert.Equal( - !attribute.IsAbstract, - abstractAttributesDoNotIncludeType.HasNoViolations(Architecture) - ); - Assert.Equal( - attribute.IsAbstract, - notAbstractAttributesDoNotIncludeType.HasNoViolations(Architecture) - ); - } - - var abstractAttributesAreAbstract = Attributes() - .That() - .AreAbstract() - .Should() - .BeAbstract(); - var abstractAttributesAreNotAbstract = Attributes() - .That() - .AreAbstract() - .Should() - .NotBeAbstract() - .AndShould() - .Exist(); - var notAbstractAttributesAreAbstract = Attributes() - .That() - .AreNotAbstract() - .Should() - .BeAbstract() - .AndShould() - .Exist(); - var notAbstractAttributesAreNotAbstract = Attributes() - .That() - .AreNotAbstract() - .Should() - .NotBeAbstract(); - - Assert.True(abstractAttributesAreAbstract.HasNoViolations(Architecture)); - Assert.False(abstractAttributesAreNotAbstract.HasNoViolations(Architecture)); - Assert.False(notAbstractAttributesAreAbstract.HasNoViolations(Architecture)); - Assert.True(notAbstractAttributesAreNotAbstract.HasNoViolations(Architecture)); + var helper = new AttributeAssemblyTestHelpers(); + + helper.AddSnapshotHeader("No Violations"); + var should = Attributes().That().Are(helper.SealedAttribute).Should(); + + helper.AddSnapshotSubHeader("Conditions"); + should.BeSealed().AssertNoViolations(helper); + + helper.AddSnapshotSubHeader("Predicates"); + should.Be(Attributes().That().AreSealed()).AssertNoViolations(helper); + + helper.AddSnapshotHeader("Violations"); + should = Attributes().That().Are(helper.RegularAttribute).Should(); + + helper.AddSnapshotSubHeader("Conditions"); + should.BeSealed().AssertOnlyViolations(helper); + + helper.AddSnapshotSubHeader("Predicates"); + should.Be(Attributes().That().AreSealed()).AssertOnlyViolations(helper); + + helper.AddSnapshotHeader("Multiple inputs"); + Attributes().That().Are(helper.SealedAttribute, helper.OtherSealedAttribute).Should().BeSealed().AssertNoViolations(helper); + Attributes().That().Are(helper.SealedAttribute, helper.RegularAttribute).Should().BeSealed().AssertAnyViolations(helper); + + await helper.AssertSnapshotMatches(); } [Fact] - public void AreSealedTest() + public async Task NotBeSealedTest() { - foreach (var attribute in _attributes) - { - var attributeIsSealed = Attributes().That().Are(attribute).Should().BeSealed(); - var attributeIsNotSealed = Attributes() - .That() - .Are(attribute) - .Should() - .NotBeSealed(); - var sealedAttributesDoNotIncludeType = Attributes() - .That() - .AreSealed() - .Should() - .NotBe(attribute); - var notSealedAttributesDoNotIncludeType = Attributes() - .That() - .AreNotSealed() - .Should() - .NotBe(attribute); - - Assert.Equal(attribute.IsSealed, attributeIsSealed.HasNoViolations(Architecture)); - Assert.Equal( - !attribute.IsSealed, - attributeIsNotSealed.HasNoViolations(Architecture) - ); - Assert.Equal( - !attribute.IsSealed, - sealedAttributesDoNotIncludeType.HasNoViolations(Architecture) - ); - Assert.Equal( - attribute.IsSealed, - notSealedAttributesDoNotIncludeType.HasNoViolations(Architecture) - ); - } - - var sealedAttributesAreSealed = Attributes().That().AreSealed().Should().BeSealed(); - var sealedAttributesAreNotSealed = Attributes() - .That() - .AreSealed() - .Should() - .NotBeSealed() - .AndShould() - .Exist(); - var notSealedAttributesAreSealed = Attributes() - .That() - .AreNotSealed() - .Should() - .BeSealed() - .AndShould() - .Exist(); - var notSealedAttributesAreNotSealed = Attributes() - .That() - .AreNotSealed() - .Should() - .NotBeSealed(); - - Assert.True(sealedAttributesAreSealed.HasNoViolations(Architecture)); - Assert.False(sealedAttributesAreNotSealed.HasNoViolations(Architecture)); - Assert.False(notSealedAttributesAreSealed.HasNoViolations(Architecture)); - Assert.True(notSealedAttributesAreNotSealed.HasNoViolations(Architecture)); + var helper = new AttributeAssemblyTestHelpers(); + + helper.AddSnapshotHeader("No Violations"); + var should = Attributes().That().Are(helper.RegularAttribute).Should(); + + helper.AddSnapshotSubHeader("Conditions"); + should.NotBeSealed().AssertNoViolations(helper); + + helper.AddSnapshotSubHeader("Predicates"); + should.Be(Attributes().That().AreNotSealed()).AssertNoViolations(helper); + + helper.AddSnapshotHeader("Violations"); + should = Attributes().That().Are(helper.SealedAttribute).Should(); + + helper.AddSnapshotSubHeader("Conditions"); + should.NotBeSealed().AssertOnlyViolations(helper); + + helper.AddSnapshotSubHeader("Predicates"); + should.Be(Attributes().That().AreNotSealed()).AssertOnlyViolations(helper); + + helper.AddSnapshotHeader("Multiple inputs"); + Attributes().That().Are(helper.RegularAttribute, helper.OtherRegularAttribute).Should().NotBeSealed().AssertNoViolations(helper); + Attributes().That().Are(helper.RegularAttribute, helper.SealedAttribute).Should().NotBeSealed().AssertAnyViolations(helper); + + await helper.AssertSnapshotMatches(); } } } diff --git a/ArchUnitNETTests/Fluent/Syntax/Elements/Snapshots/AttributeSyntaxElementsTests.BeAbstractTest.verified.txt b/ArchUnitNETTests/Fluent/Syntax/Elements/Snapshots/AttributeSyntaxElementsTests.BeAbstractTest.verified.txt new file mode 100644 index 000000000..766d4b4d8 --- /dev/null +++ b/ArchUnitNETTests/Fluent/Syntax/Elements/Snapshots/AttributeSyntaxElementsTests.BeAbstractTest.verified.txt @@ -0,0 +1,63 @@ +===== No Violations ===== + +----- Conditions ----- + +Query: Attributes that are "AttributeNamespace.AbstractAttribute" should be abstract +Result: True +Description: AttributeNamespace.AbstractAttribute passed +Message: +All Evaluations passed + +----- Predicates ----- + +Query: Attributes that are "AttributeNamespace.AbstractAttribute" should be Attributes that are abstract +Result: True +Description: AttributeNamespace.AbstractAttribute passed +Message: +All Evaluations passed + +===== Violations ===== + +----- Conditions ----- + +Query: Attributes that are "AttributeNamespace.RegularAttribute" should be abstract +Result: False +Description: AttributeNamespace.RegularAttribute is not abstract +Message: +"Attributes that are "AttributeNamespace.RegularAttribute" should be abstract" failed: + AttributeNamespace.RegularAttribute is not abstract + + + +----- Predicates ----- + +Query: Attributes that are "AttributeNamespace.RegularAttribute" should be Attributes that are abstract +Result: False +Description: AttributeNamespace.RegularAttribute is not Attributes that are abstract +Message: +"Attributes that are "AttributeNamespace.RegularAttribute" should be Attributes that are abstract" failed: + AttributeNamespace.RegularAttribute is not Attributes that are abstract + + + +===== Multiple inputs ===== + +Query: Attributes that are "AttributeNamespace.AbstractAttribute" or "AttributeNamespace.OtherAbstractAttribute" should be abstract +Result: True +Description: AttributeNamespace.AbstractAttribute passed +Result: True +Description: AttributeNamespace.OtherAbstractAttribute passed +Message: +All Evaluations passed + +Query: Attributes that are "AttributeNamespace.AbstractAttribute" or "AttributeNamespace.RegularAttribute" should be abstract +Result: False +Description: AttributeNamespace.RegularAttribute is not abstract +Result: True +Description: AttributeNamespace.AbstractAttribute passed +Message: +"Attributes that are "AttributeNamespace.AbstractAttribute" or "AttributeNamespace.RegularAttribute" should be abstract" failed: + AttributeNamespace.RegularAttribute is not abstract + + + diff --git a/ArchUnitNETTests/Fluent/Syntax/Elements/Snapshots/AttributeSyntaxElementsTests.BeSealedTest.verified.txt b/ArchUnitNETTests/Fluent/Syntax/Elements/Snapshots/AttributeSyntaxElementsTests.BeSealedTest.verified.txt new file mode 100644 index 000000000..ce257e0e1 --- /dev/null +++ b/ArchUnitNETTests/Fluent/Syntax/Elements/Snapshots/AttributeSyntaxElementsTests.BeSealedTest.verified.txt @@ -0,0 +1,63 @@ +===== No Violations ===== + +----- Conditions ----- + +Query: Attributes that are "AttributeNamespace.SealedAttribute" should be sealed +Result: True +Description: AttributeNamespace.SealedAttribute passed +Message: +All Evaluations passed + +----- Predicates ----- + +Query: Attributes that are "AttributeNamespace.SealedAttribute" should be Attributes that are sealed +Result: True +Description: AttributeNamespace.SealedAttribute passed +Message: +All Evaluations passed + +===== Violations ===== + +----- Conditions ----- + +Query: Attributes that are "AttributeNamespace.RegularAttribute" should be sealed +Result: False +Description: AttributeNamespace.RegularAttribute is not sealed +Message: +"Attributes that are "AttributeNamespace.RegularAttribute" should be sealed" failed: + AttributeNamespace.RegularAttribute is not sealed + + + +----- Predicates ----- + +Query: Attributes that are "AttributeNamespace.RegularAttribute" should be Attributes that are sealed +Result: False +Description: AttributeNamespace.RegularAttribute is not Attributes that are sealed +Message: +"Attributes that are "AttributeNamespace.RegularAttribute" should be Attributes that are sealed" failed: + AttributeNamespace.RegularAttribute is not Attributes that are sealed + + + +===== Multiple inputs ===== + +Query: Attributes that are "AttributeNamespace.SealedAttribute" or "AttributeNamespace.OtherSealedAttribute" should be sealed +Result: True +Description: AttributeNamespace.SealedAttribute passed +Result: True +Description: AttributeNamespace.OtherSealedAttribute passed +Message: +All Evaluations passed + +Query: Attributes that are "AttributeNamespace.SealedAttribute" or "AttributeNamespace.RegularAttribute" should be sealed +Result: False +Description: AttributeNamespace.RegularAttribute is not sealed +Result: True +Description: AttributeNamespace.SealedAttribute passed +Message: +"Attributes that are "AttributeNamespace.SealedAttribute" or "AttributeNamespace.RegularAttribute" should be sealed" failed: + AttributeNamespace.RegularAttribute is not sealed + + + diff --git a/ArchUnitNETTests/Fluent/Syntax/Elements/Snapshots/AttributeSyntaxElementsTests.NotBeAbstractTest.verified.txt b/ArchUnitNETTests/Fluent/Syntax/Elements/Snapshots/AttributeSyntaxElementsTests.NotBeAbstractTest.verified.txt new file mode 100644 index 000000000..2f6b90413 --- /dev/null +++ b/ArchUnitNETTests/Fluent/Syntax/Elements/Snapshots/AttributeSyntaxElementsTests.NotBeAbstractTest.verified.txt @@ -0,0 +1,63 @@ +===== No Violations ===== + +----- Conditions ----- + +Query: Attributes that are "AttributeNamespace.RegularAttribute" should not be abstract +Result: True +Description: AttributeNamespace.RegularAttribute passed +Message: +All Evaluations passed + +----- Predicates ----- + +Query: Attributes that are "AttributeNamespace.RegularAttribute" should be Attributes that are not abstract +Result: True +Description: AttributeNamespace.RegularAttribute passed +Message: +All Evaluations passed + +===== Violations ===== + +----- Conditions ----- + +Query: Attributes that are "AttributeNamespace.AbstractAttribute" should not be abstract +Result: False +Description: AttributeNamespace.AbstractAttribute is abstract +Message: +"Attributes that are "AttributeNamespace.AbstractAttribute" should not be abstract" failed: + AttributeNamespace.AbstractAttribute is abstract + + + +----- Predicates ----- + +Query: Attributes that are "AttributeNamespace.AbstractAttribute" should be Attributes that are not abstract +Result: False +Description: AttributeNamespace.AbstractAttribute is not Attributes that are not abstract +Message: +"Attributes that are "AttributeNamespace.AbstractAttribute" should be Attributes that are not abstract" failed: + AttributeNamespace.AbstractAttribute is not Attributes that are not abstract + + + +===== Multiple inputs ===== + +Query: Attributes that are "AttributeNamespace.RegularAttribute" or "AttributeNamespace.OtherRegularAttribute" should not be abstract +Result: True +Description: AttributeNamespace.RegularAttribute passed +Result: True +Description: AttributeNamespace.OtherRegularAttribute passed +Message: +All Evaluations passed + +Query: Attributes that are "AttributeNamespace.RegularAttribute" or "AttributeNamespace.AbstractAttribute" should not be abstract +Result: True +Description: AttributeNamespace.RegularAttribute passed +Result: False +Description: AttributeNamespace.AbstractAttribute is abstract +Message: +"Attributes that are "AttributeNamespace.RegularAttribute" or "AttributeNamespace.AbstractAttribute" should not be abstract" failed: + AttributeNamespace.AbstractAttribute is abstract + + + diff --git a/ArchUnitNETTests/Fluent/Syntax/Elements/Snapshots/AttributeSyntaxElementsTests.NotBeSealedTest.verified.txt b/ArchUnitNETTests/Fluent/Syntax/Elements/Snapshots/AttributeSyntaxElementsTests.NotBeSealedTest.verified.txt new file mode 100644 index 000000000..867fd0a68 --- /dev/null +++ b/ArchUnitNETTests/Fluent/Syntax/Elements/Snapshots/AttributeSyntaxElementsTests.NotBeSealedTest.verified.txt @@ -0,0 +1,63 @@ +===== No Violations ===== + +----- Conditions ----- + +Query: Attributes that are "AttributeNamespace.RegularAttribute" should not be sealed +Result: True +Description: AttributeNamespace.RegularAttribute passed +Message: +All Evaluations passed + +----- Predicates ----- + +Query: Attributes that are "AttributeNamespace.RegularAttribute" should be Attributes that are not sealed +Result: True +Description: AttributeNamespace.RegularAttribute passed +Message: +All Evaluations passed + +===== Violations ===== + +----- Conditions ----- + +Query: Attributes that are "AttributeNamespace.SealedAttribute" should not be sealed +Result: False +Description: AttributeNamespace.SealedAttribute is sealed +Message: +"Attributes that are "AttributeNamespace.SealedAttribute" should not be sealed" failed: + AttributeNamespace.SealedAttribute is sealed + + + +----- Predicates ----- + +Query: Attributes that are "AttributeNamespace.SealedAttribute" should be Attributes that are not sealed +Result: False +Description: AttributeNamespace.SealedAttribute is not Attributes that are not sealed +Message: +"Attributes that are "AttributeNamespace.SealedAttribute" should be Attributes that are not sealed" failed: + AttributeNamespace.SealedAttribute is not Attributes that are not sealed + + + +===== Multiple inputs ===== + +Query: Attributes that are "AttributeNamespace.RegularAttribute" or "AttributeNamespace.OtherRegularAttribute" should not be sealed +Result: True +Description: AttributeNamespace.RegularAttribute passed +Result: True +Description: AttributeNamespace.OtherRegularAttribute passed +Message: +All Evaluations passed + +Query: Attributes that are "AttributeNamespace.RegularAttribute" or "AttributeNamespace.SealedAttribute" should not be sealed +Result: True +Description: AttributeNamespace.RegularAttribute passed +Result: False +Description: AttributeNamespace.SealedAttribute is sealed +Message: +"Attributes that are "AttributeNamespace.RegularAttribute" or "AttributeNamespace.SealedAttribute" should not be sealed" failed: + AttributeNamespace.SealedAttribute is sealed + + + diff --git a/TestAssemblies/AttributeAssembly/AttributeAssembly.cs b/TestAssemblies/AttributeAssembly/AttributeAssembly.cs index ac5e7f337..ab945fa8e 100644 --- a/TestAssemblies/AttributeAssembly/AttributeAssembly.cs +++ b/TestAssemblies/AttributeAssembly/AttributeAssembly.cs @@ -145,3 +145,16 @@ public OnceUsedAttribute( [OnceUsed] public class ClassWithSingleUniquelyUsedAttribute { } + +// Baseline (not abstract, not sealed) +public class RegularAttribute : System.Attribute { } + +public class OtherRegularAttribute : System.Attribute { } + +public abstract class AbstractAttribute : System.Attribute { } + +public abstract class OtherAbstractAttribute : System.Attribute { } + +public sealed class SealedAttribute : System.Attribute { } + +public sealed class OtherSealedAttribute : System.Attribute { }