|
| 1 | +using ArchUnitNET.Domain; |
| 2 | +using ArchUnitNET.Fluent; |
| 3 | +using ArchUnitNET.Fluent.Extensions; |
| 4 | +using ArchUnitNET.Loader; |
| 5 | +using ArchUnitNET.MSTestV2; |
| 6 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 7 | +using static ArchUnitNET.Fluent.ArchRuleDefinition; |
| 8 | + |
| 9 | +namespace ArchUnitNET.MSTestV3Tests |
| 10 | +{ |
| 11 | + [TestClass] |
| 12 | + public class RuleEvaluationTests |
| 13 | + { |
| 14 | + private static Architecture _architecture; |
| 15 | + private static string _expectedErrorMessage; |
| 16 | + private static IArchRule _falseRule; |
| 17 | + private static IArchRule _trueRule; |
| 18 | + |
| 19 | + [ClassInitialize] |
| 20 | + public static void Setup(TestContext context) |
| 21 | + { |
| 22 | + _architecture = new ArchLoader() |
| 23 | + .LoadAssemblies(System.Reflection.Assembly.Load("ArchUnitNET.MSTestV3Tests")) |
| 24 | + .Build(); |
| 25 | + _trueRule = Classes().That().Are(typeof(RuleEvaluationTests)).Should().Exist(); |
| 26 | + _falseRule = Classes().That().Are(typeof(RuleEvaluationTests)).Should().NotExist(); |
| 27 | + _expectedErrorMessage = _falseRule.Evaluate(_architecture).ToErrorMessage(); |
| 28 | + } |
| 29 | + |
| 30 | + [TestMethod] |
| 31 | + public void ArchRuleAssertTest() |
| 32 | + { |
| 33 | + ArchRuleAssert.FulfilsRule(_architecture, _trueRule); |
| 34 | + Assert.ThrowsExactly<AssertFailedException>(() => |
| 35 | + ArchRuleAssert.FulfilsRule(_architecture, _falseRule) |
| 36 | + ); |
| 37 | + Assert.AreEqual( |
| 38 | + _expectedErrorMessage, |
| 39 | + RemoveAssertionText( |
| 40 | + Assert |
| 41 | + .ThrowsExactly<AssertFailedException>(() => |
| 42 | + ArchRuleAssert.FulfilsRule(_architecture, _falseRule) |
| 43 | + ) |
| 44 | + .Message |
| 45 | + ) |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + [TestMethod] |
| 50 | + public void ArchRuleExtensionsTest() |
| 51 | + { |
| 52 | + _architecture.CheckRule(_trueRule); |
| 53 | + _trueRule.Check(_architecture); |
| 54 | + Assert.ThrowsExactly<AssertFailedException>(() => _architecture.CheckRule(_falseRule)); |
| 55 | + Assert.ThrowsExactly<AssertFailedException>(() => _falseRule.Check(_architecture)); |
| 56 | + Assert.AreEqual( |
| 57 | + _expectedErrorMessage, |
| 58 | + RemoveAssertionText( |
| 59 | + Assert |
| 60 | + .ThrowsExactly<AssertFailedException>(() => |
| 61 | + _architecture.CheckRule(_falseRule) |
| 62 | + ) |
| 63 | + .Message |
| 64 | + ) |
| 65 | + ); |
| 66 | + Assert.AreEqual( |
| 67 | + _expectedErrorMessage, |
| 68 | + RemoveAssertionText( |
| 69 | + Assert |
| 70 | + .ThrowsExactly<AssertFailedException>(() => _falseRule.Check(_architecture)) |
| 71 | + .Message |
| 72 | + ) |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + private static string RemoveAssertionText(string exceptionMessage) |
| 77 | + { |
| 78 | + return exceptionMessage.Replace("Assert.Fail failed. ", string.Empty); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments