Skip to content

Commit 74e213e

Browse files
committed
Feat: Some Reorganization
1 parent 5e5e530 commit 74e213e

17 files changed

+337
-76
lines changed
Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// ---------------------------------------------------------------------------------------------------------------------
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
4+
using CodeOfChaos.Testing.TUnit.Conditions.Library;
45
using Microsoft.CodeAnalysis;
56
using System.Collections.Immutable;
67
using TUnit.Assertions.AssertConditions;
@@ -10,18 +11,6 @@ namespace CodeOfChaos.Testing.TUnit.Conditions;
1011
// ---------------------------------------------------------------------------------------------------------------------
1112
// Code
1213
// ---------------------------------------------------------------------------------------------------------------------
13-
public class CompilationContainsDiagnosticAssertCondition(string expectedId)
14-
: ExpectedValueAssertCondition<Compilation, string>(expectedId) {
15-
16-
protected override string GetExpectation() => $"to have a diagnostic with Id \"{ExpectedValue}\"";
17-
18-
protected override Task<AssertionResult> GetResult(Compilation? actualValue, string? expectedValue) {
19-
if (actualValue is null) return AssertionResult.Fail("Compilation is null");
20-
if (expectedValue is null) return AssertionResult.Fail("Expected value is null");
21-
22-
ImmutableArray<Diagnostic> diagnostics = actualValue.GetDiagnostics();
23-
if (!diagnostics.Any()) return FailWithMessage("No diagnostics");
24-
if (diagnostics.Any(d => d.Id == expectedValue)) return AssertionResult.Passed;
25-
return FailWithMessage("No diagnostic with Id");
26-
}
14+
public class CompilationContainsDiagnosticAssertCondition(string expectedId) : ContainsDiagnosticAssertCondition<Compilation>(expectedId) {
15+
protected override ImmutableArray<Diagnostic> GetDiagnostics(Compilation value) => value.GetDiagnostics();
2716
}
Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,17 @@
11
// ---------------------------------------------------------------------------------------------------------------------
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
4+
using CodeOfChaos.Testing.TUnit.Conditions.Library;
45
using Microsoft.CodeAnalysis;
56
using System.Collections.Immutable;
6-
using TUnit.Assertions.AssertConditions;
77

88
namespace CodeOfChaos.Testing.TUnit.Conditions;
99

1010
// ---------------------------------------------------------------------------------------------------------------------
1111
// Code
1212
// ---------------------------------------------------------------------------------------------------------------------
1313
public class CompilationContainsDiagnosticsExclusivelyAssertCondition(string[] expectedIds)
14-
: ExpectedValueAssertCondition<Compilation, string[]>(expectedIds) {
14+
: ContainsDiagnosticsExclusivelyAssertCondition<Compilation>(expectedIds) {
1515

16-
protected override string GetExpectation() => $"to have a compilation output with the following Ids \"{ExpectedValue}\"";
17-
18-
protected override Task<AssertionResult> GetResult(Compilation? compilation, string[]? expectedValues) {
19-
if (compilation is null) return AssertionResult.Fail("Compilation is null");
20-
if (expectedValues is null) return AssertionResult.Fail("Expected value is null");
21-
22-
ImmutableArray<Diagnostic> diagnostics = compilation.GetDiagnostics();
23-
if (!diagnostics.Any() && expectedValues.Length == 0) return AssertionResult.Passed;
24-
if (!diagnostics.Any()) return FailWithMessage("No diagnostics");
25-
if (expectedValues.Length != diagnostics.Length) return FailWithMessage("Wrong number of diagnostics");
26-
27-
HashSet<string> diagnosticsHashSet = diagnostics.Select(d => d.Id).ToHashSet();
28-
HashSet<string> expectedValuesHashSet = expectedValues.ToHashSet();
29-
30-
if (diagnosticsHashSet.SetEquals(expectedValuesHashSet)) return AssertionResult.Passed;
31-
32-
// Find which diagnostics are missing or unexpected
33-
string[] missingDiagnostics = expectedValuesHashSet.Except(diagnosticsHashSet).ToArray();
34-
string[] unexpectedDiagnostics = diagnosticsHashSet.Except(expectedValuesHashSet).ToArray();
35-
36-
string errorMessage = "Diagnostics do not match:";
37-
38-
if (missingDiagnostics.Length != 0) {
39-
errorMessage += $"\n - Missing diagnostics: {string.Join(", ", missingDiagnostics)}";
40-
}
41-
42-
if (unexpectedDiagnostics.Length != 0) {
43-
errorMessage += $"\n - Unexpected diagnostics: {string.Join(", ", unexpectedDiagnostics)}";
44-
}
45-
46-
return AssertionResult.Fail(errorMessage);
47-
48-
}
16+
protected override ImmutableArray<Diagnostic> GetDiagnostics(Compilation value) => value.GetDiagnostics();
4917
}
Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
// ---------------------------------------------------------------------------------------------------------------------
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
4+
using CodeOfChaos.Testing.TUnit.Conditions.Library;
45
using Microsoft.CodeAnalysis;
56
using System.Collections.Immutable;
6-
using TUnit.Assertions.AssertConditions;
77

88
namespace CodeOfChaos.Testing.TUnit.Conditions;
99

1010
// ---------------------------------------------------------------------------------------------------------------------
1111
// Code
1212
// ---------------------------------------------------------------------------------------------------------------------
1313
public class CompilationDoesNotContainDiagnosticAssertCondition(string expectedId)
14-
: ExpectedValueAssertCondition<Compilation, string>(expectedId) {
14+
: DoesNotContainDiagnosticAssertCondition<Compilation>(expectedId) {
1515

16-
protected override string GetExpectation() => $"to not have a diagnostic with Id \"{ExpectedValue}\"";
17-
18-
protected override Task<AssertionResult> GetResult(Compilation? actualValue, string? expectedValue) {
19-
if (actualValue is null) return AssertionResult.Fail("Compilation is null");
20-
if (expectedValue is null) return AssertionResult.Fail("Expected value is null");
21-
22-
ImmutableArray<Diagnostic> diagnostics = actualValue.GetDiagnostics();
23-
24-
if (diagnostics.Any(d => d.Id == expectedValue)) return FailWithMessage("Diagnostic with Id");
25-
26-
return AssertionResult.Passed;
27-
}
16+
protected override ImmutableArray<Diagnostic> GetDiagnostics(Compilation value) => value.GetDiagnostics();
2817
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using CodeOfChaos.Testing.TUnit.Conditions.Library;
5+
using Microsoft.CodeAnalysis;
6+
using System.Collections.Immutable;
7+
using TUnit.Assertions.AssertConditions;
8+
9+
namespace CodeOfChaos.Testing.TUnit.Conditions;
10+
11+
// ---------------------------------------------------------------------------------------------------------------------
12+
// Code
13+
// ---------------------------------------------------------------------------------------------------------------------
14+
public class GeneratorDriverRunResultContainsDiagnosticAssertCondition(string expectedId) : ContainsDiagnosticAssertCondition<GeneratorDriverRunResult>(expectedId) {
15+
protected override ImmutableArray<Diagnostic> GetDiagnostics(GeneratorDriverRunResult value) => value.Diagnostics;
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using CodeOfChaos.Testing.TUnit.Conditions.Library;
5+
using Microsoft.CodeAnalysis;
6+
using System.Collections.Immutable;
7+
using TUnit.Assertions.AssertConditions;
8+
9+
namespace CodeOfChaos.Testing.TUnit.Conditions;
10+
11+
// ---------------------------------------------------------------------------------------------------------------------
12+
// Code
13+
// ---------------------------------------------------------------------------------------------------------------------
14+
public class GeneratorDriverRunResultContainsDiagnosticsExclusivelyAssertCondition(string[] expectedIds) : ContainsDiagnosticsExclusivelyAssertCondition<GeneratorDriverRunResult>(expectedIds) {
15+
protected override ImmutableArray<Diagnostic> GetDiagnostics(GeneratorDriverRunResult value) => value.Diagnostics;
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using CodeOfChaos.Testing.TUnit.Conditions.Library;
5+
using Microsoft.CodeAnalysis;
6+
using System.Collections.Immutable;
7+
using TUnit.Assertions.AssertConditions;
8+
9+
namespace CodeOfChaos.Testing.TUnit.Conditions;
10+
11+
// ---------------------------------------------------------------------------------------------------------------------
12+
// Code
13+
// ---------------------------------------------------------------------------------------------------------------------
14+
public class GeneratorDriverRunResultDoesNotContainDiagnosticAssertCondition(string expectedId) : DoesNotContainDiagnosticAssertCondition<GeneratorDriverRunResult>(expectedId) {
15+
protected override ImmutableArray<Diagnostic> GetDiagnostics(GeneratorDriverRunResult value) => value.Diagnostics;
16+
}

src/CodeOfChaos.Testing.TUnit/Conditions/CompilationHasSourceTextEqualToCondition.cs renamed to src/CodeOfChaos.Testing.TUnit/Conditions/GeneratorDriverRunResultHasSourceTextEqualToCondition.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace CodeOfChaos.Testing.TUnit.Conditions;
1010
// ---------------------------------------------------------------------------------------------------------------------
1111
// Code
1212
// ---------------------------------------------------------------------------------------------------------------------
13-
public class CompilationHasSourceTextEqualToCondition(string filename, string expected, StringComparison stringComparison, bool ignoreWhiteSpace, bool withTrimming)
13+
public class GeneratorDriverRunResultHasSourceTextEqualToCondition(string filename, string expected, StringComparison stringComparison, bool ignoreWhiteSpace, bool withTrimming)
1414
: ExpectedValueAssertCondition<GeneratorDriverRunResult, string>(expected) {
1515
private readonly string _expected = expected;
1616

@@ -37,11 +37,5 @@ protected async override Task<AssertionResult> GetResult(GeneratorDriverRunResul
3737
if(ignoreWhiteSpace) stringEqualsAssertCondition.IgnoringWhitespace();
3838

3939
return await stringEqualsAssertCondition.GetAssertionResult(sourceTextString, null);
40-
41-
// if (ignoreWhiteSpace) sourceTextString = string.Join(string.Empty, sourceTextString.Where(c => !char.IsWhiteSpace(c)));
42-
// if (withTrimming) sourceTextString = sourceTextString.Trim();
43-
//
44-
// return AssertionResult
45-
// .FailIf(!string.Equals(sourceTextString, expectedValue, stringComparison), "Source text does not match");
4640
}
4741
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using Microsoft.CodeAnalysis;
5+
using System.Collections.Immutable;
6+
using TUnit.Assertions.AssertConditions;
7+
8+
namespace CodeOfChaos.Testing.TUnit.Conditions.Library;
9+
10+
// ---------------------------------------------------------------------------------------------------------------------
11+
// Code
12+
// ---------------------------------------------------------------------------------------------------------------------
13+
public abstract class ContainsDiagnosticAssertCondition<T>(string expectedId): ExpectedValueAssertCondition<T, string>(expectedId) {
14+
15+
protected abstract ImmutableArray<Diagnostic> GetDiagnostics(T value);
16+
17+
// -----------------------------------------------------------------------------------------------------------------
18+
// Methods
19+
// -----------------------------------------------------------------------------------------------------------------
20+
protected override string GetExpectation() => $"to have a diagnostic with Id \"{ExpectedValue}\"";
21+
protected override Task<AssertionResult> GetResult(T? actualValue, string? expectedValue) {
22+
if (actualValue is null) return AssertionResult.Fail($"{nameof(T)} is null");
23+
if (expectedValue is null) return AssertionResult.Fail("Expected value is null");
24+
25+
ImmutableArray<Diagnostic> diagnostics = GetDiagnostics(actualValue);
26+
if (!diagnostics.Any()) return FailWithMessage("No diagnostics");
27+
if (diagnostics.Any(d => d.Id == expectedValue)) return AssertionResult.Passed;
28+
return FailWithMessage("No diagnostic with Id");
29+
}
30+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using Microsoft.CodeAnalysis;
5+
using System.Collections.Immutable;
6+
using TUnit.Assertions.AssertConditions;
7+
8+
namespace CodeOfChaos.Testing.TUnit.Conditions.Library;
9+
10+
// ---------------------------------------------------------------------------------------------------------------------
11+
// Code
12+
// ---------------------------------------------------------------------------------------------------------------------
13+
public abstract class ContainsDiagnosticsExclusivelyAssertCondition<T>(string[] expectedIds)
14+
: ExpectedValueAssertCondition<T, string[]>(expectedIds) {
15+
16+
protected abstract ImmutableArray<Diagnostic> GetDiagnostics(T value);
17+
18+
// -----------------------------------------------------------------------------------------------------------------
19+
// Methods
20+
// -----------------------------------------------------------------------------------------------------------------
21+
protected override string GetExpectation() => $"to have a compilation output with the following Ids \"{ExpectedValue}\"";
22+
protected override Task<AssertionResult> GetResult(T? actualValue, string[]? expectedValues) {
23+
if (actualValue is null) return AssertionResult.Fail($"{nameof(T)} is null");
24+
if (expectedValues is null) return AssertionResult.Fail("Expected value is null");
25+
26+
ImmutableArray<Diagnostic> diagnostics = GetDiagnostics(actualValue);
27+
if (!diagnostics.Any() && expectedValues.Length == 0) return AssertionResult.Passed;
28+
if (!diagnostics.Any()) return FailWithMessage("No diagnostics");
29+
if (expectedValues.Length != diagnostics.Length) return FailWithMessage("Wrong number of diagnostics");
30+
31+
HashSet<string> diagnosticsHashSet = diagnostics.Select(d => d.Id).ToHashSet();
32+
HashSet<string> expectedValuesHashSet = expectedValues.ToHashSet();
33+
34+
if (diagnosticsHashSet.SetEquals(expectedValuesHashSet)) return AssertionResult.Passed;
35+
36+
// Find which diagnostics are missing or unexpected
37+
string[] missingDiagnostics = expectedValuesHashSet.Except(diagnosticsHashSet).ToArray();
38+
string[] unexpectedDiagnostics = diagnosticsHashSet.Except(expectedValuesHashSet).ToArray();
39+
40+
string errorMessage = "Diagnostics do not match:";
41+
42+
if (missingDiagnostics.Length != 0) {
43+
errorMessage += $"\n - Missing diagnostics: {string.Join(", ", missingDiagnostics)}";
44+
}
45+
46+
if (unexpectedDiagnostics.Length != 0) {
47+
errorMessage += $"\n - Unexpected diagnostics: {string.Join(", ", unexpectedDiagnostics)}";
48+
}
49+
50+
return AssertionResult.Fail(errorMessage);
51+
52+
}
53+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using Microsoft.CodeAnalysis;
5+
using System.Collections.Immutable;
6+
using TUnit.Assertions.AssertConditions;
7+
8+
namespace CodeOfChaos.Testing.TUnit.Conditions.Library;
9+
10+
// ---------------------------------------------------------------------------------------------------------------------
11+
// Code
12+
// ---------------------------------------------------------------------------------------------------------------------
13+
public abstract class DoesNotContainDiagnosticAssertCondition<T>(string expectedId)
14+
: ExpectedValueAssertCondition<T, string>(expectedId) {
15+
16+
protected abstract ImmutableArray<Diagnostic> GetDiagnostics(T value);
17+
18+
// -----------------------------------------------------------------------------------------------------------------
19+
// Methods
20+
// -----------------------------------------------------------------------------------------------------------------
21+
22+
protected override string GetExpectation() => $"to not have a diagnostic with Id \"{ExpectedValue}\"";
23+
protected override Task<AssertionResult> GetResult(T? actualValue, string? expectedValue) {
24+
if (actualValue is null) return AssertionResult.Fail($"{nameof(T)} is null");
25+
if (expectedValue is null) return AssertionResult.Fail("Expected value is null");
26+
27+
ImmutableArray<Diagnostic> diagnostics = GetDiagnostics(actualValue);
28+
29+
if (diagnostics.Any(d => d.Id == expectedValue)) return FailWithMessage("Diagnostic with Id");
30+
31+
return AssertionResult.Passed;
32+
}
33+
34+
}

0 commit comments

Comments
 (0)