Files
UnrealLuaDemo/Script/lua/Tests/Extra/HotReloadAndFrameworkTest.lua
T

170 lines
8.2 KiB
Lua
Raw Normal View History

local ____exports = __module.env
local M__Tests_Harness_TestRunner = __MODULE__.Import("Tests.Harness.TestRunner", __module)
local Suite = M__Tests_Harness_TestRunner.Suite
local Test = M__Tests_Harness_TestRunner.Test
local AssertEqual = M__Tests_Harness_TestRunner.AssertEqual
local AssertTrue = M__Tests_Harness_TestRunner.AssertTrue
local AssertFalse = M__Tests_Harness_TestRunner.AssertFalse
local AssertNotNil = M__Tests_Harness_TestRunner.AssertNotNil
local AssertNil = M__Tests_Harness_TestRunner.AssertNil
local AssertArrayEqual = M__Tests_Harness_TestRunner.AssertArrayEqual
local M__Tests_Harness_MockUnLuaRuntime = __MODULE__.Import("Tests.Harness.MockUnLuaRuntime", __module)
local CreateBoundInstance = M__Tests_Harness_MockUnLuaRuntime.CreateBoundInstance
local ResetMockState = M__Tests_Harness_MockUnLuaRuntime.ResetMockState
local Bootstrap = __MODULE__.Import("Framework.Bootstrap", __module)
local EventBus = __MODULE__.Import("Framework.EventBus", __module)
local HotReload = __MODULE__.Import("Framework.HotReload", __module)
local LuaDemoActor = __MODULE__.Import("Game.LuaDemoActor", __module)
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: pure TS callback BeginPlay, Tick, Ping color cycle, HotReload & EndPlay cleanup",
function()
ResetMockState()
EventBus.Clear()
local lastStatusText = ""
local colorApplyCount = 0
local rotationAddCount = 0
local lastVerticalOffset = 0
local fakeActor = CreateBoundInstance(
{},
"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
}
)
LuaDemoActor.OnBeginPlay(fakeActor)
AssertEqual(colorApplyCount, 1)
AssertTrue((string.find(lastStatusText, "Pings: 0", 1, true)) ~= nil)
AssertTrue((string.find(lastStatusText, "Speed: 60", 1, true)) ~= nil)
LuaDemoActor.OnTick(fakeActor, 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")
AssertEqual(colorApplyCount, 3)
AssertTrue((string.find(lastStatusText, "Reloads: 1", 1, true)) ~= nil)
LuaDemoActor.OnEndPlay(fakeActor, UE.EEndPlayReason.Destroyed)
EventBus.Emit("Demo.Ping", "AfterEndPlay")
AssertEqual(colorApplyCount, 3)
EventBus.Clear()
end
)
end
)
end
function __module.UpdateImports()
Suite = M__Tests_Harness_TestRunner.Suite
Test = M__Tests_Harness_TestRunner.Test
AssertEqual = M__Tests_Harness_TestRunner.AssertEqual
AssertTrue = M__Tests_Harness_TestRunner.AssertTrue
AssertFalse = M__Tests_Harness_TestRunner.AssertFalse
AssertNotNil = M__Tests_Harness_TestRunner.AssertNotNil
AssertNil = M__Tests_Harness_TestRunner.AssertNil
AssertArrayEqual = M__Tests_Harness_TestRunner.AssertArrayEqual
CreateBoundInstance = M__Tests_Harness_MockUnLuaRuntime.CreateBoundInstance
ResetMockState = M__Tests_Harness_MockUnLuaRuntime.ResetMockState
end