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,157 @@
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 AssertFalse = ____TestRunner.AssertFalse
local AssertNotNil = ____TestRunner.AssertNotNil
local AssertNil = ____TestRunner.AssertNil
local AssertArrayEqual = ____TestRunner.AssertArrayEqual
local ____MockUnLuaRuntime = require("Tests.Harness.MockUnLuaRuntime")
local CreateBoundInstance = ____MockUnLuaRuntime.CreateBoundInstance
local ResetMockState = ____MockUnLuaRuntime.ResetMockState
local Bootstrap = require("Framework.Bootstrap")
local EventBus = require("Framework.EventBus")
local HotReload = require("Framework.HotReload")
local LuaDemoActor = require("Game.LuaDemoActor")
function ____exports.RunHotReloadAndFrameworkTests()
Suite(
"Extra.FrameworkAndHotReload",
function()
Test(
"EventBus: dedup, owner dispatch, mid-emit unsubscribe, OffAllForOwner and error isolation",
function()
EventBus.Clear()
local received = {}
local ownerA = {
tag = "A",
OnEvent = function(self, msg)
received[#received + 1] = (self.tag .. ":") .. msg
end
}
local ownerB = {
tag = "B",
OnEvent = function(self, msg)
received[#received + 1] = (self.tag .. ":") .. msg
EventBus.Off("Test.Ping", self.OnEvent, self)
end
}
EventBus.On("Test.Ping", ownerA.OnEvent, ownerA)
EventBus.On("Test.Ping", ownerA.OnEvent, ownerA)
EventBus.On("Test.Ping", ownerB.OnEvent, ownerB)
EventBus.On(
"Test.Ping",
function()
error("intentional listener error")
end
)
local afterErrorCalled = false
EventBus.On(
"Test.Ping",
function()
afterErrorCalled = true
end
)
EventBus.Emit("Test.Ping", "hello")
AssertArrayEqual(received, {"A:hello", "B:hello"})
AssertTrue(afterErrorCalled)
EventBus.Emit("Test.Ping", "world")
AssertArrayEqual(received, {"A:hello", "B:hello", "A:world"})
EventBus.OffAllForOwner(ownerA)
EventBus.Emit("Test.Ping", "ignored")
AssertArrayEqual(received, {"A:hello", "B:hello", "A:world"})
EventBus.Clear()
end
)
Test(
"Bootstrap Init/Shutdown lifecycle and _G.GameFramework registration",
function()
local fakeGI = CreateBoundInstance({}, "ULuaGameInstance")
Bootstrap.Init(fakeGI)
AssertTrue(Bootstrap.IsInitialized)
AssertEqual(Bootstrap.GameInstance, fakeGI)
AssertNotNil(_G.GameFramework)
Bootstrap.Shutdown()
AssertFalse(Bootstrap.IsInitialized)
AssertNil(Bootstrap.GameInstance)
AssertNil(_G.GameFramework)
end
)
Test(
"HotReload.ReloadModule validates arguments and emits Before/After events",
function()
EventBus.Clear()
local badOk, badErr = HotReload.ReloadModule("")
AssertFalse(badOk)
AssertEqual(badErr, "Invalid module name")
local order = {}
EventBus.On(
HotReload.EVENT_BEFORE_RELOAD,
function(modName)
order[#order + 1] = "before:" .. modName
end
)
EventBus.On(
HotReload.EVENT_AFTER_RELOAD,
function(modName)
order[#order + 1] = "after:" .. modName
end
)
local ok = HotReload.ReloadModule("Framework.Log")
AssertTrue(ok)
AssertArrayEqual(order, {"before:Framework.Log", "after:Framework.Log"})
EventBus.Clear()
end
)
Test(
"LuaDemoActor full integration: BeginPlay, Tick, Ping color cycle, HotReload event & EndPlay cleanup",
function()
ResetMockState()
EventBus.Clear()
local lastStatusText = ""
local colorApplyCount = 0
local rotationAddCount = 0
local lastVerticalOffset = 0
local demoActor = CreateBoundInstance(
LuaDemoActor,
"ALuaDemoActor",
{
SpinSpeed = 90,
BobAmplitude = 20,
BobFrequency = 2,
SetStatusText = function(self, text)
lastStatusText = text
end,
SetCubeColor = function(self, _color)
colorApplyCount = colorApplyCount + 1
end,
AddCubeLocalRotation = function(self, _delta)
rotationAddCount = rotationAddCount + 1
end,
SetCubeVerticalOffset = function(self, zOffset)
lastVerticalOffset = zOffset
end
}
)
demoActor:ReceiveBeginPlay()
AssertEqual(colorApplyCount, 1)
AssertTrue((string.find(lastStatusText, "Pings: 0", 1, true)) ~= nil)
demoActor:ReceiveTick(0.25)
AssertEqual(rotationAddCount, 1)
AssertTrue(lastVerticalOffset ~= 0)
EventBus.Emit("Demo.Ping", "TestSuite")
AssertEqual(colorApplyCount, 2)
AssertTrue((string.find(lastStatusText, "Pings: 1", 1, true)) ~= nil)
EventBus.Emit(HotReload.EVENT_AFTER_RELOAD, "Game.LuaDemoActor")
AssertTrue((string.find(lastStatusText, "Reloads: 1", 1, true)) ~= nil)
demoActor:ReceiveEndPlay(UE.EEndPlayReason.Destroyed)
EventBus.Emit("Demo.Ping", "AfterEndPlay")
AssertEqual(colorApplyCount, 2)
EventBus.Clear()
end
)
end
)
end
return ____exports
@@ -0,0 +1,93 @@
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 AssertFalse = ____TestRunner.AssertFalse
local AssertNil = ____TestRunner.AssertNil
local AssertArrayEqual = ____TestRunner.AssertArrayEqual
local ____MockUnLuaRuntime = require("Tests.Harness.MockUnLuaRuntime")
local AdvanceMockTime = ____MockUnLuaRuntime.AdvanceMockTime
local CreateBoundInstance = ____MockUnLuaRuntime.CreateBoundInstance
local GetMockState = ____MockUnLuaRuntime.GetMockState
local ResetMockState = ____MockUnLuaRuntime.ResetMockState
local EventBus = require("Framework.EventBus")
local LifecycleSafetyActor = require("Tests.Extra.LifecycleSafetyActor")
function ____exports.RunLifecycleAndMemoryTests()
Suite(
"Extra.LifecycleAndMemorySafety",
function()
Test(
"multiple @unlua instances maintain isolated state (no shared class-table arrays)",
function()
ResetMockState()
EventBus.Clear()
local cls = LifecycleSafetyActor
local a = CreateBoundInstance(cls, "AActor")
local b = CreateBoundInstance(cls, "AActor")
a:ReceiveBeginPlay()
b:ReceiveBeginPlay()
a:PushItem("only_in_a")
b:PushItem("only_in_b")
AssertArrayEqual(
a:GetItems(),
{"only_in_a"}
)
AssertArrayEqual(
b:GetItems(),
{"only_in_b"}
)
a:ReceiveEndPlay(UE.EEndPlayReason.Destroyed)
b:ReceiveEndPlay(UE.EEndPlayReason.Destroyed)
EventBus.Clear()
end
)
Test(
"paired cleanup on ReceiveEndPlay prevents leaked EventBus, Delegate, Timer & manual Ref",
function()
ResetMockState()
EventBus.Clear()
local actor = CreateBoundInstance(LifecycleSafetyActor, "AActor")
local asset = UE.UObject.Load("/Game/Data/DA_Config.DA_Config")
local weak = UE.FWeakObjectPtr()
weak:Set(actor)
actor:ReceiveBeginPlay()
actor:HoldManualRef(asset)
AssertEqual(
GetMockState().manualRefs[asset],
1
)
EventBus.Emit("Lifecycle.Broadcast", "msg1")
actor.OnTakeAnyDamage:Broadcast(
actor,
25,
nil,
nil,
nil
)
AdvanceMockTime(1)
AssertEqual(
actor:GetReceivedEvents(),
3
)
AssertTrue(weak:IsValid())
actor:ReceiveEndPlay(UE.EEndPlayReason.Destroyed)
actor:K2_DestroyActor()
AssertFalse(actor.OnTakeAnyDamage:IsBound())
AssertFalse(weak:IsValid())
AssertNil(weak:Get())
AssertNil(GetMockState().manualRefs[asset])
EventBus.Emit("Lifecycle.Broadcast", "msg2")
AdvanceMockTime(3)
AssertEqual(
actor:GetReceivedEvents(),
3
)
EventBus.Clear()
end
)
end
)
end
return ____exports
@@ -0,0 +1,54 @@
local EventBus = require("Framework.EventBus")
local LifecycleSafetyActor = UnLua.Class()
function LifecycleSafetyActor.ReceiveBeginPlay(self)
self.Overridden.ReceiveBeginPlay(self)
self.items = {}
self.receivedEvents = 0
EventBus.On("Lifecycle.Broadcast", self.OnBroadcast, self)
self.OnTakeAnyDamage:Add(self, self.OnDamaged)
self.timerHandle = UE.UKismetSystemLibrary.K2_SetTimerDelegate({self, self.OnHeartbeat}, 1, true)
end
function LifecycleSafetyActor.PushItem(self, item)
self.items[#self.items + 1] = item
end
function LifecycleSafetyActor.GetItems(self)
return self.items
end
function LifecycleSafetyActor.HoldManualRef(self, asset)
self.keptRef = asset
UnLua.Ref(asset)
end
function LifecycleSafetyActor.OnBroadcast(self, tag)
if not UE.UKismetSystemLibrary.IsValid(self) then
return
end
self.receivedEvents = self.receivedEvents + 1
self.items[#self.items + 1] = tag
end
function LifecycleSafetyActor.OnDamaged(self, _DamagedActor, Damage, _DamageType, _InstigatedBy, _DamageCauser)
if not UE.UKismetSystemLibrary.IsValid(self) then
return
end
self.receivedEvents = self.receivedEvents + 1
self.items[#self.items + 1] = "dmg:" .. tostring(Damage)
end
function LifecycleSafetyActor.OnHeartbeat(self)
if not UE.UKismetSystemLibrary.IsValid(self) then
return
end
self.receivedEvents = self.receivedEvents + 1
end
function LifecycleSafetyActor.GetReceivedEvents(self)
return self.receivedEvents
end
function LifecycleSafetyActor.ReceiveEndPlay(self, EndPlayReason)
EventBus.OffAllForOwner(self)
self.OnTakeAnyDamage:Remove(self, self.OnDamaged)
UE.UKismetSystemLibrary.K2_ClearAndInvalidateTimerHandle(self, self.timerHandle)
if self.keptRef ~= nil then
UnLua.Unref(self.keptRef)
self.keptRef = nil
end
self.Overridden.ReceiveEndPlay(self, EndPlayReason)
end
return LifecycleSafetyActor
@@ -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