mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-23 03:04:29 +08:00
- 将硬编码的命名空间路径替换为常量类中的动态引用 - 添加 PathContests 常量类统一管理框架命名空间 - 更新 LoggerGenerator 使用动态命名空间引用 - 重构 ContextAwareGenerator 生成的代码格式和命名空间引用 - 为 ContextAware 生成的属性添加 XML 文档注释 - 简化 ContextAware 生成文件的路径命名规则 - 更新测试代码以匹配新的命名空间引用方式
166 lines
6.8 KiB
C#
166 lines
6.8 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>
|
|
[Test]
|
|
public Task Generates_ContextAware_Code()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
|
|
namespace GFramework.SourceGenerators.Abstractions.rule
|
|
{
|
|
[AttributeUsage(AttributeTargets.Class)]
|
|
public sealed class ContextAwareAttribute : Attribute
|
|
{
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
using GFramework.SourceGenerators.Abstractions.rule;
|
|
|
|
[ContextAware]
|
|
public partial class MyRule: GFramework.Core.Abstractions.rule.IContextAware
|
|
{
|
|
}
|
|
}
|
|
""";
|
|
|
|
const string frameworkStub = """
|
|
namespace GFramework.Core.Abstractions.rule
|
|
{
|
|
public interface IContextAware
|
|
{
|
|
void SetContext(GFramework.Core.Abstractions.architecture.IArchitectureContext context);
|
|
}
|
|
}
|
|
|
|
namespace GFramework.Core.Abstractions.architecture
|
|
{
|
|
public interface IArchitectureContext {}
|
|
}
|
|
""";
|
|
|
|
const string expected = """
|
|
// <auto-generated/>
|
|
#nullable enable
|
|
|
|
namespace TestApp;
|
|
|
|
partial class MyRule
|
|
{
|
|
/// <summary>
|
|
/// 自动注入的架构上下文
|
|
/// </summary>
|
|
protected GFramework.Core.Abstractions.architecture.IArchitectureContext Context { get; private set; } = null!;
|
|
|
|
void GFramework.Core.Abstractions.rule.IContextAware.SetContext(
|
|
GFramework.Core.Abstractions.architecture.IArchitectureContext context)
|
|
{
|
|
Context = context;
|
|
}
|
|
}
|
|
""";
|
|
|
|
Assert.DoesNotThrowAsync(async () =>
|
|
{
|
|
await GeneratorTest<ContextAwareGenerator>.RunAsync(
|
|
source + "\n" + frameworkStub,
|
|
("MyRule.ContextAware.g.cs", expected)
|
|
);
|
|
});
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
[Test]
|
|
public Task Generates_ContextAware_Code_When_Interface_Inherits_IContextAware()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
|
|
namespace GFramework.SourceGenerators.Abstractions.rule
|
|
{
|
|
[AttributeUsage(AttributeTargets.Class)]
|
|
public sealed class ContextAwareAttribute : Attribute
|
|
{
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
using GFramework.SourceGenerators.Abstractions.rule;
|
|
using GFramework.Core.Abstractions.rule;
|
|
|
|
// 间接接口:继承自 IContextAware
|
|
public interface IMyRuleContextAware : IContextAware
|
|
{
|
|
}
|
|
|
|
[ContextAware]
|
|
public partial class MyRule : IMyRuleContextAware
|
|
{
|
|
}
|
|
}
|
|
""";
|
|
|
|
const string frameworkStub = """
|
|
namespace GFramework.Core.Abstractions.rule
|
|
{
|
|
public interface IContextAware
|
|
{
|
|
void SetContext(GFramework.Core.Abstractions.architecture.IArchitectureContext context);
|
|
}
|
|
}
|
|
|
|
namespace GFramework.Core.Abstractions.architecture
|
|
{
|
|
public interface IArchitectureContext {}
|
|
}
|
|
""";
|
|
|
|
const string expected = """
|
|
// <auto-generated/>
|
|
#nullable enable
|
|
|
|
namespace TestApp;
|
|
|
|
partial class MyRule
|
|
{
|
|
/// <summary>
|
|
/// 自动注入的架构上下文
|
|
/// </summary>
|
|
protected GFramework.Core.Abstractions.architecture.IArchitectureContext Context { get; private set; } = null!;
|
|
|
|
void GFramework.Core.Abstractions.rule.IContextAware.SetContext(
|
|
GFramework.Core.Abstractions.architecture.IArchitectureContext context)
|
|
{
|
|
Context = context;
|
|
}
|
|
}
|
|
""";
|
|
|
|
Assert.DoesNotThrowAsync(async () =>
|
|
{
|
|
await GeneratorTest<ContextAwareGenerator>.RunAsync(
|
|
source + "\n" + frameworkStub,
|
|
("MyRule.ContextAware.g.cs", expected)
|
|
);
|
|
});
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
} |