mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-07 08:44:29 +08:00
- 实现 AutoSceneGenerator 为标记了 [AutoScene] 的 Godot 节点生成场景行为样板 - 实现 AutoRegisterExportedCollectionsGenerator 为导出集合生成批量注册方法 - 添加完整的单元测试覆盖两种源代码生成器的功能和诊断 - 支持泛型类型参数约束的正确生成 - 提供详细的诊断信息帮助用户修复配置错误
220 lines
9.3 KiB
C#
220 lines
9.3 KiB
C#
using GFramework.Godot.SourceGenerators.Behavior;
|
|
using GFramework.Godot.SourceGenerators.Tests.Core;
|
|
|
|
namespace GFramework.Godot.SourceGenerators.Tests.Behavior;
|
|
|
|
[TestFixture]
|
|
public class AutoSceneGeneratorTests
|
|
{
|
|
[Test]
|
|
public async Task Generates_Scene_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 AutoSceneAttribute : Attribute
|
|
{
|
|
public AutoSceneAttribute(string key) { }
|
|
}
|
|
}
|
|
|
|
namespace Godot
|
|
{
|
|
public class Node { }
|
|
public class Node2D : Node { }
|
|
}
|
|
|
|
namespace GFramework.Game.Abstractions.Scene
|
|
{
|
|
public interface ISceneBehavior { }
|
|
}
|
|
|
|
namespace GFramework.Godot.Scene
|
|
{
|
|
using GFramework.Game.Abstractions.Scene;
|
|
using Godot;
|
|
|
|
public static class SceneBehaviorFactory
|
|
{
|
|
public static ISceneBehavior Create<T>(T owner, string key)
|
|
where T : Node
|
|
{
|
|
return null!;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
[AutoScene("Gameplay")]
|
|
public partial class GameplayRoot : Node2D
|
|
{
|
|
}
|
|
}
|
|
""";
|
|
|
|
const string expected = """
|
|
// <auto-generated />
|
|
#nullable enable
|
|
|
|
namespace TestApp;
|
|
|
|
partial class GameplayRoot
|
|
{
|
|
private global::GFramework.Game.Abstractions.Scene.ISceneBehavior? __autoSceneBehavior_Generated;
|
|
|
|
public static string SceneKeyStr => "Gameplay";
|
|
|
|
public global::GFramework.Game.Abstractions.Scene.ISceneBehavior GetScene()
|
|
{
|
|
return __autoSceneBehavior_Generated ??= global::GFramework.Godot.Scene.SceneBehaviorFactory.Create(this, SceneKeyStr);
|
|
}
|
|
}
|
|
|
|
""";
|
|
|
|
await GeneratorTest<AutoSceneGenerator>.RunAsync(
|
|
source,
|
|
("TestApp_GameplayRoot.AutoScene.g.cs", expected));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Reports_Diagnostic_When_AutoScene_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 AutoSceneAttribute : Attribute
|
|
{
|
|
public AutoSceneAttribute() { }
|
|
}
|
|
}
|
|
|
|
namespace Godot
|
|
{
|
|
public class Node { }
|
|
public class Node2D : Node { }
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
[{|#0:AutoScene|}]
|
|
public partial class GameplayRoot : Node2D
|
|
{
|
|
}
|
|
}
|
|
""";
|
|
|
|
var test = new CSharpSourceGeneratorTest<AutoSceneGenerator, DefaultVerifier>
|
|
{
|
|
TestState =
|
|
{
|
|
Sources = { source }
|
|
},
|
|
DisabledDiagnostics = { "GF_Common_Trace_001" }
|
|
};
|
|
|
|
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_AutoBehavior_004", DiagnosticSeverity.Error)
|
|
.WithLocation(0)
|
|
.WithArguments("AutoSceneAttribute", "GameplayRoot", "a single string scene key argument"));
|
|
|
|
await test.RunAsync();
|
|
}
|
|
|
|
[Test]
|
|
public async Task Generates_Type_Constraints_For_Nullable_Reference_NotNull_And_Unmanaged_Parameters()
|
|
{
|
|
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 AutoSceneAttribute : Attribute
|
|
{
|
|
public AutoSceneAttribute(string key) { }
|
|
}
|
|
}
|
|
|
|
namespace Godot
|
|
{
|
|
public class Node { }
|
|
public class Node2D : Node { }
|
|
}
|
|
|
|
namespace GFramework.Game.Abstractions.Scene
|
|
{
|
|
public interface ISceneBehavior { }
|
|
}
|
|
|
|
namespace GFramework.Godot.Scene
|
|
{
|
|
using GFramework.Game.Abstractions.Scene;
|
|
using Godot;
|
|
|
|
public static class SceneBehaviorFactory
|
|
{
|
|
public static ISceneBehavior Create<T>(T owner, string key)
|
|
where T : Node
|
|
{
|
|
return null!;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
[AutoScene("Gameplay")]
|
|
public partial class GameplayRoot<TReference, TNotNull, TValue, TUnmanaged> : Node2D
|
|
where TReference : class?
|
|
where TNotNull : notnull
|
|
where TValue : struct
|
|
where TUnmanaged : unmanaged
|
|
{
|
|
}
|
|
}
|
|
""";
|
|
|
|
const string expected = """
|
|
// <auto-generated />
|
|
#nullable enable
|
|
|
|
namespace TestApp;
|
|
|
|
partial class GameplayRoot<TReference, TNotNull, TValue, TUnmanaged>
|
|
where TReference : class?
|
|
where TNotNull : notnull
|
|
where TValue : struct
|
|
where TUnmanaged : unmanaged
|
|
{
|
|
private global::GFramework.Game.Abstractions.Scene.ISceneBehavior? __autoSceneBehavior_Generated;
|
|
|
|
public static string SceneKeyStr => "Gameplay";
|
|
|
|
public global::GFramework.Game.Abstractions.Scene.ISceneBehavior GetScene()
|
|
{
|
|
return __autoSceneBehavior_Generated ??= global::GFramework.Godot.Scene.SceneBehaviorFactory.Create(this, SceneKeyStr);
|
|
}
|
|
}
|
|
|
|
""";
|
|
|
|
await GeneratorTest<AutoSceneGenerator>.RunAsync(
|
|
source,
|
|
("TestApp_GameplayRoot.AutoScene.g.cs", expected));
|
|
}
|
|
}
|