|
| 1 | +using System.Threading; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Microsoft.CodeAnalysis; |
| 4 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 5 | +using Microsoft.CodeAnalysis.CSharp.Testing; |
| 6 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 7 | +using Microsoft.CodeAnalysis.Testing; |
| 8 | + |
| 9 | +namespace Messerli.CodeStyle.Analyzers.Test |
| 10 | +{ |
| 11 | + public static class CSharpVerifier<TAnalyzer, TCodeFix, TVerifier> |
| 12 | + where TAnalyzer : DiagnosticAnalyzer, new() |
| 13 | + where TCodeFix : CodeFixProvider, new() |
| 14 | + where TVerifier : IVerifier, new() |
| 15 | + { |
| 16 | + public static DiagnosticResult Diagnostic() |
| 17 | + => AnalyzerVerifier<TAnalyzer, CSharpCodeFixTest<TAnalyzer, TCodeFix, TVerifier>, TVerifier>.Diagnostic(); |
| 18 | + |
| 19 | + public static DiagnosticResult Diagnostic(string diagnosticId) |
| 20 | + => AnalyzerVerifier<TAnalyzer, CSharpCodeFixTest<TAnalyzer, TCodeFix, TVerifier>, TVerifier>.Diagnostic(diagnosticId); |
| 21 | + |
| 22 | + public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) |
| 23 | + => AnalyzerVerifier<TAnalyzer, CSharpCodeFixTest<TAnalyzer, TCodeFix, TVerifier>, TVerifier>.Diagnostic(descriptor); |
| 24 | + |
| 25 | + public static Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected) |
| 26 | + => AnalyzerVerifier<TAnalyzer, CSharpCodeFixTest<TAnalyzer, TCodeFix, TVerifier>, TVerifier>.VerifyAnalyzerAsync(source, expected); |
| 27 | + |
| 28 | + public static Task VerifyCodeFixAsync(string source, string fixedSource) |
| 29 | + => VerifyCodeFixAsync(source, DiagnosticResult.EmptyDiagnosticResults, fixedSource); |
| 30 | + |
| 31 | + public static Task VerifyCodeFixAsync(string source, DiagnosticResult expected, string fixedSource) |
| 32 | + => VerifyCodeFixAsync(source, new[] { expected }, fixedSource); |
| 33 | + |
| 34 | + public static Task VerifyCodeFixAsync(string source, DiagnosticResult[] expected, string fixedSource) |
| 35 | + { |
| 36 | + var test = new CSharpCodeFixTest<TAnalyzer, TCodeFix, TVerifier> |
| 37 | + { |
| 38 | + TestCode = source, |
| 39 | + FixedCode = fixedSource, |
| 40 | + ReferenceAssemblies = ReferenceAssemblies.Default, |
| 41 | + }; |
| 42 | + |
| 43 | + test.ExpectedDiagnostics.AddRange(expected); |
| 44 | + return test.RunAsync(CancellationToken.None); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
0 commit comments