test(generator): 添加上下文感知生成器的继承接口测试

- 为 ContextAwareGenerator 添加新的单元测试方法
- 测试当接口继承自 IContextAware 时的代码生成功能
- 验证间接接口实现的正确性
- 检查生成的 SetContext 方法和 Context 属性
- 确保继承场景下的代码生成符合预期
This commit is contained in:
GeWuYou 2025-12-27 13:35:44 +08:00
parent 5f211d8680
commit 383eceb36f

View File

@ -79,4 +79,72 @@ public class ContextAwareGeneratorTests
("MyRule.ContextAware.g.cs", expected) ("MyRule.ContextAware.g.cs", expected)
); );
} }
[Test]
public async Task Generates_ContextAware_Code_When_Interface_Inherits_IContextAware()
{
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;
using GFramework.Core.rule;
// 间接接口:继承自 IContextAware
public interface IMyRuleContextAware : IContextAware
{
}
[ContextAware]
public partial class MyRule : IMyRuleContextAware
{
}
}
""";
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)
);
}
} }