mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-07 00:39:00 +08:00
38 lines
1.1 KiB
C#
38 lines
1.1 KiB
C#
// Copyright (c) 2025-2026 GeWuYou
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
using Microsoft.CodeAnalysis.Diagnostics;
|
|
|
|
namespace GFramework.SourceGenerators.Tests.Core;
|
|
|
|
/// <summary>
|
|
/// 提供 Roslyn 分析器测试的通用运行入口。
|
|
/// </summary>
|
|
/// <typeparam name="TAnalyzer">要验证的分析器类型。</typeparam>
|
|
public static class AnalyzerTestDriver<TAnalyzer>
|
|
where TAnalyzer : DiagnosticAnalyzer, new()
|
|
{
|
|
/// <summary>
|
|
/// 运行分析器测试并断言期望诊断。
|
|
/// </summary>
|
|
/// <param name="source">测试输入源码。</param>
|
|
/// <param name="diagnostics">期望诊断集合。</param>
|
|
/// <returns>异步测试任务。</returns>
|
|
public static Task RunAsync(
|
|
string source,
|
|
params DiagnosticResult[] diagnostics)
|
|
{
|
|
var test = new CSharpAnalyzerTest<TAnalyzer, DefaultVerifier>
|
|
{
|
|
TestState =
|
|
{
|
|
Sources = { source }
|
|
},
|
|
DisabledDiagnostics = { "GF_Common_Trace_001" }
|
|
};
|
|
|
|
test.ExpectedDiagnostics.AddRange(diagnostics);
|
|
return test.RunAsync();
|
|
}
|
|
}
|