Skip to content

Commit

Permalink
Extend test_async_metatable_registration
Browse files Browse the repository at this point in the history
  • Loading branch information
appgurueu committed Mar 4, 2024
1 parent 8806622 commit 3a762cb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
23 changes: 19 additions & 4 deletions games/devtest/mods/unittests/async_env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,34 @@ local function test_async_metatable_registration(cb)
if pcall(core.register_async_metatable, "__builtin:vector", {}) then
error("Illegal metatable overriding allowed")
end

local custom_metatable = {}
core.register_async_metatable("unittests:custom_metatable", custom_metatable)

core.handle_async(function(x)
-- unittests.custom_metatable is registered in inside_async_env.lua
return getmetatable(x) == unittests.custom_metatable, x
end, function(metatable_preserved_async, table_after_roundtrip)
if not metatable_preserved_async then
return cb("Custom metatable not preserved (main -> async)")
end
if getmetatable(table_after_roundtrip) ~= custom_metatable then
return cb("Custom metable not preserved (after roundtrip)")
end
cb()
end, setmetatable({}, custom_metatable))
end
unittests.register("test_async_metatable_registration", test_async_metatable_registration)
unittests.register("test_async_metatable_registration", test_async_metatable_registration, {async=true})

local function test_vector_preserve(cb)
local vec = vector.new(1, 2, 3)
core.handle_async(function(x)
return x[1]
end, function(ret)
if ret == vec then -- only succeeds if metatable was preserved
cb()
else
if ret ~= vec then -- fails if metatable was not preserved
return cb("Vector value mismatch")
end
cb()
end, {vec})
end
unittests.register("test_async_vector", test_vector_preserve, {async=true})
3 changes: 3 additions & 0 deletions games/devtest/mods/unittests/inside_async_env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ unittests = {}

core.log("info", "Hello World")

unittests.custom_metatable = {}
core.register_async_metatable("unittests:custom_metatable", unittests.custom_metatable)

local function do_tests()
assert(core == minetest)
-- stuff that should not be here
Expand Down

0 comments on commit 3a762cb

Please sign in to comment.