GFramework/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs
GeWuYou 5f211d8680 fix(generator): 修复ContextAware生成器代码格式和测试配置
- 移除ContextAwareGenerator中多余的空行
- 为生成的类添加IContextAware接口实现
- 在测试中添加IContextAware接口定义
- 修复Rider测试会话配置中的IsActive属性
- 更新测试ID以匹配正确的测试类路径
2025-12-27 13:31:27 +08:00

82 lines
3.3 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 System;
namespace GFramework.SourceGenerators.Attributes.rule
{
[AttributeUsage(AttributeTargets.Class)]
public sealed class ContextAwareAttribute : Attribute
{
}
}
namespace TestApp
{
using GFramework.SourceGenerators.Attributes.rule;
[ContextAware]
public partial class MyRule: GFramework.Core.rule.IContextAware
{
}
}
""";
const string frameworkStub = """
namespace GFramework.Core.rule
{
public interface IContextAware
{
void SetContext(GFramework.Core.architecture.IArchitectureContext context);
}
}
namespace GFramework.Core.architecture
{
public interface IArchitectureContext {}
}
""";
// 定义期望的生成结果代码
const string expected = """
// <auto-generated/>
#nullable enable
namespace TestApp;
partial class MyRule
{
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 + "\n" + frameworkStub,
("MyRule.ContextAware.g.cs", expected)
);
}
}