-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #291 from TNG/ci/object-condition-tests
ci: improve test coverage and add snapshot tests for object conditions
- Loading branch information
Showing
56 changed files
with
9,733 additions
and
20 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
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
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,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"); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
ArchUnitNETTests/AssemblyTestHelper/AssemblyTestHelperExtensions.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,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); | ||
} | ||
} |
Oops, something went wrong.