luby/crates/lb/tests/task.lua
2025-06-27 04:06:35 +10:00

57 lines
1.5 KiB
Lua

local ok, task = pcall(require, "lb:task")
if not ok then return end
describe("spawn", function()
test("callback receives args", function()
spawn(function(...)
assert(select("#", ...) == 0)
end):await()
spawn(function(...)
assert(select("#", ...) == 1)
assert((...) == nil)
end, nil):await()
spawn(function(...)
assert(select("#", ...) == 4)
local args = table.pack(...)
assert(args[1] == 1 and args[2] == 2 and args[3] == nil and args[4] == 3)
end, 1, 2, nil, 3):await()
end)
test("await returns callback results", function()
local res = table.pack(task
:spawn(function()
-- no returns
end)
:await())
assert(res.n == 0)
local res = table.pack(task
:spawn(function()
return nil
end)
:await())
assert(res.n == 1 and res[1] == nil)
local res = table.pack(task
:spawn(function()
return 1, 2, nil, 3
end)
:await())
assert(res.n == 4 and res[1] == 1 and res[2] == 2 and res[3] == nil and res[4] == 3)
end)
test("callback args and results", function()
local res = table.pack(task
:spawn(function(...)
assert(select("#", ...) == 5)
local args = table.pack(...)
assert(args[1] == 5 and args[2] == 4 and args[3] == nil and args[4] == 3, args[5] == nil)
return 1, 3, nil
end, 5, 4, nil, 3, nil)
:await())
assert(res.n == 3 and res[1] == 1 and res[2] == 3 and res[3] == nil)
end)
end)