mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
- 新增 ContextAwareExtensions 扩展类,提供便捷的上下文访问方法 - 为 IContextAware 接口添加 GetContext 方法以获取架构上下文 - 更新 ContextAwareBase 基类实现 GetContext 方法 - 改进源代码生成器的 Generate 方法参数结构 - 重构 ContextAwareGenerator 生成器实现接口方法自动实现 - 更新单元测试以验证新生成的上下文感知代码正确性
167 lines
7.3 KiB
C#
167 lines
7.3 KiB
C#
using GFramework.SourceGenerators.rule;
|
|
using GFramework.SourceGenerators.Tests.core;
|
|
using NUnit.Framework;
|
|
|
|
namespace GFramework.SourceGenerators.Tests.rule;
|
|
|
|
[TestFixture]
|
|
public class ContextAwareGeneratorTests
|
|
{
|
|
[Test]
|
|
public async 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);
|
|
|
|
GFramework.Core.Abstractions.architecture.IArchitectureContext GetContext();
|
|
}
|
|
}
|
|
|
|
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 global::GFramework.Core.Abstractions.rule.IContextAware.SetContext(
|
|
global::GFramework.Core.Abstractions.architecture.IArchitectureContext context)
|
|
{
|
|
Context = context;
|
|
}
|
|
|
|
global::GFramework.Core.Abstractions.architecture.IArchitectureContext
|
|
global::GFramework.Core.Abstractions.rule.IContextAware.GetContext()
|
|
{
|
|
return Context;
|
|
}
|
|
}
|
|
""";
|
|
|
|
await GeneratorTest<ContextAwareGenerator>.RunAsync(
|
|
source + "\n" + frameworkStub,
|
|
("MyRule.ContextAware.g.cs", expected)
|
|
);
|
|
}
|
|
|
|
[Test]
|
|
public async 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;
|
|
|
|
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);
|
|
|
|
GFramework.Core.Abstractions.architecture.IArchitectureContext GetContext();
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
GFramework.Core.Abstractions.architecture.IArchitectureContext
|
|
GFramework.Core.Abstractions.rule.IContextAware.GetContext()
|
|
{
|
|
return Context;
|
|
}
|
|
}
|
|
""";
|
|
|
|
await GeneratorTest<ContextAwareGenerator>.RunAsync(
|
|
source + "\n" + frameworkStub,
|
|
("MyRule.ContextAware.g.cs", expected)
|
|
);
|
|
}
|
|
} |