-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🧪 Architecture tests to enforce Domain purity (#87)
- Loading branch information
1 parent
d16a9b8
commit 8ed2291
Showing
9 changed files
with
169 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,4 @@ public void Complete() | |
|
||
StagedEvents.Add(new TodoCompletedEvent(Id)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,6 @@ | |
|
||
app.RegisterEndpoints(appAssembly); | ||
|
||
app.Run(); | ||
app.Run(); | ||
|
||
public partial class Program; |
69 changes: 69 additions & 0 deletions
69
tests/VerticalSliceArchitecture.ArchTests/ArchitectureTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using FluentAssertions; | ||
using VerticalSliceArchitecture.ArchTests.Common; | ||
using Xunit.Abstractions; | ||
|
||
namespace VerticalSliceArchitecture.ArchTests; | ||
|
||
public class ArchitectureTests : TestBase | ||
{ | ||
private readonly ITestOutputHelper _outputHelper; | ||
|
||
public ArchitectureTests(ITestOutputHelper outputHelper) | ||
{ | ||
_outputHelper = outputHelper; | ||
} | ||
|
||
private readonly string[] _applicationNamespaces = | ||
[ | ||
"Queries", | ||
"Application", | ||
"Commands" | ||
]; | ||
|
||
private readonly string[] _domainNamespaces = | ||
[ | ||
"Domain" | ||
]; | ||
|
||
private readonly string[] _infrastructureNamespaces = | ||
[ | ||
"Infrastructure", | ||
"Persistence" | ||
]; | ||
|
||
[Fact] | ||
public void Domain_Should_Not_Depend_On_Infrastructure() | ||
{ | ||
// Arrange | ||
var domainTypes = TypesMatchingAnyPattern(_domainNamespaces); | ||
var infraTypes = TypesMatchingAnyPattern(_infrastructureNamespaces); | ||
|
||
// Act | ||
var result = domainTypes | ||
.ShouldNot() | ||
.HaveDependencyOnAny(infraTypes.GetNames()) | ||
.GetResult(); | ||
|
||
// Assert | ||
result.DumpFailingTypes(_outputHelper); | ||
result.IsSuccessful.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void Domain_Should_Not_Depend_On_Application() | ||
{ | ||
// Arrange | ||
var domainTypes = TypesMatchingAnyPattern(_domainNamespaces); | ||
var applicationTypes = TypesMatchingAnyPattern(_applicationNamespaces); | ||
|
||
// Act | ||
var result = domainTypes | ||
.ShouldNot() | ||
.HaveDependencyOnAny(applicationTypes.GetNames()) | ||
.GetResult(); | ||
|
||
// Assert | ||
result.DumpFailingTypes(_outputHelper); | ||
result.IsSuccessful.Should().BeTrue(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
tests/VerticalSliceArchitecture.ArchTests/Common/TestResultExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using NetArchTest.Rules; | ||
using Xunit.Abstractions; | ||
|
||
namespace VerticalSliceArchitecture.ArchTests.Common; | ||
|
||
public static class TestResultExtensions | ||
{ | ||
public static void DumpFailingTypes(this TestResult result, ITestOutputHelper outputHelper) | ||
{ | ||
if (result.IsSuccessful) | ||
return; | ||
|
||
outputHelper.WriteLine("Failing Types:"); | ||
|
||
foreach (var type in result.FailingTypes) | ||
outputHelper.WriteLine(type.FullName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Xunit; |
11 changes: 11 additions & 0 deletions
11
tests/VerticalSliceArchitecture.ArchTests/PredicateListExt.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using NetArchTest.Rules; | ||
|
||
namespace VerticalSliceArchitecture.ArchTests; | ||
|
||
public static class PredicateListExt | ||
{ | ||
public static string?[] GetNames(this PredicateList list) | ||
{ | ||
return list.GetTypes().Select(x => x.FullName).ToArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using NetArchTest.Rules; | ||
using System.Diagnostics; | ||
|
||
namespace VerticalSliceArchitecture.ArchTests; | ||
|
||
public abstract class TestBase | ||
{ | ||
private static readonly Types ProgramTypes = Types.InAssembly(typeof(Program).Assembly); | ||
|
||
protected PredicateList TypesMatchingAnyPattern(params string[] patterns) | ||
{ | ||
var output = ProgramTypes.That(); | ||
|
||
for (var index = 0; index < patterns.Length; index++) | ||
{ | ||
var pattern = patterns[index]; | ||
|
||
if (index == patterns.Length - 1) | ||
{ | ||
return output.ResideInNamespaceContaining(pattern); | ||
} | ||
|
||
output = output.ResideInNamespaceContaining(pattern).Or(); | ||
} | ||
|
||
throw new UnreachableException(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
tests/VerticalSliceArchitecture.ArchTests/VerticalSliceArchitecture.ArchTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/> | ||
<PackageReference Include="NetArchTest.Rules" Version="1.3.2" /> | ||
<PackageReference Include="xunit" Version="2.4.2"/> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\VerticalSliceArchitectureTemplate\VerticalSliceArchitectureTemplate.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |