content_ids.lua 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. core.register_alias("unittests:test_content_ids_alias1", "air")
  2. core.register_alias("unittests:test_content_ids_alias2", "~")
  3. local function test_content_ids()
  4. assert(core.get_content_id("air") == core.CONTENT_AIR)
  5. assert(core.get_content_id("unittests:test_content_ids_alias1") == core.CONTENT_AIR)
  6. assert(core.get_content_id("unknown") == core.CONTENT_UNKNOWN)
  7. assert(core.get_content_id("ignore") == core.CONTENT_IGNORE)
  8. assert(core.get_name_from_content_id(core.CONTENT_AIR) == "air")
  9. assert(core.get_name_from_content_id(core.CONTENT_UNKNOWN) == "unknown")
  10. assert(core.get_name_from_content_id(core.CONTENT_IGNORE) == "ignore")
  11. assert(pcall(core.get_content_id, "~") == false)
  12. assert(pcall(core.get_content_id, "unittests:test_content_ids_alias2") == false)
  13. assert(pcall(core.get_content_id) == false)
  14. assert(core.get_name_from_content_id(0xFFFF) == "unknown")
  15. assert(pcall(core.get_name_from_content_id) == false)
  16. end
  17. -- Run while mod is loading.
  18. test_content_ids()
  19. -- Run after mods have loaded.
  20. unittests.register("test_content_ids", test_content_ids)
  21. -- Run in async environment.
  22. local function test_content_ids_async(cb)
  23. local function func(test_func)
  24. local ok, err = pcall(test_func)
  25. if not ok then
  26. return err
  27. end
  28. end
  29. core.handle_async(func, cb, test_content_ids)
  30. end
  31. unittests.register("test_content_ids_async", test_content_ids_async, {async=true})