From 383eceb36ff71d49dd3f48171dcbca51c9f74ca5 Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Sat, 27 Dec 2025 13:35:44 +0800
Subject: [PATCH] =?UTF-8?q?test(generator):=20=E6=B7=BB=E5=8A=A0=E4=B8=8A?=
=?UTF-8?q?=E4=B8=8B=E6=96=87=E6=84=9F=E7=9F=A5=E7=94=9F=E6=88=90=E5=99=A8?=
=?UTF-8?q?=E7=9A=84=E7=BB=A7=E6=89=BF=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 为 ContextAwareGenerator 添加新的单元测试方法
- 测试当接口继承自 IContextAware 时的代码生成功能
- 验证间接接口实现的正确性
- 检查生成的 SetContext 方法和 Context 属性
- 确保继承场景下的代码生成符合预期
---
.../rule/ContextAwareGeneratorTests.cs | 68 +++++++++++++++++++
1 file changed, 68 insertions(+)
diff --git a/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs b/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs
index 49e3f79..bdd5418 100644
--- a/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs
+++ b/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs
@@ -79,4 +79,72 @@ public class ContextAwareGeneratorTests
("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 = """
+ //
+ #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.RunAsync(
+ source + "\n" + frameworkStub,
+ ("MyRule.ContextAware.g.cs", expected)
+ );
+ }
}
\ No newline at end of file