Skip to content

Commit 9a6e769

Browse files
committed
Change: Usage of BaseAssertCondition
1 parent 8d4475f commit 9a6e769

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

src/CodeOfChaos.Testing.TUnit/Conditions/Library/ContainsDiagnosticAssertCondition.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ namespace CodeOfChaos.Testing.TUnit.Conditions.Library;
99
// ---------------------------------------------------------------------------------------------------------------------
1010
// Code
1111
// ---------------------------------------------------------------------------------------------------------------------
12-
public class ContainsDiagnosticAssertCondition<T>(Func<T, ValueTask<ImmutableArray<Diagnostic>>> getDiagnosticsAction, string expectedId) : ExpectedValueAssertCondition<T, string>(expectedId) {
12+
public class ContainsDiagnosticAssertCondition<T>(Func<T, ValueTask<ImmutableArray<Diagnostic>>> getDiagnosticsAction, string expectedId) : BaseAssertCondition<T> {
1313
// -----------------------------------------------------------------------------------------------------------------
1414
// Methods
1515
// -----------------------------------------------------------------------------------------------------------------
16-
protected override string GetExpectation() => $"to have a diagnostic with Id \"{ExpectedValue}\"";
17-
protected override async Task<AssertionResult> GetResult(T? actualValue, string? expectedValue) {
16+
protected override string GetExpectation() => $"to have a diagnostic with Id \"{expectedId}\"";
17+
18+
protected override async Task<AssertionResult> GetResult(T? actualValue, Exception? exception, AssertionMetadata assertionMetadata) {
1819
if (actualValue is null) return AssertionResult.Fail($"{nameof(T)} is null");
19-
if (expectedValue is null) return AssertionResult.Fail("Expected value is null");
2020

2121
ImmutableArray<Diagnostic> diagnostics = await getDiagnosticsAction(actualValue);
2222
if (!diagnostics.Any()) return FailWithMessage("No diagnostics");
23-
if (diagnostics.Any(d => d.Id == expectedValue)) return AssertionResult.Passed;
23+
if (diagnostics.Any(d => d.Id == expectedId)) return AssertionResult.Passed;
2424

2525
return FailWithMessage("No diagnostic with Id");
2626
}

src/CodeOfChaos.Testing.TUnit/Conditions/Library/ContainsDiagnosticsExclusivelyAssertCondition.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,28 @@ namespace CodeOfChaos.Testing.TUnit.Conditions.Library;
99
// ---------------------------------------------------------------------------------------------------------------------
1010
// Code
1111
// ---------------------------------------------------------------------------------------------------------------------
12-
public class ContainsDiagnosticsExclusivelyAssertCondition<T>(Func<T, ValueTask<ImmutableArray<Diagnostic>>> getDiagnosticsAction, string[] expectedIds)
13-
: ExpectedValueAssertCondition<T, string[]>(expectedIds) {
12+
public class ContainsDiagnosticsExclusivelyAssertCondition<T>(Func<T, ValueTask<ImmutableArray<Diagnostic>>> getDiagnosticsAction, string[] expectedIds) : BaseAssertCondition<T> {
13+
private readonly HashSet<string> ExpectedValuesHashSet = expectedIds.ToHashSet();
1414

1515
// -----------------------------------------------------------------------------------------------------------------
1616
// Methods
1717
// -----------------------------------------------------------------------------------------------------------------
18-
protected override string GetExpectation() => $"to have a compilation output with the following Ids \"{ExpectedValue}\"";
19-
protected override async Task<AssertionResult> GetResult(T? actualValue, string[]? expectedValues) {
18+
protected override string GetExpectation() => $"to have a compilation output with the following Ids \"{expectedIds}\"";
19+
protected override async Task<AssertionResult> GetResult(T? actualValue, Exception? exception, AssertionMetadata assertionMetadata) {
2020
if (actualValue is null) return AssertionResult.Fail($"{nameof(T)} is null");
21-
if (expectedValues is null) return AssertionResult.Fail("Expected value is null");
2221

2322
ImmutableArray<Diagnostic> diagnostics = await getDiagnosticsAction(actualValue);
24-
if (!diagnostics.Any() && expectedValues.Length == 0) return AssertionResult.Passed;
23+
if (!diagnostics.Any() && expectedIds.Length == 0) return AssertionResult.Passed;
2524
if (!diagnostics.Any()) return FailWithMessage("No diagnostics");
26-
if (expectedValues.Length != diagnostics.Length) return FailWithMessage("Wrong number of diagnostics");
25+
if (expectedIds.Length != diagnostics.Length) return FailWithMessage("Wrong number of diagnostics");
2726

2827
HashSet<string> diagnosticsHashSet = diagnostics.Select(d => d.Id).ToHashSet();
29-
HashSet<string> expectedValuesHashSet = expectedValues.ToHashSet();
3028

31-
if (diagnosticsHashSet.SetEquals(expectedValuesHashSet)) return AssertionResult.Passed;
29+
if (diagnosticsHashSet.SetEquals(ExpectedValuesHashSet)) return AssertionResult.Passed;
3230

3331
// Find which diagnostics are missing or unexpected
34-
string[] missingDiagnostics = expectedValuesHashSet.Except(diagnosticsHashSet).ToArray();
35-
string[] unexpectedDiagnostics = diagnosticsHashSet.Except(expectedValuesHashSet).ToArray();
32+
string[] missingDiagnostics = ExpectedValuesHashSet.Except(diagnosticsHashSet).ToArray();
33+
string[] unexpectedDiagnostics = diagnosticsHashSet.Except(ExpectedValuesHashSet).ToArray();
3634

3735
string errorMessage = "Diagnostics do not match:";
3836

src/CodeOfChaos.Testing.TUnit/Conditions/Library/DoesNotContainDiagnosticAssertCondition.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,20 @@ namespace CodeOfChaos.Testing.TUnit.Conditions.Library;
99
// ---------------------------------------------------------------------------------------------------------------------
1010
// Code
1111
// ---------------------------------------------------------------------------------------------------------------------
12-
public class DoesNotContainDiagnosticAssertCondition<T>(Func<T, ValueTask<ImmutableArray<Diagnostic>>> getDiagnosticsAction, string expectedId)
13-
: ExpectedValueAssertCondition<T, string>(expectedId) {
12+
public class DoesNotContainDiagnosticAssertCondition<T>(Func<T, ValueTask<ImmutableArray<Diagnostic>>> getDiagnosticsAction, string expectedId) : BaseAssertCondition<T> {
1413

1514
// -----------------------------------------------------------------------------------------------------------------
1615
// Methods
1716
// -----------------------------------------------------------------------------------------------------------------
1817

19-
protected override string GetExpectation() => $"to not have a diagnostic with Id \"{ExpectedValue}\"";
20-
protected override async Task<AssertionResult> GetResult(T? actualValue, string? expectedValue) {
18+
protected override string GetExpectation() => $"to not have a diagnostic with Id \"{expectedId}\"";
19+
protected override async Task<AssertionResult> GetResult(T? actualValue, Exception? exception, AssertionMetadata assertionMetadata) {
2120
if (actualValue is null) return AssertionResult.Fail($"{nameof(T)} is null");
22-
if (expectedValue is null) return AssertionResult.Fail("Expected value is null");
2321

2422
ImmutableArray<Diagnostic> diagnostics = await getDiagnosticsAction(actualValue);
2523

26-
if (diagnostics.Any(d => d.Id == expectedValue)) return FailWithMessage("Diagnostic with Id");
27-
28-
return AssertionResult.Passed;
24+
return diagnostics.Any(d => d.Id == expectedId)
25+
? FailWithMessage("Diagnostic with Id")
26+
: AssertionResult.Passed;
2927
}
3028
}

0 commit comments

Comments
 (0)