aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2025-09-30 15:06:21 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2025-09-30 15:06:21 +0200
commitae43a00657a6505910010bdf920c9c0f4823a9c0 (patch)
tree198f7e3d816ac99152a94f5fe3a5328117a36a8e /tests
parentf011dcafb0f583c89ed9971238fd83ddcbdb5438 (diff)
downloadlanes-master.tar.gz
lanes-master.tar.bz2
lanes-master.zip
Lift restriction on functions and userdata as table keysHEADmaster
As demonstrated by the unit tests, there is no problem with using a function or a userdata as a table key, as long as they are transferable
Diffstat (limited to 'tests')
-rw-r--r--tests/errhangtest.lua20
1 files changed, 12 insertions, 8 deletions
diff --git a/tests/errhangtest.lua b/tests/errhangtest.lua
index 5b3f0c0..819bc10 100644
--- a/tests/errhangtest.lua
+++ b/tests/errhangtest.lua
@@ -42,19 +42,23 @@ if true then
42 print "OK" 42 print "OK"
43end 43end
44 44
45-- send a table that contains a function 45-- =================================================================================================
46-- send a table that contains a function, both as key and value
47-- =================================================================================================
48
46if true then 49if true then
47 print "\n#### send table with a function" 50 print "\n#### send table with a function"
48 local fun = function() print "function test ok" end 51 local fun = function() print "function test ok" return 42 end
49 local t_in = { [fun] = fun, fun = fun } 52 local t_in = { [fun] = fun, ["fun"] = fun }
50 print(pcall(linda.send, linda, 'test', t_in)) 53 print(pcall(linda.send, linda, 'test', t_in))
51 local k,t_out = linda:receive('test') -- read the contents successfully sent 54 local k, t_out = linda:receive('test') -- read the contents successfully sent
52 t_out.fun() 55 -- t_out should contain two entries, just like t_in
53 -- t_out should contain a single entry, as [fun] = fun should have been discarded because functions are not acceptable keys
54 local count = 0 56 local count = 0
55 for k,v in pairs(t_out) do count = count + 1 end 57 for k,v in pairs(t_out) do count = count + 1 end
56 assert(count == 1) 58 assert(count == 2)
57 print "OK" 59 assert(t_out["fun"] == t_out[t_out.fun]) -- t_out[t_out.fun] should be identical to t_out["fun"]
60 assert(t_out["fun"] ~= fun) -- even if the function is not the original one but a copy
61 assert(t_out["fun"]() == t_out[t_out.fun]() and t_out["fun"]() == fun()) -- all functions should return the same value, since they are the same
58end 62end
59 63
60-- send a string 64-- send a string