mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-06 16:16:44 +08:00
test(cqrs): 添加 CQRS 处理器注册生成器测试
- 创建 MetadataReferenceTestBuilder 工具类用于构建内存元数据引用 - 实现 CreateFromSource 方法将源码编译为内存程序集并返回元数据引用 - 添加 GetRuntimeMetadataReferences 方法获取当前运行时可信平台程序集引用 - 创建 CqrsHandlerRegistryGeneratorTests 测试类验证 CQRS 处理器注册生成器功能 - 添加多种测试用例验证不同场景下的处理器注册行为 - 包含嵌套处理器、隐藏实现、数组类型参数、泛型类型定义等边界情况测试 - 实现混合直接注册和精确重建注册的测试验证 - 添加对外部基类保护类型处理器的支持测试 - 验证生成器优先处理隐藏处理器而不输出遗留回退标记的功能
This commit is contained in:
parent
31b6285bbd
commit
45bcffc6ee
@ -1,6 +1,5 @@
|
||||
using System.Collections.Immutable;
|
||||
using System.IO;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
|
||||
namespace GFramework.SourceGenerators.Tests.Core;
|
||||
|
||||
@ -9,6 +8,11 @@ namespace GFramework.SourceGenerators.Tests.Core;
|
||||
/// </summary>
|
||||
public static class MetadataReferenceTestBuilder
|
||||
{
|
||||
// Reuse the runtime reference set across generator tests to avoid reparsing TRUSTED_PLATFORM_ASSEMBLIES
|
||||
// for every in-memory compilation.
|
||||
private static readonly Lazy<ImmutableArray<MetadataReference>> CachedRuntimeReferences =
|
||||
new(CreateRuntimeMetadataReferences);
|
||||
|
||||
/// <summary>
|
||||
/// 将给定源码编译为内存程序集,并返回可供测试编译消费的元数据引用。
|
||||
/// </summary>
|
||||
@ -22,7 +26,7 @@ public static class MetadataReferenceTestBuilder
|
||||
params MetadataReference[] additionalReferences)
|
||||
{
|
||||
var syntaxTree = CSharpSyntaxTree.ParseText(source);
|
||||
var references = GetRuntimeMetadataReferences()
|
||||
var references = CachedRuntimeReferences.Value
|
||||
.Concat(additionalReferences)
|
||||
.ToImmutableArray();
|
||||
var compilation = CSharpCompilation.Create(
|
||||
@ -53,6 +57,11 @@ public static class MetadataReferenceTestBuilder
|
||||
/// </summary>
|
||||
/// <returns>当前运行时可信平台程序集对应的元数据引用。</returns>
|
||||
public static ImmutableArray<MetadataReference> GetRuntimeMetadataReferences()
|
||||
{
|
||||
return CachedRuntimeReferences.Value;
|
||||
}
|
||||
|
||||
private static ImmutableArray<MetadataReference> CreateRuntimeMetadataReferences()
|
||||
{
|
||||
var trustedPlatformAssemblies = ((string?)AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES"))?
|
||||
.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries)
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
using System.Reflection;
|
||||
using GFramework.SourceGenerators.Cqrs;
|
||||
using GFramework.SourceGenerators.Tests.Core;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
|
||||
namespace GFramework.SourceGenerators.Tests.Cqrs;
|
||||
|
||||
@ -1155,7 +1154,11 @@ public class CqrsHandlerRegistryGeneratorTests
|
||||
var compilationErrors = updatedCompilation.GetDiagnostics()
|
||||
.Where(static diagnostic => diagnostic.Severity == DiagnosticSeverity.Error)
|
||||
.ToArray();
|
||||
Assert.That(compilationErrors, Is.Empty, string.Join(Environment.NewLine, compilationErrors));
|
||||
Assert.That(
|
||||
compilationErrors,
|
||||
Is.Empty,
|
||||
() =>
|
||||
$"编译生成的代码时出现错误:{Environment.NewLine}{string.Join(Environment.NewLine, compilationErrors.Select(static diagnostic => diagnostic.ToString()))}");
|
||||
|
||||
var runResult = driver.GetRunResult();
|
||||
Assert.That(runResult.Results, Has.Length.EqualTo(1));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user