feat(test): add real UE 5.8 RunLuaTests commandlet, Lua.RunTests command, and fix UnLua .d.ts math/UObject bugs
This commit is contained in:
1 parent
3dd61c32a9
commit
96e3f0d009
29 files changed
+1417
-134
No files matched your search
+7
-1
@@ -30,6 +30,7 @@ Script/ts/
|
||||
├── UnLuaExamples/ # 参考 UnLua 官方示例 01~09 的 TS 移植与验证
|
||||
├── Gameplay/ # 常用游戏系统实现(FSM/对象池/定时器/Buff属性/背包/协程/行为树)
|
||||
├── Extra/ # 框架集成、热重载、Lua 5.4 陷阱与生命周期安全测试
|
||||
├── UE/ # 真实 UE 5.8 + UnLua 2.3.6 引擎集成测试套件(UERuntimeSuite)
|
||||
└── RunAllTests.ts # 统一测试入口(支持 lua5.4 独立运行或在 UE 内调用)
|
||||
```
|
||||
|
||||
@@ -44,9 +45,12 @@ npm install
|
||||
# 全量编译到 ../lua
|
||||
npm run build
|
||||
|
||||
# 编译并在本机 Lua 5.4 下运行全部自动化测试
|
||||
# 编译并在本机 Lua 5.4 下运行全部自动化测试(~3s)
|
||||
npm test
|
||||
|
||||
# 编译并在真实 UE 5.8 + UnLua 2.3.6 引擎内无头运行全量测试(-run=RunLuaTests)
|
||||
npm run test:ue
|
||||
|
||||
# 增量监听模式(开发期配合 PIE + UnLua HotReload 使用)
|
||||
npm run watch
|
||||
|
||||
@@ -54,6 +58,8 @@ npm run watch
|
||||
npm run info
|
||||
```
|
||||
|
||||
> 在 UE 编辑器 PIE 运行期间,也可以直接打开控制台输入 `Lua.RunTests` 在当前 PIE `UWorld` 中运行全量测试。
|
||||
|
||||
## 规范与文档
|
||||
|
||||
- 项目编码约束:[AGENTS.md](../../AGENTS.md)
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
"build": "ts2lua-unreal build",
|
||||
"watch": "ts2lua-unreal watch",
|
||||
"test": "npm run build && node scripts/run-tests.mjs",
|
||||
"test:ue": "npm run build && /Applications/Unreal5/UE_5.8/Engine/Binaries/Mac/UnrealEditor-Cmd \"$(pwd)/../../UnrealLuaDemo.uproject\" -run=RunLuaTests -unattended -nop4 -nosplash -NullRHI",
|
||||
"info": "ts2lua-unreal info"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -51,8 +51,7 @@ export function RunLifecycleAndMemoryTests(): void {
|
||||
"AActor"
|
||||
);
|
||||
const asset = UE.UObject.Load("/Game/Data/DA_Config.DA_Config")!;
|
||||
const weak = UE.FWeakObjectPtr();
|
||||
weak.Set(actor);
|
||||
const weak = UE.FWeakObjectPtr(actor);
|
||||
|
||||
actor.ReceiveBeginPlay();
|
||||
actor.HoldManualRef(asset);
|
||||
|
||||
@@ -287,8 +287,16 @@ export function RunGameplayTests(): void {
|
||||
AssertArrayEqual(trace, ["start", "after_delay", "entered_room"]);
|
||||
AssertEqual(runner.GetActiveTaskCount(), 0);
|
||||
|
||||
// 绑定 UObject 存活自动取消测试
|
||||
const ownerActor = CreateBoundInstance<UE.AActor>({}, "AActor");
|
||||
// 绑定 UObject 存活自动取消测试(兼容独立 Mock 环境与真实 UE 环境)
|
||||
const world = _G.__UE_TEST_WORLD;
|
||||
const ownerActor =
|
||||
world !== undefined
|
||||
? world.SpawnActor<UE.AActor>(
|
||||
UE.AActor.StaticClass(),
|
||||
UE.FTransform(UE.FRotator(0, 0, 0).ToQuat(), UE.FVector(0, 0, 0), UE.FVector(1, 1, 1)),
|
||||
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn
|
||||
)!
|
||||
: CreateBoundInstance<UE.AActor>({}, "AActor");
|
||||
let postDestroyExecuted = false;
|
||||
runner.Start(() => {
|
||||
CoroutineTaskRunner.WaitSeconds(1.0);
|
||||
|
||||
@@ -301,7 +301,7 @@ function CreateMockTArray(): UE.TArray<any> {
|
||||
},
|
||||
AddUnique(this: unknown, item: any) {
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
if (data[i] === item) return 0; // INDEX_NONE + 1 in UnLua = 0
|
||||
if (data[i] === item) return i + 1; // UnLua C++ 返回已有元素的 1-based 索引
|
||||
}
|
||||
data[data.length] = item;
|
||||
return data.length;
|
||||
@@ -524,27 +524,47 @@ function CreateMockFVector(x?: number, y?: number, z?: number): UE.FVector {
|
||||
},
|
||||
Add(this: Record<string, any>, other: any) {
|
||||
if (type(other) === "number") {
|
||||
return CreateMockFVector(this.X + other, this.Y + other, this.Z + other);
|
||||
this.X += other;
|
||||
this.Y += other;
|
||||
this.Z += other;
|
||||
} else {
|
||||
this.X += other.X;
|
||||
this.Y += other.Y;
|
||||
this.Z += other.Z;
|
||||
}
|
||||
return CreateMockFVector(this.X + other.X, this.Y + other.Y, this.Z + other.Z);
|
||||
},
|
||||
Sub(this: Record<string, any>, other: any) {
|
||||
if (type(other) === "number") {
|
||||
return CreateMockFVector(this.X - other, this.Y - other, this.Z - other);
|
||||
this.X -= other;
|
||||
this.Y -= other;
|
||||
this.Z -= other;
|
||||
} else {
|
||||
this.X -= other.X;
|
||||
this.Y -= other.Y;
|
||||
this.Z -= other.Z;
|
||||
}
|
||||
return CreateMockFVector(this.X - other.X, this.Y - other.Y, this.Z - other.Z);
|
||||
},
|
||||
Mul(this: Record<string, any>, other: any) {
|
||||
if (type(other) === "number") {
|
||||
return CreateMockFVector(this.X * other, this.Y * other, this.Z * other);
|
||||
this.X *= other;
|
||||
this.Y *= other;
|
||||
this.Z *= other;
|
||||
} else {
|
||||
this.X *= other.X;
|
||||
this.Y *= other.Y;
|
||||
this.Z *= other.Z;
|
||||
}
|
||||
return CreateMockFVector(this.X * other.X, this.Y * other.Y, this.Z * other.Z);
|
||||
},
|
||||
Div(this: Record<string, any>, other: any) {
|
||||
if (type(other) === "number") {
|
||||
return CreateMockFVector(this.X / other, this.Y / other, this.Z / other);
|
||||
this.X /= other;
|
||||
this.Y /= other;
|
||||
this.Z /= other;
|
||||
} else {
|
||||
this.X /= other.X;
|
||||
this.Y /= other.Y;
|
||||
this.Z /= other.Z;
|
||||
}
|
||||
return CreateMockFVector(this.X / other.X, this.Y / other.Y, this.Z / other.Z);
|
||||
},
|
||||
Dot(this: Record<string, any>, other: any) {
|
||||
return this.X * other.X + this.Y * other.Y + this.Z * other.Z;
|
||||
@@ -846,8 +866,8 @@ export function InstallIfNeeded(): void {
|
||||
bNoFail: false,
|
||||
SpawnCollisionHandlingOverride: 0,
|
||||
}),
|
||||
FWeakObjectPtr: () => {
|
||||
let refObj: Record<string, any> | undefined = undefined;
|
||||
FWeakObjectPtr: (initialObj?: Record<string, any>) => {
|
||||
let refObj: Record<string, any> | undefined = initialObj;
|
||||
return {
|
||||
Set(this: unknown, o: Record<string, any> | undefined) {
|
||||
refObj = o;
|
||||
@@ -1046,6 +1066,10 @@ export function InstallIfNeeded(): void {
|
||||
(startVec.Z + endVec.Z) * 0.5
|
||||
);
|
||||
hit.ImpactPoint = hit.Location;
|
||||
// 还原 UnLua 真实语义:传入复用 Out 参数时原地修改并返回单值;传入 nil 时返回多值 (bHit, newHit)
|
||||
if (outHit !== undefined) {
|
||||
return $multi(bHit, undefined as unknown as Record<string, any>);
|
||||
}
|
||||
return $multi(bHit, hit);
|
||||
},
|
||||
},
|
||||
|
||||
@@ -145,28 +145,41 @@ export function GetSummary(): SuiteSummary {
|
||||
};
|
||||
}
|
||||
|
||||
export function PrintReport(): SuiteSummary {
|
||||
const summary = GetSummary();
|
||||
export function FormatReport(summary?: SuiteSummary): string {
|
||||
const s = summary ?? GetSummary();
|
||||
const lines: string[] = [];
|
||||
let lastSuite = "";
|
||||
for (let i = 0; i < summary.results.length; i++) {
|
||||
const r = summary.results[i];
|
||||
for (let i = 0; i < s.results.length; i++) {
|
||||
const r = s.results[i];
|
||||
if (r.suite !== lastSuite) {
|
||||
lastSuite = r.suite;
|
||||
print(string.format("\n[%s]", r.suite));
|
||||
lines[lines.length] = "";
|
||||
lines[lines.length] = string.format("[%s]", r.suite);
|
||||
}
|
||||
if (r.passed) {
|
||||
print(string.format(" PASS %s", r.name));
|
||||
lines[lines.length] = string.format(" PASS %s", r.name);
|
||||
} else {
|
||||
print(string.format(" FAIL %s\n -> %s", r.name, r.errorMessage ?? "unknown error"));
|
||||
lines[lines.length] = string.format(
|
||||
" FAIL %s\n -> %s",
|
||||
r.name,
|
||||
r.errorMessage ?? "unknown error"
|
||||
);
|
||||
}
|
||||
}
|
||||
print(
|
||||
string.format(
|
||||
"\n========================================\nTotal: %d | Passed: %d | Failed: %d\n========================================",
|
||||
summary.total,
|
||||
summary.passed,
|
||||
summary.failed
|
||||
)
|
||||
lines[lines.length] = "";
|
||||
lines[lines.length] = "========================================";
|
||||
lines[lines.length] = string.format(
|
||||
"Total: %d | Passed: %d | Failed: %d",
|
||||
s.total,
|
||||
s.passed,
|
||||
s.failed
|
||||
);
|
||||
lines[lines.length] = "========================================";
|
||||
return table.concat(lines, "\n");
|
||||
}
|
||||
|
||||
export function PrintReport(): SuiteSummary {
|
||||
const summary = GetSummary();
|
||||
print(FormatReport(summary));
|
||||
return summary;
|
||||
}
|
||||
@@ -0,0 +1,364 @@
|
||||
/** @noSelfInFile */
|
||||
|
||||
import {
|
||||
Reset,
|
||||
PrintReport,
|
||||
Suite,
|
||||
Test,
|
||||
AssertEqual,
|
||||
AssertNear,
|
||||
AssertTrue,
|
||||
AssertFalse,
|
||||
AssertNotNil,
|
||||
AssertNil,
|
||||
AssertArrayEqual,
|
||||
SuiteSummary,
|
||||
} from "../Harness/TestRunner";
|
||||
import { RunBasicTypesAndControlFlowTests } from "../Syntax/BasicTypesAndControlFlow";
|
||||
import { RunFunctionsClosuresAndTuplesTests } from "../Syntax/FunctionsClosuresAndTuples";
|
||||
import { RunPureClassesAndGenericsTests } from "../Syntax/PureClassesAndGenerics";
|
||||
import { RunGameplayTests } from "../Gameplay/GameplaySuite";
|
||||
import { RunLua54PitfallsTests } from "../Extra/Lua54PitfallsTest";
|
||||
import * as EventBus from "../../Framework/EventBus";
|
||||
import * as HotReload from "../../Framework/HotReload";
|
||||
import {
|
||||
VerifyTArrayOperations,
|
||||
VerifyTMapOperations,
|
||||
VerifyTSetOperations,
|
||||
} from "../UnLuaExamples/Ex06_NativeContainers";
|
||||
import type UnLuaBaseActor from "../Syntax/UnLuaBaseActor";
|
||||
import UnLuaBaseActorClass from "../Syntax/UnLuaBaseActor";
|
||||
import type UnLuaGrandChildActor from "../Syntax/UnLuaGrandChildActor";
|
||||
import type Ex01_HelloWorld from "../UnLuaExamples/Ex01_HelloWorld";
|
||||
import type Ex02_OverrideBlueprintEvents from "../UnLuaExamples/Ex02_OverrideBlueprintEvents";
|
||||
import type Ex04_DynamicSpawnedActor from "../UnLuaExamples/Ex04_DynamicSpawnedActor";
|
||||
import type Ex04_DynamicObject from "../UnLuaExamples/Ex04_DynamicObject";
|
||||
import type Ex07_CallLatentFunction from "../UnLuaExamples/Ex07_CallLatentFunction";
|
||||
import type Ex08_StructAndTrace from "../UnLuaExamples/Ex08_StructAndTrace";
|
||||
import type UETimerTestActor from "./UETimerTestActor";
|
||||
|
||||
function TickUEWorld(steps: number, deltaSeconds: number): void {
|
||||
const tickFn = _G.__UE_TEST_TICK;
|
||||
if (tickFn === undefined) {
|
||||
return;
|
||||
}
|
||||
for (let i = 0; i < steps; i++) {
|
||||
tickFn(deltaSeconds);
|
||||
}
|
||||
}
|
||||
|
||||
export function RunUERuntimeSuite(world: UE.UWorld): void {
|
||||
const canTickWorld = _G.__UE_TEST_TICK !== undefined;
|
||||
|
||||
Suite("UE.RealEngineIntegration", () => {
|
||||
Test("Real UE C++ TArray / TMap / TSet containers & native __pairs iteration", () => {
|
||||
VerifyTArrayOperations();
|
||||
VerifyTMapOperations();
|
||||
VerifyTSetOperations();
|
||||
});
|
||||
|
||||
Test("Real UE SpawnActor + @unlua UnLuaBaseActor (Overridden, static methods, destructuring)", () => {
|
||||
// UnLua 动态绑定按 UClass 缓存映射,每个独立 @unlua 测试类使用各自的轻量级原生 UClass
|
||||
const actor = world.SpawnActor<UnLuaBaseActor>(
|
||||
UE.ATargetPoint.StaticClass(),
|
||||
UE.FTransform(UE.FRotator(0, 0, 0).ToQuat(), UE.FVector(0, 0, 0), UE.FVector(1, 1, 1)),
|
||||
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn,
|
||||
undefined,
|
||||
undefined,
|
||||
"Tests.Syntax.UnLuaBaseActor"
|
||||
);
|
||||
AssertNotNil(actor);
|
||||
AssertTrue(UE.UKismetSystemLibrary.IsValid(actor));
|
||||
AssertTrue(actor!.IsA(UE.AActor.StaticClass()));
|
||||
|
||||
// SpawnActor 会自动触发 ReceiveBeginPlay
|
||||
AssertEqual(actor!.GetHp(), 100);
|
||||
AssertArrayEqual(actor!.GetTrace(), ["Base.ReceiveBeginPlay"]);
|
||||
|
||||
// 解构形参 + 静态方法(冒号/点号)
|
||||
AssertEqual(actor!.ApplyConfig({ name: "UEHero" }), "UEHero:100");
|
||||
AssertEqual(actor!.ApplyConfig({ name: "UEBoss", hp: 888 }), "UEBoss:888");
|
||||
AssertEqual(actor!.SumScores([10]), 15);
|
||||
AssertEqual(actor!.SumScores([1, 2, 3, 4]), 10);
|
||||
AssertEqual(UnLuaBaseActorClass.StaticWithSelf(25), 125);
|
||||
AssertEqual(UnLuaBaseActorClass.StaticNoSelf(7, 8), 15);
|
||||
AssertEqual(UnLuaBaseActorClass.StaticDocNoSelf(9), 27);
|
||||
|
||||
actor!.K2_DestroyActor();
|
||||
});
|
||||
|
||||
Test("Real UE SpawnActor + 3-level @unlua inheritance (GrandChild -> Sub -> Base -> AActor)", () => {
|
||||
const grand = world.SpawnActor<UnLuaGrandChildActor>(
|
||||
UE.ATriggerBox.StaticClass(),
|
||||
UE.FTransform(UE.FRotator(0, 0, 0).ToQuat(), UE.FVector(0, 0, 0), UE.FVector(1, 1, 1)),
|
||||
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn,
|
||||
undefined,
|
||||
undefined,
|
||||
"Tests.Syntax.UnLuaGrandChildActor"
|
||||
);
|
||||
AssertNotNil(grand);
|
||||
AssertArrayEqual(grand!.GetTrace(), [
|
||||
"Base.ReceiveBeginPlay",
|
||||
"Sub.ReceiveBeginPlay",
|
||||
"GrandChild.ReceiveBeginPlay",
|
||||
]);
|
||||
AssertEqual(grand!.GetFaction(), "Ally");
|
||||
AssertEqual(grand!.GetTier(), 3);
|
||||
AssertEqual(grand!.ComputeBonus(10), 33);
|
||||
AssertEqual(grand!.SetupWithFaction({ name: "Knight", hp: 250 }, "Enemy"), "Knight:250[Enemy]");
|
||||
|
||||
grand!.K2_DestroyActor();
|
||||
});
|
||||
|
||||
Test("Real UE Ex01_HelloWorld & Ex02_OverrideBlueprintEvents lifecycle", () => {
|
||||
const hello = world.SpawnActor<Ex01_HelloWorld>(
|
||||
UE.ATriggerSphere.StaticClass(),
|
||||
UE.FTransform(UE.FRotator(0, 0, 0).ToQuat(), UE.FVector(0, 0, 0), UE.FVector(1, 1, 1)),
|
||||
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn,
|
||||
undefined,
|
||||
undefined,
|
||||
"Tests.UnLuaExamples.Ex01_HelloWorld"
|
||||
);
|
||||
AssertNotNil(hello);
|
||||
AssertTrue(string.find(hello!.GetGreeting(), "Hello UnLua from TypeScript", 1, true)[0] !== undefined);
|
||||
hello!.K2_DestroyActor();
|
||||
|
||||
const eventsActor = world.SpawnActor<Ex02_OverrideBlueprintEvents>(
|
||||
UE.ATriggerCapsule.StaticClass(),
|
||||
UE.FTransform(UE.FRotator(0, 0, 0).ToQuat(), UE.FVector(0, 0, 0), UE.FVector(1, 1, 1)),
|
||||
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn,
|
||||
undefined,
|
||||
undefined,
|
||||
"Tests.UnLuaExamples.Ex02_OverrideBlueprintEvents"
|
||||
);
|
||||
AssertNotNil(eventsActor);
|
||||
const events = eventsActor!.GetEvents();
|
||||
AssertTrue(events.length >= 2);
|
||||
AssertEqual(events[0], "Initialize");
|
||||
|
||||
eventsActor!.K2_DestroyActor();
|
||||
});
|
||||
|
||||
Test("Real UE Ex04 DynamicBinding: SpawnActor, SpawnActorEx & UE.NewObject with Initializer", () => {
|
||||
const spawned = world.SpawnActor<Ex04_DynamicSpawnedActor>(
|
||||
UE.APlayerStart.StaticClass(),
|
||||
UE.FTransform(UE.FRotator(0, 90, 0).ToQuat(), UE.FVector(100, 200, 300), UE.FVector(1, 1, 1)),
|
||||
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn,
|
||||
undefined,
|
||||
undefined,
|
||||
"Tests.UnLuaExamples.Ex04_DynamicSpawnedActor",
|
||||
{ InitTag: "RealUESpawn", InitPower: 555 }
|
||||
);
|
||||
AssertNotNil(spawned);
|
||||
AssertEqual(spawned!.GetInitTag(), "RealUESpawn");
|
||||
AssertEqual(spawned!.GetInitPower(), 555);
|
||||
AssertTrue(spawned!.IsBeginPlayCalled());
|
||||
spawned!.K2_DestroyActor();
|
||||
|
||||
const params = UE.FActorSpawnParameters();
|
||||
params.bNoFail = true;
|
||||
params.SpawnCollisionHandlingOverride = UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn;
|
||||
const spawnedEx = world.SpawnActorEx<Ex04_DynamicSpawnedActor>(
|
||||
UE.APlayerStart.StaticClass(),
|
||||
UE.FTransform(UE.FRotator(0, 0, 0).ToQuat(), UE.FVector(10, 20, 30), UE.FVector(1, 1, 1)),
|
||||
{ InitTag: "RealUESpawnEx", InitPower: 777 },
|
||||
"Tests.UnLuaExamples.Ex04_DynamicSpawnedActor",
|
||||
params
|
||||
);
|
||||
AssertNotNil(spawnedEx);
|
||||
AssertEqual(spawnedEx!.GetInitTag(), "RealUESpawnEx");
|
||||
AssertEqual(spawnedEx!.GetInitPower(), 777);
|
||||
spawnedEx!.K2_DestroyActor();
|
||||
|
||||
// 注意:UnLua 不允许直接对基类 UObject 绑定模块,且需使用非抽象的具体 UObject 子类
|
||||
const dynObj = UE.NewObject<Ex04_DynamicObject>(
|
||||
UE.ULuaBundleAsset.StaticClass(),
|
||||
world,
|
||||
"RealUEDynObj",
|
||||
"Tests.UnLuaExamples.Ex04_DynamicObject",
|
||||
{ Seed: 10 }
|
||||
);
|
||||
AssertNotNil(dynObj);
|
||||
AssertEqual(dynObj.GetSeed(), 10);
|
||||
AssertEqual(dynObj.NextValue(), 21);
|
||||
AssertEqual(dynObj.NextValue(), 43);
|
||||
});
|
||||
|
||||
Test("Real UE Ex08 Struct math, K2_SetActorLocation & LineTraceSingle multi-return", () => {
|
||||
const actor = world.SpawnActor<Ex08_StructAndTrace>(
|
||||
UE.ATextRenderActor.StaticClass(),
|
||||
UE.FTransform(UE.FRotator(0, 0, 0).ToQuat(), UE.FVector(0, 0, 0), UE.FVector(1, 1, 1)),
|
||||
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn,
|
||||
undefined,
|
||||
undefined,
|
||||
"Tests.UnLuaExamples.Ex08_StructAndTrace"
|
||||
);
|
||||
AssertNotNil(actor);
|
||||
|
||||
const [size, dot, dist] = actor!.ComputeVectorMath();
|
||||
AssertNear(size, 13, 1e-3);
|
||||
AssertNear(dot, 0, 1e-3);
|
||||
AssertNear(dist, 13, 1e-3);
|
||||
|
||||
// 1. 验证传入复用 OutHit 原地写入(零 GC)
|
||||
const [bHit, hitDist, impact] = actor!.TraceForward(500);
|
||||
AssertFalse(bHit); // 空测试世界无阻挡物
|
||||
AssertEqual(type(hitDist), "number");
|
||||
AssertNotNil(impact);
|
||||
|
||||
// 2. 验证传入 undefined 触发 UnLua 多返回值分配
|
||||
const [bHit2, outHit2] = actor!.TraceForwardMultiReturn(500);
|
||||
AssertFalse(bHit2);
|
||||
AssertNotNil(outHit2);
|
||||
|
||||
actor!.K2_DestroyActor();
|
||||
});
|
||||
|
||||
Test("Real UE UnLua.Ref / UnLua.Unref & FWeakObjectPtr validity", () => {
|
||||
const obj = UE.NewObject<UE.UObject>(UE.UObjectLibrary.StaticClass(), world, "WeakRefTarget");
|
||||
AssertTrue(UE.UKismetSystemLibrary.IsValid(obj));
|
||||
|
||||
const refProxy = UnLua.Ref(obj);
|
||||
AssertNotNil(refProxy);
|
||||
|
||||
// UnLua C++ 中 FWeakObjectPtr 构造必须传入 1 个 UObject 参数
|
||||
const weak = UE.FWeakObjectPtr(obj);
|
||||
AssertTrue(weak.IsValid());
|
||||
AssertNotNil(weak.Get());
|
||||
|
||||
weak.Reset();
|
||||
AssertFalse(weak.IsValid());
|
||||
AssertNil(weak.Get());
|
||||
|
||||
UnLua.Unref(obj);
|
||||
});
|
||||
|
||||
Test("Real UE ALuaDemoActor (IUnLuaInterface static binding) + EventBus + Tick + EndPlay", () => {
|
||||
EventBus.Clear();
|
||||
const demoActor = world.SpawnActor<UE.ALuaDemoActor>(
|
||||
UE.ALuaDemoActor.StaticClass(),
|
||||
UE.FTransform(UE.FRotator(0, 0, 0).ToQuat(), UE.FVector(0, 0, 100), UE.FVector(1, 1, 1)),
|
||||
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn
|
||||
);
|
||||
AssertNotNil(demoActor);
|
||||
AssertTrue(demoActor!.IsA(UE.ALuaDemoActor.StaticClass()));
|
||||
|
||||
// Spawn 时已自动通过 IUnLuaInterface 绑定 Game.LuaDemoActor 并执行 ReceiveBeginPlay
|
||||
const initText = demoActor!.GetStatusTextString();
|
||||
AssertTrue(string.find(initText, "UnLua Active", 1, true)[0] !== undefined);
|
||||
AssertTrue(string.find(initText, "Pings: 0", 1, true)[0] !== undefined);
|
||||
|
||||
if (canTickWorld) {
|
||||
TickUEWorld(5, 0.05);
|
||||
}
|
||||
|
||||
// 触发 EventBus Ping,验证 C++ 3D 文本实时更新
|
||||
EventBus.Emit("Demo.Ping", "UERealEngineTest");
|
||||
const pingedText = demoActor!.GetStatusTextString();
|
||||
AssertTrue(string.find(pingedText, "Pings: 1", 1, true)[0] !== undefined);
|
||||
AssertTrue(string.find(pingedText, "Color: Gold", 1, true)[0] !== undefined);
|
||||
|
||||
// 验证 ALuaDemoActor 位置移动(带 SceneRoot 组件)
|
||||
const [bMoved] = demoActor!.K2_SetActorLocation(UE.FVector(120, 240, 360), false, undefined, true);
|
||||
AssertTrue(bMoved);
|
||||
const loc = demoActor!.K2_GetActorLocation();
|
||||
AssertNear(loc.X, 120, 1e-2);
|
||||
AssertNear(loc.Y, 240, 1e-2);
|
||||
AssertNear(loc.Z, 360, 1e-2);
|
||||
|
||||
demoActor!.K2_DestroyActor();
|
||||
EventBus.Clear();
|
||||
});
|
||||
|
||||
if (canTickWorld) {
|
||||
Test("Real UE FTimerManager + K2_SetTimerDelegate(UnLua.Delegate) ticked by UWorld", () => {
|
||||
const timerActor = world.SpawnActor<UETimerTestActor>(
|
||||
UE.ACameraActor.StaticClass(),
|
||||
UE.FTransform(UE.FRotator(0, 0, 0).ToQuat(), UE.FVector(0, 0, 0), UE.FVector(1, 1, 1)),
|
||||
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn,
|
||||
undefined,
|
||||
undefined,
|
||||
"Tests.UE.UETimerTestActor"
|
||||
);
|
||||
AssertNotNil(timerActor);
|
||||
AssertTrue(timerActor!.IsTimerActive());
|
||||
AssertEqual(timerActor!.GetTimerCount(), 0);
|
||||
|
||||
// 推进真实 UWorld 0.5s(FTimerManager 首帧激活 Pending 列表,后续每 0.1s 触发一次,应 >= 3 次)
|
||||
TickUEWorld(10, 0.05);
|
||||
AssertTrue(timerActor!.GetTimerCount() >= 3);
|
||||
|
||||
const countBeforeStop = timerActor!.GetTimerCount();
|
||||
timerActor!.StopTimer();
|
||||
AssertFalse(timerActor!.IsTimerActive());
|
||||
|
||||
TickUEWorld(6, 0.05);
|
||||
AssertEqual(timerActor!.GetTimerCount(), countBeforeStop);
|
||||
|
||||
timerActor!.K2_DestroyActor();
|
||||
});
|
||||
|
||||
Test("Real UE FLatentActionManager + UKismetSystemLibrary.Delay coroutine (Ex07)", () => {
|
||||
const latentActor = world.SpawnActor<Ex07_CallLatentFunction>(
|
||||
UE.ADecalActor.StaticClass(),
|
||||
UE.FTransform(UE.FRotator(0, 0, 0).ToQuat(), UE.FVector(0, 0, 0), UE.FVector(1, 1, 1)),
|
||||
UE.ESpawnActorCollisionHandlingMethod.AlwaysSpawn,
|
||||
undefined,
|
||||
undefined,
|
||||
"Tests.UnLuaExamples.Ex07_CallLatentFunction"
|
||||
);
|
||||
AssertNotNil(latentActor);
|
||||
AssertArrayEqual(latentActor!.GetSteps(), ["Step1:Start"]);
|
||||
AssertFalse(latentActor!.IsCompleted());
|
||||
|
||||
// 推进 0.6s -> 越过第一个 0.5s Delay
|
||||
TickUEWorld(6, 0.1);
|
||||
AssertArrayEqual(latentActor!.GetSteps(), ["Step1:Start", "Step2:After0.5s"]);
|
||||
AssertFalse(latentActor!.IsCompleted());
|
||||
|
||||
// 再推进 1.1s -> 越过第二个 1.0s Delay
|
||||
TickUEWorld(11, 0.1);
|
||||
AssertArrayEqual(latentActor!.GetSteps(), [
|
||||
"Step1:Start",
|
||||
"Step2:After0.5s",
|
||||
"Step3:Done",
|
||||
]);
|
||||
AssertTrue(latentActor!.IsCompleted());
|
||||
|
||||
latentActor!.K2_DestroyActor();
|
||||
});
|
||||
}
|
||||
|
||||
Test("Real UE HotReload.ReloadModule validates arguments and emits events", () => {
|
||||
EventBus.Clear();
|
||||
const [badOk] = HotReload.ReloadModule("");
|
||||
AssertFalse(badOk);
|
||||
const [ok] = HotReload.ReloadModule("Framework.Log");
|
||||
AssertTrue(ok);
|
||||
EventBus.Clear();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 在真实 UE 5.8 + UnLua 2.3.6 环境下运行的统一入口。
|
||||
* 可由 RunLuaTestsCommandlet 或编辑器控制台命令 `Lua.RunTests` 调用。
|
||||
*/
|
||||
export function RunAllInUE(world?: UE.UWorld): SuiteSummary {
|
||||
const targetWorld = world ?? _G.__UE_TEST_WORLD;
|
||||
Reset();
|
||||
|
||||
// 1. 纯逻辑与语法测试(运行在 UnLua 内嵌 Lua 5.4.3 VM 中)
|
||||
RunBasicTypesAndControlFlowTests();
|
||||
RunFunctionsClosuresAndTuplesTests();
|
||||
RunPureClassesAndGenericsTests();
|
||||
RunGameplayTests();
|
||||
RunLua54PitfallsTests();
|
||||
|
||||
// 2. 真实 UE 5.8 引擎 + UnLua 反射/绑定/容器/定时器/协程集成测试
|
||||
if (targetWorld !== undefined) {
|
||||
RunUERuntimeSuite(targetWorld);
|
||||
}
|
||||
|
||||
return PrintReport();
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* 用于真实 UE 5.8 环境下测试 K2_SetTimerDelegate + UnLua.Delegate(this, this.OnTimer)
|
||||
* 与真实 UWorld::Tick -> FTimerManager 调度的 @unlua Actor。
|
||||
*/
|
||||
@unlua
|
||||
export default class UETimerTestActor extends UE.AActor {
|
||||
private timerCount!: number;
|
||||
private timerHandle!: UE.FTimerHandle;
|
||||
|
||||
ReceiveBeginPlay(): void {
|
||||
super.ReceiveBeginPlay();
|
||||
this.timerCount = 0;
|
||||
this.timerHandle = UE.UKismetSystemLibrary.K2_SetTimerDelegate(
|
||||
UnLua.Delegate(this, this.OnTimerTick),
|
||||
0.1,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
OnTimerTick(): void {
|
||||
if (!UE.UKismetSystemLibrary.IsValid(this)) {
|
||||
return;
|
||||
}
|
||||
this.timerCount++;
|
||||
}
|
||||
|
||||
StopTimer(): void {
|
||||
UE.UKismetSystemLibrary.K2_ClearAndInvalidateTimerHandle(this, this.timerHandle);
|
||||
}
|
||||
|
||||
GetTimerCount(): number {
|
||||
return this.timerCount;
|
||||
}
|
||||
|
||||
IsTimerActive(): boolean {
|
||||
return UE.UKismetSystemLibrary.K2_IsTimerActiveHandle(this, this.timerHandle);
|
||||
}
|
||||
|
||||
ReceiveEndPlay(EndPlayReason: UE.EEndPlayReason): void {
|
||||
this.StopTimer();
|
||||
super.ReceiveEndPlay(EndPlayReason);
|
||||
}
|
||||
}
|
||||
@@ -38,8 +38,8 @@ export function VerifyTArrayOperations(): void {
|
||||
AssertTrue(arr.Contains(25));
|
||||
AssertFalse(arr.Contains(999));
|
||||
|
||||
// AddUnique
|
||||
AssertEqual(arr.AddUnique(25), 0);
|
||||
// AddUnique:UnLua C++ 中若元素已存在,返回已有元素的 1-based 索引;否则追加并返回新索引
|
||||
AssertEqual(arr.AddUnique(25), 2);
|
||||
AssertEqual(arr.AddUnique(40), 4);
|
||||
AssertEqual(arr.Length(), 4);
|
||||
|
||||
|
||||
@@ -21,10 +21,11 @@ export default class Ex08_StructAndTrace extends UE.AActor {
|
||||
ComputeVectorMath(): LuaMultiReturn<[number, number, number]> {
|
||||
const a = UE.FVector(3, 4, 0);
|
||||
const b = UE.FVector(0, 0, 12);
|
||||
const c = a.Add(b); // (3, 4, 12), size = 13
|
||||
const dot = a.Dot(b); // 0
|
||||
const dist = UE.FVector.Dist(UE.FVector(0, 0, 0), c); // 13
|
||||
return $multi(c.Size(), dot, dist);
|
||||
// UnLua C++ 中 FVector:Add(b) 原地修改 self(无返回值),零额外结构体分配
|
||||
a.Add(b); // a 变为 (3, 4, 12), size = 13
|
||||
const dist = UE.FVector.Dist(UE.FVector(0, 0, 0), a); // 13
|
||||
return $multi(a.Size(), dot, dist);
|
||||
}
|
||||
|
||||
TraceForward(distance: number): LuaMultiReturn<[boolean, number, UE.FVector]> {
|
||||
@@ -32,7 +33,9 @@ export default class Ex08_StructAndTrace extends UE.AActor {
|
||||
this._tmpStart.Set(origin.X, origin.Y, origin.Z);
|
||||
this._tmpEnd.Set(origin.X + distance, origin.Y, origin.Z);
|
||||
|
||||
const [bHit, hit] = UE.UKismetSystemLibrary.LineTraceSingle(
|
||||
// UnLua 中传入已分配的 Out 参数(this._tmpHit)时会原地写入该结构体(零 GC),不再作为第二个返回值压栈;
|
||||
// 传入 undefined 时才新分配并作为第二个返回值返回。此处同时兼容复用与多返回值模式。
|
||||
const [bHit, returnedHit] = UE.UKismetSystemLibrary.LineTraceSingle(
|
||||
this,
|
||||
this._tmpStart,
|
||||
this._tmpEnd,
|
||||
@@ -43,9 +46,28 @@ export default class Ex08_StructAndTrace extends UE.AActor {
|
||||
this._tmpHit,
|
||||
true
|
||||
);
|
||||
const hit = returnedHit ?? this._tmpHit;
|
||||
return $multi(bHit, hit.Distance, hit.ImpactPoint);
|
||||
}
|
||||
|
||||
TraceForwardMultiReturn(distance: number): LuaMultiReturn<[boolean, UE.FHitResult]> {
|
||||
const origin = this.K2_GetActorLocation();
|
||||
this._tmpStart.Set(origin.X, origin.Y, origin.Z);
|
||||
this._tmpEnd.Set(origin.X + distance, origin.Y, origin.Z);
|
||||
|
||||
return UE.UKismetSystemLibrary.LineTraceSingle(
|
||||
this,
|
||||
this._tmpStart,
|
||||
this._tmpEnd,
|
||||
UE.ETraceTypeQuery.TraceTypeQuery1,
|
||||
false,
|
||||
this._ignoreActors,
|
||||
UE.EDrawDebugTrace.None,
|
||||
undefined,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
TeleportTo(x: number, y: number, z: number): boolean {
|
||||
const target = UE.FVector(x, y, z);
|
||||
const [bMoved] = this.K2_SetActorLocation(target, false, this._tmpHit, true);
|
||||
|
||||
@@ -6,14 +6,14 @@
|
||||
export default class Ex09_AssetRefAndUMG extends UE.UUserWidget {
|
||||
private loadedAsset!: UE.UObject | undefined;
|
||||
private refProxy!: unknown;
|
||||
private weakRef!: UE.FWeakObjectPtr;
|
||||
private weakRef!: UE.FWeakObjectPtr | undefined;
|
||||
private isConstructed!: boolean;
|
||||
private isDestructed!: boolean;
|
||||
|
||||
Initialize(_Initializer?: unknown): void {
|
||||
this.isConstructed = false;
|
||||
this.isDestructed = false;
|
||||
this.weakRef = UE.FWeakObjectPtr();
|
||||
this.weakRef = undefined;
|
||||
}
|
||||
|
||||
Construct(): void {
|
||||
@@ -21,7 +21,7 @@ export default class Ex09_AssetRefAndUMG extends UE.UUserWidget {
|
||||
this.loadedAsset = UE.UObject.Load("/Game/UI/Textures/T_Icon.T_Icon");
|
||||
if (this.loadedAsset !== undefined) {
|
||||
this.refProxy = UnLua.Ref(this.loadedAsset);
|
||||
this.weakRef.Set(this.loadedAsset);
|
||||
this.weakRef = UE.FWeakObjectPtr(this.loadedAsset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,11 +45,14 @@ export default class Ex09_AssetRefAndUMG extends UE.UUserWidget {
|
||||
this.refProxy = undefined;
|
||||
this.loadedAsset = undefined;
|
||||
}
|
||||
this.weakRef.Reset();
|
||||
if (this.weakRef !== undefined) {
|
||||
this.weakRef.Reset();
|
||||
this.weakRef = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
HasValidWeakAsset(): boolean {
|
||||
return this.weakRef.IsValid();
|
||||
return this.weakRef !== undefined && this.weakRef.IsValid();
|
||||
}
|
||||
|
||||
IsConstructed(): boolean {
|
||||
|
||||
Vendored
+2
@@ -12,3 +12,5 @@ interface LuaPackageTable {
|
||||
}
|
||||
|
||||
declare var GameFramework: Record<string, unknown> | undefined;
|
||||
declare var __UE_TEST_WORLD: UE.UWorld | undefined;
|
||||
declare var __UE_TEST_TICK: ((this: void, deltaSeconds: number) => void) | undefined;
|
||||
+29
-31
@@ -1434,10 +1434,10 @@ declare namespace UE {
|
||||
X: number;
|
||||
Y: number;
|
||||
Set(X?: number, Y?: number): void;
|
||||
Add(other: FIntPoint | number): FIntPoint;
|
||||
Sub(other: FIntPoint | number): FIntPoint;
|
||||
Mul(other: FIntPoint | number): FIntPoint;
|
||||
Div(other: FIntPoint | number): FIntPoint;
|
||||
Add(other: FIntPoint | number): void;
|
||||
Sub(other: FIntPoint | number): void;
|
||||
Mul(other: FIntPoint | number): void;
|
||||
Div(other: FIntPoint | number): void;
|
||||
Size(): number;
|
||||
SizeSquared(): number;
|
||||
}
|
||||
@@ -1457,10 +1457,10 @@ declare namespace UE {
|
||||
Z: number;
|
||||
Set(X?: number, Y?: number, Z?: number): void;
|
||||
SizeSquared(): number;
|
||||
Add(other: FIntVector | number): FIntVector;
|
||||
Sub(other: FIntVector | number): FIntVector;
|
||||
Mul(other: FIntVector | number): FIntVector;
|
||||
Div(other: FIntVector | number): FIntVector;
|
||||
Add(other: FIntVector | number): void;
|
||||
Sub(other: FIntVector | number): void;
|
||||
Mul(other: FIntVector | number): void;
|
||||
Div(other: FIntVector | number): void;
|
||||
Size(): number;
|
||||
}
|
||||
function FIntVector(this: void): FIntVector;
|
||||
@@ -1487,10 +1487,10 @@ declare namespace UE {
|
||||
B: number;
|
||||
A: number;
|
||||
Set(R?: number, G?: number, B?: number, A?: number): void;
|
||||
Add(other: FLinearColor): FLinearColor;
|
||||
Sub(other: FLinearColor): FLinearColor;
|
||||
Mul(other: FLinearColor | number): FLinearColor;
|
||||
Div(other: FLinearColor | number): FLinearColor;
|
||||
Add(other: FLinearColor): void;
|
||||
Sub(other: FLinearColor): void;
|
||||
Mul(other: FLinearColor | number): void;
|
||||
Div(other: FLinearColor | number): void;
|
||||
ToFColor(P0: boolean): FColor;
|
||||
Clamp(P0: number, P1: number): FLinearColor;
|
||||
}
|
||||
@@ -1727,7 +1727,7 @@ declare namespace UE {
|
||||
Set(X?: number, Y?: number, Z?: number, W?: number): void;
|
||||
Normalize(Tolerance?: number): void;
|
||||
GetNormalized(Tolerance?: number): FQuat;
|
||||
Mul(other: FQuat | number): FQuat;
|
||||
Mul(other: FQuat | number): void;
|
||||
IsNormalized(): boolean;
|
||||
Size(): number;
|
||||
SizeSquared(): number;
|
||||
@@ -1852,9 +1852,9 @@ declare namespace UE {
|
||||
GetRightVector(Out?: FVector): FVector;
|
||||
GetUpVector(Out?: FVector): FVector;
|
||||
GetUnitAxis(Axis: EAxis, Out?: FVector): FVector;
|
||||
Add(other: FRotator): FRotator;
|
||||
Sub(other: FRotator): FRotator;
|
||||
Mul(scale: number): FRotator;
|
||||
Add(other: FRotator): void;
|
||||
Sub(other: FRotator): void;
|
||||
Mul(scale: number): void;
|
||||
Normalize(): void;
|
||||
GetNormalized(): FRotator;
|
||||
RotateVector(P0: FVector): FVector;
|
||||
@@ -2094,7 +2094,7 @@ declare namespace UE {
|
||||
Scale3D: FVector;
|
||||
Blend(B: FTransform, C: FTransform, Alpha: number): void;
|
||||
BlendWith(B: FTransform, Alpha: number): void;
|
||||
Mul(other: FTransform | number): FTransform;
|
||||
Mul(other: FTransform | number): void;
|
||||
Inverse(): FTransform;
|
||||
TransformPosition(P0: FVector): FVector;
|
||||
TransformPositionNoScale(P0: FVector): FVector;
|
||||
@@ -2241,10 +2241,10 @@ declare namespace UE {
|
||||
Z: number;
|
||||
Set(X?: number, Y?: number, Z?: number): void;
|
||||
Normalize(Tolerance?: number): boolean;
|
||||
Add(other: FVector | number): FVector;
|
||||
Sub(other: FVector | number): FVector;
|
||||
Mul(other: FVector | number): FVector;
|
||||
Div(other: FVector | number): FVector;
|
||||
Add(other: FVector | number): void;
|
||||
Sub(other: FVector | number): void;
|
||||
Mul(other: FVector | number): void;
|
||||
Div(other: FVector | number): void;
|
||||
Dot(P0: FVector): number;
|
||||
Cross(P0: FVector): FVector;
|
||||
Size(): number;
|
||||
@@ -2273,10 +2273,10 @@ declare namespace UE {
|
||||
Set(X?: number, Y?: number): void;
|
||||
Normalize(Tolerance?: number): void;
|
||||
IsNormalized(): boolean;
|
||||
Add(other: FVector2D | number): FVector2D;
|
||||
Sub(other: FVector2D | number): FVector2D;
|
||||
Mul(other: FVector2D | number): FVector2D;
|
||||
Div(other: FVector2D | number): FVector2D;
|
||||
Add(other: FVector2D | number): void;
|
||||
Sub(other: FVector2D | number): void;
|
||||
Mul(other: FVector2D | number): void;
|
||||
Div(other: FVector2D | number): void;
|
||||
Dot(P0: FVector2D): number;
|
||||
Cross(P0: FVector2D): number;
|
||||
Size(): number;
|
||||
@@ -2317,10 +2317,10 @@ declare namespace UE {
|
||||
Z: number;
|
||||
W: number;
|
||||
Set(X?: number, Y?: number, Z?: number, W?: number): void;
|
||||
Add(other: FVector4 | number): FVector4;
|
||||
Sub(other: FVector4 | number): FVector4;
|
||||
Mul(other: FVector4 | number): FVector4;
|
||||
Div(other: FVector4 | number): FVector4;
|
||||
Add(other: FVector4 | number): void;
|
||||
Sub(other: FVector4 | number): void;
|
||||
Mul(other: FVector4 | number): void;
|
||||
Div(other: FVector4 | number): void;
|
||||
Cross(P0: FVector4): FVector4;
|
||||
Size(): number;
|
||||
Size3(): number;
|
||||
@@ -2427,7 +2427,6 @@ declare namespace UE {
|
||||
static Load<T extends UObject = UClass>(this: void, classPath: string): T | undefined;
|
||||
IsChildOf(targetClass: UClass): boolean;
|
||||
GetDefaultObject<T extends UObject = UObject>(): T;
|
||||
static StaticClass(this: void): UClass;
|
||||
}
|
||||
|
||||
class UClassCookedMetaData extends UCookedMetaDataBase {
|
||||
@@ -2700,7 +2699,6 @@ declare namespace UE {
|
||||
Release(): void;
|
||||
Destroy(): void;
|
||||
static Load<T extends UObject = UObject>(this: void, objectPath: string): T | undefined;
|
||||
static StaticClass(this: void): UClass;
|
||||
}
|
||||
|
||||
class UObjectProperty extends UObjectPropertyBase {
|
||||
|
||||
Reference in new issue
Block a user