mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-04-01 02:56:44 +08:00
- 实现 BindNodeSignalGenerator 源生成器,用于自动生成 Godot 节点事件绑定与解绑逻辑 - 添加 BindNodeSignalAttribute 特性,标记需要生成绑定逻辑的事件处理方法 - 实现完整的诊断系统,包括嵌套类型、静态方法、字段类型等错误检查 - 添加生命周期方法调用检查,在 _Ready 和 _ExitTree 中验证生成方法的调用 - 支持方法签名与事件委托的兼容性验证 - 实现单元测试覆盖各种使用场景和错误情况
467 lines
20 KiB
C#
467 lines
20 KiB
C#
using GFramework.Godot.SourceGenerators.Tests.Core;
|
|
|
|
namespace GFramework.Godot.SourceGenerators.Tests.BindNodeSignal;
|
|
|
|
/// <summary>
|
|
/// 验证 <see cref="BindNodeSignalGenerator" /> 的生成与诊断行为。
|
|
/// </summary>
|
|
[TestFixture]
|
|
public class BindNodeSignalGeneratorTests
|
|
{
|
|
/// <summary>
|
|
/// 验证生成器会为已有生命周期调用生成成对的绑定与解绑方法。
|
|
/// </summary>
|
|
[Test]
|
|
public async Task Generates_Bind_And_Unbind_Methods_For_Existing_Lifecycle_Hooks()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
using GFramework.Godot.SourceGenerators.Abstractions;
|
|
using Godot;
|
|
|
|
namespace GFramework.Godot.SourceGenerators.Abstractions
|
|
{
|
|
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
|
|
public sealed class BindNodeSignalAttribute : Attribute
|
|
{
|
|
public BindNodeSignalAttribute(string nodeFieldName, string signalName)
|
|
{
|
|
NodeFieldName = nodeFieldName;
|
|
SignalName = signalName;
|
|
}
|
|
|
|
public string NodeFieldName { get; }
|
|
|
|
public string SignalName { get; }
|
|
}
|
|
}
|
|
|
|
namespace Godot
|
|
{
|
|
public class Node
|
|
{
|
|
public virtual void _Ready() {}
|
|
|
|
public virtual void _ExitTree() {}
|
|
}
|
|
|
|
public class Button : Node
|
|
{
|
|
public event Action? Pressed
|
|
{
|
|
add {}
|
|
remove {}
|
|
}
|
|
}
|
|
|
|
public class SpinBox : Node
|
|
{
|
|
public delegate void ValueChangedEventHandler(double value);
|
|
|
|
public event ValueChangedEventHandler? ValueChanged
|
|
{
|
|
add {}
|
|
remove {}
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
public partial class Hud : Node
|
|
{
|
|
private Button _startButton = null!;
|
|
private SpinBox _startOreSpinBox = null!;
|
|
|
|
[BindNodeSignal(nameof(_startButton), nameof(Button.Pressed))]
|
|
private void OnStartButtonPressed()
|
|
{
|
|
}
|
|
|
|
[BindNodeSignal(nameof(_startOreSpinBox), nameof(SpinBox.ValueChanged))]
|
|
private void OnStartOreValueChanged(double value)
|
|
{
|
|
}
|
|
|
|
public override void _Ready()
|
|
{
|
|
__BindNodeSignals_Generated();
|
|
}
|
|
|
|
public override void _ExitTree()
|
|
{
|
|
__UnbindNodeSignals_Generated();
|
|
}
|
|
}
|
|
}
|
|
""";
|
|
|
|
const string expected = """
|
|
// <auto-generated />
|
|
#nullable enable
|
|
|
|
namespace TestApp;
|
|
|
|
partial class Hud
|
|
{
|
|
private void __BindNodeSignals_Generated()
|
|
{
|
|
_startButton.Pressed += OnStartButtonPressed;
|
|
_startOreSpinBox.ValueChanged += OnStartOreValueChanged;
|
|
}
|
|
|
|
private void __UnbindNodeSignals_Generated()
|
|
{
|
|
_startButton.Pressed -= OnStartButtonPressed;
|
|
_startOreSpinBox.ValueChanged -= OnStartOreValueChanged;
|
|
}
|
|
}
|
|
|
|
""";
|
|
|
|
await GeneratorTest<BindNodeSignalGenerator>.RunAsync(
|
|
source,
|
|
("TestApp_Hud.BindNodeSignal.g.cs", expected));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 验证一个处理方法可以通过多个特性绑定到多个节点事件,且能与 GetNode 声明共存。
|
|
/// </summary>
|
|
[Test]
|
|
public async Task Generates_Multiple_Subscriptions_For_The_Same_Handler_And_Coexists_With_GetNode()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
using GFramework.Godot.SourceGenerators.Abstractions;
|
|
using Godot;
|
|
|
|
namespace GFramework.Godot.SourceGenerators.Abstractions
|
|
{
|
|
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
|
|
public sealed class BindNodeSignalAttribute : Attribute
|
|
{
|
|
public BindNodeSignalAttribute(string nodeFieldName, string signalName)
|
|
{
|
|
NodeFieldName = nodeFieldName;
|
|
SignalName = signalName;
|
|
}
|
|
|
|
public string NodeFieldName { get; }
|
|
|
|
public string SignalName { get; }
|
|
}
|
|
|
|
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
|
|
public sealed class GetNodeAttribute : Attribute
|
|
{
|
|
}
|
|
}
|
|
|
|
namespace Godot
|
|
{
|
|
public class Node
|
|
{
|
|
public virtual void _Ready() {}
|
|
|
|
public virtual void _ExitTree() {}
|
|
}
|
|
|
|
public class Button : Node
|
|
{
|
|
public event Action? Pressed
|
|
{
|
|
add {}
|
|
remove {}
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
public partial class Hud : Node
|
|
{
|
|
[GetNode]
|
|
private Button _startButton = null!;
|
|
|
|
[GetNode]
|
|
private Button _cancelButton = null!;
|
|
|
|
[BindNodeSignal(nameof(_startButton), nameof(Button.Pressed))]
|
|
[BindNodeSignal(nameof(_cancelButton), nameof(Button.Pressed))]
|
|
private void OnAnyButtonPressed()
|
|
{
|
|
}
|
|
}
|
|
}
|
|
""";
|
|
|
|
const string expected = """
|
|
// <auto-generated />
|
|
#nullable enable
|
|
|
|
namespace TestApp;
|
|
|
|
partial class Hud
|
|
{
|
|
private void __BindNodeSignals_Generated()
|
|
{
|
|
_startButton.Pressed += OnAnyButtonPressed;
|
|
_cancelButton.Pressed += OnAnyButtonPressed;
|
|
}
|
|
|
|
private void __UnbindNodeSignals_Generated()
|
|
{
|
|
_startButton.Pressed -= OnAnyButtonPressed;
|
|
_cancelButton.Pressed -= OnAnyButtonPressed;
|
|
}
|
|
}
|
|
|
|
""";
|
|
|
|
await GeneratorTest<BindNodeSignalGenerator>.RunAsync(
|
|
source,
|
|
("TestApp_Hud.BindNodeSignal.g.cs", expected));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 验证引用不存在的事件时会报告错误。
|
|
/// </summary>
|
|
[Test]
|
|
public async Task Reports_Diagnostic_When_Signal_Does_Not_Exist()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
using GFramework.Godot.SourceGenerators.Abstractions;
|
|
using Godot;
|
|
|
|
namespace GFramework.Godot.SourceGenerators.Abstractions
|
|
{
|
|
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
|
|
public sealed class BindNodeSignalAttribute : Attribute
|
|
{
|
|
public BindNodeSignalAttribute(string nodeFieldName, string signalName)
|
|
{
|
|
NodeFieldName = nodeFieldName;
|
|
SignalName = signalName;
|
|
}
|
|
|
|
public string NodeFieldName { get; }
|
|
|
|
public string SignalName { get; }
|
|
}
|
|
}
|
|
|
|
namespace Godot
|
|
{
|
|
public class Node
|
|
{
|
|
}
|
|
|
|
public class Button : Node
|
|
{
|
|
public event Action? Pressed
|
|
{
|
|
add {}
|
|
remove {}
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
public partial class Hud : Node
|
|
{
|
|
private Button _startButton = null!;
|
|
|
|
[{|#0:BindNodeSignal(nameof(_startButton), "Released")|}]
|
|
private void OnStartButtonPressed()
|
|
{
|
|
}
|
|
}
|
|
}
|
|
""";
|
|
|
|
var test = new CSharpSourceGeneratorTest<BindNodeSignalGenerator, DefaultVerifier>
|
|
{
|
|
TestState =
|
|
{
|
|
Sources = { source }
|
|
},
|
|
DisabledDiagnostics = { "GF_Common_Trace_001" },
|
|
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck
|
|
};
|
|
|
|
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_Godot_BindNodeSignal_006", DiagnosticSeverity.Error)
|
|
.WithLocation(0)
|
|
.WithArguments("_startButton", "Released"));
|
|
|
|
await test.RunAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 验证方法签名与事件委托不匹配时会报告错误。
|
|
/// </summary>
|
|
[Test]
|
|
public async Task Reports_Diagnostic_When_Method_Signature_Does_Not_Match_Event()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
using GFramework.Godot.SourceGenerators.Abstractions;
|
|
using Godot;
|
|
|
|
namespace GFramework.Godot.SourceGenerators.Abstractions
|
|
{
|
|
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
|
|
public sealed class BindNodeSignalAttribute : Attribute
|
|
{
|
|
public BindNodeSignalAttribute(string nodeFieldName, string signalName)
|
|
{
|
|
NodeFieldName = nodeFieldName;
|
|
SignalName = signalName;
|
|
}
|
|
|
|
public string NodeFieldName { get; }
|
|
|
|
public string SignalName { get; }
|
|
}
|
|
}
|
|
|
|
namespace Godot
|
|
{
|
|
public class Node
|
|
{
|
|
}
|
|
|
|
public class SpinBox : Node
|
|
{
|
|
public delegate void ValueChangedEventHandler(double value);
|
|
|
|
public event ValueChangedEventHandler? ValueChanged
|
|
{
|
|
add {}
|
|
remove {}
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
public partial class Hud : Node
|
|
{
|
|
private SpinBox _startOreSpinBox = null!;
|
|
|
|
[{|#0:BindNodeSignal(nameof(_startOreSpinBox), nameof(SpinBox.ValueChanged))|}]
|
|
private void OnStartOreValueChanged()
|
|
{
|
|
}
|
|
}
|
|
}
|
|
""";
|
|
|
|
var test = new CSharpSourceGeneratorTest<BindNodeSignalGenerator, DefaultVerifier>
|
|
{
|
|
TestState =
|
|
{
|
|
Sources = { source }
|
|
},
|
|
DisabledDiagnostics = { "GF_Common_Trace_001" },
|
|
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck
|
|
};
|
|
|
|
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_Godot_BindNodeSignal_007", DiagnosticSeverity.Error)
|
|
.WithLocation(0)
|
|
.WithArguments("OnStartOreValueChanged", "ValueChanged", "_startOreSpinBox"));
|
|
|
|
await test.RunAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 验证已有生命周期方法但未调用生成方法时会报告对称的警告。
|
|
/// </summary>
|
|
[Test]
|
|
public async Task Reports_Warnings_When_Lifecycle_Methods_Do_Not_Call_Generated_Methods()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
using GFramework.Godot.SourceGenerators.Abstractions;
|
|
using Godot;
|
|
|
|
namespace GFramework.Godot.SourceGenerators.Abstractions
|
|
{
|
|
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
|
|
public sealed class BindNodeSignalAttribute : Attribute
|
|
{
|
|
public BindNodeSignalAttribute(string nodeFieldName, string signalName)
|
|
{
|
|
NodeFieldName = nodeFieldName;
|
|
SignalName = signalName;
|
|
}
|
|
|
|
public string NodeFieldName { get; }
|
|
|
|
public string SignalName { get; }
|
|
}
|
|
}
|
|
|
|
namespace Godot
|
|
{
|
|
public class Node
|
|
{
|
|
public virtual void _Ready() {}
|
|
|
|
public virtual void _ExitTree() {}
|
|
}
|
|
|
|
public class Button : Node
|
|
{
|
|
public event Action? Pressed
|
|
{
|
|
add {}
|
|
remove {}
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
public partial class Hud : Node
|
|
{
|
|
private Button _startButton = null!;
|
|
|
|
[BindNodeSignal(nameof(_startButton), nameof(Button.Pressed))]
|
|
private void OnStartButtonPressed()
|
|
{
|
|
}
|
|
|
|
public override void {|#0:_Ready|}()
|
|
{
|
|
}
|
|
|
|
public override void {|#1:_ExitTree|}()
|
|
{
|
|
}
|
|
}
|
|
}
|
|
""";
|
|
|
|
var test = new CSharpSourceGeneratorTest<BindNodeSignalGenerator, DefaultVerifier>
|
|
{
|
|
TestState =
|
|
{
|
|
Sources = { source }
|
|
},
|
|
DisabledDiagnostics = { "GF_Common_Trace_001" },
|
|
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck
|
|
};
|
|
|
|
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_Godot_BindNodeSignal_008", DiagnosticSeverity.Warning)
|
|
.WithLocation(0)
|
|
.WithArguments("Hud"));
|
|
|
|
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_Godot_BindNodeSignal_009", DiagnosticSeverity.Warning)
|
|
.WithLocation(1)
|
|
.WithArguments("Hud"));
|
|
|
|
await test.RunAsync();
|
|
}
|
|
} |