api_spec.lua 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package.path = '../../?.lua;' .. -- tests root
  2. '../?.lua;' .. -- mod root
  3. package.path
  4. require("crafting/tests/dummy")
  5. require("crafting/api")
  6. local recipe1 = {
  7. type = "test",
  8. output = "default:torch",
  9. items = { "default:stick", "default:coal" },
  10. always_known = true,
  11. }
  12. local recipe2 = {
  13. type = "test",
  14. output = "default:stone",
  15. items = { "default:cobble" },
  16. always_known = true,
  17. }
  18. describe("Recipes and types", function()
  19. crafting.recipes = {}
  20. it("can register type", function()
  21. assert.is_nil(crafting.recipes["test"])
  22. crafting.register_type("test")
  23. assert.is_not_nil(crafting.recipes["test"])
  24. end)
  25. it("can register type", function()
  26. assert(not crafting.recipes["test"][1])
  27. crafting.register_recipe(recipe1)
  28. assert.is_not_nil(crafting.recipes["test"][1])
  29. assert.equals(crafting.recipes["test"][1].output, recipe1.output)
  30. end)
  31. end)
  32. describe("Getting all outputs", function()
  33. crafting.recipes = {}
  34. crafting.register_type("test")
  35. -- Recipe 1
  36. crafting.register_recipe(recipe1)
  37. assert.equals(#crafting.recipes["test"], 1)
  38. assert.equals(crafting.recipes["test"][1].output, recipe1.output)
  39. -- Recipe 2
  40. crafting.register_recipe(recipe2)
  41. assert.equals(#crafting.recipes["test"], 2)
  42. assert.equals(crafting.recipes["test"][1].output, recipe1.output)
  43. assert.equals(crafting.recipes["test"][2].output, recipe2.output)
  44. it("get with no items", function()
  45. local recipes = crafting.get_all("test", 1, {}, {})
  46. assert.equals(2, #recipes)
  47. assert.equals(recipe1, recipes[1].recipe)
  48. assert.equals(recipe2, recipes[2].recipe)
  49. assert.is_false(recipes[1].craftable)
  50. assert.is_false(recipes[2].craftable)
  51. end)
  52. it("get with right for one", function()
  53. local items_hash = {}
  54. items_hash["default:cobble"] = 1
  55. local recipes = crafting.get_all("test", 1, items_hash, {})
  56. assert.equals(2, #recipes)
  57. assert.equals(recipe1, recipes[1].recipe)
  58. assert.equals(recipe2, recipes[2].recipe)
  59. assert.is_false(recipes[1].craftable)
  60. assert.is_true(recipes[2].craftable)
  61. end)
  62. end)