Files
awesome-copilot/skills/roslyn-analyzers/references/CSharpCodeFixVerifier.cs
T
github-actions[bot] fbee16ba71 chore: publish from main
2026-08-26 00:23:08 +00:00

71 lines
2.5 KiB
C#

// 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;
/// <summary>
/// Convenience methods for testing a C# analyzer and its code fix together.
/// </summary>
/// <typeparam name="TAnalyzer">The analyzer under test.</typeparam>
/// <typeparam name="TCodeFix">The code fix under test, or <see cref="EmptyCodeFixProvider"/>.</typeparam>
public static class CSharpCodeFixVerifier<TAnalyzer, TCodeFix>
where TAnalyzer : DiagnosticAnalyzer, new()
where TCodeFix : CodeFixProvider, new()
{
public static DiagnosticResult Diagnostic()
=> CSharpCodeFixVerifier<TAnalyzer, TCodeFix, DefaultVerifier>.Diagnostic();
public static DiagnosticResult Diagnostic(string diagnosticId)
=> CSharpCodeFixVerifier<TAnalyzer, TCodeFix, DefaultVerifier>.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<TAnalyzer, TCodeFix, DefaultVerifier>
{
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<TAnalyzer, TCodeFix, DefaultVerifier>
{
TestCode = source,
FixedCode = fixedSource,
};
test.ExpectedDiagnostics.AddRange(expected);
return test.RunAsync();
}
}