Files

189 lines
7.9 KiB
Lua

local ____lualib = require("lualib_bundle")
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
local __TS__New = ____lualib.__TS__New
local ____exports = __module.env
local M__framework_context_game_context = __MODULE__.Import("framework.context.game_context", __module)
local GameInstanceContext = M__framework_context_game_context.GameInstanceContext
local M__framework_module_mgr_base = __MODULE__.Import("framework.module.mgr_base", __module)
local ControllerBase = M__framework_module_mgr_base.ControllerBase
local MgrBase = M__framework_module_mgr_base.MgrBase
local MgrDataBase = M__framework_module_mgr_base.MgrDataBase
local M__share_event_event_hub = __MODULE__.Import("share.event.event_hub", __module)
local EventHub = M__share_event_event_hub.EventHub
local M__tests_harness_test_runner = __MODULE__.Import("tests.harness.test_runner", __module)
local Assert = M__tests_harness_test_runner.Assert
local AssertArrayEqual = M__tests_harness_test_runner.AssertArrayEqual
local AssertEqual = M__tests_harness_test_runner.AssertEqual
local SampleData = __TS__DefineClass("SampleData", __module)
SampleData.name = "SampleData"
__TS__ClassExtends(SampleData, MgrDataBase)
function SampleData.prototype.____constructor(self, ...)
MgrDataBase.prototype.____constructor(self, ...)
self.value = 0
self.disposed = false
end
function SampleData.prototype.Dispose(self)
self.disposed = true
end
local SampleSubController = __TS__DefineClass("SampleSubController", __module)
SampleSubController.name = "SampleSubController"
__TS__ClassExtends(SampleSubController, ControllerBase)
function SampleSubController.prototype.____constructor(self, ...)
ControllerBase.prototype.____constructor(self, ...)
self.events = {}
end
function SampleSubController.prototype.OnEnter(self)
self.events[#self.events + 1] = "enter"
end
function SampleSubController.prototype.OnActive(self)
self.events[#self.events + 1] = "active"
end
function SampleSubController.prototype.OnExit(self)
self.events[#self.events + 1] = "exit"
end
local SampleMgr = __TS__DefineClass("SampleMgr", __module)
SampleMgr.name = "SampleMgr"
__TS__ClassExtends(SampleMgr, MgrBase)
function SampleMgr.prototype.____constructor(self, ...)
MgrBase.prototype.____constructor(self, ...)
self.lifecycle = {}
end
function SampleMgr.prototype.OnCreate(self)
self.lifecycle[#self.lifecycle + 1] = "create"
end
function SampleMgr.prototype.OnBindControllers(self)
self.subCtrl = self:AddController(SampleSubController)
end
function SampleMgr.prototype.OnBindEvents(self)
self.lifecycle[#self.lifecycle + 1] = "bind"
end
function SampleMgr.prototype.OnClean(self)
self.lifecycle[#self.lifecycle + 1] = "clean"
end
function ____exports.CreateContextAndMgrSuite()
return {
name = "Step1.ContextAndMgr",
cases = {
{
name = "MgrRegistry creates, looks up by ctor and name, and cleans both indexes on DelMgr",
fn = function()
local ctx = __TS__New(GameInstanceContext)
local mgr = ctx.mgrRegistry:CreateMgr(SampleMgr, "SampleMgr")
local data = ctx.mgrRegistry:CreateMgrData(SampleData)
data.value = 123
AssertEqual(
ctx.mgrRegistry:GetMgr(SampleMgr),
mgr
)
AssertEqual(
ctx.mgrRegistry:GetMgrByName("SampleMgr"),
mgr
)
AssertEqual(
ctx.mgrRegistry:GetMgrData(SampleData),
data
)
AssertArrayEqual(mgr.lifecycle, {"create", "bind"})
Assert(mgr.subCtrl ~= nil, "sub controller attached")
AssertArrayEqual(mgr.subCtrl.events, {"enter", "active"})
ctx.mgrRegistry:DelMgr(SampleMgr)
AssertEqual(
ctx.mgrRegistry:GetMgr(SampleMgr),
nil
)
AssertEqual(
ctx.mgrRegistry:GetMgrByName("SampleMgr"),
nil,
"name index must be cleared"
)
Assert(
mgr:IsDisposed(),
"mgr disposed"
)
AssertArrayEqual(mgr.lifecycle, {"create", "bind", "clean"})
AssertArrayEqual(mgr.subCtrl.events, {"enter", "active", "exit"})
ctx:Dispose()
Assert(data.disposed, "data disposed with context")
end
},
{
name = "Two GameInstanceContexts coexist in one Lua VM; disposing #1 keeps #2 intact",
fn = function()
local ctx1 = __TS__New(GameInstanceContext)
local ctx2 = __TS__New(GameInstanceContext)
local hub1 = __TS__New(EventHub, "Hub1")
local hub2 = __TS__New(EventHub, "Hub2")
ctx1:RegisterService("bizHub", hub1)
ctx2:RegisterService("bizHub", hub2)
local mgr1 = ctx1.mgrRegistry:CreateMgr(SampleMgr, "SampleMgr")
local mgr2 = ctx2.mgrRegistry:CreateMgr(SampleMgr, "SampleMgr")
local log1 = {}
local log2 = {}
hub1.on.Notify(
function(msg)
log1[#log1 + 1] = msg
end,
mgr1
)
hub2.on.Notify(
function(msg)
log2[#log2 + 1] = msg
end,
mgr2
)
ctx1.scheduler:AddMonotonicRepeatTimer(
0.05,
function()
log1[#log1 + 1] = "tick1"
end,
mgr1
)
ctx2.scheduler:AddMonotonicRepeatTimer(
0.05,
function()
log2[#log2 + 1] = "tick2"
end,
mgr2
)
local shutdownOrder = {}
ctx1:AddShutdownHook(function()
shutdownOrder[#shutdownOrder + 1] = "hook1"
end)
ctx1:AddShutdownHook(function()
shutdownOrder[#shutdownOrder + 1] = "hook2"
end)
ctx1:Dispose()
AssertArrayEqual(shutdownOrder, {"hook2", "hook1"}, "shutdown hooks run in reverse order")
Assert(
mgr1:IsDisposed(),
"mgr1 disposed"
)
Assert(
not mgr2:IsDisposed(),
"mgr2 still alive"
)
hub2.emit.Notify("alive")
ctx2:Tick(0.06)
AssertArrayEqual(log1, {})
AssertArrayEqual(log2, {"alive", "tick2"})
ctx2:Dispose()
Assert(
mgr2:IsDisposed(),
"mgr2 disposed cleanly"
)
end
}
}
}
end
function __module.UpdateImports()
GameInstanceContext = M__framework_context_game_context.GameInstanceContext
ControllerBase = M__framework_module_mgr_base.ControllerBase
MgrBase = M__framework_module_mgr_base.MgrBase
MgrDataBase = M__framework_module_mgr_base.MgrDataBase
EventHub = M__share_event_event_hub.EventHub
Assert = M__tests_harness_test_runner.Assert
AssertArrayEqual = M__tests_harness_test_runner.AssertArrayEqual
AssertEqual = M__tests_harness_test_runner.AssertEqual
end