gpt4 book ai didi

c# - 使用 Microsoft.CodeAnalysis.CSharp.Testing.XUnit.AnalyzerVerifier 测试 Roslyn 分析器时如何加载程序集?

转载 作者:行者123 更新时间:2023-12-05 05:48:36 33 4
gpt4 key购买 nike

验证者有一个方法:

public static Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected)

是否可以加载类型为代码的程序集?

最佳答案

以下代码使它对我有用:

@测试类的用法:

using BindHandlerAnalyzerVerifier = ExtendedAnalyzerVerifier<AnalyzerNamespace>;

在测试中:

var diagnostic = BindHandlerAnalyzerVerifier.Diagnostic().WithSpan(4, 1, 12, 2).WithArguments("TestCommand");
await BindHandlerAnalyzerVerifier.VerifyAnalyzerAsync(input, Configure, diagnostic);

程序集配置:

private void Configure(CSharpAnalyzerTest<CommandCallsBindHandlerAnalyzer, XUnitVerifier> configuration)
{
var root = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var enumerateFiles = Directory.EnumerateFiles(root, "*.dll", SearchOption.TopDirectoryOnly).ToArray();
configuration.ReferenceAssemblies = ReferenceAssemblies.Net.Net50;
configuration.TestState.AdditionalReferences.AddRange(
new[]
{
MetadataReference.CreateFromFile(typeof(Command).GetTypeInfo().Assembly.Location)
});
}

工具类:

public class ExtendedAnalyzerVerifier<TAnalyzer> : ExtendedAnalyzerVerifier<TAnalyzer, CSharpAnalyzerTest<TAnalyzer, XUnitVerifier>, XUnitVerifier>
where TAnalyzer : DiagnosticAnalyzer, new(){}

public class ExtendedAnalyzerVerifier<TAnalyzer, TTest, TVerifier>
where TAnalyzer : DiagnosticAnalyzer, new()
where TTest : AnalyzerTest<TVerifier>, new()
where TVerifier : IVerifier, new()
{
public static DiagnosticResult Diagnostic()
{
var analyzer = new TAnalyzer();
try
{
return Diagnostic(analyzer.SupportedDiagnostics.Single());
}
catch (InvalidOperationException ex)
{
throw new InvalidOperationException(
$"'{nameof(Diagnostic)}()' can only be used when the analyzer has a single supported diagnostic. Use the '{nameof(Diagnostic)}(DiagnosticDescriptor)' overload to specify the descriptor from which to create the expected result.",
ex);
}
}

public static DiagnosticResult Diagnostic(string diagnosticId)
{
var analyzer = new TAnalyzer();
try
{
return Diagnostic(analyzer.SupportedDiagnostics.Single(i => i.Id == diagnosticId));
}
catch (InvalidOperationException ex)
{
throw new InvalidOperationException(
$"'{nameof(Diagnostic)}(string)' can only be used when the analyzer has a single supported diagnostic with the specified ID. Use the '{nameof(Diagnostic)}(DiagnosticDescriptor)' overload to specify the descriptor from which to create the expected result.",
ex);
}
}

public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) => new DiagnosticResult(descriptor);

public static Task VerifyAnalyzerAsync(string source, Action<TTest>? configure = default, params DiagnosticResult[] expected)
{
var test = new TTest
{
TestCode = source,
};

test.ExpectedDiagnostics.AddRange(expected);
configure?.Invoke(test);

return test.RunAsync(CancellationToken.None);
}
}

关于c# - 使用 Microsoft.CodeAnalysis.CSharp.Testing.XUnit.AnalyzerVerifier 测试 Roslyn 分析器时如何加载程序集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70786350/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com