Fix node callbacks unit test

This commit is contained in:
sfan5 2024-02-24 00:53:22 +01:00
parent 9b97147637
commit 91ea47fddf
3 changed files with 22 additions and 13 deletions

@ -5966,6 +5966,7 @@ Environment access
returns `{name="ignore", param1=0, param2=0}` for unloaded areas. returns `{name="ignore", param1=0, param2=0}` for unloaded areas.
* `minetest.get_node_or_nil(pos)` * `minetest.get_node_or_nil(pos)`
* Same as `get_node` but returns `nil` for unloaded areas. * Same as `get_node` but returns `nil` for unloaded areas.
* Note that areas may still contain "ignore" despite being loaded.
* `minetest.get_node_light(pos[, timeofday])` * `minetest.get_node_light(pos[, timeofday])`
* Gets the light value at the given position. Note that the light value * Gets the light value at the given position. Note that the light value
"inside" the node at the given position is returned, so you usually want "inside" the node at the given position is returned, so you usually want

@ -108,9 +108,9 @@ local function wait_for_player(callback)
end) end)
end end
local function wait_for_map(player, callback) local function wait_for_map(pos, callback)
local function check() local function check()
if core.get_node_or_nil(player:get_pos()) ~= nil then if core.get_node(pos).name ~= "ignore" then
callback() callback()
else else
core.after(0, check) core.after(0, check)
@ -119,8 +119,8 @@ local function wait_for_map(player, callback)
check() check()
end end
-- This runs in a coroutine so it uses await()
function unittests.run_all() function unittests.run_all()
-- This runs in a coroutine so it uses await().
local counters = { time = 0, total = 0, passed = 0 } local counters = { time = 0, total = 0, passed = 0 }
-- Run standalone tests first -- Run standalone tests first
@ -143,10 +143,11 @@ function unittests.run_all()
end end
-- Wait for the world to generate/load, run tests that require map access -- Wait for the world to generate/load, run tests that require map access
local pos = player:get_pos():round():offset(0, 5, 0)
core.forceload_block(pos, true, -1)
await(function(cb) await(function(cb)
wait_for_map(player, cb) wait_for_map(pos, cb)
end) end)
local pos = vector.round(player:get_pos())
for idx = 1, #unittests.list do for idx = 1, #unittests.list do
local def = unittests.list[idx] local def = unittests.list[idx]
if not def.done then if not def.done then

@ -99,17 +99,24 @@ local function test_clear_meta(_, pos)
end end
unittests.register("test_clear_meta", test_clear_meta, {map=true}) unittests.register("test_clear_meta", test_clear_meta, {map=true})
local on_punch_called local on_punch_called, on_place_called
minetest.register_on_punchnode(function() core.register_on_placenode(function()
on_place_called = true
end)
core.register_on_punchnode(function()
on_punch_called = true on_punch_called = true
end) end)
unittests.register("test_punch_node", function(_, pos) local function test_node_callbacks(_, pos)
minetest.place_node(pos, {name="basenodes:dirt"}) on_place_called = false
on_punch_called = false on_punch_called = false
minetest.punch_node(pos)
minetest.remove_node(pos) core.place_node(pos, {name="basenodes:dirt"})
-- currently failing: assert(on_punch_called) assert(on_place_called, "on_place not called")
end, {map=true}) core.punch_node(pos)
assert(on_punch_called, "on_punch not called")
core.remove_node(pos)
end
unittests.register("test_node_callbacks", test_node_callbacks, {map=true})
local function test_hashing() local function test_hashing()
local input = "hello\000world" local input = "hello\000world"