GFramework/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs
GwWuYou 68cf0c71ee test(generator): 添加源代码生成器测试框架和ContextAwareGenerator测试
- 创建GeneratorTest通用测试类用于源代码生成器测试
- 实现ContextAwareGeneratorTests测试用例验证上下文感知代码生成
- 在项目文件中添加测试项目排除规则
- 更新解决方案文件包含测试项目配置
- 创建GFramework.SourceGenerators.Tests项目文件并配置测试依赖
2025-12-26 22:28:40 +08:00

58 lines
2.1 KiB
C#

using GFramework.SourceGenerators.rule;
using GFramework.SourceGenerators.Tests.core;
using NUnit.Framework;
namespace GFramework.SourceGenerators.Tests.rule;
/// <summary>
/// 测试ContextAwareGenerator源代码生成器的功能
/// </summary>
[TestFixture]
public class ContextAwareGeneratorTests
{
/// <summary>
/// 测试ContextAware代码生成功能
/// 验证当使用[ContextAware]特性标记的类能够正确生成上下文感知的相关代码
/// </summary>
/// <returns>异步任务</returns>
[Test]
public async Task Generates_ContextAware_Code()
{
// 定义输入源代码,包含使用[ContextAware]特性的部分类
const string source = """
using GFramework.Core.rule;
using GFramework.Core.architecture;
namespace TestApp;
[ContextAware]
public partial class MyRule
{
}
""";
// 定义期望的生成结果代码
const string expected = """
// <auto-generated/>
#nullable enable
namespace TestApp;
partial class MyRule : GFramework.Core.rule.IContextAware
{
protected GFramework.Core.architecture.IArchitectureContext Context { get; private set; } = null!;
void GFramework.Core.rule.IContextAware.SetContext(
GFramework.Core.architecture.IArchitectureContext context)
{
Context = context;
}
}
""";
// 执行源代码生成器测试
await GeneratorTest<ContextAwareGenerator>.RunAsync(
source,
("MyRule.ContextAware.g.cs", expected)
);
}
}