test: add comprehensive ts2lua-unreal TS/Lua 5.4 test suites and upgrade to v0.2.2
This commit is contained in:
1 parent
ee2e62bb34
commit
3dd61c32a9
75 files changed
+9929
-10
No files matched your search
@@ -0,0 +1,142 @@
|
||||
/** @noSelfInFile */
|
||||
|
||||
import {
|
||||
Suite,
|
||||
Test,
|
||||
AssertEqual,
|
||||
AssertTrue,
|
||||
AssertFalse,
|
||||
AssertNear,
|
||||
AssertArrayEqual,
|
||||
} from "../Harness/TestRunner";
|
||||
|
||||
function EscapeLuaPattern(literal: string): string {
|
||||
const [escaped] = string.gsub(literal, "([%(%)%.%%%+%-%*%?%[%]%^%$])", "%%%1");
|
||||
return escaped;
|
||||
}
|
||||
|
||||
function DeepCloneTable<T>(src: T, seen?: LuaTable<object, unknown>): T {
|
||||
if (type(src) !== "table") {
|
||||
return src;
|
||||
}
|
||||
const visited = seen ?? new LuaTable<object, unknown>();
|
||||
const existing = visited.get(src as unknown as object);
|
||||
if (existing !== undefined) {
|
||||
return existing as T;
|
||||
}
|
||||
const copy: Record<any, any> = {};
|
||||
visited.set(src as unknown as object, copy);
|
||||
for (const [k, v] of pairs(src as unknown as Record<any, any>)) {
|
||||
copy[DeepCloneTable(k, visited)] = DeepCloneTable(v, visited);
|
||||
}
|
||||
return copy as T;
|
||||
}
|
||||
|
||||
export function RunLua54PitfallsTests(): void {
|
||||
Suite("Extra.Lua54PitfallsAndEdgeCases", () => {
|
||||
Test("Lua truthiness vs JS: explicit checks for 0 and empty string, ?? vs ||", () => {
|
||||
const zero = 0;
|
||||
const empty = "";
|
||||
|
||||
// 在 Lua 中 0 和 "" 均为 truthy!业务代码必须使用显式比较
|
||||
AssertTrue(zero === 0);
|
||||
AssertTrue(empty === "");
|
||||
AssertTrue((zero as unknown as boolean) ? true : false);
|
||||
AssertTrue((empty as unknown as boolean) ? true : false);
|
||||
|
||||
// ?? 在 TSTL 中安全地仅判断 == nil,不会误替换 0 / "" / false
|
||||
const optZero: number | undefined = 0;
|
||||
const optEmpty: string | undefined = "";
|
||||
const optFalse: boolean | undefined = false;
|
||||
const optNil: number | undefined = undefined;
|
||||
|
||||
AssertEqual(optZero ?? 100, 0);
|
||||
AssertEqual(optEmpty ?? "fallback", "");
|
||||
AssertEqual(optFalse ?? true, false);
|
||||
AssertEqual(optNil ?? 100, 100);
|
||||
});
|
||||
|
||||
Test("Lua 5.4 UTF-8 character counting for CJK game text vs byte length", () => {
|
||||
const cnText = "你好UE5世界";
|
||||
// UTF-8 字节数: 4 个汉字 * 3 + 3 ASCII = 15
|
||||
AssertEqual(cnText.length, 15);
|
||||
// utf8.len 返回 Unicode 码点数 = 7
|
||||
const charCount = utf8.len(cnText);
|
||||
AssertEqual(charCount, 7);
|
||||
|
||||
// utf8.codepoint 提取全部码点
|
||||
const codes = [...utf8.codepoint(cnText, 1, -1)];
|
||||
AssertEqual(codes.length, 7);
|
||||
AssertEqual(codes[0], 0x4f60); // '你'
|
||||
AssertEqual(codes[2], 85); // 'U'
|
||||
});
|
||||
|
||||
Test("Lua pattern magic character escaping for literal search & replace", () => {
|
||||
const raw = "Item[Epic]+10 (50.5%)";
|
||||
const escaped = EscapeLuaPattern("[Epic]+10 (50.5%)");
|
||||
const [startIdx] = string.find(raw, escaped);
|
||||
AssertTrue(startIdx !== undefined);
|
||||
|
||||
// 注意:string.gsub 的 replacement 字符串中字面量 % 同样需要转义为 %%
|
||||
const [replaced] = string.gsub(raw, EscapeLuaPattern("(50.5%)"), "(100%%)");
|
||||
AssertEqual(replaced, "Item[Epic]+10 (100%)");
|
||||
});
|
||||
|
||||
Test("0-based TS array vs 1-based UE.TArray interop without sparse holes", () => {
|
||||
const tsArr: string[] = ["first", "second", "third"];
|
||||
const ueArr = UE.TArray<string>(UE.FString);
|
||||
|
||||
for (let i = 0; i < tsArr.length; i++) {
|
||||
ueArr.Add(tsArr[i]);
|
||||
}
|
||||
AssertEqual(ueArr.Length(), tsArr.length);
|
||||
AssertEqual(ueArr.Get(1), tsArr[0]);
|
||||
AssertEqual(ueArr.Get(3), tsArr[2]);
|
||||
|
||||
// Swap-remove O(1) 删除保持紧凑数组无空洞
|
||||
const dense = [10, 20, 30, 40];
|
||||
const removeIdx = 1; // 移除 20
|
||||
dense[removeIdx] = dense[dense.length - 1];
|
||||
table.remove(dense);
|
||||
AssertEqual(dense.length, 3);
|
||||
AssertArrayEqual(dense, [10, 40, 30]);
|
||||
});
|
||||
|
||||
Test("deep clone plain tables safely including circular references", () => {
|
||||
interface Node {
|
||||
name: string;
|
||||
tags: string[];
|
||||
selfRef?: Node;
|
||||
}
|
||||
const original: Node = { name: "Root", tags: ["a", "b"] };
|
||||
original.selfRef = original;
|
||||
|
||||
const cloned = DeepCloneTable(original);
|
||||
AssertEqual(cloned.name, "Root");
|
||||
AssertArrayEqual(cloned.tags, ["a", "b"]);
|
||||
AssertTrue(cloned !== original);
|
||||
AssertTrue(cloned.tags !== original.tags);
|
||||
AssertEqual(cloned.selfRef, cloned);
|
||||
|
||||
cloned.tags.push("c");
|
||||
AssertEqual(original.tags.length, 2);
|
||||
AssertEqual(cloned.tags.length, 3);
|
||||
});
|
||||
|
||||
Test("zero-GC hot-path Tick simulation with cached FVector/FRotator (10,000 frames)", () => {
|
||||
const pos = UE.FVector(0, 0, 0);
|
||||
const vel = UE.FVector(10, 20, -5);
|
||||
const dt = 0.016;
|
||||
|
||||
for (let frame = 0; frame < 10000; frame++) {
|
||||
pos.X += vel.X * dt;
|
||||
pos.Y += vel.Y * dt;
|
||||
pos.Z += vel.Z * dt;
|
||||
}
|
||||
|
||||
AssertNear(pos.X, 1600, 0.1);
|
||||
AssertNear(pos.Y, 3200, 0.1);
|
||||
AssertNear(pos.Z, -800, 0.1);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user