aboutsummaryrefslogtreecommitdiff
path: root/games/devtest/mods/unittests
diff options
context:
space:
mode:
authorJude Melton-Houghton <jwmhjwmh@gmail.com>2022-09-18 11:46:48 -0400
committerGitHub <noreply@github.com>2022-09-18 17:46:48 +0200
commit310b12b5edaaa415e664a7766b9226fc95ce89e4 (patch)
treefcea11433f6407d692cdcef7e2f934335b0d2d62 /games/devtest/mods/unittests
parent006d974c584933dc4482c4a1b05f6bc0c249c8e0 (diff)
downloadminetest-310b12b5edaaa415e664a7766b9226fc95ce89e4.tar.xz
Content ID caching in Lua (#12444)
* Cache content IDs in Lua Co-authored-by: sfan5 <sfan5@live.de>
Diffstat (limited to 'games/devtest/mods/unittests')
-rw-r--r--games/devtest/mods/unittests/content_ids.lua37
-rw-r--r--games/devtest/mods/unittests/init.lua1
2 files changed, 38 insertions, 0 deletions
diff --git a/games/devtest/mods/unittests/content_ids.lua b/games/devtest/mods/unittests/content_ids.lua
new file mode 100644
index 000000000..d2f1e0c35
--- /dev/null
+++ b/games/devtest/mods/unittests/content_ids.lua
@@ -0,0 +1,37 @@
+core.register_alias("unittests:test_content_ids_alias1", "air")
+core.register_alias("unittests:test_content_ids_alias2", "~")
+
+local function test_content_ids()
+ assert(core.get_content_id("air") == core.CONTENT_AIR)
+ assert(core.get_content_id("unittests:test_content_ids_alias1") == core.CONTENT_AIR)
+ assert(core.get_content_id("unknown") == core.CONTENT_UNKNOWN)
+ assert(core.get_content_id("ignore") == core.CONTENT_IGNORE)
+
+ assert(core.get_name_from_content_id(core.CONTENT_AIR) == "air")
+ assert(core.get_name_from_content_id(core.CONTENT_UNKNOWN) == "unknown")
+ assert(core.get_name_from_content_id(core.CONTENT_IGNORE) == "ignore")
+
+ assert(pcall(core.get_content_id, "~") == false)
+ assert(pcall(core.get_content_id, "unittests:test_content_ids_alias2") == false)
+ assert(pcall(core.get_content_id) == false)
+ assert(core.get_name_from_content_id(0xFFFF) == "unknown")
+ assert(pcall(core.get_name_from_content_id) == false)
+end
+
+-- Run while mod is loading.
+test_content_ids()
+
+-- Run after mods have loaded.
+unittests.register("test_content_ids", test_content_ids)
+
+-- Run in async environment.
+local function test_content_ids_async(cb)
+ local function func(test_func)
+ local ok, err = pcall(test_func)
+ if not ok then
+ return err
+ end
+ end
+ core.handle_async(func, cb, test_content_ids)
+end
+unittests.register("test_content_ids_async", test_content_ids_async, {async=true})
diff --git a/games/devtest/mods/unittests/init.lua b/games/devtest/mods/unittests/init.lua
index 0e041be76..f93d516d7 100644
--- a/games/devtest/mods/unittests/init.lua
+++ b/games/devtest/mods/unittests/init.lua
@@ -178,6 +178,7 @@ dofile(modpath .. "/crafting.lua")
dofile(modpath .. "/itemdescription.lua")
dofile(modpath .. "/async_env.lua")
dofile(modpath .. "/entity.lua")
+dofile(modpath .. "/content_ids.lua")
--------------