init_spec.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package.path = "../?.lua;" .. package.path
  2. require("init")
  3. math.randomseed(123)
  4. --mocking
  5. local rand1 = {}
  6. function rand1:next(min, max) return math.random(min, max) end
  7. _G.ItemStack = function(str)
  8. return {
  9. string = str,
  10. count = 1,
  11. set_count = function(stack, count) stack.count = count end,
  12. get_count = function(stack) return stack.count end,
  13. }
  14. end
  15. describe("get_items1", function()
  16. local loot_table =
  17. {
  18. rolls = 1,
  19. rolls_max = 2,
  20. loots =
  21. {
  22. {
  23. weight = 10,
  24. loot = "teststring",
  25. repetitions = 1,
  26. },
  27. {
  28. weight = 1,
  29. loot = "teststring2",
  30. repetitions = 1,
  31. repetitions_max = 10,
  32. },
  33. accumulated_weight = 11,
  34. },
  35. }
  36. it("picks inside_counts", function()
  37. local min_count = math.huge
  38. local max_count = -math.huge
  39. local min_length = math.huge
  40. local max_length = -math.huge
  41. for i = 1, 1000
  42. do
  43. local items = inventory_populator.get_items(loot_table, rand1)
  44. for i, v in ipairs(items)
  45. do
  46. min_count = math.min(min_count, v.count)
  47. max_count = math.max(max_count, v.count)
  48. end
  49. min_length = math.min(min_length, #items)
  50. max_length = math.max(max_length, #items)
  51. end
  52. assert.equals(1, min_length)
  53. assert.equals(1, min_count)
  54. assert.equals(2, max_length)
  55. assert.equals(10, max_count)
  56. end)
  57. local loot_table2 =
  58. {
  59. rolls = 5,
  60. rolls_max = 5,
  61. loots =
  62. {
  63. {
  64. weight = 2,
  65. loot = "teststring",
  66. repetitions = 1,
  67. },
  68. accumulated_weight = 2,
  69. },
  70. }
  71. local loot_table3 =
  72. {
  73. rolls = 1,
  74. rolls_max = 1,
  75. loots =
  76. {
  77. {
  78. weight = 1,
  79. loot = loot_table2,
  80. repetitions = 1,
  81. },
  82. accumulated_weight = 1,
  83. },
  84. }
  85. it("recurs", function()
  86. local items = inventory_populator.get_items(loot_table3, rand1)
  87. assert.equals(5, #items)
  88. end)
  89. end)