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