1
0

itemdescription.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. local full_description = "Description Test Item\nFor testing item decription"
  2. core.register_tool("unittests:description_test", {
  3. description = full_description,
  4. inventory_image = "unittests_description_test.png",
  5. groups = { dummy = 1 },
  6. })
  7. core.register_chatcommand("item_description", {
  8. param = "",
  9. description = "Show the short and full description of the wielded item.",
  10. func = function(name)
  11. local player = core.get_player_by_name(name)
  12. local item = player:get_wielded_item()
  13. return true, string.format("short_description: %s\ndescription: %s",
  14. item:get_short_description(), item:get_description())
  15. end
  16. })
  17. local function test_short_desc()
  18. local function get_short_description(item)
  19. return ItemStack(item):get_short_description()
  20. end
  21. local stack = ItemStack("unittests:description_test")
  22. assert(stack:get_short_description() == "Description Test Item")
  23. assert(get_short_description("unittests:description_test") == "Description Test Item")
  24. assert(core.registered_items["unittests:description_test"].short_description == nil)
  25. assert(stack:get_description() == full_description)
  26. assert(stack:get_description() == core.registered_items["unittests:description_test"].description)
  27. stack:get_meta():set_string("description", "Hello World")
  28. assert(stack:get_short_description() == "Hello World")
  29. assert(stack:get_description() == "Hello World")
  30. assert(get_short_description(stack) == "Hello World")
  31. assert(get_short_description("unittests:description_test") == "Description Test Item")
  32. stack:get_meta():set_string("short_description", "Foo Bar")
  33. assert(stack:get_short_description() == "Foo Bar")
  34. assert(stack:get_description() == "Hello World")
  35. return true
  36. end
  37. unittests.register("test_short_desc", test_short_desc)