// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CSharp.Testing; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Testing; namespace Product.Analyzers.Tests; /// /// Convenience methods for testing a C# analyzer and its code fix together. /// /// The analyzer under test. /// The code fix under test, or . public static class CSharpCodeFixVerifier where TAnalyzer : DiagnosticAnalyzer, new() where TCodeFix : CodeFixProvider, new() { public static DiagnosticResult Diagnostic() => CSharpCodeFixVerifier.Diagnostic(); public static DiagnosticResult Diagnostic(string diagnosticId) => CSharpCodeFixVerifier.Diagnostic(diagnosticId); public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) => new(descriptor); public static Task VerifyAnalyzerAsync( [StringSyntax("c#-test")] string source, params DiagnosticResult[] expected) { var test = new CSharpCodeFixTest { TestCode = source, }; test.ExpectedDiagnostics.AddRange(expected); return test.RunAsync(); } public static Task VerifyCodeFixAsync( [StringSyntax("c#-test")] string source, [StringSyntax("c#-test")] string fixedSource) => VerifyCodeFixAsync(source, DiagnosticResult.EmptyDiagnosticResults, fixedSource); public static Task VerifyCodeFixAsync( [StringSyntax("c#-test")] string source, DiagnosticResult expected, [StringSyntax("c#-test")] string fixedSource) => VerifyCodeFixAsync(source, [expected], fixedSource); public static Task VerifyCodeFixAsync( [StringSyntax("c#-test")] string source, DiagnosticResult[] expected, [StringSyntax("c#-test")] string fixedSource) { var test = new CSharpCodeFixTest { TestCode = source, FixedCode = fixedSource, }; test.ExpectedDiagnostics.AddRange(expected); return test.RunAsync(); } }