mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-07 17:21:16 +08:00
### 属性参数校验(Attribute Validation)
- AutoUiPageGenerator
- 新增 GF_AutoBehavior_004 诊断:
- 检测 AutoUiPageAttribute 参数无效情况
- 添加测试用例验证错误参数的诊断报告
- AutoRegisterExportedCollectionsGenerator
- 新增 GF_AutoExport_008 诊断:
- 检测 RegisterExportedCollectionAttribute 参数无效情况
- 改进 TryGetRegistrationAttributeArguments 方法:
- 精确报告错误位置
- 更新文档以包含新增诊断规则
### 泛型约束支持(Generic Constraints)
- AutoUiPageGenerator / AutoRegisterModuleGenerator
- 支持以下泛型约束的正确生成:
- class?
- notnull
- unmanaged
- 添加对应测试用例确保生成正确性
### 诊断体系优化(Diagnostics Improvements)
- AutoRegisterModuleGenerator
- 重构 AutoRegisterModuleDiagnostics:
- 优化诊断定义顺序,提高可读性与维护性
274 lines
12 KiB
C#
274 lines
12 KiB
C#
using GFramework.Godot.SourceGenerators.Behavior;
|
|
using GFramework.Godot.SourceGenerators.Tests.Core;
|
|
|
|
namespace GFramework.Godot.SourceGenerators.Tests.Behavior;
|
|
|
|
[TestFixture]
|
|
public class AutoUiPageGeneratorTests
|
|
{
|
|
[Test]
|
|
public async Task Generates_Ui_Page_Behavior_Boilerplate()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
using GFramework.Godot.SourceGenerators.Abstractions;
|
|
using Godot;
|
|
|
|
namespace GFramework.Godot.SourceGenerators.Abstractions
|
|
{
|
|
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
|
public sealed class AutoUiPageAttribute : Attribute
|
|
{
|
|
public AutoUiPageAttribute(string key, string layerName) { }
|
|
}
|
|
}
|
|
|
|
namespace Godot
|
|
{
|
|
public class Node { }
|
|
public class CanvasItem : Node { }
|
|
public class Control : CanvasItem { }
|
|
}
|
|
|
|
namespace GFramework.Game.Abstractions.Enums
|
|
{
|
|
public enum UiLayer
|
|
{
|
|
Page,
|
|
Overlay,
|
|
Modal
|
|
}
|
|
}
|
|
|
|
namespace GFramework.Game.Abstractions.UI
|
|
{
|
|
public interface IUiPageBehavior { }
|
|
}
|
|
|
|
namespace GFramework.Godot.UI
|
|
{
|
|
using GFramework.Game.Abstractions.Enums;
|
|
using GFramework.Game.Abstractions.UI;
|
|
using Godot;
|
|
|
|
public static class UiPageBehaviorFactory
|
|
{
|
|
public static IUiPageBehavior Create<T>(T owner, string key, UiLayer layer)
|
|
where T : CanvasItem
|
|
{
|
|
return null!;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
[AutoUiPage("MainMenu", "Page")]
|
|
public partial class MainMenu : Control
|
|
{
|
|
}
|
|
}
|
|
""";
|
|
|
|
const string expected = """
|
|
// <auto-generated />
|
|
#nullable enable
|
|
|
|
namespace TestApp;
|
|
|
|
partial class MainMenu
|
|
{
|
|
private global::GFramework.Game.Abstractions.UI.IUiPageBehavior? __autoUiPageBehavior_Generated;
|
|
|
|
public static string UiKeyStr => "MainMenu";
|
|
|
|
public global::GFramework.Game.Abstractions.UI.IUiPageBehavior GetPage()
|
|
{
|
|
return __autoUiPageBehavior_Generated ??= global::GFramework.Godot.UI.UiPageBehaviorFactory.Create(this, UiKeyStr, global::GFramework.Game.Abstractions.Enums.UiLayer.Page);
|
|
}
|
|
}
|
|
|
|
""";
|
|
|
|
await GeneratorTest<AutoUiPageGenerator>.RunAsync(
|
|
source,
|
|
("TestApp_MainMenu.AutoUiPage.g.cs", expected));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Reports_Diagnostic_When_AutoUiPage_Attribute_Arguments_Are_Invalid()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
using GFramework.Godot.SourceGenerators.Abstractions;
|
|
using Godot;
|
|
|
|
namespace GFramework.Godot.SourceGenerators.Abstractions
|
|
{
|
|
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
|
public sealed class AutoUiPageAttribute : Attribute
|
|
{
|
|
public AutoUiPageAttribute(string key) { }
|
|
}
|
|
}
|
|
|
|
namespace Godot
|
|
{
|
|
public class Node { }
|
|
public class CanvasItem : Node { }
|
|
public class Control : CanvasItem { }
|
|
}
|
|
|
|
namespace GFramework.Game.Abstractions.Enums
|
|
{
|
|
public enum UiLayer
|
|
{
|
|
Page
|
|
}
|
|
}
|
|
|
|
namespace GFramework.Game.Abstractions.UI
|
|
{
|
|
public interface IUiPageBehavior { }
|
|
}
|
|
|
|
namespace GFramework.Godot.UI
|
|
{
|
|
using GFramework.Game.Abstractions.Enums;
|
|
using GFramework.Game.Abstractions.UI;
|
|
using Godot;
|
|
|
|
public static class UiPageBehaviorFactory
|
|
{
|
|
public static IUiPageBehavior Create<T>(T owner, string key, UiLayer layer)
|
|
where T : CanvasItem
|
|
{
|
|
return null!;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
[{|#0:AutoUiPage("MainMenu")|}]
|
|
public partial class MainMenu : Control
|
|
{
|
|
}
|
|
}
|
|
""";
|
|
|
|
var test = new CSharpSourceGeneratorTest<AutoUiPageGenerator, DefaultVerifier>
|
|
{
|
|
TestState =
|
|
{
|
|
Sources = { source }
|
|
},
|
|
DisabledDiagnostics = { "GF_Common_Trace_001" },
|
|
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck
|
|
};
|
|
|
|
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_AutoBehavior_004", DiagnosticSeverity.Error)
|
|
.WithLocation(0)
|
|
.WithArguments(
|
|
"AutoUiPageAttribute",
|
|
"MainMenu",
|
|
"a string key argument and a string UiLayer name argument"));
|
|
|
|
await test.RunAsync();
|
|
}
|
|
|
|
[Test]
|
|
public async Task Generates_Type_Constraints_For_ClassNullable_NotNull_And_Unmanaged()
|
|
{
|
|
const string source = """
|
|
#nullable enable
|
|
using System;
|
|
using GFramework.Godot.SourceGenerators.Abstractions;
|
|
using Godot;
|
|
|
|
namespace GFramework.Godot.SourceGenerators.Abstractions
|
|
{
|
|
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
|
public sealed class AutoUiPageAttribute : Attribute
|
|
{
|
|
public AutoUiPageAttribute(string key, string layerName) { }
|
|
}
|
|
}
|
|
|
|
namespace Godot
|
|
{
|
|
public class Node { }
|
|
public class CanvasItem : Node { }
|
|
public class Control : CanvasItem { }
|
|
}
|
|
|
|
namespace GFramework.Game.Abstractions.Enums
|
|
{
|
|
public enum UiLayer
|
|
{
|
|
Page
|
|
}
|
|
}
|
|
|
|
namespace GFramework.Game.Abstractions.UI
|
|
{
|
|
public interface IUiPageBehavior { }
|
|
}
|
|
|
|
namespace GFramework.Godot.UI
|
|
{
|
|
using GFramework.Game.Abstractions.Enums;
|
|
using GFramework.Game.Abstractions.UI;
|
|
using Godot;
|
|
|
|
public static class UiPageBehaviorFactory
|
|
{
|
|
public static IUiPageBehavior Create<T>(T owner, string key, UiLayer layer)
|
|
where T : CanvasItem
|
|
{
|
|
return null!;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
[AutoUiPage("MainMenu", "Page")]
|
|
public partial class MainMenu<TReference, TNotNull, TUnmanaged> : Control
|
|
where TReference : class?
|
|
where TNotNull : notnull
|
|
where TUnmanaged : unmanaged
|
|
{
|
|
}
|
|
}
|
|
""";
|
|
|
|
const string expected = """
|
|
// <auto-generated />
|
|
#nullable enable
|
|
|
|
namespace TestApp;
|
|
|
|
partial class MainMenu<TReference, TNotNull, TUnmanaged>
|
|
where TReference : class?
|
|
where TNotNull : notnull
|
|
where TUnmanaged : unmanaged
|
|
{
|
|
private global::GFramework.Game.Abstractions.UI.IUiPageBehavior? __autoUiPageBehavior_Generated;
|
|
|
|
public static string UiKeyStr => "MainMenu";
|
|
|
|
public global::GFramework.Game.Abstractions.UI.IUiPageBehavior GetPage()
|
|
{
|
|
return __autoUiPageBehavior_Generated ??= global::GFramework.Godot.UI.UiPageBehaviorFactory.Create(this, UiKeyStr, global::GFramework.Game.Abstractions.Enums.UiLayer.Page);
|
|
}
|
|
}
|
|
|
|
""";
|
|
|
|
await GeneratorTest<AutoUiPageGenerator>.RunAsync(
|
|
source,
|
|
("TestApp_MainMenu.AutoUiPage.g.cs", expected));
|
|
}
|
|
}
|