From af8eb084334d327358e660a2e849f2c008a1bf20 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Mon, 23 Jan 2023 19:13:18 -0500 Subject: [PATCH] Update unit tests & typedefs --- luneTypes.d.luau | 16 ++++++++-------- src/tests/task/cancel.luau | 4 +++- src/tests/task/defer.luau | 5 +++-- src/tests/task/delay.luau | 5 +++-- src/tests/task/spawn.luau | 14 ++++++++++++-- 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/luneTypes.d.luau b/luneTypes.d.luau index 6fb9fa3..b0e92cc 100644 --- a/luneTypes.d.luau +++ b/luneTypes.d.luau @@ -53,19 +53,19 @@ declare process: { } declare task: { - cancel: (t: thread) -> (), - defer: (f: thread | (...any) -> (...any)) -> thread, - delay: (duration: number?, f: thread | (...any) -> (...any)) -> thread, - spawn: (f: thread | (...any) -> (...any)) -> thread, + cancel: (thread: thread) -> (), + defer: (functionOrThread: thread | (...any) -> (...any)) -> thread, + delay: (duration: number?, functionOrThread: thread | (...any) -> (...any)) -> thread, + spawn: (functionOrThread: thread | (...any) -> (...any)) -> thread, wait: (duration: number?) -> (number), } --[[ declare task: { - cancel: (t: thread) -> (), - defer: (f: thread | (T...) -> (...any), T...) -> thread, - delay: (duration: number?, f: thread | (T...) -> (...any), T...) -> thread, - spawn: (f: thread | (T...) -> (...any), T...) -> thread, + cancel: (thread: thread) -> (), + defer: (functionOrThread: thread | (T...) -> (...any), T...) -> thread, + delay: (duration: number?, functionOrThread: thread | (T...) -> (...any), T...) -> thread, + spawn: (functionOrThread: thread | (T...) -> (...any), T...) -> thread, wait: (duration: number?) -> (number), } ]] diff --git a/src/tests/task/cancel.luau b/src/tests/task/cancel.luau index e81e441..17ab87d 100644 --- a/src/tests/task/cancel.luau +++ b/src/tests/task/cancel.luau @@ -2,14 +2,16 @@ local flag: boolean = false local thread = task.defer(function() + task.wait(0.1) flag = true end) local thread2 = task.delay(0, function() + task.wait(0.1) flag = true end) task.cancel(thread) task.cancel(thread2) -task.wait(0.1) +task.wait(0.2) assert(not flag, "Cancel should handle non-immediate threads") -- Cancellation should be as immediate as possible diff --git a/src/tests/task/defer.luau b/src/tests/task/defer.luau index 46884ea..6fa2a77 100644 --- a/src/tests/task/defer.luau +++ b/src/tests/task/defer.luau @@ -40,6 +40,8 @@ assert(not flag2, "Defer should run after spawned threads") -- Varargs should get passed correctly +-- TODO: Uncomment when vararg support is added +--[[ 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") @@ -49,5 +51,4 @@ end task.defer(f, "", 1, f) task.defer(f, "inf", math.huge, f) task.defer(f, "NaN", 0 / 0, f) - -task.wait(0.1) +]] diff --git a/src/tests/task/delay.luau b/src/tests/task/delay.luau index fa0327e..724f924 100644 --- a/src/tests/task/delay.luau +++ b/src/tests/task/delay.luau @@ -28,6 +28,8 @@ assert(not flag2, "Delay should work with yielding (2)") -- Varargs should get passed correctly +-- TODO: Uncomment when vararg support is added +--[[ 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") @@ -37,5 +39,4 @@ end task.delay(0, f, "", 1, f) task.delay(0, f, "inf", math.huge, f) task.delay(0, f, "NaN", 0 / 0, f) - -task.wait(0.1) +]] diff --git a/src/tests/task/spawn.luau b/src/tests/task/spawn.luau index 24f8c00..a91a94b 100644 --- a/src/tests/task/spawn.luau +++ b/src/tests/task/spawn.luau @@ -22,8 +22,19 @@ assert(not flag2, "Spawn should work with yielding (1)") task.wait(0.2) assert(flag2, "Spawn should work with yielding (2)") +-- Spawned functions should be able to run threads created with the coroutine global + +local flag3: boolean = false +local thread2 = coroutine.create(function() + flag3 = true +end) +task.spawn(thread2) +assert(flag3, "Spawn should run threads made from coroutine.create") + -- Varargs should get passed correctly +-- TODO: Uncomment when vararg support is added +--[[ 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") @@ -33,5 +44,4 @@ end task.spawn(f, "", 1, f) task.spawn(f, "inf", math.huge, f) task.spawn(f, "NaN", 0 / 0, f) - -task.wait(0.1) +]]