83 lines
3.5 KiB
TypeScript
83 lines
3.5 KiB
TypeScript
/** @noSelfInFile */
|
|
|
|
import {
|
|
Suite,
|
|
Test,
|
|
AssertEqual,
|
|
AssertTrue,
|
|
AssertArrayEqual,
|
|
} from "../Harness/TestRunner";
|
|
import { CreateBoundInstance, GetMockState, ResetMockState } from "../Harness/MockUnLuaRuntime";
|
|
import UnLuaBaseActor from "./UnLuaBaseActor";
|
|
import UnLuaSubActor from "./UnLuaSubActor";
|
|
import UnLuaGrandChildActor from "./UnLuaGrandChildActor";
|
|
import UnLuaExplicitWidget from "./UnLuaExplicitWidget";
|
|
|
|
export function RunUnLuaSyntaxTests(): void {
|
|
Suite("Syntax.UnLuaClassFeatures", () => {
|
|
Test("@unlua base class: UnLua.Class(), self.Overridden, static methods and destructured params", () => {
|
|
ResetMockState();
|
|
const actor = CreateBoundInstance<UnLuaBaseActor>(
|
|
UnLuaBaseActor as unknown as Record<string, any>,
|
|
"AActor"
|
|
);
|
|
actor.ReceiveBeginPlay();
|
|
|
|
AssertEqual(actor.GetHp(), 100);
|
|
AssertArrayEqual(actor.GetTrace(), ["Base.ReceiveBeginPlay"]);
|
|
AssertArrayEqual(GetMockState().overriddenCalls, ["AActor.ReceiveBeginPlay"]);
|
|
|
|
// 解构对象与数组参数(含默认值与 rest)
|
|
AssertEqual(actor.ApplyConfig({ name: "Hero" }), "Hero:100");
|
|
AssertEqual(actor.ApplyConfig({ name: "Boss", hp: 500 }), "Boss:500");
|
|
AssertEqual(actor.SumScores([10]), 15);
|
|
AssertEqual(actor.SumScores([1, 2, 3, 4]), 10);
|
|
|
|
// 静态方法(含 self 冒号调用与 @noSelf/this:void 点号调用)
|
|
AssertEqual(UnLuaBaseActor.StaticWithSelf(25), 125);
|
|
AssertEqual(UnLuaBaseActor.StaticNoSelf(7, 8), 15);
|
|
AssertEqual(UnLuaBaseActor.StaticDocNoSelf(9), 27);
|
|
|
|
// UObject IsA 与 IsValid
|
|
AssertTrue(actor.IsA(UE.AActor.StaticClass()));
|
|
AssertTrue(UE.UKismetSystemLibrary.IsValid(actor));
|
|
});
|
|
|
|
Test("@unlua 3-level inheritance (GrandChild -> Sub -> Base) avoids self.Super infinite recursion", () => {
|
|
ResetMockState();
|
|
const grand = CreateBoundInstance<UnLuaGrandChildActor>(
|
|
UnLuaGrandChildActor as unknown as Record<string, any>,
|
|
"AActor"
|
|
);
|
|
grand.ReceiveBeginPlay();
|
|
|
|
AssertArrayEqual(grand.GetTrace(), [
|
|
"Base.ReceiveBeginPlay",
|
|
"Sub.ReceiveBeginPlay",
|
|
"GrandChild.ReceiveBeginPlay",
|
|
]);
|
|
AssertArrayEqual(GetMockState().overriddenCalls, ["AActor.ReceiveBeginPlay"]);
|
|
AssertEqual(grand.GetFaction(), "Ally");
|
|
AssertEqual(grand.GetTier(), 3);
|
|
|
|
// Base: 10*2 = 20, Sub: +10 = 30, GrandChild: +3 = 33
|
|
AssertEqual(grand.ComputeBonus(10), 33);
|
|
AssertEqual(grand.ComputeBonus(10, 4), 53);
|
|
|
|
// 混合默认+纯类型导入的父类方法调用
|
|
const summary = grand.SetupWithFaction({ name: "Knight", hp: 250 }, "Enemy");
|
|
AssertEqual(summary, "Knight:250[Enemy]");
|
|
AssertEqual(grand.GetFaction(), "Enemy");
|
|
});
|
|
|
|
Test("@unlua explicit base path class without extends", () => {
|
|
const widgetCls = UnLuaExplicitWidget as unknown as Record<string, any>;
|
|
AssertEqual(widgetCls.Super, UnLuaBaseActor);
|
|
|
|
const widget = CreateBoundInstance<UnLuaExplicitWidget>(widgetCls, "UUserWidget");
|
|
widget.Construct({ title: "MainHUD" });
|
|
AssertEqual(widget.GetTitle(), "MainHUD");
|
|
});
|
|
});
|
|
}
|