From 4b9d23dd0cbcc21fe5c615ff37f597271043ab5c Mon Sep 17 00:00:00 2001 From: Fritz Brandhuber Date: Mon, 22 Feb 2021 14:23:47 +0100 Subject: [PATCH] tests for custom conditions and predicates Signed-off-by: Fritz Brandhuber --- .../Elements/CustomSyntaxElementsTests.cs | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 ArchUnitNETTests/Fluent/Syntax/Elements/CustomSyntaxElementsTests.cs diff --git a/ArchUnitNETTests/Fluent/Syntax/Elements/CustomSyntaxElementsTests.cs b/ArchUnitNETTests/Fluent/Syntax/Elements/CustomSyntaxElementsTests.cs new file mode 100644 index 000000000..0b27ab08d --- /dev/null +++ b/ArchUnitNETTests/Fluent/Syntax/Elements/CustomSyntaxElementsTests.cs @@ -0,0 +1,58 @@ +// Copyright 2019 Florian Gather +// Copyright 2019 Fritz Brandhuber +// Copyright 2020 Pavel Fischer +// +// SPDX-License-Identifier: Apache-2.0 +// + +using System.Linq; +using ArchUnitNET.Domain; +using ArchUnitNET.Domain.Extensions; +using ArchUnitNET.Loader; +using Xunit; +using static ArchUnitNET.Fluent.ArchRuleDefinition; + +namespace ArchUnitNETTests.Fluent.Syntax.Elements +{ + public class CustomSyntaxElementsTests + { + private static readonly Architecture Architecture = + new ArchLoader().LoadAssemblies(typeof(CustomSyntaxElementsTests).Assembly).Build(); + + private readonly Class _testClass; + + public CustomSyntaxElementsTests() + { + _testClass = Architecture.GetClassOfType(typeof(CustomRuleTestClass)); + } + + [Fact] + public void CustomConditionTest() + { + var passingRule = Classes().That().Are(typeof(CustomRuleTestClass)).Should() + .FollowCustomCondition(cls => cls.FullName.Contains(nameof(CustomRuleTestClass)), + "passing custom condition", "failed custom condition which should have passed"); + var failingRule = Classes().That().Are(typeof(CustomRuleTestClass)).Should() + .FollowCustomCondition(cls => !cls.FullName.Contains(nameof(CustomRuleTestClass)), + "failing custom condition", "failed custom condition which should have failed"); + + passingRule.Check(Architecture); + Assert.False(failingRule.HasNoViolations(Architecture)); + } + + [Fact] + public void CustomPredicateTest() + { + var predicateTestObjects = Classes().That() + .FollowCustomPredicate(cls => cls.Name.Equals(nameof(CustomRuleTestClass)), "custom predicate") + .GetObjects(Architecture); + + Assert.Single(predicateTestObjects); + Assert.Equal(_testClass, predicateTestObjects.First()); + } + } + + internal class CustomRuleTestClass + { + } +} \ No newline at end of file