lune/src/tests/task/defer.luau

46 lines
1.1 KiB
Text
Raw Normal View History

-- Deferred functions should run after other threads
local flag: boolean = false
task.defer(function()
flag = true
end)
assert(not flag, "Defer should run after other threads, including the main thread")
-- Deferred functions should work with yielding
local flag2: boolean = false
task.defer(function()
task.wait()
flag2 = true
end)
assert(not flag2, "Defer should work with yielding")
-- Deferred functions should run after other spawned threads
local flag3: boolean = false
task.defer(function()
if flag3 == true then
flag3 = false
end
end)
task.spawn(function()
if flag3 == false then
flag3 = true
end
end)
task.wait()
assert(not flag2, "Defer should run after spawned threads")
-- Varargs should get passed correctly
local function f(arg1: string, arg2: number, f2: (...any) -> ...any)
assert(type(arg1) == "string", "Invalid arg 1 passed to function")
assert(type(arg2) == "number", "Invalid arg 2 passed to function")
assert(type(arg3) == "function", "Invalid arg 3 passed to function")
end
task.defer(f, "", 1, f)
task.defer(f, "inf", math.huge, f)
task.defer(f, "NaN", 0 / 0, f)
task.wait(0.1)