Skip to content

Commit 00954e1

Browse files
committed
feat: add TUnit
1 parent d2f7e9e commit 00954e1

File tree

7 files changed

+198
-0
lines changed

7 files changed

+198
-0
lines changed

ArchUnit.sln

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.xUnitTests", "A
4040
EndProject
4141
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.xUnitV3Tests", "ArchUnitNET.xUnitV3Tests\ArchUnitNET.xUnitV3Tests.csproj", "{439E00CE-5CB8-4474-9A27-3FFC5770DB7C}"
4242
EndProject
43+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.TUnit", "ArchUnitNET.TUnit\ArchUnitNET.TUnit.csproj", "{AD4D9490-F6F1-47DC-9F52-FD15497CDE55}"
44+
EndProject
45+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.TUnitTests", "ArchUnitNET.TUnitTests\ArchUnitNET.TUnitTests.csproj", "{C54143CE-3256-4313-B991-0706705EEA1D}"
46+
EndProject
4347
Global
4448
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4549
Debug|Any CPU = Debug|Any CPU
@@ -118,6 +122,14 @@ Global
118122
{439E00CE-5CB8-4474-9A27-3FFC5770DB7C}.Debug|Any CPU.Build.0 = Debug|Any CPU
119123
{439E00CE-5CB8-4474-9A27-3FFC5770DB7C}.Release|Any CPU.ActiveCfg = Release|Any CPU
120124
{439E00CE-5CB8-4474-9A27-3FFC5770DB7C}.Release|Any CPU.Build.0 = Release|Any CPU
125+
{AD4D9490-F6F1-47DC-9F52-FD15497CDE55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
126+
{AD4D9490-F6F1-47DC-9F52-FD15497CDE55}.Debug|Any CPU.Build.0 = Debug|Any CPU
127+
{AD4D9490-F6F1-47DC-9F52-FD15497CDE55}.Release|Any CPU.ActiveCfg = Release|Any CPU
128+
{AD4D9490-F6F1-47DC-9F52-FD15497CDE55}.Release|Any CPU.Build.0 = Release|Any CPU
129+
{C54143CE-3256-4313-B991-0706705EEA1D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
130+
{C54143CE-3256-4313-B991-0706705EEA1D}.Debug|Any CPU.Build.0 = Debug|Any CPU
131+
{C54143CE-3256-4313-B991-0706705EEA1D}.Release|Any CPU.ActiveCfg = Release|Any CPU
132+
{C54143CE-3256-4313-B991-0706705EEA1D}.Release|Any CPU.Build.0 = Release|Any CPU
121133
EndGlobalSection
122134
GlobalSection(SolutionProperties) = preSolution
123135
HideSolutionNode = FALSE

ArchUnitNET.TUnit/ArchRuleAssert.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using ArchUnitNET.Domain;
2+
using ArchUnitNET.Fluent;
3+
4+
// ReSharper disable once CheckNamespace
5+
namespace ArchUnitNET.TUnit
6+
{
7+
public static class ArchRuleAssert
8+
{
9+
/// <summary>
10+
/// Verifies that the architecture meets the criteria of the archrule.
11+
/// </summary>
12+
/// <param name="architecture">The architecture to be tested</param>
13+
/// <param name="archRule">The rule to test the architecture with</param>
14+
public static void CheckRule(Architecture architecture, IArchRule archRule)
15+
{
16+
if (!archRule.HasNoViolations(architecture))
17+
{
18+
var results = archRule.Evaluate(architecture);
19+
throw new FailedArchRuleException(architecture, archRule);
20+
}
21+
}
22+
}
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using ArchUnitNET.Domain;
2+
using ArchUnitNET.Fluent;
3+
4+
// ReSharper disable once CheckNamespace
5+
namespace ArchUnitNET.TUnit
6+
{
7+
public static class ArchRuleExtensions
8+
{
9+
/// <summary>
10+
/// Verifies that the architecture meets the criteria of the archrule.
11+
/// </summary>
12+
/// <param name="archRule">The rule to test the architecture with</param>
13+
/// <param name="architecture">The architecture to be tested</param>
14+
public static void Check(this IArchRule archRule, Architecture architecture)
15+
{
16+
ArchRuleAssert.CheckRule(architecture, archRule);
17+
}
18+
19+
/// <summary>
20+
/// Verifies that the architecture meets the criteria of the archrule.
21+
/// </summary>
22+
/// <param name="architecture">The architecture to be tested</param>
23+
/// <param name="archRule">The rule to test the architecture with</param>
24+
public static void CheckRule(this Architecture architecture, IArchRule archRule)
25+
{
26+
ArchRuleAssert.CheckRule(architecture, archRule);
27+
}
28+
}
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFrameworks>netstandard2.0;net472</TargetFrameworks>
4+
<LangVersion>latest</LangVersion>
5+
<IsPackable>true</IsPackable>
6+
<Title>ArchUnit C# TUnit Extension</Title>
7+
<Description>TUnit Extension for the C# Version of ArchUnit (see: archunit.org)</Description>
8+
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
9+
<RepositoryUrl>https://github.com/TNG/ArchUnitNET</RepositoryUrl>
10+
<PackageTags>test;arch;archunit;tunit</PackageTags>
11+
<IncludeSource>False</IncludeSource>
12+
<Company>TNG Technology Consulting GmbH</Company>
13+
<PackageId>TngTech.ArchUnitNET.TUnit</PackageId>
14+
<IsTestProject>false</IsTestProject>
15+
<IncludeSymbols>true</IncludeSymbols>
16+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
17+
<PackageReadmeFile>README.md</PackageReadmeFile>
18+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
19+
<RootNamespace>ArchUnitNET.TUnit</RootNamespace>
20+
</PropertyGroup>
21+
<ItemGroup>
22+
<ProjectReference Include="..\ArchUnitNET\ArchUnitNET.csproj" />
23+
</ItemGroup>
24+
<ItemGroup>
25+
<PackageReference Include="System.Net.Http" Version="4.3.4" />
26+
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
27+
<PackageReference Include="TUnit.Assertions" Version="0.52.51" />
28+
</ItemGroup>
29+
<ItemGroup>
30+
<None Include="..\Logo\ArchUnitNET-Logo.png">
31+
<Pack>true</Pack>
32+
<PackagePath>/Logo/</PackagePath>
33+
<Link>ArchUnitNET-Logo.png</Link>
34+
</None>
35+
<None Include="..\README.md">
36+
<Pack>true</Pack>
37+
<PackagePath></PackagePath>
38+
<Link>README.md</Link>
39+
</None>
40+
</ItemGroup>
41+
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections.Generic;
2+
using ArchUnitNET.Domain;
3+
using ArchUnitNET.Fluent;
4+
using ArchUnitNET.Fluent.Extensions;
5+
using TUnit.Assertions.Exceptions;
6+
7+
// ReSharper disable once CheckNamespace
8+
namespace ArchUnitNET.TUnit
9+
{
10+
public class FailedArchRuleException : BaseAssertionException
11+
{
12+
/// <summary>
13+
/// Creates a new instance of the <see href="FailedArchRuleException" /> class.
14+
/// </summary>
15+
/// <param name="architecture">The architecture which was tested</param>
16+
/// <param name="archRule">The archrule that failed</param>
17+
public FailedArchRuleException(Architecture architecture, IArchRule archRule)
18+
: this(archRule.Evaluate(architecture)) { }
19+
20+
/// <summary>
21+
/// Creates a new instance of the <see href="FailedArchRuleException" /> class.
22+
/// </summary>
23+
/// <param name="evaluationResults">The results of the evaluation of the archrule</param>
24+
public FailedArchRuleException(IEnumerable<EvaluationResult> evaluationResults)
25+
: base(evaluationResults.ToErrorMessage()) { }
26+
}
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net9.0</TargetFramework>
4+
<IsPackable>false</IsPackable>
5+
<Company>TNG Technology Consulting GmbH</Company>
6+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
10+
<PackageReference Include="TUnit" Version="0.52.51" />
11+
</ItemGroup>
12+
<ItemGroup>
13+
<ProjectReference Include="..\ArchUnitNET.TUnit\ArchUnitNET.TUnit.csproj" />
14+
</ItemGroup>
15+
</Project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Threading.Tasks;
2+
using ArchUnitNET.Domain;
3+
using ArchUnitNET.Fluent;
4+
using ArchUnitNET.Fluent.Extensions;
5+
using ArchUnitNET.Loader;
6+
using ArchUnitNET.TUnit;
7+
using static ArchUnitNET.Fluent.ArchRuleDefinition;
8+
9+
namespace ArchUnitNET.TUnitTests
10+
{
11+
public class RuleEvaluationTests
12+
{
13+
private readonly Architecture _architecture;
14+
private readonly string _expectedErrorMessage;
15+
private readonly IArchRule _falseRule;
16+
private readonly IArchRule _trueRule;
17+
18+
public RuleEvaluationTests()
19+
{
20+
_architecture = new ArchLoader()
21+
.LoadAssemblies(System.Reflection.Assembly.Load("ArchUnitNET.TUnitTests"))
22+
.Build();
23+
_trueRule = Classes().That().Are(typeof(RuleEvaluationTests)).Should().Exist();
24+
_falseRule = Classes().That().Are(typeof(RuleEvaluationTests)).Should().NotExist();
25+
_expectedErrorMessage = _falseRule.Evaluate(_architecture).ToErrorMessage();
26+
}
27+
28+
[Test]
29+
public async Task ArchRuleAssertTest()
30+
{
31+
ArchRuleAssert.CheckRule(_architecture, _trueRule);
32+
var ex = Assert.Throws<FailedArchRuleException>(() =>
33+
ArchRuleAssert.CheckRule(_architecture, _falseRule)
34+
);
35+
await Assert.That(ex.Message).IsEqualTo(_expectedErrorMessage);
36+
}
37+
38+
[Test]
39+
public async Task ArchRuleExtensionsTest()
40+
{
41+
_architecture.CheckRule(_trueRule);
42+
_trueRule.Check(_architecture);
43+
var ex1 = Assert.Throws<FailedArchRuleException>(() =>
44+
_architecture.CheckRule(_falseRule)
45+
);
46+
var ex2 = Assert.Throws<FailedArchRuleException>(() => _falseRule.Check(_architecture));
47+
await Assert.That(ex1.Message).IsEqualTo(_expectedErrorMessage);
48+
await Assert.That(ex2.Message).IsEqualTo(_expectedErrorMessage);
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)