mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 19:03:29 +08:00
- 将多行字符串插值改为逐行添加以改善代码可读性 - 更新测试用例中的源代码和期望输出格式 - 修改项目文件中的目标框架为net8.0 - 添加必要的包引用Microsoft.CodeAnalysis.CSharp和Microsoft.CodeAnalysis.Common - 移除解决方案用户设置中的过期配置项
68 lines
2.5 KiB
C#
68 lines
2.5 KiB
C#
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 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 = """
|
|
// <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)
|
|
);
|
|
}
|
|
} |