Skip to content

Commit 2e2c871

Browse files
committed
Feat: Ability to easily test for Diagnostic Analyzers
1 parent 3ec9cb7 commit 2e2c871

18 files changed

+169
-125
lines changed

src/CodeOfChaos.Testing.TUnit/Conditions/CompilationContainsDiagnosticAssertCondition.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/CodeOfChaos.Testing.TUnit/Conditions/CompilationContainsDiagnosticsExclusivelyAssertCondition.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/CodeOfChaos.Testing.TUnit/Conditions/CompilationDoesNotContainDiagnosticAssertCondition.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/CodeOfChaos.Testing.TUnit/Conditions/GeneratorDriverRunResultContainsDiagnosticAssertCondition.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/CodeOfChaos.Testing.TUnit/Conditions/GeneratorDriverRunResultContainsDiagnosticsExclusivelyAssertCondition.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/CodeOfChaos.Testing.TUnit/Conditions/GeneratorDriverRunResultDoesNotContainDiagnosticAssertCondition.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ namespace CodeOfChaos.Testing.TUnit.Conditions.Library;
1010
// ---------------------------------------------------------------------------------------------------------------------
1111
// Code
1212
// ---------------------------------------------------------------------------------------------------------------------
13-
public abstract class ContainsDiagnosticAssertCondition<T>(string expectedId): ExpectedValueAssertCondition<T, string>(expectedId) {
14-
15-
protected abstract ImmutableArray<Diagnostic> GetDiagnostics(T value);
16-
13+
public class ContainsDiagnosticAssertCondition<T>(Func<T, ImmutableArray<Diagnostic>> getDiagnosticsAction, string expectedId): ExpectedValueAssertCondition<T, string>(expectedId) {
1714
// -----------------------------------------------------------------------------------------------------------------
1815
// Methods
1916
// -----------------------------------------------------------------------------------------------------------------
@@ -22,9 +19,9 @@ protected override Task<AssertionResult> GetResult(T? actualValue, string? expec
2219
if (actualValue is null) return AssertionResult.Fail($"{nameof(T)} is null");
2320
if (expectedValue is null) return AssertionResult.Fail("Expected value is null");
2421

25-
ImmutableArray<Diagnostic> diagnostics = GetDiagnostics(actualValue);
22+
ImmutableArray<Diagnostic> diagnostics = getDiagnosticsAction(actualValue);
2623
if (!diagnostics.Any()) return FailWithMessage("No diagnostics");
2724
if (diagnostics.Any(d => d.Id == expectedValue)) return AssertionResult.Passed;
2825
return FailWithMessage("No diagnostic with Id");
2926
}
30-
}
27+
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ namespace CodeOfChaos.Testing.TUnit.Conditions.Library;
1010
// ---------------------------------------------------------------------------------------------------------------------
1111
// Code
1212
// ---------------------------------------------------------------------------------------------------------------------
13-
public abstract class ContainsDiagnosticsExclusivelyAssertCondition<T>(string[] expectedIds)
13+
public class ContainsDiagnosticsExclusivelyAssertCondition<T>(Func<T, ImmutableArray<Diagnostic>> getDiagnosticsAction, string[] expectedIds)
1414
: ExpectedValueAssertCondition<T, string[]>(expectedIds) {
15-
16-
protected abstract ImmutableArray<Diagnostic> GetDiagnostics(T value);
1715

1816
// -----------------------------------------------------------------------------------------------------------------
1917
// Methods
@@ -23,7 +21,7 @@ protected override Task<AssertionResult> GetResult(T? actualValue, string[]? exp
2321
if (actualValue is null) return AssertionResult.Fail($"{nameof(T)} is null");
2422
if (expectedValues is null) return AssertionResult.Fail("Expected value is null");
2523

26-
ImmutableArray<Diagnostic> diagnostics = GetDiagnostics(actualValue);
24+
ImmutableArray<Diagnostic> diagnostics = getDiagnosticsAction(actualValue);
2725
if (!diagnostics.Any() && expectedValues.Length == 0) return AssertionResult.Passed;
2826
if (!diagnostics.Any()) return FailWithMessage("No diagnostics");
2927
if (expectedValues.Length != diagnostics.Length) return FailWithMessage("Wrong number of diagnostics");

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ namespace CodeOfChaos.Testing.TUnit.Conditions.Library;
1010
// ---------------------------------------------------------------------------------------------------------------------
1111
// Code
1212
// ---------------------------------------------------------------------------------------------------------------------
13-
public abstract class DoesNotContainDiagnosticAssertCondition<T>(string expectedId)
13+
public class DoesNotContainDiagnosticAssertCondition<T>(Func<T, ImmutableArray<Diagnostic>> getDiagnosticsAction, string expectedId)
1414
: ExpectedValueAssertCondition<T, string>(expectedId) {
15-
16-
protected abstract ImmutableArray<Diagnostic> GetDiagnostics(T value);
1715

1816
// -----------------------------------------------------------------------------------------------------------------
1917
// Methods
@@ -24,7 +22,7 @@ protected override Task<AssertionResult> GetResult(T? actualValue, string? expec
2422
if (actualValue is null) return AssertionResult.Fail($"{nameof(T)} is null");
2523
if (expectedValue is null) return AssertionResult.Fail("Expected value is null");
2624

27-
ImmutableArray<Diagnostic> diagnostics = GetDiagnostics(actualValue);
25+
ImmutableArray<Diagnostic> diagnostics = getDiagnosticsAction(actualValue);
2826

2927
if (diagnostics.Any(d => d.Id == expectedValue)) return FailWithMessage("Diagnostic with Id");
3028

src/CodeOfChaos.Testing.TUnit/Extensions/TUnitExtensionsCompilation.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Imports
33
// ---------------------------------------------------------------------------------------------------------------------
44
using CodeOfChaos.Testing.TUnit.Conditions;
5+
using CodeOfChaos.Testing.TUnit.Conditions.Library;
56
using Microsoft.CodeAnalysis;
67
using System.Runtime.CompilerServices;
78
using TUnit.Assertions.AssertConditions;
@@ -17,21 +18,30 @@ namespace CodeOfChaos.Testing.TUnit;
1718
public static class TUnitExtensionsCompilation {
1819
public static InvokableValueAssertionBuilder<Compilation> ContainsDiagnostic(this IValueSource<Compilation> valueSource, string expectedId, [CallerArgumentExpression(nameof(expectedId))] string doNotPopulateThisValue1 = "") {
1920
return valueSource.RegisterAssertion(
20-
assertCondition: new CompilationContainsDiagnosticAssertCondition(expectedId),
21+
assertCondition: new ContainsDiagnosticAssertCondition<Compilation>(
22+
static compilation => compilation.GetDiagnostics(),
23+
expectedId
24+
),
2125
argumentExpressions: [doNotPopulateThisValue1]
2226
);
2327
}
2428

2529
public static InvokableValueAssertionBuilder<Compilation> DoesNotContainDiagnostic(this IValueSource<Compilation> valueSource, string expectedId, [CallerArgumentExpression(nameof(expectedId))] string doNotPopulateThisValue1 = "") {
2630
return valueSource.RegisterAssertion(
27-
assertCondition: new CompilationDoesNotContainDiagnosticAssertCondition(expectedId),
31+
assertCondition: new DoesNotContainDiagnosticAssertCondition<Compilation>(
32+
static compilation => compilation.GetDiagnostics(),
33+
expectedId
34+
),
2835
argumentExpressions: [doNotPopulateThisValue1]
2936
);
3037
}
3138

3239
public static InvokableValueAssertionBuilder<Compilation> ContainsDiagnosticsExclusively(this IValueSource<Compilation> valueSource, string[] expectedIds, [CallerArgumentExpression(nameof(expectedIds))] string doNotPopulateThisValue1 = "") {
3340
return valueSource.RegisterAssertion(
34-
assertCondition: new CompilationContainsDiagnosticsExclusivelyAssertCondition(expectedIds),
41+
assertCondition: new ContainsDiagnosticsExclusivelyAssertCondition<Compilation>(
42+
static compilation => compilation.GetDiagnostics(),
43+
expectedIds
44+
),
3545
argumentExpressions: [doNotPopulateThisValue1]
3646
);
3747
}

0 commit comments

Comments
 (0)