-
Notifications
You must be signed in to change notification settings - Fork 508
/
StyleCopDiagnosticVerifier`1.cs
98 lines (80 loc) · 3.83 KB
/
StyleCopDiagnosticVerifier`1.cs
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
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#nullable disable
namespace StyleCop.Analyzers.Test.Verifiers
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Testing.Verifiers;
using StyleCop.Analyzers.Lightup;
internal static class StyleCopDiagnosticVerifier<TAnalyzer>
where TAnalyzer : DiagnosticAnalyzer, new()
{
internal static DiagnosticResult Diagnostic()
=> CSharpCodeFixVerifier<TAnalyzer, EmptyCodeFixProvider, XUnitVerifier>.Diagnostic();
internal static DiagnosticResult Diagnostic(string diagnosticId)
=> CSharpCodeFixVerifier<TAnalyzer, EmptyCodeFixProvider, XUnitVerifier>.Diagnostic(diagnosticId);
internal static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor)
=> new DiagnosticResult(descriptor);
internal static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult expected, CancellationToken cancellationToken)
=> VerifyCSharpDiagnosticAsync(source, new[] { expected }, cancellationToken);
internal static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken)
{
var test = new CSharpTest
{
TestCode = source,
};
test.ExpectedDiagnostics.AddRange(expected);
return test.RunAsync(cancellationToken);
}
internal static Task VerifyCSharpDiagnosticAsync(LanguageVersion? languageVersion, string source, DiagnosticResult expected, CancellationToken cancellationToken)
=> VerifyCSharpDiagnosticAsync(languageVersion, source, settings: null, new[] { expected }, cancellationToken);
internal static Task VerifyCSharpDiagnosticAsync(LanguageVersion? languageVersion, string source, string settings, DiagnosticResult[] expected, CancellationToken cancellationToken)
{
var test = new CSharpTest(languageVersion)
{
TestCode = source,
Settings = settings,
};
test.ExpectedDiagnostics.AddRange(expected);
return test.RunAsync(cancellationToken);
}
internal class CSharpTest : StyleCopCodeFixVerifier<TAnalyzer, EmptyCodeFixProvider>.CSharpTest
{
public CSharpTest()
: this(languageVersion: null)
{
}
public CSharpTest(LanguageVersion? languageVersion)
{
this.LanguageVersion = languageVersion ?? this.GetDefaultLanguageVersion();
}
private LanguageVersion? LanguageVersion { get; }
protected override ParseOptions CreateParseOptions()
{
var parseOptions = base.CreateParseOptions();
if (this.LanguageVersion is { } languageVersion)
{
parseOptions = ((CSharpParseOptions)parseOptions).WithLanguageVersion(languageVersion);
}
return parseOptions;
}
// TODO: Remove when c# 11 is a supported language version
private LanguageVersion? GetDefaultLanguageVersion()
{
// Temporary fix since c# 11 is not yet the default language version
// in the c# 11 test project.
if (LightupHelpers.SupportsCSharp11)
{
return LanguageVersionEx.Preview;
}
return null;
}
}
}
}