Skip to content

feat: add TUnit #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ArchUnit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.xUnitTests", "A
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.xUnitV3Tests", "ArchUnitNET.xUnitV3Tests\ArchUnitNET.xUnitV3Tests.csproj", "{439E00CE-5CB8-4474-9A27-3FFC5770DB7C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.TUnit", "ArchUnitNET.TUnit\ArchUnitNET.TUnit.csproj", "{AD4D9490-F6F1-47DC-9F52-FD15497CDE55}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.TUnitTests", "ArchUnitNET.TUnitTests\ArchUnitNET.TUnitTests.csproj", "{C54143CE-3256-4313-B991-0706705EEA1D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -118,6 +122,14 @@ Global
{439E00CE-5CB8-4474-9A27-3FFC5770DB7C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{439E00CE-5CB8-4474-9A27-3FFC5770DB7C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{439E00CE-5CB8-4474-9A27-3FFC5770DB7C}.Release|Any CPU.Build.0 = Release|Any CPU
{AD4D9490-F6F1-47DC-9F52-FD15497CDE55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD4D9490-F6F1-47DC-9F52-FD15497CDE55}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD4D9490-F6F1-47DC-9F52-FD15497CDE55}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD4D9490-F6F1-47DC-9F52-FD15497CDE55}.Release|Any CPU.Build.0 = Release|Any CPU
{C54143CE-3256-4313-B991-0706705EEA1D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C54143CE-3256-4313-B991-0706705EEA1D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C54143CE-3256-4313-B991-0706705EEA1D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C54143CE-3256-4313-B991-0706705EEA1D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
23 changes: 23 additions & 0 deletions ArchUnitNET.TUnit/ArchRuleAssert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using ArchUnitNET.Domain;
using ArchUnitNET.Fluent;

// ReSharper disable once CheckNamespace
namespace ArchUnitNET.TUnit
{
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 CheckRule(Architecture architecture, IArchRule archRule)
{
if (!archRule.HasNoViolations(architecture))
{
var results = archRule.Evaluate(architecture);
throw new FailedArchRuleException(architecture, archRule);
}
}
}
}
29 changes: 29 additions & 0 deletions ArchUnitNET.TUnit/ArchRuleExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using ArchUnitNET.Domain;
using ArchUnitNET.Fluent;

// ReSharper disable once CheckNamespace
namespace ArchUnitNET.TUnit
{
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.CheckRule(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.CheckRule(architecture, archRule);
}
}
}
41 changes: 41 additions & 0 deletions ArchUnitNET.TUnit/ArchUnitNET.TUnit.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net472</TargetFrameworks>
<LangVersion>latest</LangVersion>
<IsPackable>true</IsPackable>
<Title>ArchUnit C# TUnit Extension</Title>
<Description>TUnit 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;tunit</PackageTags>
<IncludeSource>False</IncludeSource>
<Company>TNG Technology Consulting GmbH</Company>
<PackageId>TngTech.ArchUnitNET.TUnit</PackageId>
<IsTestProject>false</IsTestProject>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageReadmeFile>README.md</PackageReadmeFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<RootNamespace>ArchUnitNET.TUnit</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ArchUnitNET\ArchUnitNET.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageReference Include="TUnit.Assertions" Version="0.52.51" />
</ItemGroup>
<ItemGroup>
<None Include="..\Logo\ArchUnitNET-Logo.png">
<Pack>true</Pack>
<PackagePath>/Logo/</PackagePath>
<Link>ArchUnitNET-Logo.png</Link>
</None>
<None Include="..\README.md">
<Pack>true</Pack>
<PackagePath></PackagePath>
<Link>README.md</Link>
</None>
</ItemGroup>
</Project>
27 changes: 27 additions & 0 deletions ArchUnitNET.TUnit/FailedArchRuleException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Generic;
using ArchUnitNET.Domain;
using ArchUnitNET.Fluent;
using ArchUnitNET.Fluent.Extensions;
using TUnit.Assertions.Exceptions;

// ReSharper disable once CheckNamespace
namespace ArchUnitNET.TUnit
{
public class FailedArchRuleException : BaseAssertionException
{
/// <summary>
/// Creates a new instance of the <see href="FailedArchRuleException" /> class.
/// </summary>
/// <param name="architecture">The architecture which was tested</param>
/// <param name="archRule">The archrule that failed</param>
public FailedArchRuleException(Architecture architecture, IArchRule archRule)
: this(archRule.Evaluate(architecture)) { }

/// <summary>
/// Creates a new instance of the <see href="FailedArchRuleException" /> class.
/// </summary>
/// <param name="evaluationResults">The results of the evaluation of the archrule</param>
public FailedArchRuleException(IEnumerable<EvaluationResult> evaluationResults)
: base(evaluationResults.ToErrorMessage()) { }
}
}
15 changes: 15 additions & 0 deletions ArchUnitNET.TUnitTests/ArchUnitNET.TUnitTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<IsPackable>false</IsPackable>
<Company>TNG Technology Consulting GmbH</Company>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="TUnit" Version="0.52.51" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ArchUnitNET.TUnit\ArchUnitNET.TUnit.csproj" />
</ItemGroup>
</Project>
49 changes: 49 additions & 0 deletions ArchUnitNET.TUnitTests/RuleEvaluationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using ArchUnitNET.Domain;
using ArchUnitNET.Fluent;
using ArchUnitNET.Fluent.Extensions;
using ArchUnitNET.Loader;
using ArchUnitNET.TUnit;
using static ArchUnitNET.Fluent.ArchRuleDefinition;
using System.Threading.Tasks;

namespace ArchUnitNET.TUnitTests
{
public class RuleEvaluationTests
{
private readonly Architecture _architecture;
private readonly string _expectedErrorMessage;
private readonly IArchRule _falseRule;
private readonly IArchRule _trueRule;

public RuleEvaluationTests()
{
_architecture = new ArchLoader()
.LoadAssemblies(System.Reflection.Assembly.Load("ArchUnitNET.TUnitTests"))
.Build();
_trueRule = Classes().That().Are(typeof(RuleEvaluationTests)).Should().Exist();
_falseRule = Classes().That().Are(typeof(RuleEvaluationTests)).Should().NotExist();
_expectedErrorMessage = _falseRule.Evaluate(_architecture).ToErrorMessage();
}

[Test]
public async Task ArchRuleAssertTest()
{
ArchRuleAssert.CheckRule(_architecture, _trueRule);
var ex = Assert.Throws<FailedArchRuleException>(() =>
ArchRuleAssert.CheckRule(_architecture, _falseRule)
);
await Assert.That(ex.Message).IsEqualTo(_expectedErrorMessage);
}

[Test]
public async Task ArchRuleExtensionsTest()
{
_architecture.CheckRule(_trueRule);
_trueRule.Check(_architecture);
var ex1 = Assert.Throws<FailedArchRuleException>(() => _architecture.CheckRule(_falseRule));
var ex2 = Assert.Throws<FailedArchRuleException>(() => _falseRule.Check(_architecture));
await Assert.That(ex1.Message).IsEqualTo(_expectedErrorMessage);
await Assert.That(ex2.Message).IsEqualTo(_expectedErrorMessage);
}
}
}