mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
test(generator): 添加源代码生成器测试框架和ContextAwareGenerator测试
- 创建GeneratorTest通用测试类用于源代码生成器测试 - 实现ContextAwareGeneratorTests测试用例验证上下文感知代码生成 - 在项目文件中添加测试项目排除规则 - 更新解决方案文件包含测试项目配置 - 创建GFramework.SourceGenerators.Tests项目文件并配置测试依赖
This commit is contained in:
parent
a888e76842
commit
68cf0c71ee
@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<TargetFrameworks>net10.0;net8.0;net9.0</TargetFrameworks>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.14.0"/>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" Version="1.1.2"/>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1"/>
|
||||
<PackageReference Include="NUnit" Version="4.4.0"/>
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="6.0.1"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\GFramework.SourceGenerators\GFramework.SourceGenerators.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
40
GFramework.SourceGenerators.Tests/core/GeneratorTest.cs
Normal file
40
GFramework.SourceGenerators.Tests/core/GeneratorTest.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using Microsoft.CodeAnalysis.CSharp.Testing;
|
||||
using Microsoft.CodeAnalysis.Testing;
|
||||
|
||||
namespace GFramework.SourceGenerators.Tests.core;
|
||||
|
||||
/// <summary>
|
||||
/// 提供源代码生成器测试的通用功能
|
||||
/// </summary>
|
||||
/// <typeparam name="TGenerator">要测试的源代码生成器类型,必须具有无参构造函数</typeparam>
|
||||
public static class GeneratorTest<TGenerator>
|
||||
where TGenerator : new()
|
||||
{
|
||||
/// <summary>
|
||||
/// 运行源代码生成器测试
|
||||
/// </summary>
|
||||
/// <param name="source">输入的源代码</param>
|
||||
/// <param name="generatedSources">期望生成的源文件集合,包含文件名和内容的元组</param>
|
||||
/// <returns>异步操作任务</returns>
|
||||
public static async Task RunAsync(
|
||||
string source,
|
||||
params (string filename, string content)[] generatedSources)
|
||||
{
|
||||
var test = new CSharpSourceGeneratorTest<TGenerator, DefaultVerifier>
|
||||
{
|
||||
TestState =
|
||||
{
|
||||
Sources = { source }
|
||||
}
|
||||
};
|
||||
|
||||
// 添加期望的生成源文件到测试状态中
|
||||
foreach (var (filename, content) in generatedSources)
|
||||
{
|
||||
test.TestState.GeneratedSources.Add(
|
||||
(typeof(TGenerator), filename, content));
|
||||
}
|
||||
|
||||
await test.RunAsync();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,58 @@
|
||||
using GFramework.SourceGenerators.rule;
|
||||
using GFramework.SourceGenerators.Tests.core;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace GFramework.SourceGenerators.Tests.rule;
|
||||
|
||||
/// <summary>
|
||||
/// 测试ContextAwareGenerator源代码生成器的功能
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
public class ContextAwareGeneratorTests
|
||||
{
|
||||
/// <summary>
|
||||
/// 测试ContextAware代码生成功能
|
||||
/// 验证当使用[ContextAware]特性标记的类能够正确生成上下文感知的相关代码
|
||||
/// </summary>
|
||||
/// <returns>异步任务</returns>
|
||||
[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 = """
|
||||
// <auto-generated/>
|
||||
#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<ContextAwareGenerator>.RunAsync(
|
||||
source,
|
||||
("MyRule.ContextAware.g.cs", expected)
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -45,6 +45,7 @@
|
||||
<None Remove="GFramework.SourceGenerators\**"/>
|
||||
<None Remove="GFramework.SourceGenerators.Attributes\**"/>
|
||||
<None Remove="GFramework.SourceGenerators.Common\**"/>
|
||||
<None Remove="GFramework.SourceGenerators.Tests\**"/>
|
||||
</ItemGroup>
|
||||
<!-- 聚合核心模块 -->
|
||||
<ItemGroup>
|
||||
@ -70,6 +71,7 @@
|
||||
<Compile Remove="GFramework.SourceGenerators\**"/>
|
||||
<Compile Remove="GFramework.SourceGenerators.Attributes\**"/>
|
||||
<Compile Remove="GFramework.SourceGenerators.Common\**"/>
|
||||
<Compile Remove="GFramework.SourceGenerators.Tests\**"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Remove="GFramework.Core\**"/>
|
||||
@ -81,6 +83,7 @@
|
||||
<EmbeddedResource Remove="GFramework.SourceGenerators\**"/>
|
||||
<EmbeddedResource Remove="GFramework.SourceGenerators.Attributes\**"/>
|
||||
<EmbeddedResource Remove="GFramework.SourceGenerators.Common\**"/>
|
||||
<EmbeddedResource Remove="GFramework.SourceGenerators.Tests\**"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.14.0"/>
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user