GFramework/GFramework.SourceGenerators.Tests/Architectures/AutoRegisterModuleGeneratorTests.cs
GeWuYou eb307bf188 feat(generator): 添加代码生成器诊断规则和测试用例
- 定义了 Godot 源代码生成器的诊断规则表格
- 添加了上下文获取生成器的全面单元测试
- 实现了自动生成行为和注册导出集合的诊断功能
- 配置了全局 using 语句简化代码生成器实现
- 添加了完整的分析器发布跟踪文档记录新规则
2026-04-13 10:01:46 +08:00

109 lines
5.3 KiB
C#

using GFramework.SourceGenerators.Architectures;
using GFramework.SourceGenerators.Tests.Core;
namespace GFramework.SourceGenerators.Tests.Architectures;
[TestFixture]
public class AutoRegisterModuleGeneratorTests
{
[Test]
public async Task Generates_Module_Install_Method_In_Attribute_Order()
{
const string source = """
using System;
using GFramework.SourceGenerators.Abstractions.Architectures;
namespace GFramework.SourceGenerators.Abstractions.Architectures
{
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class AutoRegisterModuleAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
public sealed class RegisterModelAttribute : Attribute
{
public RegisterModelAttribute(Type modelType) { }
}
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
public sealed class RegisterSystemAttribute : Attribute
{
public RegisterSystemAttribute(Type systemType) { }
}
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
public sealed class RegisterUtilityAttribute : Attribute
{
public RegisterUtilityAttribute(Type utilityType) { }
}
}
namespace GFramework.Core.Abstractions.Architectures
{
public interface IArchitecture
{
T RegisterModel<T>(T model) where T : GFramework.Core.Abstractions.Model.IModel;
T RegisterSystem<T>(T system) where T : GFramework.Core.Abstractions.Systems.ISystem;
T RegisterUtility<T>(T utility) where T : GFramework.Core.Abstractions.Utility.IUtility;
}
}
namespace GFramework.Core.Abstractions.Model
{
public interface IModel { }
}
namespace GFramework.Core.Abstractions.Systems
{
public interface ISystem { }
}
namespace GFramework.Core.Abstractions.Utility
{
public interface IUtility { }
}
namespace TestApp
{
using GFramework.Core.Abstractions.Model;
using GFramework.Core.Abstractions.Systems;
using GFramework.Core.Abstractions.Utility;
using GFramework.SourceGenerators.Abstractions.Architectures;
public sealed class PlayerModel : IModel { }
public sealed class CombatSystem : ISystem { }
public sealed class AudioUtility : IUtility { }
[AutoRegisterModule]
[RegisterSystem(typeof(CombatSystem))]
[RegisterModel(typeof(PlayerModel))]
[RegisterUtility(typeof(AudioUtility))]
public partial class GameplayModule
{
}
}
""";
const string expected = """
// <auto-generated />
#nullable enable
namespace TestApp;
partial class GameplayModule
{
public void Install(global::GFramework.Core.Abstractions.Architectures.IArchitecture architecture)
{
architecture.RegisterSystem(new global::TestApp.CombatSystem());
architecture.RegisterModel(new global::TestApp.PlayerModel());
architecture.RegisterUtility(new global::TestApp.AudioUtility());
}
}
""";
await GeneratorTest<AutoRegisterModuleGenerator>.RunAsync(
source,
("TestApp_GameplayModule.AutoRegisterModule.g.cs", expected));
}
}