Skip to content

Commit 9e2798c

Browse files
committed
Use new language features and reformat code in test project
1 parent 8f9a9dc commit 9e2798c

File tree

73 files changed

+28394
-28614
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+28394
-28614
lines changed
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
using System.Threading.Tasks;
21
using CSharpGuidelinesAnalyzer.Test.RoslynTestFramework;
3-
using JetBrains.Annotations;
42

5-
namespace CSharpGuidelinesAnalyzer.Test
3+
namespace CSharpGuidelinesAnalyzer.Test;
4+
5+
public abstract class CSharpGuidelinesAnalysisTestFixture : AnalysisTestFixture
66
{
7-
public abstract class CSharpGuidelinesAnalysisTestFixture : AnalysisTestFixture
7+
private protected async Task VerifyGuidelineDiagnosticAsync(ParsedSourceCode source, params string[] messages)
88
{
9-
private protected async Task VerifyGuidelineDiagnosticAsync(ParsedSourceCode source, params string[] messages)
10-
{
11-
Guard.NotNull(source, nameof(source));
12-
Guard.NotNull(messages, nameof(messages));
9+
Guard.NotNull(source, nameof(source));
10+
Guard.NotNull(messages, nameof(messages));
1311

14-
await AssertDiagnosticsAsync(source.TestContext, messages);
15-
}
12+
await AssertDiagnosticsAsync(source.TestContext, messages);
1613
}
1714
}
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
using CSharpGuidelinesAnalyzer.Test.RoslynTestFramework;
2-
using JetBrains.Annotations;
32

4-
namespace CSharpGuidelinesAnalyzer.Test
3+
namespace CSharpGuidelinesAnalyzer.Test;
4+
5+
internal sealed class ParsedSourceCode
56
{
6-
internal sealed class ParsedSourceCode
7-
{
8-
public AnalyzerTestContext TestContext { get; }
7+
public AnalyzerTestContext TestContext { get; }
98

10-
public ParsedSourceCode(string sourceText, AnalyzerTestContext testContext)
11-
{
12-
Guard.NotNull(sourceText, nameof(sourceText));
13-
Guard.NotNull(testContext, nameof(testContext));
9+
public ParsedSourceCode(string sourceText, AnalyzerTestContext testContext)
10+
{
11+
Guard.NotNull(sourceText, nameof(sourceText));
12+
Guard.NotNull(testContext, nameof(testContext));
1413

15-
var document = new FixableDocument(sourceText);
16-
TestContext = testContext.WithCode(document.SourceText, document.SourceSpans);
17-
}
14+
var document = new FixableDocument(sourceText);
15+
TestContext = testContext.WithCode(document.SourceText, document.SourceSpans);
1816
}
1917
}
Lines changed: 127 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,201 +1,189 @@
1-
using System;
2-
using System.Collections.Generic;
31
using System.Collections.Immutable;
4-
using System.Linq;
5-
using System.Threading;
6-
using System.Threading.Tasks;
72
using FluentAssertions;
8-
using JetBrains.Annotations;
93
using Microsoft.CodeAnalysis;
104
using Microsoft.CodeAnalysis.Diagnostics;
115
using Microsoft.CodeAnalysis.Text;
126

13-
namespace CSharpGuidelinesAnalyzer.Test.RoslynTestFramework
7+
namespace CSharpGuidelinesAnalyzer.Test.RoslynTestFramework;
8+
9+
public abstract class AnalysisTestFixture
1410
{
15-
public abstract class AnalysisTestFixture
16-
{
17-
protected abstract string DiagnosticId { get; }
11+
protected abstract string DiagnosticId { get; }
1812

19-
protected abstract DiagnosticAnalyzer CreateAnalyzer();
13+
protected abstract DiagnosticAnalyzer CreateAnalyzer();
2014

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));
2519

26-
await RunDiagnosticsAsync(context, messages);
27-
}
20+
await RunDiagnosticsAsync(context, messages);
21+
}
2822

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);
3226

33-
VerifyDiagnosticCount(result);
34-
VerifyDiagnostics(result);
35-
}
27+
VerifyDiagnosticCount(result);
28+
VerifyDiagnostics(result);
29+
}
3630

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);
4034

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+
}
4438

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>();
4842

