mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 19:03:29 +08:00
- 修复 ILogger.cs 中多余的逗号 - 统一 ContextAwareDiagnostic.cs 中的注释缩进格式 - 统一 ContextAwareGenerator.cs 中的注释缩进格式 - 统一 ContextAwareAttribute.cs 中的注释缩进格式 - 统一 CommonDiagnostics.cs 中的注释缩进格式 - 简化 AttributeClassGeneratorBase.cs 中的 isEnabledByDefault 参数 - 统一 GeneratorTest.cs 中的注释缩进格式 - 优化 ContextAwareGeneratorTests.cs 中的代码结构 - 调整 AnalyzerReleases.Unshipped.md 中的表格格式
156 lines
6.5 KiB
C#
156 lines
6.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 Task Generates_ContextAware_Code()
|
|
{
|
|
// 定义输入源代码,包含使用[ContextAware]特性的部分类
|
|
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.rule.IContextAware
|
|
{
|
|
}
|
|
}
|
|
""";
|
|
|
|
const string frameworkStub = """
|
|
namespace GFramework.Core.rule
|
|
{
|
|
public interface IContextAware
|
|
{
|
|
void SetContext(GFramework.Core.architecture.IArchitectureContext context);
|
|
}
|
|
}
|
|
|
|
namespace GFramework.Core.architecture
|
|
{
|
|
public interface IArchitectureContext {}
|
|
}
|
|
""";
|
|
// 定义期望的生成结果代码
|
|
const string expected = """
|
|
// <auto-generated/>
|
|
#nullable enable
|
|
namespace TestApp;
|
|
|
|
partial class MyRule
|
|
{
|
|
protected GFramework.Core.architecture.IArchitectureContext Context { get; private set; } = null!;
|
|
void GFramework.Core.rule.IContextAware.SetContext(
|
|
GFramework.Core.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.rule;
|
|
|
|
// 间接接口:继承自 IContextAware
|
|
public interface IMyRuleContextAware : IContextAware
|
|
{
|
|
}
|
|
|
|
[ContextAware]
|
|
public partial class MyRule : IMyRuleContextAware
|
|
{
|
|
}
|
|
}
|
|
""";
|
|
|
|
const string frameworkStub = """
|
|
namespace GFramework.Core.rule
|
|
{
|
|
public interface IContextAware
|
|
{
|
|
void SetContext(GFramework.Core.architecture.IArchitectureContext context);
|
|
}
|
|
}
|
|
|
|
namespace GFramework.Core.architecture
|
|
{
|
|
public interface IArchitectureContext {}
|
|
}
|
|
""";
|
|
|
|
const string expected = """
|
|
// <auto-generated/>
|
|
#nullable enable
|
|
namespace TestApp;
|
|
|
|
partial class MyRule
|
|
{
|
|
protected GFramework.Core.architecture.IArchitectureContext Context { get; private set; } = null!;
|
|
void GFramework.Core.rule.IContextAware.SetContext(
|
|
GFramework.Core.architecture.IArchitectureContext context)
|
|
{
|
|
Context = context;
|
|
}
|
|
}
|
|
""";
|
|
Assert.DoesNotThrowAsync(async () =>
|
|
{
|
|
await GeneratorTest<ContextAwareGenerator>.RunAsync(
|
|
source + "\n" + frameworkStub,
|
|
("MyRule.ContextAware.g.cs", expected)
|
|
);
|
|
});
|
|
return Task.CompletedTask;
|
|
}
|
|
} |