GFramework/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorSnapshotTests.cs
GeWuYou 3ecce110ed feat(architecture): 添加架构上下文提供者和相关测试
- 新增 IArchitectureContextProvider 接口定义
- 实现 GameContextProvider 类提供上下文获取功能
- 添加 GameContext 静态类用于获取架构上下文
- 创建 ContextProviderTests 测试上下文提供者功能
- 实现 RegistryInitializationHookBase 抽象基类的完整测试
- 修改 IArchitectureContext.GetUtility 方法为虚拟方法以支持重写
2026-03-02 12:42:09 +08:00

96 lines
4.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.IO;
using GFramework.SourceGenerators.rule;
using GFramework.SourceGenerators.Tests.core;
using NUnit.Framework;
namespace GFramework.SourceGenerators.Tests.rule;
/// <summary>
/// 上下文感知生成器快照测试类
/// 用于测试ContextAwareGenerator源代码生成器的输出快照
/// </summary>
[TestFixture]
public class ContextAwareGeneratorSnapshotTests
{
/// <summary>
/// 测试ContextAwareGenerator源代码生成器的快照功能
/// 验证生成器对带有ContextAware特性的类的处理结果
/// </summary>
/// <returns>异步任务,无返回值</returns>
[Test]
public async Task Snapshot_ContextAwareGenerator()
{
// 定义测试用的源代码包含ContextAware特性和相关接口定义
const string source = """
using System;
namespace GFramework.SourceGenerators.Abstractions.rule
{
[AttributeUsage(AttributeTargets.Class)]
public sealed class ContextAwareAttribute : Attribute { }
}
namespace GFramework.Core.Abstractions.rule
{
public interface IContextAware
{
void SetContext(
GFramework.Core.Abstractions.architecture.IArchitectureContext context);
GFramework.Core.Abstractions.architecture.IArchitectureContext GetContext();
}
}
namespace GFramework.Core.Abstractions.architecture
{
public interface IArchitectureContext { }
public interface IArchitectureContextProvider
{
IArchitectureContext GetContext();
bool TryGetContext<T>(out T? context) where T : class, IArchitectureContext;
}
}
namespace GFramework.Core.architecture
{
using GFramework.Core.Abstractions.architecture;
public sealed class GameContextProvider : IArchitectureContextProvider
{
public IArchitectureContext GetContext() => null;
public bool TryGetContext<T>(out T? context) where T : class, IArchitectureContext
{
context = null;
return false;
}
}
public static class GameContext
{
public static IArchitectureContext GetFirstArchitectureContext() => null;
}
}
namespace TestApp
{
using GFramework.SourceGenerators.Abstractions.rule;
using GFramework.Core.Abstractions.rule;
[ContextAware]
public partial class MyRule : IContextAware
{
}
}
""";
// 执行生成器快照测试,将生成的代码与预期快照进行比较
await GeneratorSnapshotTest<ContextAwareGenerator>.RunAsync(
source,
Path.Combine(
TestContext.CurrentContext.TestDirectory,
"rule",
"snapshots",
"ContextAwareGenerator"));
}
}