|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | 1 | using System.Collections.Immutable; |
4 | | -using System.Linq; |
5 | | -using System.Threading; |
6 | | -using System.Threading.Tasks; |
7 | 2 | using FluentAssertions; |
8 | | -using JetBrains.Annotations; |
9 | 3 | using Microsoft.CodeAnalysis; |
10 | 4 | using Microsoft.CodeAnalysis.Diagnostics; |
11 | 5 | using Microsoft.CodeAnalysis.Text; |
12 | 6 |
|
13 | | -namespace CSharpGuidelinesAnalyzer.Test.RoslynTestFramework |
| 7 | +namespace CSharpGuidelinesAnalyzer.Test.RoslynTestFramework; |
| 8 | + |
| 9 | +public abstract class AnalysisTestFixture |
14 | 10 | { |
15 | | - public abstract class AnalysisTestFixture |
16 | | - { |
17 | | - protected abstract string DiagnosticId { get; } |
| 11 | + protected abstract string DiagnosticId { get; } |
18 | 12 |
|
19 | | - protected abstract DiagnosticAnalyzer CreateAnalyzer(); |
| 13 | + protected abstract DiagnosticAnalyzer CreateAnalyzer(); |
20 | 14 |
|
21 | | - protected async Task AssertDiagnosticsAsync(AnalyzerTestContext context, params string[] messages) |
22 | | - { |
23 | | - FrameworkGuard.NotNull(context, nameof(context)); |
24 | | - FrameworkGuard.NotNull(messages, nameof(messages)); |
| 15 | + protected async Task AssertDiagnosticsAsync(AnalyzerTestContext context, params string[] messages) |
| 16 | + { |
| 17 | + FrameworkGuard.NotNull(context, nameof(context)); |
| 18 | + FrameworkGuard.NotNull(messages, nameof(messages)); |
25 | 19 |
|
26 | | - await RunDiagnosticsAsync(context, messages); |
27 | | - } |
| 20 | + await RunDiagnosticsAsync(context, messages); |
| 21 | + } |
28 | 22 |
|
29 | | - private async Task RunDiagnosticsAsync(AnalyzerTestContext context, params string[] messages) |
30 | | - { |
31 | | - AnalysisResult result = await GetAnalysisResultAsync(context, messages); |
| 23 | + private async Task RunDiagnosticsAsync(AnalyzerTestContext context, params string[] messages) |
| 24 | + { |
| 25 | + AnalysisResult result = await GetAnalysisResultAsync(context, messages); |
32 | 26 |
|
33 | | - VerifyDiagnosticCount(result); |
34 | | - VerifyDiagnostics(result); |
35 | | - } |
| 27 | + VerifyDiagnosticCount(result); |
| 28 | + VerifyDiagnostics(result); |
| 29 | + } |
36 | 30 |
|
37 | | - private async Task<AnalysisResult> GetAnalysisResultAsync(AnalyzerTestContext context, string[] messages) |
38 | | - { |
39 | | - var document = DocumentFactory.ToDocument(context.SourceCode, context); |
| 31 | + private async Task<AnalysisResult> GetAnalysisResultAsync(AnalyzerTestContext context, string[] messages) |
| 32 | + { |
| 33 | + var document = DocumentFactory.ToDocument(context.SourceCode, context); |
40 | 34 |
|
41 | | - IList<Diagnostic> diagnostics = await GetSortedAnalyzerDiagnosticsAsync(document, context); |
42 | | - return new AnalysisResult(diagnostics, context.SourceSpans, messages); |
43 | | - } |
| 35 | + IList<Diagnostic> diagnostics = await GetSortedAnalyzerDiagnosticsAsync(document, context); |
| 36 | + return new AnalysisResult(diagnostics, context.SourceSpans, messages); |
| 37 | + } |
44 | 38 |
|
45 | | - private async Task<IList<Diagnostic>> GetSortedAnalyzerDiagnosticsAsync(Document document, AnalyzerTestContext context) |
46 | | - { |
47 | | - var diagnostics = new List<Diagnostic>(); |
| 39 | + private async Task<IList<Diagnostic>> GetSortedAnalyzerDiagnosticsAsync(Document document, AnalyzerTestContext context) |
| 40 | + { |
| 41 | + var diagnostics = new List<Diagnostic>(); |
48 | 42 |
|
49 | | - await foreach (Diagnostic diagnostic in EnumerateDiagnosticsForDocumentAsync(document, context)) |
| 43 | + await foreach (Diagnostic diagnostic in EnumerateDiagnosticsForDocumentAsync(document, context)) |
| 44 | + { |
| 45 | + if (diagnostic.Id == DiagnosticId) |
50 | 46 | { |
51 | | - if (diagnostic.Id == DiagnosticId) |
52 | | - { |
53 | | - diagnostics.Add(diagnostic); |
54 | | - } |
| 47 | + diagnostics.Add(diagnostic); |
55 | 48 | } |
56 | | - |
57 | | - return diagnostics.OrderBy(diagnostic => diagnostic.Location.SourceSpan).ToImmutableArray(); |
58 | 49 | } |
59 | 50 |
|
60 | | - private async IAsyncEnumerable<Diagnostic> EnumerateDiagnosticsForDocumentAsync(Document document, AnalyzerTestContext context) |
61 | | - { |
62 | | - CompilationWithAnalyzers compilationWithAnalyzers = |
63 | | - await GetCompilationWithAnalyzersAsync(document, context.ValidationMode, context.Options); |
64 | | - |
65 | | - SyntaxTree? tree = await document.GetSyntaxTreeAsync(); |
| 51 | + return diagnostics.OrderBy(diagnostic => diagnostic.Location.SourceSpan).ToImmutableArray(); |
| 52 | + } |
66 | 53 |
|
67 | | - await foreach (Diagnostic diagnostic in EnumerateAnalyzerDiagnosticsAsync(compilationWithAnalyzers, tree!)) |
68 | | - { |
69 | | - yield return diagnostic; |
70 | | - } |
71 | | - } |
| 54 | + private async IAsyncEnumerable<Diagnostic> EnumerateDiagnosticsForDocumentAsync(Document document, AnalyzerTestContext context) |
| 55 | + { |
| 56 | + CompilationWithAnalyzers compilationWithAnalyzers = await GetCompilationWithAnalyzersAsync(document, context.ValidationMode, context.Options); |
| 57 | + SyntaxTree? tree = await document.GetSyntaxTreeAsync(); |
72 | 58 |
|
73 | | - private async Task<CompilationWithAnalyzers> GetCompilationWithAnalyzersAsync(Document document, TestValidationMode validationMode, AnalyzerOptions options) |
| 59 | + await foreach (Diagnostic diagnostic in EnumerateAnalyzerDiagnosticsAsync(compilationWithAnalyzers, tree!)) |
74 | 60 | { |
75 | | - DiagnosticAnalyzer analyzer = CreateAnalyzer(); |
| 61 | + yield return diagnostic; |
| 62 | + } |
| 63 | + } |
76 | 64 |
|
77 | | - Compilation? compilation = await document.Project.GetCompilationAsync(); |
78 | | - compilation = EnsureAnalyzerIsEnabled(analyzer, compilation!); |
| 65 | + private async Task<CompilationWithAnalyzers> GetCompilationWithAnalyzersAsync(Document document, TestValidationMode validationMode, AnalyzerOptions options) |
| 66 | + { |
| 67 | + DiagnosticAnalyzer analyzer = CreateAnalyzer(); |
79 | 68 |
|
80 | | - ImmutableArray<Diagnostic> compilerDiagnostics = compilation.GetDiagnostics(CancellationToken.None); |
| 69 | + Compilation? compilation = await document.Project.GetCompilationAsync(); |
| 70 | + compilation = EnsureAnalyzerIsEnabled(analyzer, compilation!); |
81 | 71 |
|
82 | | - if (validationMode != TestValidationMode.AllowCompileErrors) |
83 | | - { |
84 | | - ValidateCompileErrors(compilerDiagnostics); |
85 | | - } |
| 72 | + ImmutableArray<Diagnostic> compilerDiagnostics = compilation.GetDiagnostics(CancellationToken.None); |
86 | 73 |
|
87 | | - ImmutableArray<DiagnosticAnalyzer> analyzers = ImmutableArray.Create(analyzer); |
88 | | - return compilation.WithAnalyzers(analyzers, options); |
89 | | - } |
90 | | - |
91 | | - private static Compilation EnsureAnalyzerIsEnabled(DiagnosticAnalyzer analyzer, Compilation compilation) |
| 74 | + if (validationMode != TestValidationMode.AllowCompileErrors) |
92 | 75 | { |
93 | | - ImmutableDictionary<string, ReportDiagnostic> diagnosticOptions = compilation.Options.SpecificDiagnosticOptions; |
94 | | - |
95 | | - foreach (DiagnosticDescriptor descriptor in analyzer.SupportedDiagnostics) |
96 | | - { |
97 | | - if (!descriptor.IsEnabledByDefault) |
98 | | - { |
99 | | - diagnosticOptions = diagnosticOptions.Add(descriptor.Id, ReportDiagnostic.Warn); |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - CompilationOptions compilationWithSpecificOptions = compilation.Options.WithSpecificDiagnosticOptions(diagnosticOptions); |
104 | | - return compilation.WithOptions(compilationWithSpecificOptions); |
| 76 | + ValidateCompileErrors(compilerDiagnostics); |
105 | 77 | } |
106 | 78 |
|
107 | | - private void ValidateCompileErrors(ImmutableArray<Diagnostic> compilerDiagnostics) |
108 | | - { |
109 | | - Diagnostic[] compilerErrors = compilerDiagnostics.Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error) |
110 | | - .ToArray(); |
| 79 | + ImmutableArray<DiagnosticAnalyzer> analyzers = ImmutableArray.Create(analyzer); |
| 80 | + return compilation.WithAnalyzers(analyzers, options); |
| 81 | + } |
111 | 82 |
|
112 | | - compilerErrors.Should().BeEmpty("test should not have compile errors"); |
113 | | - } |
| 83 | + private static Compilation EnsureAnalyzerIsEnabled(DiagnosticAnalyzer analyzer, Compilation compilation) |
| 84 | + { |
| 85 | + ImmutableDictionary<string, ReportDiagnostic> diagnosticOptions = compilation.Options.SpecificDiagnosticOptions; |
114 | 86 |
|
115 | | - private static async IAsyncEnumerable<Diagnostic> EnumerateAnalyzerDiagnosticsAsync(CompilationWithAnalyzers compilationWithAnalyzers, SyntaxTree tree) |
| 87 | + foreach (DiagnosticDescriptor descriptor in analyzer.SupportedDiagnostics) |
116 | 88 | { |
117 | | - foreach (Diagnostic diagnostic in await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync()) |
| 89 | + if (!descriptor.IsEnabledByDefault) |
118 | 90 | { |
119 | | - ThrowForCrashingAnalyzer(diagnostic); |
120 | | - |
121 | | - if (diagnostic.Location.IsInSource) |
122 | | - { |
123 | | - diagnostic.Location.SourceTree.Should().Be(tree); |
124 | | - } |
125 | | - |
126 | | - yield return diagnostic; |
| 91 | + diagnosticOptions = diagnosticOptions.Add(descriptor.Id, ReportDiagnostic.Warn); |
127 | 92 | } |
128 | 93 | } |
129 | 94 |
|
130 | | - private static void ThrowForCrashingAnalyzer(Diagnostic diagnostic) |
| 95 | + CompilationOptions compilationWithSpecificOptions = compilation.Options.WithSpecificDiagnosticOptions(diagnosticOptions); |
| 96 | + return compilation.WithOptions(compilationWithSpecificOptions); |
| 97 | + } |
| 98 | + |
| 99 | + private void ValidateCompileErrors(ImmutableArray<Diagnostic> compilerDiagnostics) |
| 100 | + { |
| 101 | + Diagnostic[] compilerErrors = compilerDiagnostics.Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error).ToArray(); |
| 102 | + compilerErrors.Should().BeEmpty("test should not have compile errors"); |
| 103 | + } |
| 104 | + |
| 105 | + private static async IAsyncEnumerable<Diagnostic> EnumerateAnalyzerDiagnosticsAsync(CompilationWithAnalyzers compilationWithAnalyzers, SyntaxTree tree) |
| 106 | + { |
| 107 | + foreach (Diagnostic diagnostic in await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync()) |
131 | 108 | { |
132 | | - if (diagnostic.Id == "AD0001") |
| 109 | + ThrowForCrashingAnalyzer(diagnostic); |
| 110 | + |
| 111 | + if (diagnostic.Location.IsInSource) |
133 | 112 | { |
134 | | - string message = diagnostic.Descriptor.Description.ToString(); |
135 | | - throw new Exception(message); |
| 113 | + diagnostic.Location.SourceTree.Should().Be(tree); |
136 | 114 | } |
137 | | - } |
138 | 115 |
|
139 | | - private static void VerifyDiagnosticCount(AnalysisResult result) |
140 | | - { |
141 | | - result.DiagnosticsWithLocation.Should().HaveSameCount(result.SpansExpected); |
142 | | - result.Diagnostics.Should().HaveSameCount(result.MessagesExpected); |
| 116 | + yield return diagnostic; |
143 | 117 | } |
| 118 | + } |
144 | 119 |
|
145 | | - private static void VerifyDiagnostics(AnalysisResult result) |
| 120 | + private static void ThrowForCrashingAnalyzer(Diagnostic diagnostic) |
| 121 | + { |
| 122 | + if (diagnostic.Id == "AD0001") |
146 | 123 | { |
147 | | - VerifyDiagnosticMessages(result); |
148 | | - VerifyDiagnosticLocations(result); |
| 124 | + string message = diagnostic.Descriptor.Description.ToString(); |
| 125 | + throw new Exception(message); |
149 | 126 | } |
| 127 | + } |
150 | 128 |
|
151 | | - private static void VerifyDiagnosticMessages(AnalysisResult result) |
152 | | - { |
153 | | - int messageIndex = 0; |
| 129 | + private static void VerifyDiagnosticCount(AnalysisResult result) |
| 130 | + { |
| 131 | + result.DiagnosticsWithLocation.Should().HaveSameCount(result.SpansExpected); |
| 132 | + result.Diagnostics.Should().HaveSameCount(result.MessagesExpected); |
| 133 | + } |
154 | 134 |
|
155 | | - foreach (Diagnostic diagnostic in result.Diagnostics) |
156 | | - { |
157 | | - string messageActual = diagnostic.GetMessage(); |
158 | | - string messageExpected = result.MessagesExpected[messageIndex]; |
159 | | - messageActual.Should().Be(messageExpected); |
| 135 | + private static void VerifyDiagnostics(AnalysisResult result) |
| 136 | + { |
| 137 | + VerifyDiagnosticMessages(result); |
| 138 | + VerifyDiagnosticLocations(result); |
| 139 | + } |
160 | 140 |
|
161 | | - messageIndex++; |
162 | | - } |
163 | | - } |
| 141 | + private static void VerifyDiagnosticMessages(AnalysisResult result) |
| 142 | + { |
| 143 | + int messageIndex = 0; |
164 | 144 |
|
165 | | - private static void VerifyDiagnosticLocations(AnalysisResult result) |
| 145 | + foreach (Diagnostic diagnostic in result.Diagnostics) |
166 | 146 | { |
167 | | - int spanIndex = 0; |
| 147 | + string messageActual = diagnostic.GetMessage(); |
| 148 | + string messageExpected = result.MessagesExpected[messageIndex]; |
| 149 | + messageActual.Should().Be(messageExpected); |
168 | 150 |
|
169 | | - foreach (Diagnostic diagnostic in result.DiagnosticsWithLocation) |
170 | | - { |
171 | | - TextSpan locationSpanExpected = result.SpansExpected[spanIndex]; |
172 | | - diagnostic.Location.SourceSpan.Should().Be(locationSpanExpected); |
173 | | - |
174 | | - spanIndex++; |
175 | | - } |
| 151 | + messageIndex++; |
176 | 152 | } |
| 153 | + } |
177 | 154 |
|
178 | | - private sealed class AnalysisResult |
| 155 | + private static void VerifyDiagnosticLocations(AnalysisResult result) |
| 156 | + { |
| 157 | + int spanIndex = 0; |
| 158 | + |
| 159 | + foreach (Diagnostic diagnostic in result.DiagnosticsWithLocation) |
179 | 160 | { |
180 | | - public IList<Diagnostic> Diagnostics { get; } |
| 161 | + TextSpan locationSpanExpected = result.SpansExpected[spanIndex]; |
| 162 | + diagnostic.Location.SourceSpan.Should().Be(locationSpanExpected); |
181 | 163 |
|
182 | | - public IList<Diagnostic> DiagnosticsWithLocation => Diagnostics.Where(diagnostic => diagnostic.Location.IsInSource).ToArray(); |
| 164 | + spanIndex++; |
| 165 | + } |
| 166 | + } |
183 | 167 |
|
184 | | - public IList<TextSpan> SpansExpected { get; } |
| 168 | + private sealed class AnalysisResult |
| 169 | + { |
| 170 | + public IList<Diagnostic> Diagnostics { get; } |
185 | 171 |
|
186 | | - public IList<string> MessagesExpected { get; } |
| 172 | + public IList<Diagnostic> DiagnosticsWithLocation => Diagnostics.Where(diagnostic => diagnostic.Location.IsInSource).ToArray(); |
187 | 173 |
|
188 | | - public AnalysisResult(IList<Diagnostic> diagnostics, IList<TextSpan> spansExpected, |
189 | | - IList<string> messagesExpected) |
190 | | - { |
191 | | - FrameworkGuard.NotNull(diagnostics, nameof(diagnostics)); |
192 | | - FrameworkGuard.NotNull(spansExpected, nameof(spansExpected)); |
193 | | - FrameworkGuard.NotNull(messagesExpected, nameof(messagesExpected)); |
| 174 | + public IList<TextSpan> SpansExpected { get; } |
194 | 175 |
|
195 | | - Diagnostics = diagnostics; |
196 | | - SpansExpected = spansExpected; |
197 | | - MessagesExpected = messagesExpected; |
198 | | - } |
| 176 | + public IList<string> MessagesExpected { get; } |
| 177 | + |
| 178 | + public AnalysisResult(IList<Diagnostic> diagnostics, IList<TextSpan> spansExpected, IList<string> messagesExpected) |
| 179 | + { |
| 180 | + FrameworkGuard.NotNull(diagnostics, nameof(diagnostics)); |
| 181 | + FrameworkGuard.NotNull(spansExpected, nameof(spansExpected)); |
| 182 | + FrameworkGuard.NotNull(messagesExpected, nameof(messagesExpected)); |
| 183 | + |
| 184 | + Diagnostics = diagnostics; |
| 185 | + SpansExpected = spansExpected; |
| 186 | + MessagesExpected = messagesExpected; |
199 | 187 | } |
200 | 188 | } |
201 | 189 | } |
0 commit comments