Skip to content

Commit

Permalink
Add Arch Unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
oveldman committed Jul 8, 2023
1 parent 076bbab commit 6993447
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 0 deletions.
2 changes: 2 additions & 0 deletions MadWorld/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
<PackageVersion Include="Moq" Version="4.18.4" />
<PackageVersion Include="Radzen.Blazor" Version="4.12.0" />
<PackageVersion Include="Shouldly" Version="4.2.1" />
<PackageVersion Include="TngTech.ArchUnitNET" Version="0.10.5" />
<PackageVersion Include="TngTech.ArchUnitNET.xUnit" Version="0.10.5" />
<PackageVersion Include="WireMock.Net" Version="1.5.26" />
<PackageVersion Include="WireMock.Net.Abstractions" Version="1.5.26" />
<PackageVersion Include="WireMock.Net.FluentAssertions" Version="1.5.26" />
Expand Down
61 changes: 61 additions & 0 deletions MadWorld/MadWorld.Architecture.Tests/DependencyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using ArchUnitNET.Domain.Extensions;
using ArchUnitNET.Fluent;
using ArchUnitNET.Loader;
using ArchUnitNET.xUnit;
using MadWorld.Backend.API.Anonymous;
using MadWorld.Backend.API.Authorized;
using MadWorld.Backend.API.Shared;
using MadWorld.Backend.Application;
using MadWorld.Backend.Domain;
using MadWorld.Backend.Infrastructure;

//add a using directive to ArchUnitNET.Fluent.ArchRuleDefinition to easily define ArchRules
using static ArchUnitNET.Fluent.ArchRuleDefinition;

namespace MadWorld.Architecture.Tests;

public class DependencyTests
{
private static readonly ArchUnitNET.Domain.Architecture Architecture = new ArchLoader().LoadAssemblies(
typeof(DomainMarker).Assembly,
typeof(AnonymousMarker).Assembly,
typeof(AuthorizedMarker).Assembly,
typeof(SharedMarker).Assembly,
typeof(ApplicationMarker).Assembly,
typeof(InfrastructureMarker).Assembly)
.Build();

private static readonly string DomainNamespace = typeof(DomainMarker).Namespace!;
private static readonly string AnonymousNamespace = typeof(AnonymousMarker).Namespace!;
private static readonly string AuthorizedNamespace = typeof(AuthorizedMarker).Namespace!;
private static readonly string SharedNamespace = typeof(SharedMarker).Namespace!;
private static readonly string ApplicationNamespace = typeof(ApplicationMarker).Namespace!;
private static readonly string InfrastructureNamespace = typeof(InfrastructureMarker).Namespace!;

[Fact]
public void ApplicationDependsNotOnInfrastructure()
{
var rule = Types().That().ResideInNamespace($"{ApplicationNamespace}.*", true)
.Should().NotDependOnAny($"{InfrastructureNamespace}.*", true);

rule.Check(Architecture);
}

[Fact]
public void DomainDependsNotOnBackendProject()
{
var backendProjects = new List<string>()
{
$"{AnonymousNamespace}.*",
$"{AuthorizedNamespace}.*",
$"{SharedNamespace}.*",
$"{ApplicationNamespace}.*",
$"{InfrastructureNamespace}.*",
};

var rule = Types().That().ResideInNamespace($"{DomainNamespace}.*", true)
.Should().NotDependOnAny(backendProjects, true);

rule.Check(Architecture);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="TngTech.ArchUnitNET" />
<PackageReference Include="TngTech.ArchUnitNET.xUnit" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MadWorld.Backend.API.Anonymous\MadWorld.Backend.API.Anonymous.csproj" />
<ProjectReference Include="..\MadWorld.Backend.API.Authorized\MadWorld.Backend.API.Authorized.csproj" />
<ProjectReference Include="..\MadWorld.Backend.Application\MadWorld.Backend.Application.csproj" />
<ProjectReference Include="..\MadWorld.Backend.Infrastructure\MadWorld.Backend.Infrastructure.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions MadWorld/MadWorld.Architecture.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
6 changes: 6 additions & 0 deletions MadWorld/MadWorld.Backend.API.Anonymous/AnonymousMarker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MadWorld.Backend.API.Anonymous;

public interface AnonymousMarker
{

}
6 changes: 6 additions & 0 deletions MadWorld/MadWorld.Backend.API.Authorized/AuthorizedMarker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MadWorld.Backend.API.Authorized;

public interface AuthorizedMarker
{

}
6 changes: 6 additions & 0 deletions MadWorld/MadWorld.Backend.API.Shared/SharedMarker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MadWorld.Backend.API.Shared;

public interface SharedMarker
{

}
6 changes: 6 additions & 0 deletions MadWorld/MadWorld.Backend.Application/ApplicationMarker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MadWorld.Backend.Application;

public interface ApplicationMarker
{

}
6 changes: 6 additions & 0 deletions MadWorld/MadWorld.Backend.Domain/DomainMarker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MadWorld.Backend.Domain;

public interface DomainMarker
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MadWorld.Backend.Infrastructure;

public interface InfrastructureMarker
{

}
10 changes: 10 additions & 0 deletions MadWorld/MadWorld.sln
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MadWorld.Frontend.UI.Admin.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MadWorld.Backend.Domain.Unittests", "MadWorld.Backend.Domain.Unittests\MadWorld.Backend.Domain.Unittests.csproj", "{F28F0DEE-738E-499B-8234-6034C891B155}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ArchitectureTest", "ArchitectureTest", "{67EF329C-BD74-4980-854A-624B3CC41864}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MadWorld.Architecture.Tests", "MadWorld.Architecture.Tests\MadWorld.Architecture.Tests.csproj", "{9F93E72F-BB54-4779-8C70-805704302C46}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -161,6 +165,8 @@ Global
{C3BA6B58-C4C3-411C-95A2-A66B1EA682AF} = {8BBC9D54-C485-4FD7-99C4-0747D2837C71}
{00E588A9-3052-4DB5-8742-D673D397760C} = {C3BA6B58-C4C3-411C-95A2-A66B1EA682AF}
{F28F0DEE-738E-499B-8234-6034C891B155} = {DF837823-254A-44E6-9F46-133C31114CE5}
{67EF329C-BD74-4980-854A-624B3CC41864} = {5470C4F1-9CEA-4F86-8041-64C444941FDA}
{9F93E72F-BB54-4779-8C70-805704302C46} = {67EF329C-BD74-4980-854A-624B3CC41864}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{ED8A1D3F-49F6-43AF-A8F7-AADAC4D7E84C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -247,5 +253,9 @@ Global
{F28F0DEE-738E-499B-8234-6034C891B155}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F28F0DEE-738E-499B-8234-6034C891B155}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F28F0DEE-738E-499B-8234-6034C891B155}.Release|Any CPU.Build.0 = Release|Any CPU
{9F93E72F-BB54-4779-8C70-805704302C46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9F93E72F-BB54-4779-8C70-805704302C46}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F93E72F-BB54-4779-8C70-805704302C46}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F93E72F-BB54-4779-8C70-805704302C46}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

0 comments on commit 6993447

Please sign in to comment.