GFramework/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorSnapshotTests.cs
GwWuYou 0be919d8b1 feat(architecture): 添加架构上下文管理和绑定功能
- 在Architecture类中添加GameContext.Bind调用以绑定架构上下文
- 创建GameContext类用于管理架构上下文实例的注册和获取
- 实现架构上下文的绑定、获取、查找和移除功能
- 更新ContextAwareGenerator生成器以使用懒加载方式获取上下文
- 在测试架构中添加就绪事件注册功能
- 添加架构上下文按类型注册的测试用例
2025-12-29 23:11:50 +08:00

84 lines
3.7 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 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 { }
}
namespace TestApp
{
using GFramework.SourceGenerators.Abstractions.rule;
using GFramework.Core.Abstractions.rule;
[ContextAware]
public partial class MyRule : IContextAware
{
}
}
namespace GFramework.Core.architecture
{
using GFramework.Core.Abstractions.architecture;
public class GameContext{
/// <summary>
/// 获取字典中的第一个架构上下文
/// </summary>
/// <returns>返回字典中的第一个架构上下文实例</returns>
/// <exception cref="InvalidOperationException">当字典为空时抛出</exception>
public static IArchitectureContext GetFirstArchitecture()
{
return null;
}
}
}
""";
// 执行生成器快照测试,将生成的代码与预期快照进行比较
await GeneratorSnapshotTest<ContextAwareGenerator>.RunAsync(
source,
snapshotFolder: Path.Combine(
TestContext.CurrentContext.TestDirectory,
"rule",
"snapshots",
"ContextAwareGenerator"));
}
}