216 lines
8.9 KiB
Lua
216 lines
8.9 KiB
Lua
local ____lualib = require("lualib_bundle")
|
|||
|
|
local __TS__New = ____lualib.__TS__New
|
||
|
|
local ____exports = __module.env
|
||
|
|
local M__share_base_scoped_object = __MODULE__.Import("share.base.scoped_object", __module)
|
||
|
|
local ScopedObject = M__share_base_scoped_object.ScopedObject
|
||
|
|
local M__share_base_traceback = __MODULE__.Import("share.base.traceback", __module)
|
||
|
|
local SetupTracebackHandler = M__share_base_traceback.SetupTracebackHandler
|
||
|
|
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
|
||
|
|
function ____exports.CreateEventHubSuite()
|
||
|
|
return {
|
||
|
|
name = "Step1.EventHub",
|
||
|
|
cases = {
|
||
|
|
{
|
||
|
|
name = "Duplicate (emitter, event, owner) subscription replaces previous listener",
|
||
|
|
fn = function()
|
||
|
|
local hub = __TS__New(EventHub, "DupTest")
|
||
|
|
local owner = __TS__New(ScopedObject)
|
||
|
|
local log = {}
|
||
|
|
hub.on.Ping(
|
||
|
|
function(a)
|
||
|
|
log[#log + 1] = "old:" .. tostring(a)
|
||
|
|
end,
|
||
|
|
owner
|
||
|
|
)
|
||
|
|
hub.on.Ping(
|
||
|
|
function(a)
|
||
|
|
log[#log + 1] = "new:" .. tostring(a)
|
||
|
|
end,
|
||
|
|
owner
|
||
|
|
)
|
||
|
|
AssertEqual(
|
||
|
|
hub:GetListenerCount("Ping"),
|
||
|
|
1,
|
||
|
|
"listener count"
|
||
|
|
)
|
||
|
|
hub.emit.Ping(42)
|
||
|
|
AssertArrayEqual(log, {"new:42"})
|
||
|
|
owner:Dispose()
|
||
|
|
AssertEqual(
|
||
|
|
hub:GetListenerCount("Ping"),
|
||
|
|
0,
|
||
|
|
"after dispose"
|
||
|
|
)
|
||
|
|
hub:Dispose()
|
||
|
|
end
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name = "Callback receives exact event args (no owner prepended) and respects Enable/Disable/OffAll",
|
||
|
|
fn = function()
|
||
|
|
local hub = __TS__New(EventHub, "ArgsTest")
|
||
|
|
local owner = __TS__New(ScopedObject)
|
||
|
|
local received = {}
|
||
|
|
hub.on.Ping(
|
||
|
|
function(a, b, c)
|
||
|
|
received[#received + 1] = string.format(
|
||
|
|
"%s|%s|%s",
|
||
|
|
tostring(a),
|
||
|
|
tostring(b),
|
||
|
|
tostring(c)
|
||
|
|
)
|
||
|
|
end,
|
||
|
|
owner
|
||
|
|
)
|
||
|
|
hub.emit.Ping(7, "hello", 9)
|
||
|
|
owner:DisableAllEventListeners()
|
||
|
|
hub.emit.Ping(8, "skip", 10)
|
||
|
|
owner:EnableAllEventListeners()
|
||
|
|
hub.emit.Ping(11, nil, 13)
|
||
|
|
owner:OffAllEventListeners()
|
||
|
|
hub.emit.Ping(99, "gone")
|
||
|
|
AssertArrayEqual(received, {"7|hello|9", "11|nil|13"})
|
||
|
|
hub:Dispose()
|
||
|
|
end
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name = "Cancelling next listener or disposing next owner during dispatch skips it immediately",
|
||
|
|
fn = function()
|
||
|
|
local subB
|
||
|
|
local hub = __TS__New(EventHub, "MutationTest")
|
||
|
|
local ownerA = __TS__New(ScopedObject)
|
||
|
|
local ownerB = __TS__New(ScopedObject)
|
||
|
|
local ownerC = __TS__New(ScopedObject)
|
||
|
|
local order = {}
|
||
|
|
local listenerB = hub.on.Ping(
|
||
|
|
function()
|
||
|
|
order[#order + 1] = "B"
|
||
|
|
end,
|
||
|
|
ownerB
|
||
|
|
)
|
||
|
|
listenerB:Off()
|
||
|
|
hub.on.Ping(
|
||
|
|
function()
|
||
|
|
order[#order + 1] = "A"
|
||
|
|
subB:Off()
|
||
|
|
ownerC:Dispose()
|
||
|
|
end,
|
||
|
|
ownerA
|
||
|
|
)
|
||
|
|
subB = hub.on.Ping(
|
||
|
|
function()
|
||
|
|
order[#order + 1] = "B"
|
||
|
|
end,
|
||
|
|
ownerB
|
||
|
|
)
|
||
|
|
hub.on.Ping(
|
||
|
|
function()
|
||
|
|
order[#order + 1] = "C"
|
||
|
|
end,
|
||
|
|
ownerC
|
||
|
|
)
|
||
|
|
hub.emit.Ping(1)
|
||
|
|
AssertArrayEqual(order, {"A"})
|
||
|
|
ownerA:Dispose()
|
||
|
|
ownerB:Dispose()
|
||
|
|
hub:Dispose()
|
||
|
|
end
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name = "Nested Emit and listener error isolation",
|
||
|
|
fn = function()
|
||
|
|
local capturedErrors = {}
|
||
|
|
SetupTracebackHandler({DumpTraceback = function(msg)
|
||
|
|
capturedErrors[#capturedErrors + 1] = msg
|
||
|
|
end})
|
||
|
|
local hub = __TS__New(EventHub, "NestedTest")
|
||
|
|
local ownerA = __TS__New(ScopedObject)
|
||
|
|
local ownerB = __TS__New(ScopedObject)
|
||
|
|
local order = {}
|
||
|
|
hub.on.Chain(
|
||
|
|
function(step)
|
||
|
|
order[#order + 1] = "A:" .. tostring(step)
|
||
|
|
if step == 1 then
|
||
|
|
hub.emit.Chain(2)
|
||
|
|
end
|
||
|
|
if step == 2 then
|
||
|
|
error("boom-in-listener")
|
||
|
|
end
|
||
|
|
end,
|
||
|
|
ownerA
|
||
|
|
)
|
||
|
|
hub.on.Chain(
|
||
|
|
function(step)
|
||
|
|
order[#order + 1] = "B:" .. tostring(step)
|
||
|
|
end,
|
||
|
|
ownerB
|
||
|
|
)
|
||
|
|
hub.emit.Chain(1)
|
||
|
|
SetupTracebackHandler(nil)
|
||
|
|
AssertArrayEqual(order, {"A:1", "A:2", "B:2", "B:1"})
|
||
|
|
Assert(#capturedErrors == 1, "expected 1 isolated error")
|
||
|
|
ownerA:Dispose()
|
||
|
|
ownerB:Dispose()
|
||
|
|
hub:Dispose()
|
||
|
|
end
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name = "preEmit and asyncEmit preserve trailing nil via table.pack.n",
|
||
|
|
fn = function()
|
||
|
|
local pending = {}
|
||
|
|
local preCalls = {}
|
||
|
|
local hub = __TS__New(
|
||
|
|
EventHub,
|
||
|
|
"AsyncTest",
|
||
|
|
{
|
||
|
|
preEmit = function(evName)
|
||
|
|
preCalls[#preCalls + 1] = evName
|
||
|
|
end,
|
||
|
|
asyncEmit = function(_evName, emit)
|
||
|
|
pending[#pending + 1] = emit
|
||
|
|
end
|
||
|
|
}
|
||
|
|
)
|
||
|
|
local owner = __TS__New(ScopedObject)
|
||
|
|
local argCount = -1
|
||
|
|
local capturedA
|
||
|
|
local capturedB
|
||
|
|
local capturedC
|
||
|
|
hub.on.Trailing(
|
||
|
|
function(...)
|
||
|
|
local args = {...}
|
||
|
|
argCount = select("#", ...)
|
||
|
|
capturedA = args[1]
|
||
|
|
capturedB = args[2]
|
||
|
|
capturedC = args[3]
|
||
|
|
end,
|
||
|
|
owner
|
||
|
|
)
|
||
|
|
hub.emit.Trailing("first", nil, nil)
|
||
|
|
AssertEqual(#preCalls, 0, "preEmit should not run before async flush")
|
||
|
|
AssertEqual(#pending, 1, "one queued async emit")
|
||
|
|
pending[1]()
|
||
|
|
AssertArrayEqual(preCalls, {"Trailing"})
|
||
|
|
AssertEqual(argCount, 3, "trailing nils must be preserved")
|
||
|
|
AssertEqual(capturedA, "first")
|
||
|
|
AssertEqual(capturedB, nil)
|
||
|
|
AssertEqual(capturedC, nil)
|
||
|
|
owner:Dispose()
|
||
|
|
hub:Dispose()
|
||
|
|
end
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
end
|
||
|
|
function __module.UpdateImports()
|
||
|
|
ScopedObject = M__share_base_scoped_object.ScopedObject
|
||
|
|
SetupTracebackHandler = M__share_base_traceback.SetupTracebackHandler
|
||
|
|
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
|