From dc3de6ef8d4bb1a8ce7b2932515a0f6287ab1e76 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Thu, 21 Aug 2025 09:13:35 +0200 Subject: Improved func_is_string unit test --- tests/func_is_string.lua | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) (limited to 'tests/func_is_string.lua') diff --git a/tests/func_is_string.lua b/tests/func_is_string.lua index 3c91603..3378a91 100644 --- a/tests/func_is_string.lua +++ b/tests/func_is_string.lua @@ -1,6 +1,6 @@ -local lanes = require "lanes" +local lanes = require "lanes".configure() --- Lua 5.4 specific: +-- Lua 5.4+ specific: if _VERSION >= "Lua 5.4" then -- go through a string so that the script we are in doesn't trigger a parse error with older Lua local res = assert(load [[ @@ -14,24 +14,32 @@ if _VERSION >= "Lua 5.4" then ]])() -- when the do...end block is exited, the to-be-closed variable h is closed, which internally calls h:join() -- therefore the status of the lane should be "done" - assert(res == "done") - print("close result:", res) + assert(res == "done", "got " .. tostring(res)) end +-- TEST: a string with a parse error is handled properly +do + -- the generator does not compile the string yet + local g = lanes.gen("*", { name = 'auto' }, "retrun true") + -- invoking the generator compiles the string + local s, r = pcall(g) + assert(s == false and type(r) == 'string' and string.find(r, "error when parsing lane function code"), "got " .. r) +end -local options = {globals = { b = 666 }} - -local gen1 = lanes.gen("*", { name = 'auto' }, "return true, error('bob')") - -fibLane = gen1() -lanes.sleep(0.1) -print(fibLane, fibLane.status) -local _r, _err, _stk = fibLane:join() -assert(_r == nil, "got " .. tostring(_r) .. " " .. tostring(_err) .. " " .. tostring(_stk)) - -local gen2 = lanes.gen(options, { name = 'auto' }, "return b") -local retLane1, retLane2 = gen2(), gen2() +-- TEST: lane executes fine, raises the expected error +do + local g = lanes.gen("*", { name = 'auto' }, "return true, error('bob')") + fibLane = g() + lanes.sleep(0.1) + assert(fibLane.status == "error") + local _r, _err, _stk = fibLane:join() + assert(_r == nil and _err == [[[string "return true, error('bob')"]:1: bob]], "got " .. tostring(_r) .. " " .. tostring(_err) .. " " .. tostring(_stk)) +end -print( retLane1[1], retLane2[1]) -assert(retLane1[1] == 666 and retLane2[1] == 666) -print "TEST OK" +-- TEST: lanes execute and return the expected value obtained from the provided globals +do + local options = {globals = { b = 666 }} + local g = lanes.gen(options, { name = 'auto' }, "return b") + local retLane1, retLane2 = g(), g() + assert(retLane1[1] == 666 and retLane2[1] == 666) +end -- cgit v1.2.3-55-g6feb