test: add comprehensive ts2lua-unreal TS/Lua 5.4 test suites and upgrade to v0.2.2

This commit is contained in:
szy73711 committed 2026-11-29 03:35:49 +08:00
1 parent ee2e62bb34
commit 3dd61c32a9
75 files changed
+9929 -10

No files matched your search

@@ -0,0 +1,171 @@
local ____exports = {}
local ____TestRunner = require("Tests.Harness.TestRunner")
local Suite = ____TestRunner.Suite
local Test = ____TestRunner.Test
local AssertEqual = ____TestRunner.AssertEqual
local AssertTrue = ____TestRunner.AssertTrue
local AssertNear = ____TestRunner.AssertNear
local AssertArrayEqual = ____TestRunner.AssertArrayEqual
local function EscapeLuaPattern(literal)
local escaped = string.gsub(literal, "([%(%)%.%%%+%-%*%?%[%]%^%$])", "%%%1")
return escaped
end
local function DeepCloneTable(src, seen)
if type(src) ~= "table" then
return src
end
local visited = seen or ({})
local existing = visited[src]
if existing ~= nil then
return existing
end
local copy = {}
visited[src] = copy
for k, v in pairs(src) do
copy[DeepCloneTable(k, visited)] = DeepCloneTable(v, visited)
end
return copy
end
function ____exports.RunLua54PitfallsTests()
Suite(
"Extra.Lua54PitfallsAndEdgeCases",
function()
Test(
"Lua truthiness vs JS: explicit checks for 0 and empty string, ?? vs ||",
function()
local zero = 0
local empty = ""
AssertTrue(zero == 0)
AssertTrue(empty == "")
local ____AssertTrue_1 = AssertTrue
local ____temp_0
if zero then
____temp_0 = true
else
____temp_0 = false
end
____AssertTrue_1(____temp_0)
local ____AssertTrue_3 = AssertTrue
local ____temp_2
if empty then
____temp_2 = true
else
____temp_2 = false
end
____AssertTrue_3(____temp_2)
local optZero = 0
local optEmpty = ""
local optFalse = false
local optNil = nil
AssertEqual(optZero or 100, 0)
AssertEqual(optEmpty or "fallback", "")
local ____AssertEqual_5 = AssertEqual
local ____optFalse_4 = optFalse
if ____optFalse_4 == nil then
____optFalse_4 = true
end
____AssertEqual_5(____optFalse_4, false)
AssertEqual(optNil or 100, 100)
end
)
Test(
"Lua 5.4 UTF-8 character counting for CJK game text vs byte length",
function()
local cnText = "你好UE5世界"
AssertEqual(#cnText, 15)
local charCount = utf8.len(cnText)
AssertEqual(charCount, 7)
local codes = {utf8.codepoint(cnText, 1, -1)}
AssertEqual(#codes, 7)
AssertEqual(codes[1], 20320)
AssertEqual(codes[3], 85)
end
)
Test(
"Lua pattern magic character escaping for literal search & replace",
function()
local raw = "Item[Epic]+10 (50.5%)"
local escaped = EscapeLuaPattern("[Epic]+10 (50.5%)")
local startIdx = string.find(raw, escaped)
AssertTrue(startIdx ~= nil)
local replaced = string.gsub(
raw,
EscapeLuaPattern("(50.5%)"),
"(100%%)"
)
AssertEqual(replaced, "Item[Epic]+10 (100%)")
end
)
Test(
"0-based TS array vs 1-based UE.TArray interop without sparse holes",
function()
local tsArr = {"first", "second", "third"}
local ueArr = UE.TArray(UE.FString)
do
local i = 0
while i < #tsArr do
ueArr:Add(tsArr[i + 1])
i = i + 1
end
end
AssertEqual(
ueArr:Length(),
#tsArr
)
AssertEqual(
ueArr:Get(1),
tsArr[1]
)
AssertEqual(
ueArr:Get(3),
tsArr[3]
)
local dense = {10, 20, 30, 40}
local removeIdx = 1
dense[removeIdx + 1] = dense[#dense]
table.remove(dense)
AssertEqual(#dense, 3)
AssertArrayEqual(dense, {10, 40, 30})
end
)
Test(
"deep clone plain tables safely including circular references",
function()
local original = {name = "Root", tags = {"a", "b"}}
original.selfRef = original
local cloned = DeepCloneTable(original)
AssertEqual(cloned.name, "Root")
AssertArrayEqual(cloned.tags, {"a", "b"})
AssertTrue(cloned ~= original)
AssertTrue(cloned.tags ~= original.tags)
AssertEqual(cloned.selfRef, cloned)
local ____cloned_tags_6 = cloned.tags
____cloned_tags_6[#____cloned_tags_6 + 1] = "c"
AssertEqual(#original.tags, 2)
AssertEqual(#cloned.tags, 3)
end
)
Test(
"zero-GC hot-path Tick simulation with cached FVector/FRotator (10,000 frames)",
function()
local pos = UE.FVector(0, 0, 0)
local vel = UE.FVector(10, 20, -5)
local dt = 0.016
do
local frame = 0
while frame < 10000 do
pos.X = pos.X + vel.X * dt
pos.Y = pos.Y + vel.Y * dt
pos.Z = pos.Z + vel.Z * dt
frame = frame + 1
end
end
AssertNear(pos.X, 1600, 0.1)
AssertNear(pos.Y, 3200, 0.1)
AssertNear(pos.Z, -800, 0.1)
end
)
end
)
end
return ____exports