mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
- 新增 PriorityGenerator 源生成器,自动生成 IPrioritized 接口实现 - 添加 PriorityAttribute 特性,用于标记类的优先级值 - 实现 PriorityUsageAnalyzer 分析器,检测优先级使用建议 - 添加预定义的 PriorityGroup 常量,提供标准优先级分组 - 在 AnalyzerReleases.Unshipped.md 中注册新的诊断规则 - 更新项目依赖,升级 Meziantou.Analyzer 和 Polyfill 版本 - 为测试项目添加源生成器项目引用 - 添加 PriorityGenerator 的快照测试用例
266 lines
10 KiB
C#
266 lines
10 KiB
C#
using System.IO;
|
|
using GFramework.SourceGenerators.bases;
|
|
using GFramework.SourceGenerators.Tests.core;
|
|
using NUnit.Framework;
|
|
|
|
namespace GFramework.SourceGenerators.Tests.bases;
|
|
|
|
/// <summary>
|
|
/// Priority 生成器快照测试类
|
|
/// </summary>
|
|
[TestFixture]
|
|
public class PriorityGeneratorSnapshotTests
|
|
{
|
|
/// <summary>
|
|
/// 测试基本的 Priority 特性生成
|
|
/// </summary>
|
|
[Test]
|
|
public async Task Snapshot_BasicPriority()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
|
|
namespace GFramework.SourceGenerators.Abstractions.bases
|
|
{
|
|
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
|
public sealed class PriorityAttribute : Attribute
|
|
{
|
|
public int Value { get; }
|
|
public PriorityAttribute(int value) { Value = value; }
|
|
}
|
|
}
|
|
|
|
namespace GFramework.Core.Abstractions.bases
|
|
{
|
|
public interface IPrioritized
|
|
{
|
|
int Priority { get; }
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
using GFramework.SourceGenerators.Abstractions.bases;
|
|
|
|
[Priority(10)]
|
|
public partial class MySystem
|
|
{
|
|
}
|
|
}
|
|
""";
|
|
|
|
await GeneratorSnapshotTest<PriorityGenerator>.RunAsync(
|
|
source,
|
|
Path.Combine(
|
|
TestContext.CurrentContext.TestDirectory,
|
|
"bases",
|
|
"snapshots",
|
|
"PriorityGenerator",
|
|
"BasicPriority"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试负数优先级
|
|
/// </summary>
|
|
[Test]
|
|
public async Task Snapshot_NegativePriority()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
|
|
namespace GFramework.SourceGenerators.Abstractions.bases
|
|
{
|
|
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
|
public sealed class PriorityAttribute : Attribute
|
|
{
|
|
public int Value { get; }
|
|
public PriorityAttribute(int value) { Value = value; }
|
|
}
|
|
}
|
|
|
|
namespace GFramework.Core.Abstractions.bases
|
|
{
|
|
public interface IPrioritized
|
|
{
|
|
int Priority { get; }
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
using GFramework.SourceGenerators.Abstractions.bases;
|
|
|
|
[Priority(-100)]
|
|
public partial class CriticalSystem
|
|
{
|
|
}
|
|
}
|
|
""";
|
|
|
|
await GeneratorSnapshotTest<PriorityGenerator>.RunAsync(
|
|
source,
|
|
Path.Combine(
|
|
TestContext.CurrentContext.TestDirectory,
|
|
"bases",
|
|
"snapshots",
|
|
"PriorityGenerator",
|
|
"NegativePriority"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试使用 PriorityGroup 枚举
|
|
/// </summary>
|
|
[Test]
|
|
public async Task Snapshot_PriorityGroup()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
|
|
namespace GFramework.SourceGenerators.Abstractions.bases
|
|
{
|
|
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
|
public sealed class PriorityAttribute : Attribute
|
|
{
|
|
public int Value { get; }
|
|
public PriorityAttribute(int value) { Value = value; }
|
|
}
|
|
}
|
|
|
|
namespace GFramework.Core.Abstractions.bases
|
|
{
|
|
public interface IPrioritized
|
|
{
|
|
int Priority { get; }
|
|
}
|
|
|
|
public static class PriorityGroup
|
|
{
|
|
public const int Critical = -100;
|
|
public const int High = -50;
|
|
public const int Normal = 0;
|
|
public const int Low = 50;
|
|
public const int Deferred = 100;
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
using GFramework.SourceGenerators.Abstractions.bases;
|
|
using GFramework.Core.Abstractions.bases;
|
|
|
|
[Priority(PriorityGroup.High)]
|
|
public partial class HighPrioritySystem
|
|
{
|
|
}
|
|
}
|
|
""";
|
|
|
|
await GeneratorSnapshotTest<PriorityGenerator>.RunAsync(
|
|
source,
|
|
Path.Combine(
|
|
TestContext.CurrentContext.TestDirectory,
|
|
"bases",
|
|
"snapshots",
|
|
"PriorityGenerator",
|
|
"PriorityGroup"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试泛型类支持
|
|
/// </summary>
|
|
[Test]
|
|
public async Task Snapshot_GenericClass()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
|
|
namespace GFramework.SourceGenerators.Abstractions.bases
|
|
{
|
|
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
|
public sealed class PriorityAttribute : Attribute
|
|
{
|
|
public int Value { get; }
|
|
public PriorityAttribute(int value) { Value = value; }
|
|
}
|
|
}
|
|
|
|
namespace GFramework.Core.Abstractions.bases
|
|
{
|
|
public interface IPrioritized
|
|
{
|
|
int Priority { get; }
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
using GFramework.SourceGenerators.Abstractions.bases;
|
|
|
|
[Priority(20)]
|
|
public partial class GenericSystem<T>
|
|
{
|
|
}
|
|
}
|
|
""";
|
|
|
|
await GeneratorSnapshotTest<PriorityGenerator>.RunAsync(
|
|
source,
|
|
Path.Combine(
|
|
TestContext.CurrentContext.TestDirectory,
|
|
"bases",
|
|
"snapshots",
|
|
"PriorityGenerator",
|
|
"GenericClass"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试嵌套类支持
|
|
/// </summary>
|
|
[Test]
|
|
public async Task Snapshot_NestedClass()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
|
|
namespace GFramework.SourceGenerators.Abstractions.bases
|
|
{
|
|
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
|
public sealed class PriorityAttribute : Attribute
|
|
{
|
|
public int Value { get; }
|
|
public PriorityAttribute(int value) { Value = value; }
|
|
}
|
|
}
|
|
|
|
namespace GFramework.Core.Abstractions.bases
|
|
{
|
|
public interface IPrioritized
|
|
{
|
|
int Priority { get; }
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
using GFramework.SourceGenerators.Abstractions.bases;
|
|
|
|
public class OuterClass
|
|
{
|
|
[Priority(30)]
|
|
public partial class NestedSystem
|
|
{
|
|
}
|
|
}
|
|
}
|
|
""";
|
|
|
|
await GeneratorSnapshotTest<PriorityGenerator>.RunAsync(
|
|
source,
|
|
Path.Combine(
|
|
TestContext.CurrentContext.TestDirectory,
|
|
"bases",
|
|
"snapshots",
|
|
"PriorityGenerator",
|
|
"NestedClass"));
|
|
}
|
|
} |