diff options
Diffstat (limited to 'games/minimal/mods/unittests')
-rw-r--r-- | games/minimal/mods/unittests/crafting.lua | 120 | ||||
-rw-r--r-- | games/minimal/mods/unittests/crafting_prepare.lua | 88 | ||||
-rw-r--r-- | games/minimal/mods/unittests/init.lua | 16 | ||||
-rw-r--r-- | games/minimal/mods/unittests/mod.conf | 2 | ||||
-rw-r--r-- | games/minimal/mods/unittests/player.lua | 73 | ||||
-rw-r--r-- | games/minimal/mods/unittests/random.lua | 10 | ||||
-rw-r--r-- | games/minimal/mods/unittests/textures/unittests_coal_lump.png | bin | 0 -> 160 bytes | |||
-rw-r--r-- | games/minimal/mods/unittests/textures/unittests_iron_lump.png | bin | 0 -> 154 bytes | |||
-rw-r--r-- | games/minimal/mods/unittests/textures/unittests_repairable_tool.png | bin | 0 -> 160 bytes | |||
-rw-r--r-- | games/minimal/mods/unittests/textures/unittests_steel_ingot.png | bin | 0 -> 159 bytes | |||
-rw-r--r-- | games/minimal/mods/unittests/textures/unittests_stick.png | bin | 0 -> 147 bytes | |||
-rw-r--r-- | games/minimal/mods/unittests/textures/unittests_torch.png | bin | 0 -> 155 bytes | |||
-rw-r--r-- | games/minimal/mods/unittests/textures/unittests_unrepairable_tool.png | bin | 0 -> 157 bytes |
13 files changed, 309 insertions, 0 deletions
diff --git a/games/minimal/mods/unittests/crafting.lua b/games/minimal/mods/unittests/crafting.lua new file mode 100644 index 000000000..eff13ce09 --- /dev/null +++ b/games/minimal/mods/unittests/crafting.lua @@ -0,0 +1,120 @@ +-- Test minetest.clear_craft function +local function test_clear_craft() + minetest.log("info", "[unittests] Testing minetest.clear_craft") + -- Clearing by output + minetest.register_craft({ + output = "foo", + recipe = {{"bar"}} + }) + minetest.register_craft({ + output = "foo 4", + recipe = {{"foo", "bar"}} + }) + assert(#minetest.get_all_craft_recipes("foo") == 2) + minetest.clear_craft({output="foo"}) + assert(minetest.get_all_craft_recipes("foo") == nil) + -- Clearing by input + minetest.register_craft({ + output = "foo 4", + recipe = {{"foo", "bar"}} + }) + assert(#minetest.get_all_craft_recipes("foo") == 1) + minetest.clear_craft({recipe={{"foo", "bar"}}}) + assert(minetest.get_all_craft_recipes("foo") == nil) +end + +-- Test minetest.get_craft_result function +local function test_get_craft_result() + minetest.log("info", "[unittests] Testing minetest.get_craft_result") + + -- normal + local input = { + method = "normal", + width = 2, + items = {"", "unittests:coal_lump", "", "unittests:stick"} + } + minetest.log("info", "[unittests] torch crafting input: "..dump(input)) + local output, decremented_input = minetest.get_craft_result(input) + minetest.log("info", "[unittests] torch crafting output: "..dump(output)) + minetest.log("info", "[unittests] torch crafting decremented input: "..dump(decremented_input)) + assert(output.item) + minetest.log("info", "[unittests] torch crafting output.item:to_table(): "..dump(output.item:to_table())) + assert(output.item:get_name() == "unittests:torch") + assert(output.item:get_count() == 4) + + -- fuel + input = { + method = "fuel", + width = 1, + items = {"unittests:coal_lump"} + } + minetest.log("info", "[unittests] coal fuel input: "..dump(input)) + output, decremented_input = minetest.get_craft_result(input) + minetest.log("info", "[unittests] coal fuel output: "..dump(output)) + minetest.log("info", "[unittests] coal fuel decremented input: "..dump(decremented_input)) + assert(output.time) + assert(output.time > 0) + + -- cooking + input = { + method = "cooking", + width = 1, + items = {"unittests:iron_lump"} + } + minetest.log("info", "[unittests] iron lump cooking input: "..dump(output)) + output, decremented_input = minetest.get_craft_result(input) + minetest.log("info", "[unittests] iron lump cooking output: "..dump(output)) + minetest.log("info", "[unittests] iron lump cooking decremented input: "..dump(decremented_input)) + assert(output.time) + assert(output.time > 0) + assert(output.item) + minetest.log("info", "[unittests] iron lump cooking output.item:to_table(): "..dump(output.item:to_table())) + assert(output.item:get_name() == "unittests:steel_ingot") + assert(output.item:get_count() == 1) + + -- tool repair (repairable) + input = { + method = "normal", + width = 2, + -- Using a wear of 60000 + items = {"unittests:repairable_tool 1 60000", "unittests:repairable_tool 1 60000"} + } + minetest.log("info", "[unittests] repairable tool crafting input: "..dump(input)) + output, decremented_input = minetest.get_craft_result(input) + minetest.log("info", "[unittests] repairable tool crafting output: "..dump(output)) + minetest.log("info", "[unittests] repairable tool crafting decremented input: "..dump(decremented_input)) + assert(output.item) + minetest.log("info", "[unittests] repairable tool crafting output.item:to_table(): "..dump(output.item:to_table())) + assert(output.item:get_name() == "unittests:repairable_tool") + -- Test the wear value. + -- See src/craftdef.cpp in Minetest source code for the formula. The formula to calculate + -- the value 51187 is: + -- 65536 - ((65536-60000)+(65536-60000)) + floor(additonal_wear * 65536 + 0.5) = 51187 + -- where additional_wear = 0.05 + assert(output.item:get_wear() == 51187) + assert(output.item:get_count() == 1) + + -- failing tool repair (unrepairable) + input = { + method = "normal", + width = 2, + items = {"unittests:unrepairable_tool 1 60000", "unittests:unrepairable_tool 1 60000"} + } + minetest.log("info", "[unittests] unrepairable tool crafting input: "..dump(input)) + output, decremented_input = minetest.get_craft_result(input) + minetest.log("info", "[unittests] unrepairable tool crafting output: "..dump(output)) + minetest.log("info", "[unittests] unrepairable tool crafting decremented input: "..dump(decremented_input)) + assert(output.item) + minetest.log("info", "[unittests] unrepairable tool crafting output.item:to_table(): "..dump(output.item:to_table())) + -- unrepairable tool must not yield any output + assert(output.item:get_name() == "") + +end + +function unittests.test_crafting() + test_clear_craft() + test_get_craft_result() + minetest.log("action", "[unittests] Crafting tests passed!") + return true +end + diff --git a/games/minimal/mods/unittests/crafting_prepare.lua b/games/minimal/mods/unittests/crafting_prepare.lua new file mode 100644 index 000000000..a09734827 --- /dev/null +++ b/games/minimal/mods/unittests/crafting_prepare.lua @@ -0,0 +1,88 @@ +-- Registering some dummy items and recipes for the crafting tests + +minetest.register_craftitem("unittests:torch", { + description = "Crafting Test Item: Torch", + inventory_image = "unittests_torch.png", + + groups = { dummy = 1 }, +}) +minetest.register_craftitem("unittests:coal_lump", { + description = "Crafting Test Item: Coal Lump", + inventory_image = "unittests_coal_lump.png", + + groups = { dummy = 1 }, +}) +minetest.register_craftitem("unittests:stick", { + description = "Crafting Test Item: Stick", + inventory_image = "unittests_stick.png", + + groups = { dummy = 1 }, +}) +minetest.register_craftitem("unittests:iron_lump", { + description = "Crafting Test Item: Iron Lump", + inventory_image = "unittests_iron_lump.png", + + groups = { dummy = 1 }, +}) +minetest.register_craftitem("unittests:steel_ingot", { + description = "Crafting Test Item: Steel Ingot", + inventory_image = "unittests_steel_ingot.png", + + groups = { dummy = 1 }, +}) + +-- Recipes for tests: Normal crafting, cooking and fuel + +minetest.register_craft({ + output = 'unittests:torch 4', + recipe = { + {'unittests:coal_lump'}, + {'unittests:stick'}, + } +}) + +minetest.register_craft({ + type = "cooking", + output = "unittests:steel_ingot", + recipe = "unittests:iron_lump", +}) + +minetest.register_craft({ + type = "fuel", + recipe = "unittests:coal_lump", + burntime = 40, +}) + +-- Test tool repair +minetest.register_craft({ + type = "toolrepair", + additional_wear = -0.05, +}) + +-- Test the disable_repair=1 group +minetest.register_tool("unittests:unrepairable_tool", { + description = "Crafting Test Item: Unrepairable Tool", + inventory_image = "unittests_unrepairable_tool.png", + tool_capabilities = { + groupcaps = { + cracky = { + times = {3, 2, 1}, + } + } + }, + groups = { disable_repair = 1, dummy = 1 } +}) + +minetest.register_tool("unittests:repairable_tool", { + description = "Crafting Test Item: Repairable Tool", + inventory_image = "unittests_repairable_tool.png", + tool_capabilities = { + groupcaps = { + cracky = { + times = {3, 2, 1}, + } + } + }, + + groups = { dummy = 1 }, +}) diff --git a/games/minimal/mods/unittests/init.lua b/games/minimal/mods/unittests/init.lua new file mode 100644 index 000000000..6c1728420 --- /dev/null +++ b/games/minimal/mods/unittests/init.lua @@ -0,0 +1,16 @@ +unittests = {} + +local modpath = minetest.get_modpath("unittests") +dofile(modpath .. "/random.lua") +dofile(modpath .. "/player.lua") +dofile(modpath .. "/crafting_prepare.lua") +dofile(modpath .. "/crafting.lua") + +if minetest.settings:get_bool("devtest_unittests_autostart", false) then + unittests.test_random() + unittests.test_crafting() + minetest.register_on_joinplayer(function(player) + unittests.test_player(player) + end) +end + diff --git a/games/minimal/mods/unittests/mod.conf b/games/minimal/mods/unittests/mod.conf new file mode 100644 index 000000000..0d5e3c959 --- /dev/null +++ b/games/minimal/mods/unittests/mod.conf @@ -0,0 +1,2 @@ +name = unittests +description = Adds automated unit tests for the engine diff --git a/games/minimal/mods/unittests/player.lua b/games/minimal/mods/unittests/player.lua new file mode 100644 index 000000000..10781a95f --- /dev/null +++ b/games/minimal/mods/unittests/player.lua @@ -0,0 +1,73 @@ +-- +-- HP Change Reasons +-- +local expect = nil +local function run_hpchangereason_tests(player) + expect = { type = "set_hp", from = "mod" } + player:set_hp(3) + assert(expect == nil) + + expect = { a = 234, type = "set_hp", from = "mod" } + player:set_hp(7, { a= 234 }) + assert(expect == nil) + + expect = { df = 3458973454, type = "fall", from = "mod" } + player:set_hp(10, { type = "fall", df = 3458973454 }) + assert(expect == nil) + + player:set_hp(20) +end + +local function run_player_meta_tests(player) + local meta = player:get_meta() + meta:set_string("foo", "bar") + assert(meta:contains("foo")) + assert(meta:get_string("foo") == "bar") + assert(meta:get("foo") == "bar") + + local meta2 = player:get_meta() + assert(meta2:get_string("foo") == "bar") + assert(meta2:get("foo") == "bar") + assert(meta:equals(meta2)) + + meta:set_string("bob", "dillan") + assert(meta:get_string("foo") == "bar") + assert(meta:get_string("bob") == "dillan") + assert(meta:get("bob") == "dillan") + assert(meta2:get_string("foo") == "bar") + assert(meta2:get_string("bob") == "dillan") + assert(meta2:get("bob") == "dillan") + assert(meta:equals(meta2)) + + meta:set_string("foo", "") + assert(not meta:contains("foo")) + assert(meta:get("foo") == nil) + assert(meta:get_string("foo") == "") + assert(meta:equals(meta2)) +end + +function unittests.test_player(player) + minetest.register_on_player_hpchange(function(player, hp, reason) + if not expect then + return + end + + for key, value in pairs(reason) do + assert(expect[key] == value) + end + + for key, value in pairs(expect) do + assert(reason[key] == value) + end + + expect = nil + end) + + run_hpchangereason_tests(player) + run_player_meta_tests(player) + local msg = "Player tests passed for player '"..player:get_player_name().."'!" + minetest.chat_send_all(msg) + minetest.log("action", "[unittests] "..msg) + return true +end + diff --git a/games/minimal/mods/unittests/random.lua b/games/minimal/mods/unittests/random.lua new file mode 100644 index 000000000..f94f0a88e --- /dev/null +++ b/games/minimal/mods/unittests/random.lua @@ -0,0 +1,10 @@ +function unittests.test_random() + -- Try out PseudoRandom + minetest.log("action", "[unittests] Testing PseudoRandom ...") + local pseudo = PseudoRandom(13) + assert(pseudo:next() == 22290) + assert(pseudo:next() == 13854) + minetest.log("action", "[unittests] PseudoRandom test passed!") + return true +end + diff --git a/games/minimal/mods/unittests/textures/unittests_coal_lump.png b/games/minimal/mods/unittests/textures/unittests_coal_lump.png Binary files differnew file mode 100644 index 000000000..f460d909e --- /dev/null +++ b/games/minimal/mods/unittests/textures/unittests_coal_lump.png diff --git a/games/minimal/mods/unittests/textures/unittests_iron_lump.png b/games/minimal/mods/unittests/textures/unittests_iron_lump.png Binary files differnew file mode 100644 index 000000000..22f43e9cc --- /dev/null +++ b/games/minimal/mods/unittests/textures/unittests_iron_lump.png diff --git a/games/minimal/mods/unittests/textures/unittests_repairable_tool.png b/games/minimal/mods/unittests/textures/unittests_repairable_tool.png Binary files differnew file mode 100644 index 000000000..46fbbaa74 --- /dev/null +++ b/games/minimal/mods/unittests/textures/unittests_repairable_tool.png diff --git a/games/minimal/mods/unittests/textures/unittests_steel_ingot.png b/games/minimal/mods/unittests/textures/unittests_steel_ingot.png Binary files differnew file mode 100644 index 000000000..6977696a2 --- /dev/null +++ b/games/minimal/mods/unittests/textures/unittests_steel_ingot.png diff --git a/games/minimal/mods/unittests/textures/unittests_stick.png b/games/minimal/mods/unittests/textures/unittests_stick.png Binary files differnew file mode 100644 index 000000000..ffdce70d4 --- /dev/null +++ b/games/minimal/mods/unittests/textures/unittests_stick.png diff --git a/games/minimal/mods/unittests/textures/unittests_torch.png b/games/minimal/mods/unittests/textures/unittests_torch.png Binary files differnew file mode 100644 index 000000000..ba5eebef0 --- /dev/null +++ b/games/minimal/mods/unittests/textures/unittests_torch.png diff --git a/games/minimal/mods/unittests/textures/unittests_unrepairable_tool.png b/games/minimal/mods/unittests/textures/unittests_unrepairable_tool.png Binary files differnew file mode 100644 index 000000000..c676213a5 --- /dev/null +++ b/games/minimal/mods/unittests/textures/unittests_unrepairable_tool.png |