49-
await foreach (Diagnostic diagnostic in EnumerateDiagnosticsForDocumentAsync(document, context))
43+
await foreach (Diagnostic diagnostic in EnumerateDiagnosticsForDocumentAsync(document, context))
44+
{
45+
if (diagnostic.Id == DiagnosticId)
5046
{
51-
if (diagnostic.Id == DiagnosticId)
52-
{
53-
diagnostics.Add(diagnostic);
54-
}
47+
diagnostics.Add(diagnostic);
5548
}
56-
57-
return diagnostics.OrderBy(diagnostic => diagnostic.Location.SourceSpan).ToImmutableArray();
5849
}
5950

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+
}
6653

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();
7258

73-
private async Task<CompilationWithAnalyzers> GetCompilationWithAnalyzersAsync(Document document, TestValidationMode validationMode, AnalyzerOptions options)
59+
await foreach (Diagnostic diagnostic in EnumerateAnalyzerDiagnosticsAsync(compilationWithAnalyzers, tree!))
7460
{
75-
DiagnosticAnalyzer analyzer = CreateAnalyzer();
61+
yield return diagnostic;
62+
}
63+
}
7664

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();
7968

80-
ImmutableArray<Diagnostic> compilerDiagnostics = compilation.GetDiagnostics(CancellationToken.None);
69+
Compilation? compilation = await document.Project.GetCompilationAsync();
70+
compilation = EnsureAnalyzerIsEnabled(analyzer, compilation!);
8171

82-
if (validationMode != TestValidationMode.AllowCompileErrors)
83-
{
84-
ValidateCompileErrors(compilerDiagnostics);
85-
}
72+
ImmutableArray<Diagnostic> compilerDiagnostics = compilation.GetDiagnostics(CancellationToken.None);
8673

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)
9275
{
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);
10577
}
10678

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+
}
11182

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;
11486

115-
private static async IAsyncEnumerable<Diagnostic> EnumerateAnalyzerDiagnosticsAsync(CompilationWithAnalyzers compilationWithAnalyzers, SyntaxTree tree)
87+
foreach (DiagnosticDescriptor descriptor in analyzer.SupportedDiagnostics)
11688
{
117-
foreach (Diagnostic diagnostic in await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync())
89+
if (!descriptor.IsEnabledByDefault)
11890
{
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);
12792
}
12893
}
12994

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())
131108
{
132-
if (diagnostic.Id == "AD0001")
109+
ThrowForCrashingAnalyzer(diagnostic);
110+
111+
if (diagnostic.Location.IsInSource)
133112
{
134-
string message = diagnostic.Descriptor.Description.ToString();
135-
throw new Exception(message);
113+
diagnostic.Location.SourceTree.Should().Be(tree);
136114
}
137-
}
138115

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;
143117
}
118+
}
144119

145-
private static void VerifyDiagnostics(AnalysisResult result)
120+
private static void ThrowForCrashingAnalyzer(Diagnostic diagnostic)
121+
{
122+
if (diagnostic.Id == "AD0001")
146123
{
147-
VerifyDiagnosticMessages(result);
148-
VerifyDiagnosticLocations(result);
124+
string message = diagnostic.Descriptor.Description.ToString();
125+
throw new Exception(message);
149126
}
127+
}
150128

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+
}
154134

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+
}
160140

161-
messageIndex++;
162-
}
163-
}
141+
private static void VerifyDiagnosticMessages(AnalysisResult result)
142+
{
143+
int messageIndex = 0;
164144

165-
private static void VerifyDiagnosticLocations(AnalysisResult result)
145+
foreach (Diagnostic diagnostic in result.Diagnostics)
166146
{
167-
int spanIndex = 0;
147+
string messageActual = diagnostic.GetMessage();
148+
string messageExpected = result.MessagesExpected[messageIndex];
149+
messageActual.Should().Be(messageExpected);
168150

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++;
176152
}
153+
}
177154

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)
179160
{
180-
public IList<Diagnostic> Diagnostics { get; }
161+
TextSpan locationSpanExpected = result.SpansExpected[spanIndex];
162+
diagnostic.Location.SourceSpan.Should().Be(locationSpanExpected);
181163

182-
public IList<Diagnostic> DiagnosticsWithLocation => Diagnostics.Where(diagnostic => diagnostic.Location.IsInSource).ToArray();
164+
spanIndex++;
165+
}
166+
}
183167

184-
public IList<TextSpan> SpansExpected { get; }
168+
private sealed class AnalysisResult
169+
{
170+
public IList<Diagnostic> Diagnostics { get; }
185171

186-
public IList<string> MessagesExpected { get; }
172+
public IList<Diagnostic> DiagnosticsWithLocation => Diagnostics.Where(diagnostic => diagnostic.Location.IsInSource).ToArray();
187173

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; }
194175

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;
199187
}
200188
}
201189
}

0 commit comments

Comments
 (0)