From 68cf0c71eef6e222c546bfa85fa59e12137fa38a Mon Sep 17 00:00:00 2001
From: GwWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Fri, 26 Dec 2025 22:28:40 +0800
Subject: [PATCH] =?UTF-8?q?test(generator):=20=E6=B7=BB=E5=8A=A0=E6=BA=90?=
=?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E5=99=A8=E6=B5=8B=E8=AF=95?=
=?UTF-8?q?=E6=A1=86=E6=9E=B6=E5=92=8CContextAwareGenerator=E6=B5=8B?=
=?UTF-8?q?=E8=AF=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 创建GeneratorTest通用测试类用于源代码生成器测试
- 实现ContextAwareGeneratorTests测试用例验证上下文感知代码生成
- 在项目文件中添加测试项目排除规则
- 更新解决方案文件包含测试项目配置
- 创建GFramework.SourceGenerators.Tests项目文件并配置测试依赖
---
.../GFramework.SourceGenerators.Tests.csproj | 22 +++++++
.../core/GeneratorTest.cs | 40 +++++++++++++
.../rule/ContextAwareGeneratorTests.cs | 58 +++++++++++++++++++
GFramework.csproj | 3 +
GFramework.sln | 6 ++
5 files changed, 129 insertions(+)
create mode 100644 GFramework.SourceGenerators.Tests/GFramework.SourceGenerators.Tests.csproj
create mode 100644 GFramework.SourceGenerators.Tests/core/GeneratorTest.cs
create mode 100644 GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs
diff --git a/GFramework.SourceGenerators.Tests/GFramework.SourceGenerators.Tests.csproj b/GFramework.SourceGenerators.Tests/GFramework.SourceGenerators.Tests.csproj
new file mode 100644
index 0000000..de90d21
--- /dev/null
+++ b/GFramework.SourceGenerators.Tests/GFramework.SourceGenerators.Tests.csproj
@@ -0,0 +1,22 @@
+
+
+
+ enable
+ enable
+ net10.0;net8.0;net9.0
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/GFramework.SourceGenerators.Tests/core/GeneratorTest.cs b/GFramework.SourceGenerators.Tests/core/GeneratorTest.cs
new file mode 100644
index 0000000..7798649
--- /dev/null
+++ b/GFramework.SourceGenerators.Tests/core/GeneratorTest.cs
@@ -0,0 +1,40 @@
+using Microsoft.CodeAnalysis.CSharp.Testing;
+using Microsoft.CodeAnalysis.Testing;
+
+namespace GFramework.SourceGenerators.Tests.core;
+
+///
+/// 提供源代码生成器测试的通用功能
+///
+/// 要测试的源代码生成器类型,必须具有无参构造函数
+public static class GeneratorTest
+ where TGenerator : new()
+{
+ ///
+ /// 运行源代码生成器测试
+ ///
+ /// 输入的源代码
+ /// 期望生成的源文件集合,包含文件名和内容的元组
+ /// 异步操作任务
+ public static async Task RunAsync(
+ string source,
+ params (string filename, string content)[] generatedSources)
+ {
+ var test = new CSharpSourceGeneratorTest
+ {
+ TestState =
+ {
+ Sources = { source }
+ }
+ };
+
+ // 添加期望的生成源文件到测试状态中
+ foreach (var (filename, content) in generatedSources)
+ {
+ test.TestState.GeneratedSources.Add(
+ (typeof(TGenerator), filename, content));
+ }
+
+ await test.RunAsync();
+ }
+}
\ No newline at end of file
diff --git a/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs b/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs
new file mode 100644
index 0000000..3a70a92
--- /dev/null
+++ b/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs
@@ -0,0 +1,58 @@
+using GFramework.SourceGenerators.rule;
+using GFramework.SourceGenerators.Tests.core;
+using NUnit.Framework;
+
+namespace GFramework.SourceGenerators.Tests.rule;
+
+///
+/// 测试ContextAwareGenerator源代码生成器的功能
+///
+[TestFixture]
+public class ContextAwareGeneratorTests
+{
+ ///
+ /// 测试ContextAware代码生成功能
+ /// 验证当使用[ContextAware]特性标记的类能够正确生成上下文感知的相关代码
+ ///
+ /// 异步任务
+ [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 = """
+ //
+ #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.RunAsync(
+ source,
+ ("MyRule.ContextAware.g.cs", expected)
+ );
+ }
+}
\ No newline at end of file
diff --git a/GFramework.csproj b/GFramework.csproj
index 286960a..2ea6e5e 100644
--- a/GFramework.csproj
+++ b/GFramework.csproj
@@ -45,6 +45,7 @@
+
@@ -70,6 +71,7 @@
+
@@ -81,6 +83,7 @@
+
diff --git a/GFramework.sln b/GFramework.sln
index ea86a8c..f1efe57 100644
--- a/GFramework.sln
+++ b/GFramework.sln
@@ -18,6 +18,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GFramework.Godot.SourceGene
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GFramework.SourceGenerators.Common", "GFramework.SourceGenerators.Common\GFramework.SourceGenerators.Common.csproj", "{3DB57A3A-ACCF-47BE-A17B-2ADD68B6C8AA}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GFramework.SourceGenerators.Tests", "GFramework.SourceGenerators.Tests\GFramework.SourceGenerators.Tests.csproj", "{BB047F43-6AA0-4EA0-8AE9-E6B9784D9E8E}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -60,5 +62,9 @@ Global
{3DB57A3A-ACCF-47BE-A17B-2ADD68B6C8AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3DB57A3A-ACCF-47BE-A17B-2ADD68B6C8AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3DB57A3A-ACCF-47BE-A17B-2ADD68B6C8AA}.Release|Any CPU.Build.0 = Release|Any CPU
+ {BB047F43-6AA0-4EA0-8AE9-E6B9784D9E8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {BB047F43-6AA0-4EA0-8AE9-E6B9784D9E8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {BB047F43-6AA0-4EA0-8AE9-E6B9784D9E8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {BB047F43-6AA0-4EA0-8AE9-E6B9784D9E8E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal