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

157 lines
7.1 KiB
Lua
Raw Normal View History

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