-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: Introduce DiagnosticContext to replace Parser mutable state #57
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
Merged
+208
−50
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
141 changes: 141 additions & 0 deletions
141
src/CompositeKey.SourceGeneration.UnitTests/DiagnosticContextTests.cs
This file contains hidden or 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,141 @@ | ||
| using CompositeKey.SourceGeneration.Core; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.Text; | ||
|
|
||
| namespace CompositeKey.SourceGeneration.UnitTests; | ||
|
|
||
| public static class DiagnosticContextTests | ||
| { | ||
| #pragma warning disable RS2008 // Enable analyzer release tracking | ||
| private static readonly DiagnosticDescriptor TestDescriptor = new( | ||
| id: "TEST0001", | ||
| title: "Test Diagnostic", | ||
| messageFormat: "Test message: {0}", | ||
| category: "Test", | ||
| defaultSeverity: DiagnosticSeverity.Error, | ||
| isEnabledByDefault: true); | ||
| #pragma warning restore RS2008 | ||
|
|
||
| private static Compilation CreateTestCompilation() | ||
| { | ||
| return CompilationHelper.CreateCompilation("public class Placeholder { }"); | ||
| } | ||
|
|
||
| private static Location CreateLocationInCompilation(Compilation compilation) | ||
| { | ||
| var tree = compilation.SyntaxTrees.First(); | ||
| return Location.Create(tree, TextSpan.FromBounds(0, 1)); | ||
| } | ||
|
|
||
| private static Location CreateLocationOutsideCompilation() | ||
| { | ||
| var externalTree = CSharpSyntaxTree.ParseText("public class External { }"); | ||
| return Location.Create(externalTree, TextSpan.FromBounds(0, 1)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ReportDiagnostic_WithValidLocation_UsesProvidedLocation() | ||
| { | ||
| var compilation = CreateTestCompilation(); | ||
| var context = new DiagnosticContext(compilation); | ||
| var currentLocation = CreateLocationInCompilation(compilation); | ||
| context.CurrentLocation = currentLocation; | ||
|
|
||
| var providedLocation = CreateLocationInCompilation(compilation); | ||
| context.ReportDiagnostic(TestDescriptor, providedLocation, "arg1"); | ||
|
|
||
| context.Diagnostics.Count.ShouldBe(1); | ||
| context.Diagnostics[0].Location.ShouldNotBeNull(); | ||
| context.Diagnostics[0].Location!.SourceSpan.ShouldBe(providedLocation.SourceSpan); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ReportDiagnostic_WithNullLocation_FallsBackToCurrentLocation() | ||
| { | ||
| var compilation = CreateTestCompilation(); | ||
| var context = new DiagnosticContext(compilation); | ||
| var currentLocation = CreateLocationInCompilation(compilation); | ||
| context.CurrentLocation = currentLocation; | ||
|
|
||
| context.ReportDiagnostic(TestDescriptor, null, "arg1"); | ||
|
|
||
| context.Diagnostics.Count.ShouldBe(1); | ||
| context.Diagnostics[0].Location.ShouldNotBeNull(); | ||
| context.Diagnostics[0].Location!.SourceSpan.ShouldBe(currentLocation.SourceSpan); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ReportDiagnostic_WithLocationFromUnknownTree_FallsBackToCurrentLocation() | ||
| { | ||
| var compilation = CreateTestCompilation(); | ||
| var context = new DiagnosticContext(compilation); | ||
| var currentLocation = CreateLocationInCompilation(compilation); | ||
| context.CurrentLocation = currentLocation; | ||
|
|
||
| var externalLocation = CreateLocationOutsideCompilation(); | ||
| context.ReportDiagnostic(TestDescriptor, externalLocation, "arg1"); | ||
|
|
||
| context.Diagnostics.Count.ShouldBe(1); | ||
| context.Diagnostics[0].Location.ShouldNotBeNull(); | ||
| context.Diagnostics[0].Location!.SourceSpan.ShouldBe(currentLocation.SourceSpan); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ReportDiagnostic_AccumulatesMultipleDiagnostics() | ||
| { | ||
| var compilation = CreateTestCompilation(); | ||
| var context = new DiagnosticContext(compilation); | ||
| context.CurrentLocation = CreateLocationInCompilation(compilation); | ||
|
|
||
| context.ReportDiagnostic(TestDescriptor, null, "first"); | ||
| context.ReportDiagnostic(TestDescriptor, null, "second"); | ||
| context.ReportDiagnostic(TestDescriptor, null, "third"); | ||
|
|
||
| context.Diagnostics.Count.ShouldBe(3); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ReportDiagnostic_PassesMessageArgsCorrectly() | ||
| { | ||
| var compilation = CreateTestCompilation(); | ||
| var context = new DiagnosticContext(compilation); | ||
| context.CurrentLocation = CreateLocationInCompilation(compilation); | ||
|
|
||
| context.ReportDiagnostic(TestDescriptor, null, "test-value"); | ||
|
|
||
| context.Diagnostics.Count.ShouldBe(1); | ||
| var diagnostic = context.Diagnostics[0].CreateDiagnostic(); | ||
| diagnostic.GetMessage().ShouldBe("Test message: test-value"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void Diagnostics_IsEmptyByDefault() | ||
| { | ||
| var compilation = CreateTestCompilation(); | ||
| var context = new DiagnosticContext(compilation); | ||
|
|
||
| context.Diagnostics.Count.ShouldBe(0); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void CurrentLocation_IsNullByDefault() | ||
| { | ||
| var compilation = CreateTestCompilation(); | ||
| var context = new DiagnosticContext(compilation); | ||
|
|
||
| context.CurrentLocation.ShouldBeNull(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void CurrentLocation_CanBeSet() | ||
| { | ||
| var compilation = CreateTestCompilation(); | ||
| var context = new DiagnosticContext(compilation); | ||
| var location = CreateLocationInCompilation(compilation); | ||
|
|
||
| context.CurrentLocation = location; | ||
|
|
||
| context.CurrentLocation.ShouldBe(location); | ||
| } | ||
| } | ||
This file contains hidden or 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,27 @@ | ||
| using System.Diagnostics; | ||
| using CompositeKey.SourceGeneration.Core; | ||
| using Microsoft.CodeAnalysis; | ||
|
|
||
| namespace CompositeKey.SourceGeneration; | ||
|
|
||
| internal sealed class DiagnosticContext(Compilation compilation) | ||
| { | ||
| private readonly Compilation _compilation = compilation; | ||
| private readonly List<DiagnosticInfo> _diagnostics = []; | ||
|
|
||
| public Location? CurrentLocation { get; set; } | ||
|
|
||
| public IReadOnlyList<DiagnosticInfo> Diagnostics => _diagnostics; | ||
|
|
||
| public void ReportDiagnostic(DiagnosticDescriptor descriptor, Location? location, params object?[]? messageArgs) | ||
| { | ||
| Debug.Assert(CurrentLocation != null); | ||
|
|
||
| if (location is null || (location.SourceTree is not null && !_compilation.ContainsSyntaxTree(location.SourceTree))) | ||
| location = CurrentLocation; | ||
DrBarnabus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| _diagnostics.Add(DiagnosticInfo.Create(descriptor, location, messageArgs)); | ||
| } | ||
|
|
||
| public ImmutableEquatableArray<DiagnosticInfo> ToImmutableDiagnostics() => _diagnostics.ToImmutableEquatableArray(); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.