-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalyzerTestBase.cs
More file actions
114 lines (99 loc) · 3.45 KB
/
Copy pathAnalyzerTestBase.cs
File metadata and controls
114 lines (99 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Microsoft.CodeAnalysis.Diagnostics;
namespace Purview.SourceGeneratorFramework.Testing;
/// <summary>
/// Framework-agnostic base class for diagnostic analyzer tests.
/// </summary>
/// <typeparam name="TAnalyzer">The type of diagnostic analyzer.</typeparam>
public abstract class AnalyzerTestBase<TAnalyzer> : AnalyzerTestBase<TAnalyzer, AnalyzerTestOptions>
where TAnalyzer : DiagnosticAnalyzer, new();
/// <summary>
/// Framework-agnostic base class for diagnostic analyzer tests.
/// </summary>
/// <typeparam name="TAnalyzer">The type of diagnostic analyzer.</typeparam>
/// <typeparam name="TOptions">The type of test options.</typeparam>
public abstract class AnalyzerTestBase<TAnalyzer, TOptions>
where TAnalyzer : DiagnosticAnalyzer, new()
where TOptions : AnalyzerTestOptions, new()
{
readonly DiagnosticAnalyzerTestRunner<TAnalyzer> _runner = new();
/// <summary>
/// Runs the analyzer against the supplied source.
/// </summary>
protected Task<AnalyzerTestResult> AnalyzeAsync(string source, CancellationToken cancellationToken = default) =>
AnalyzeAsync(source, null!, cancellationToken);
/// <summary>
/// Runs the analyzer against the supplied sources.
/// </summary>
protected Task<AnalyzerTestResult> AnalyzeAsync(
IEnumerable<string> sources,
CancellationToken cancellationToken = default
) => AnalyzeAsync(sources, null!, cancellationToken);
/// <summary>
/// Runs the analyzer against the supplied source and options.
/// </summary>
protected Task<AnalyzerTestResult> AnalyzeAsync(
string source,
TOptions options,
CancellationToken cancellationToken = default
) => AnalyzeAsync([source], options, cancellationToken);
/// <summary>
/// Runs the analyzer against the supplied sources and options.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Performance",
"CA1849:Call async methods when in an async method"
)]
protected async Task<AnalyzerTestResult> AnalyzeAsync(
IEnumerable<string> sources,
TOptions options,
CancellationToken cancellationToken = default
)
{
if (sources is null)
throw new ArgumentNullException(nameof(sources));
options ??= new();
options = OnBeforeRun(sources, options, cancellationToken);
options = await OnBeforeRunAsync(sources, options, cancellationToken);
var result = await _runner.RunAsync(sources, options, cancellationToken);
OnAfterRun(result, sources, options, cancellationToken);
await OnAfterRunAsync(result, sources, options, cancellationToken);
return result;
}
/// <summary>
/// Called before the analyzer is run.
/// </summary>
protected virtual TOptions OnBeforeRun(
IEnumerable<string> sources,
TOptions options,
CancellationToken cancellationToken
) => options;
/// <summary>
/// Called asynchronously before the analyzer is run.
/// </summary>
protected virtual Task<TOptions> OnBeforeRunAsync(
IEnumerable<string> sources,
TOptions options,
CancellationToken cancellationToken
) => Task.FromResult(options);
/// <summary>
/// Called after the analyzer is run.
/// </summary>
protected virtual void OnAfterRun(
AnalyzerTestResult result,
IEnumerable<string> sources,
TOptions options,
CancellationToken cancellationToken
)
{
// No-op by default.
}
/// <summary>
/// Called asynchronously after the analyzer is run.
/// </summary>
protected virtual Task OnAfterRunAsync(
AnalyzerTestResult result,
IEnumerable<string> sources,
TOptions options,
CancellationToken cancellationToken
) => Task.CompletedTask;
}