Skip to content

Commit

Permalink
Add extensions for MSTest V2 framework.
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastijan Mueller <Sebastijan.Mueller@varian.com>
  • Loading branch information
Sebastijan Mueller authored and Sebastijan Mueller committed May 1, 2021
1 parent ad5f0a2 commit 250138c
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 6 deletions.
24 changes: 18 additions & 6 deletions ArchUnit.sln
Expand Up @@ -5,17 +5,21 @@ VisualStudioVersion = 15.0.26430.16
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestAssembly", "TestAssembly\TestAssembly.csproj", "{7DEF5F34-AB86-457B-819D-5E7387B3FF87}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET", "ArchUnitNET\ArchUnitNET.csproj", "{2855911F-6AE2-4BE1-BA88-9A0B2A03BFA3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ArchUnitNET", "ArchUnitNET\ArchUnitNET.csproj", "{2855911F-6AE2-4BE1-BA88-9A0B2A03BFA3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNETTests", "ArchUnitNETTests\ArchUnitNETTests.csproj", "{F38CBB2C-BDD0-4195-82CB-8B76332B0890}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ArchUnitNETTests", "ArchUnitNETTests\ArchUnitNETTests.csproj", "{F38CBB2C-BDD0-4195-82CB-8B76332B0890}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleTest", "ExampleTest\ExampleTest.csproj", "{180688C2-7917-4A23-89A7-F3D5317E54F3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExampleTest", "ExampleTest\ExampleTest.csproj", "{180688C2-7917-4A23-89A7-F3D5317E54F3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.xUnit", "ArchUnitNET.xUnit\ArchUnitNET.xUnit.csproj", "{53DF58F8-0C82-4259-96A6-31974D0A503A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ArchUnitNET.xUnit", "ArchUnitNET.xUnit\ArchUnitNET.xUnit.csproj", "{53DF58F8-0C82-4259-96A6-31974D0A503A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.NUnit", "ArchUnitNET.NUnit\ArchUnitNET.NUnit.csproj", "{0BD564F3-D34F-4D39-94B3-EA2705EDF594}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ArchUnitNET.NUnit", "ArchUnitNET.NUnit\ArchUnitNET.NUnit.csproj", "{0BD564F3-D34F-4D39-94B3-EA2705EDF594}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.NUnitTests", "ArchUnitNET.NUnitTests\ArchUnitNET.NUnitTests.csproj", "{D2AB683F-0A64-491E-9C56-EE98588C493C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ArchUnitNET.NUnitTests", "ArchUnitNET.NUnitTests\ArchUnitNET.NUnitTests.csproj", "{D2AB683F-0A64-491E-9C56-EE98588C493C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.MSTestV2", "ArchUnitNET.MSTestV2\ArchUnitNET.MSTestV2.csproj", "{3D057987-358A-41C6-AF55-6490FCA5875F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.MSTestV2Tests", "ArchUnitNET.MSTestV2Tests\ArchUnitNET.MSTestV2Tests.csproj", "{6D6B6EFE-DA0B-4C4D-B710-FA658F0C68CF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -51,6 +55,14 @@ Global
{D2AB683F-0A64-491E-9C56-EE98588C493C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2AB683F-0A64-491E-9C56-EE98588C493C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2AB683F-0A64-491E-9C56-EE98588C493C}.Release|Any CPU.Build.0 = Release|Any CPU
{3D057987-358A-41C6-AF55-6490FCA5875F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3D057987-358A-41C6-AF55-6490FCA5875F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3D057987-358A-41C6-AF55-6490FCA5875F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3D057987-358A-41C6-AF55-6490FCA5875F}.Release|Any CPU.Build.0 = Release|Any CPU
{6D6B6EFE-DA0B-4C4D-B710-FA658F0C68CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6D6B6EFE-DA0B-4C4D-B710-FA658F0C68CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6D6B6EFE-DA0B-4C4D-B710-FA658F0C68CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6D6B6EFE-DA0B-4C4D-B710-FA658F0C68CF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
29 changes: 29 additions & 0 deletions ArchUnitNET.MSTestV2/ArchRuleAssert.cs
@@ -0,0 +1,29 @@
// Copyright 2019 Florian Gather <florian.gather@tngtech.com>
// Copyright 2019 Paula Ruiz <paularuiz22@gmail.com>
// Copyright 2019 Fritz Brandhuber <fritz.brandhuber@tngtech.com>
//
// SPDX-License-Identifier: Apache-2.0

using ArchUnitNET.Domain;
using ArchUnitNET.Fluent;
using ArchUnitNET.Fluent.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ArchUnitNET.MSTestV2
{
public static class ArchRuleAssert
{
/// <summary>
/// Verifies that the architecture meets the criteria of the archrule.
/// </summary>
/// <param name="architecture">The architecture to be tested</param>
/// <param name="archRule">The rule to test the architecture with</param>
public static void FulfilsRule(Architecture architecture, IArchRule archRule)
{
if (!archRule.HasNoViolations(architecture))
{
Assert.Fail(archRule.Evaluate(architecture).ToErrorMessage());
}
}
}
}
34 changes: 34 additions & 0 deletions ArchUnitNET.MSTestV2/ArchRuleExtensions.cs
@@ -0,0 +1,34 @@
// Copyright 2019 Florian Gather <florian.gather@tngtech.com>
// Copyright 2019 Paula Ruiz <paularuiz22@gmail.com>
// Copyright 2019 Fritz Brandhuber <fritz.brandhuber@tngtech.com>
//
// SPDX-License-Identifier: Apache-2.0

using ArchUnitNET.Domain;
using ArchUnitNET.Fluent;

namespace ArchUnitNET.MSTestV2
{
public static class ArchRuleExtensions
{
/// <summary>
/// Verifies that the architecture meets the criteria of the archrule.
/// </summary>
/// <param name="archRule">The rule to test the architecture with</param>
/// <param name="architecture">The architecture to be tested</param>
public static void Check(this IArchRule archRule, Architecture architecture)
{
ArchRuleAssert.FulfilsRule(architecture, archRule);
}

/// <summary>
/// Verifies that the architecture meets the criteria of the archrule.
/// </summary>
/// <param name="architecture">The architecture to be tested</param>
/// <param name="archRule">The rule to test the architecture with</param>
public static void CheckRule(this Architecture architecture, IArchRule archRule)
{
ArchRuleAssert.FulfilsRule(architecture, archRule);
}
}
}
30 changes: 30 additions & 0 deletions ArchUnitNET.MSTestV2/ArchUnitNET.MSTestV2.csproj
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
<IsPackable>true</IsPackable>
<Title>ArchUnit C# MSTestV2 Extension</Title>
<Authors>Florian Gather, Fritz Brandhuber</Authors>
<Description>NUnit Extension for the C# Version of ArchUnit (see: archunit.org)</Description>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<RepositoryUrl>https://github.com/TNG/ArchUnitNET</RepositoryUrl>
<PackageTags>test;arch;archunit;mstest;mstestv2</PackageTags>
<PackageReleaseNotes>Initial release</PackageReleaseNotes>
<IncludeSource>False</IncludeSource>
<Company>TNG Technology Consulting GmbH</Company>
<PackageId>TngTech.ArchUnitNET.MSTestV2</PackageId>
<IsTestProject>false</IsTestProject>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ArchUnitNET\ArchUnitNET.csproj" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions ArchUnitNET.MSTestV2Tests/ArchUnitNET.MSTestV2Tests.csproj
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>

<Company>TNG Technology Consulting GmbH</Company>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ArchUnitNET.MSTestV2\ArchUnitNET.MSTestV2.csproj" />
</ItemGroup>

</Project>
60 changes: 60 additions & 0 deletions ArchUnitNET.MSTestV2Tests/RuleEvaluationTests.cs
@@ -0,0 +1,60 @@
// Copyright 2019 Florian Gather <florian.gather@tngtech.com>
// Copyright 2019 Paula Ruiz <paularuiz22@gmail.com>
// Copyright 2019 Fritz Brandhuber <fritz.brandhuber@tngtech.com>
//
// SPDX-License-Identifier: Apache-2.0

using ArchUnitNET.Domain;
using ArchUnitNET.Fluent;
using ArchUnitNET.Fluent.Extensions;
using ArchUnitNET.Loader;
using ArchUnitNET.MSTestV2;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using static ArchUnitNET.Fluent.ArchRuleDefinition;

namespace ArchUnitNET.MSTestV2Tests
{
[TestClass]
public class RuleEvaluationTests
{
private static Architecture _architecture;
private static string _expectedErrorMessage;
private static IArchRule _falseRule;
private static IArchRule _trueRule;

[ClassInitialize]
public static void Setup(TestContext context)
{
_architecture = new ArchLoader().LoadAssemblies(typeof(RuleEvaluationTests).Assembly).Build();
_trueRule = Classes().That().Are(typeof(RuleEvaluationTests)).Should().Exist();
_falseRule = Classes().That().Are(typeof(RuleEvaluationTests)).Should().NotExist();
_expectedErrorMessage = _falseRule.Evaluate(_architecture).ToErrorMessage();
}

[TestMethod]
public void ArchRuleAssertTest()
{
ArchRuleAssert.FulfilsRule(_architecture, _trueRule);
Assert.ThrowsException<AssertFailedException>(() => ArchRuleAssert.FulfilsRule(_architecture, _falseRule));
Assert.AreEqual(_expectedErrorMessage,
RemoveAssertionText(Assert.ThrowsException<AssertFailedException>(() => ArchRuleAssert.FulfilsRule(_architecture, _falseRule)).Message));
}

[TestMethod]
public void ArchRuleExtensionsTest()
{
_architecture.CheckRule(_trueRule);
_trueRule.Check(_architecture);
Assert.ThrowsException<AssertFailedException>(() => _architecture.CheckRule(_falseRule));
Assert.ThrowsException<AssertFailedException>(() => _falseRule.Check(_architecture));
Assert.AreEqual(_expectedErrorMessage,
RemoveAssertionText(Assert.ThrowsException<AssertFailedException>(() => _architecture.CheckRule(_falseRule)).Message));
Assert.AreEqual(_expectedErrorMessage,
RemoveAssertionText(Assert.ThrowsException<AssertFailedException>(() => _falseRule.Check(_architecture)).Message));
}

private static string RemoveAssertionText(string exceptionMessage) {
return exceptionMessage.Replace("Assert.Fail failed. ", string.Empty);
}
}
}

0 comments on commit 250138c

Please sign in to comment.