forked from minetest/minetest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unittest: Add inventory callback tests
- Loading branch information
1 parent
bb0928d
commit 918904b
Showing
6 changed files
with
178 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,92 @@ | ||
minetest.register_allow_player_inventory_action(function(player, action, inventory, info) | ||
-- Table to keep track of callback executions | ||
-- [i + 0] = count of expected patterns of index (i + 1) | ||
-- [i + 1] = pattern to check | ||
local PATTERN_NORMAL = { 4, "allow_%w", 2, "on_take", 1, "on_put", 1 } | ||
local PATTERN_SWAP = { 8, "allow_%w", 4, "on_take", 2, "on_put", 2 } | ||
local exec_listing = {} -- List of logged callbacks (e.g. "on_take", "allow_put") | ||
local exec_pattern = nil -- The expected pattern table (PATTERN_NORMAL or PATTERN_SWAP) | ||
|
||
-- Checks whether the logged callbacks equal the expected pattern | ||
minetest.__helper_check_callbacks = function() | ||
local ok = #exec_listing == exec_pattern[1] | ||
if ok then | ||
local list_index = 1 | ||
for i = 2, #exec_pattern, 2 do | ||
for n = 1, exec_pattern[i + 1] do | ||
-- Match the list for "n" occurrences of the wanted callback name pattern | ||
ok = exec_listing[list_index]:find(exec_pattern[i]) | ||
list_index = list_index + 1 | ||
if not ok then break end | ||
end | ||
if not ok then break end | ||
end | ||
end | ||
|
||
if not ok then | ||
print("Execution order mismatch!") | ||
print("Expected patterns: ", dump(exec_pattern)) | ||
print("Got list: ", dump(exec_listing)) | ||
end | ||
exec_listing = {} | ||
exec_pattern = nil | ||
return ok | ||
end | ||
|
||
-- Uncomment the other line for easier callback debugging | ||
local log = function(...) end | ||
--local log = print | ||
|
||
minetest.register_allow_player_inventory_action(function(_, action, inv, info) | ||
log("\tallow " .. action, info.count or info.stack:to_string()) | ||
|
||
if action == "move" then | ||
-- testMoveFillStack | ||
return info.count | ||
end | ||
|
||
if info.stack:get_name() == "default:water" then | ||
return 0 | ||
end | ||
if action == "take" or action == "put" then | ||
assert(not info.stack:is_empty(), "Stack empty in: " .. action) | ||
|
||
if info.stack:get_name() == "default:lava" then | ||
return 5 | ||
-- testMoveUnallowed | ||
-- testSwapFromUnallowed | ||
-- testSwapToUnallowed | ||
if info.stack:get_name() == "default:takeput_deny" then | ||
return 0 | ||
end | ||
|
||
-- testMovePartial | ||
if info.stack:get_name() == "default:takeput_max_5" then | ||
return 5 | ||
end | ||
|
||
-- testCallbacks | ||
if info.stack:get_name():find("default:takeput_cb_%d") then | ||
if #exec_listing == 0 then | ||
if inv:get_stack(info.listname, info.index):is_empty() then | ||
exec_pattern = PATTERN_NORMAL | ||
else | ||
exec_pattern = PATTERN_SWAP | ||
end | ||
end | ||
-- Log callback as executed | ||
table.insert(exec_listing, "allow_" .. action) | ||
return -- Unlimited | ||
end | ||
end | ||
|
||
return info.stack:get_count() | ||
return -- Unlimited | ||
end) | ||
|
||
minetest.register_on_player_inventory_action(function(_, action, inv, info) | ||
log("\ton " .. action, info.count or info.stack:to_string()) | ||
|
||
if action == "take" or action == "put" then | ||
assert(not info.stack:is_empty(), action) | ||
|
||
if info.stack:get_name():find("default:takeput_cb_%d") then | ||
-- Log callback as executed | ||
table.insert(exec_listing, "on_" .. action) | ||
return | ||
end | ||
end | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters