From eaa706a7e421fa1e620cb8cbd25b9c77f2b9d9fc Mon Sep 17 00:00:00 2001
From: GwWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Fri, 26 Dec 2025 22:51:57 +0800
Subject: [PATCH] =?UTF-8?q?refactor(source-generator):=20=E9=87=8D?=
=?UTF-8?q?=E6=9E=84ContextAware=E7=94=9F=E6=88=90=E5=99=A8=E7=9A=84?=
=?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 将多行字符串插值改为逐行添加以改善代码可读性
- 更新测试用例中的源代码和期望输出格式
- 修改项目文件中的目标框架为net8.0
- 添加必要的包引用Microsoft.CodeAnalysis.CSharp和Microsoft.CodeAnalysis.Common
- 移除解决方案用户设置中的过期配置项
---
.../GFramework.SourceGenerators.Tests.csproj | 4 +++-
.../rule/ContextAwareGeneratorTests.cs | 16 +++++++++++++---
.../rule/ContextAwareGenerator.cs | 13 ++++++-------
GFramework.sln.DotSettings.user | 7 ++++++-
4 files changed, 28 insertions(+), 12 deletions(-)
diff --git a/GFramework.SourceGenerators.Tests/GFramework.SourceGenerators.Tests.csproj b/GFramework.SourceGenerators.Tests/GFramework.SourceGenerators.Tests.csproj
index de90d21..ec7d901 100644
--- a/GFramework.SourceGenerators.Tests/GFramework.SourceGenerators.Tests.csproj
+++ b/GFramework.SourceGenerators.Tests/GFramework.SourceGenerators.Tests.csproj
@@ -3,11 +3,13 @@
enable
enable
- net10.0;net8.0;net9.0
+ net8.0
+
+
diff --git a/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs b/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs
index 3a70a92..c1b5b37 100644
--- a/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs
+++ b/GFramework.SourceGenerators.Tests/rule/ContextAwareGeneratorTests.cs
@@ -20,17 +20,27 @@ public class ContextAwareGeneratorTests
{
// 定义输入源代码,包含使用[ContextAware]特性的部分类
const string source = """
- using GFramework.Core.rule;
- using GFramework.Core.architecture;
+ 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
{
}
""";
+
// 定义期望的生成结果代码
const string expected = """
//
@@ -40,7 +50,6 @@ public class ContextAwareGeneratorTests
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)
{
@@ -49,6 +58,7 @@ public class ContextAwareGeneratorTests
}
""";
+
// 执行源代码生成器测试
await GeneratorTest.RunAsync(
source,
diff --git a/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs b/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs
index 0fd1cb7..2c2de4f 100644
--- a/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs
+++ b/GFramework.SourceGenerators/rule/ContextAwareGenerator.cs
@@ -99,13 +99,12 @@ public sealed class ContextAwareGenerator : IIncrementalGenerator
sb.AppendLine(
" protected GFramework.Core.architecture.IArchitectureContext Context { get; private set; } = null!;");
- sb.AppendLine("""
- void GFramework.Core.rule.IContextAware.SetContext(
- GFramework.Core.architecture.IArchitectureContext context)
- {
- Context = context;
- }
- """);
+ sb.AppendLine(" void GFramework.Core.rule.IContextAware.SetContext(");
+ sb.AppendLine(" GFramework.Core.architecture.IArchitectureContext context)");
+ sb.AppendLine(" {");
+ sb.AppendLine(" Context = context;");
+ sb.AppendLine(" }");
+
sb.AppendLine("}");
diff --git a/GFramework.sln.DotSettings.user b/GFramework.sln.DotSettings.user
index 56537ed..8865a13 100644
--- a/GFramework.sln.DotSettings.user
+++ b/GFramework.sln.DotSettings.user
@@ -1,3 +1,8 @@
ForceIncluded
- ForceIncluded
\ No newline at end of file
+ ForceIncluded
+ <SessionState ContinuousTestingMode="0" IsActive="True" Name="Generates_ContextAware_Code" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session">
+ <TestAncestor>
+ <TestId>NUnit3x::BB047F43-6AA0-4EA0-8AE9-E6B9784D9E8E::net8.0::GFramework.SourceGenerators.Tests.rule.ContextAwareGeneratorTests</TestId>
+ </TestAncestor>
+</SessionState>
\ No newline at end of file