Skip to content
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

ci: improve test coverage and add snapshot tests for object conditions #291

Merged
merged 4 commits into from
Sep 3, 2024
Merged
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
21 changes: 19 additions & 2 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,24 @@
"tools": {
"csharpier": {
"version": "0.28.2",
"commands": ["dotnet-csharpier"]
"commands": [
"dotnet-csharpier"
],
"rollForward": false
},
"verify.tool": {
"version": "0.6.0",
"commands": [
"dotnet-verify"
],
"rollForward": false
},
"dotnet-reportgenerator-globaltool": {
"version": "5.3.8",
"commands": [
"reportgenerator"
],
"rollForward": false
}
}
}
}
25 changes: 25 additions & 0 deletions ArchUnit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.MSTestV2", "Arc
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchUnitNET.MSTestV2Tests", "ArchUnitNET.MSTestV2Tests\ArchUnitNET.MSTestV2Tests.csproj", "{6D6B6EFE-DA0B-4C4D-B710-FA658F0C68CF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TestAssemblies", "TestAssemblies", "{B1191F18-91CB-4387-B775-A5EB64D3AC30}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencyAssembly", "TestAssemblies\DependencyAssembly\DependencyAssembly.csproj", "{10A70A38-A18D-4FA8-AF25-2B25B3D60BE6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AttributeAssembly", "TestAssemblies\AttributeAssembly\AttributeAssembly.csproj", "{FB457140-47B4-4B20-8505-BA9BFC73C705}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VisibilityAssembly", "TestAssemblies\VisibilityAssembly\VisibilityAssembly.csproj", "{FBCD91F2-4DB9-44AC-8214-6F2FFF9178D5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -62,8 +70,25 @@ Global
{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
{10A70A38-A18D-4FA8-AF25-2B25B3D60BE6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{10A70A38-A18D-4FA8-AF25-2B25B3D60BE6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{10A70A38-A18D-4FA8-AF25-2B25B3D60BE6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{10A70A38-A18D-4FA8-AF25-2B25B3D60BE6}.Release|Any CPU.Build.0 = Release|Any CPU
{FB457140-47B4-4B20-8505-BA9BFC73C705}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FB457140-47B4-4B20-8505-BA9BFC73C705}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FB457140-47B4-4B20-8505-BA9BFC73C705}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FB457140-47B4-4B20-8505-BA9BFC73C705}.Release|Any CPU.Build.0 = Release|Any CPU
{FBCD91F2-4DB9-44AC-8214-6F2FFF9178D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FBCD91F2-4DB9-44AC-8214-6F2FFF9178D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FBCD91F2-4DB9-44AC-8214-6F2FFF9178D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FBCD91F2-4DB9-44AC-8214-6F2FFF9178D5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{10A70A38-A18D-4FA8-AF25-2B25B3D60BE6} = {B1191F18-91CB-4387-B775-A5EB64D3AC30}
{FB457140-47B4-4B20-8505-BA9BFC73C705} = {B1191F18-91CB-4387-B775-A5EB64D3AC30}
{FBCD91F2-4DB9-44AC-8214-6F2FFF9178D5} = {B1191F18-91CB-4387-B775-A5EB64D3AC30}
EndGlobalSection
EndGlobal
21 changes: 10 additions & 11 deletions ArchUnitNET/Fluent/Syntax/Elements/ObjectConditionsDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,11 @@ public static ICondition<TRuleType> DependOnAny(
)
{
var patternList = patterns.ToList();

bool Condition(TRuleType ruleType)
bool Condition(TRuleType ruleType, Architecture architecture)
{
return !ruleType.GetTypeDependencies().IsNullOrEmpty()
return !ruleType.GetTypeDependencies(architecture).IsNullOrEmpty()
&& ruleType
.GetTypeDependencies()
.GetTypeDependencies(architecture)
.Any(target =>
patternList.Any(pattern =>
target.FullNameMatches(pattern, useRegularExpressions)
Expand Down Expand Up @@ -421,7 +420,7 @@ bool Condition(TRuleType ruleType)
);
}

return new SimpleCondition<TRuleType>(Condition, description, failDescription);
return new ArchitectureCondition<TRuleType>(Condition, description, failDescription);
}

public static ICondition<TRuleType> DependOnAny(IType firstType, params IType[] moreTypes)
Expand Down Expand Up @@ -1943,7 +1942,7 @@ bool Condition(TRuleType obj, Architecture architecture)
goto NextAttribute;
}
}
else if (!argumentList.Contains(arg))
else if (!attributeArgs.Contains(arg))
{
goto NextAttribute;
}
Expand Down Expand Up @@ -2034,7 +2033,7 @@ bool Condition(TRuleType obj, Architecture architecture)
goto NextAttribute;
}
}
else if (!argumentList.Contains(arg))
else if (!attributeArgs.Contains(arg))
{
goto NextAttribute;
}
Expand Down Expand Up @@ -2136,7 +2135,7 @@ bool Condition(TRuleType obj, Architecture architecture)
goto NextAttribute;
}
}
else if (!argumentList.Contains(arg))
else if (!attributeArgs.Contains(arg))
{
goto NextAttribute;
}
Expand Down Expand Up @@ -3742,7 +3741,7 @@ bool Condition(TRuleType obj, Architecture architecture)
goto NextAttribute;
}
}
else if (!argumentList.Contains(arg))
else if (!attributeArgs.Contains(arg))
{
goto NextAttribute;
}
Expand Down Expand Up @@ -3833,7 +3832,7 @@ bool Condition(TRuleType obj, Architecture architecture)
goto NextAttribute;
}
}
else if (!argumentList.Contains(arg))
else if (!attributeArgs.Contains(arg))
{
goto NextAttribute;
}
Expand Down Expand Up @@ -3935,7 +3934,7 @@ bool Condition(TRuleType obj, Architecture architecture)
goto NextAttribute;
}
}
else if (!argumentList.Contains(arg))
else if (!attributeArgs.Contains(arg))
{
goto NextAttribute;
}
Expand Down
6 changes: 5 additions & 1 deletion ArchUnitNETTests/ArchUnitNETTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<ItemGroup>
<ProjectReference Include="..\ArchUnitNET.xUnit\ArchUnitNET.xUnit.csproj" />
<ProjectReference Include="..\TestAssembly\TestAssembly.csproj" />
<ProjectReference Include="..\TestAssemblies\AttributeAssembly\AttributeAssembly.csproj" />
<ProjectReference Include="..\TestAssemblies\DependencyAssembly\DependencyAssembly.csproj" />
<ProjectReference Include="..\TestAssemblies\VisibilityAssembly\VisibilityAssembly.csproj" />
</ItemGroup>

<ItemGroup>
Expand All @@ -18,7 +21,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="Verify.xunit" Version="26.1.6" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
</ItemGroup>

Expand Down
89 changes: 89 additions & 0 deletions ArchUnitNETTests/AssemblyTestHelper/AssemblyTestHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using ArchUnitNET.Domain;
using ArchUnitNET.Fluent;
using ArchUnitNET.Fluent.Extensions;
using VerifyXunit;
using Xunit;

namespace ArchUnitNETTests.AssemblyTestHelper;

public abstract class AssemblyTestHelper
{
private StringBuilder snapshot = new StringBuilder();

public readonly string NonExistentObjectName = "NotTheNameOfAnyObject";

public abstract Architecture Architecture { get; }

public void AddSnapshotHeader(string header)
{
snapshot.AppendLine("===== " + header + " =====\n");
}

private string FormatSnapshot(IArchRule rule, IEnumerable<EvaluationResult> results)
{
var formatted = new StringBuilder();
formatted.Append("Query: ");
formatted.AppendLine(rule.Description);
foreach (var result in results)
{
formatted.Append("Result: ");
formatted.AppendLine(result.Passed.ToString());
formatted.Append("Description: ");
formatted.AppendLine(result.ToString());
}
formatted.AppendLine("Message: ");
formatted.AppendLine(results.ToErrorMessage());
formatted.AppendLine();
return formatted.ToString();
}

public void AssertNoViolations(IArchRule rule)
{
var results = rule.Evaluate(Architecture);
var output = FormatSnapshot(rule, results);
if (!results.All(result => result.Passed))
{
Assert.Fail(output);
}
snapshot.Append(output);
}

public void AssertAnyViolations(IArchRule rule)
{
var results = rule.Evaluate(Architecture);
var output = FormatSnapshot(rule, results);
if (results.All(result => !result.Passed))
{
Assert.Fail("AssertOnlyViolations should be used for tests without passing results.");
}
if (results.All(result => result.Passed))
{
Assert.Fail(output);
}
snapshot.Append(output);
}

public void AssertOnlyViolations(IArchRule rule)
{
var results = rule.Evaluate(Architecture);
var output = FormatSnapshot(rule, results);
if (results.Any(result => result.Passed))
{
Assert.Fail(output);
}
snapshot.Append(output);
}

public Task AssertSnapshotMatches([CallerFilePath] string sourceFile = "")
{
return Verifier
.Verify(snapshot.ToString(), null, sourceFile)
.DisableDiff() // Don't open diff tool during the test
.UseDirectory("Snapshots");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using ArchUnitNET.Fluent;

namespace ArchUnitNETTests.AssemblyTestHelper;

public static class AssemblyTestHelperExtensions
{
public static void AssertNoViolations(this IArchRule archRule, AssemblyTestHelper testHelper)
{
testHelper.AssertNoViolations(archRule);
}

public static void AssertAnyViolations(this IArchRule archRule, AssemblyTestHelper testHelper)
{
testHelper.AssertAnyViolations(archRule);
}

public static void AssertOnlyViolations(this IArchRule archRule, AssemblyTestHelper testHelper)
{
testHelper.AssertOnlyViolations(archRule);
}
}
Loading
Loading