From 91ea47fddf6c59f04fed4af66fa23127466e46fb Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sat, 24 Feb 2024 00:53:22 +0100 Subject: [PATCH] Fix node callbacks unit test --- doc/lua_api.md | 1 + games/devtest/mods/unittests/init.lua | 11 ++++++----- games/devtest/mods/unittests/misc.lua | 23 +++++++++++++++-------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/doc/lua_api.md b/doc/lua_api.md index 6dbc58cb0..6f3fccb9f 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -5966,6 +5966,7 @@ Environment access returns `{name="ignore", param1=0, param2=0}` for unloaded areas. * `minetest.get_node_or_nil(pos)` * 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])` * 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 diff --git a/games/devtest/mods/unittests/init.lua b/games/devtest/mods/unittests/init.lua index f048b7aa6..b01a6271c 100644 --- a/games/devtest/mods/unittests/init.lua +++ b/games/devtest/mods/unittests/init.lua @@ -108,9 +108,9 @@ local function wait_for_player(callback) end) end -local function wait_for_map(player, callback) +local function wait_for_map(pos, callback) local function check() - if core.get_node_or_nil(player:get_pos()) ~= nil then + if core.get_node(pos).name ~= "ignore" then callback() else core.after(0, check) @@ -119,8 +119,8 @@ local function wait_for_map(player, callback) check() end +-- This runs in a coroutine so it uses await() function unittests.run_all() - -- This runs in a coroutine so it uses await(). local counters = { time = 0, total = 0, passed = 0 } -- Run standalone tests first @@ -143,10 +143,11 @@ function unittests.run_all() end -- 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) - wait_for_map(player, cb) + wait_for_map(pos, cb) end) - local pos = vector.round(player:get_pos()) for idx = 1, #unittests.list do local def = unittests.list[idx] if not def.done then diff --git a/games/devtest/mods/unittests/misc.lua b/games/devtest/mods/unittests/misc.lua index 034b99b52..6ff5c7e84 100644 --- a/games/devtest/mods/unittests/misc.lua +++ b/games/devtest/mods/unittests/misc.lua @@ -99,17 +99,24 @@ local function test_clear_meta(_, pos) end unittests.register("test_clear_meta", test_clear_meta, {map=true}) -local on_punch_called -minetest.register_on_punchnode(function() +local on_punch_called, on_place_called +core.register_on_placenode(function() + on_place_called = true +end) +core.register_on_punchnode(function() on_punch_called = true end) -unittests.register("test_punch_node", function(_, pos) - minetest.place_node(pos, {name="basenodes:dirt"}) +local function test_node_callbacks(_, pos) + on_place_called = false on_punch_called = false - minetest.punch_node(pos) - minetest.remove_node(pos) - -- currently failing: assert(on_punch_called) -end, {map=true}) + + core.place_node(pos, {name="basenodes:dirt"}) + assert(on_place_called, "on_place not called") + 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 input = "hello\000world"