/bravo/tests/inventory/test_slots.py

https://github.com/mmcgill/bravo · Python · 271 lines · 199 code · 29 blank · 43 comment · 3 complexity · 26bfe0cf5ba789a83b5f528c50b27ff6 MD5 · raw file

  1. from twisted.trial import unittest
  2. import bravo.blocks
  3. from bravo.beta.structures import Slot
  4. from bravo.ibravo import IRecipe
  5. from bravo.inventory.slots import comblist, Crafting, Workbench, ChestStorage
  6. from bravo.plugin import retrieve_plugins
  7. class TestComblist(unittest.TestCase):
  8. def setUp(self):
  9. self.a = [1, 2]
  10. self.b = [3, 4]
  11. self.i = comblist(self.a, self.b)
  12. def test_length(self):
  13. self.assertEqual(len(self.i), 4)
  14. def test_getitem(self):
  15. self.assertEqual(self.i[0], 1)
  16. self.assertEqual(self.i[1], 2)
  17. self.assertEqual(self.i[2], 3)
  18. self.assertEqual(self.i[3], 4)
  19. self.assertRaises(IndexError, self.i.__getitem__, 5 )
  20. def test_setitem(self):
  21. self.i[1] = 5
  22. self.i[2] = 6
  23. self.assertRaises(IndexError, self.i.__setitem__, 5, 0 )
  24. class TestCraftingInternals(unittest.TestCase):
  25. def setUp(self):
  26. self.i = Crafting()
  27. def test_internals(self):
  28. self.assertEqual(self.i.crafted, [None])
  29. self.assertEqual(self.i.crafting, [None] * 4)
  30. class TestCraftingWood(unittest.TestCase):
  31. """
  32. Test basic crafting functionality.
  33. These tests require a "wood" recipe, which turns logs into wood. This
  34. recipe was chosen because it is the simplest and most essential recipe
  35. from which all crafting is derived.
  36. """
  37. def setUp(self):
  38. recipes = retrieve_plugins(IRecipe)
  39. if "wood" not in recipes:
  40. raise unittest.SkipTest("Plugin not present")
  41. self.i = Crafting()
  42. def test_check_crafting(self):
  43. self.i.crafting[0] = Slot(bravo.blocks.blocks["log"].slot, 0, 1)
  44. # Force crafting table to be rechecked.
  45. self.i.update_crafted()
  46. self.assertTrue(self.i.recipe)
  47. self.assertEqual(self.i.crafted[0],
  48. (bravo.blocks.blocks["wood"].slot, 0, 4))
  49. def test_check_crafting_multiple(self):
  50. self.i.crafting[0] = Slot(bravo.blocks.blocks["log"].slot, 0, 2)
  51. # Force crafting table to be rechecked.
  52. self.i.update_crafted()
  53. # Only checking count of crafted table; the previous test assured that
  54. # the recipe was selected.
  55. self.assertEqual(self.i.crafted[0],
  56. (bravo.blocks.blocks["wood"].slot, 0, 4))
  57. def test_check_crafting_offset(self):
  58. self.i.crafting[1] = Slot(bravo.blocks.blocks["log"].slot, 0, 1)
  59. # Force crafting table to be rechecked.
  60. self.i.update_crafted()
  61. self.assertTrue(self.i.recipe)
  62. class TestCraftingSticks(unittest.TestCase):
  63. """
  64. Test basic crafting functionality.
  65. Assumes that the basic wood->stick recipe is present and enabled. This
  66. recipe was chosen because it is the simplest recipe with more than one
  67. ingredient.
  68. """
  69. def setUp(self):
  70. self.i = Crafting()
  71. def test_check_crafting(self):
  72. self.i.crafting[0] = Slot(bravo.blocks.blocks["wood"].slot, 0, 1)
  73. self.i.crafting[2] = Slot(bravo.blocks.blocks["wood"].slot, 0, 1)
  74. # Force crafting table to be rechecked.
  75. self.i.update_crafted()
  76. self.assertTrue(self.i.recipe)
  77. self.assertEqual(self.i.crafted[0],
  78. (bravo.blocks.items["stick"].slot, 0, 4))
  79. def test_check_crafting_multiple(self):
  80. self.i.crafting[0] = Slot(bravo.blocks.blocks["wood"].slot, 0, 2)
  81. self.i.crafting[2] = Slot(bravo.blocks.blocks["wood"].slot, 0, 2)
  82. # Force crafting table to be rechecked.
  83. self.i.update_crafted()
  84. # Only checking count of crafted table; the previous test assured that
  85. # the recipe was selected.
  86. self.assertEqual(self.i.crafted[0],
  87. (bravo.blocks.items["stick"].slot, 0, 4))
  88. def test_check_crafting_offset(self):
  89. self.i.crafting[1] = Slot(bravo.blocks.blocks["wood"].slot, 0, 1)
  90. self.i.crafting[3] = Slot(bravo.blocks.blocks["wood"].slot, 0, 1)
  91. # Force crafting table to be rechecked.
  92. self.i.update_crafted()
  93. self.assertTrue(self.i.recipe)
  94. class TestCraftingTorches(unittest.TestCase):
  95. """
  96. Test basic crafting functionality.
  97. Assumes that the basic torch recipe is present and enabled. This recipe
  98. was chosen because somebody was having problems crafting torches.
  99. """
  100. def setUp(self):
  101. recipes = retrieve_plugins(IRecipe)
  102. if "torches" not in recipes:
  103. raise unittest.SkipTest("Plugin not present")
  104. self.i = Crafting()
  105. def test_check_crafting(self):
  106. self.i.crafting[0] = Slot(bravo.blocks.items["coal"].slot, 0, 1)
  107. self.i.crafting[2] = Slot(bravo.blocks.items["stick"].slot, 0, 1)
  108. # Force crafting table to be rechecked.
  109. self.i.update_crafted()
  110. self.assertTrue(self.i.recipe)
  111. self.assertEqual(self.i.crafted[0],
  112. (bravo.blocks.blocks["torch"].slot, 0, 4))
  113. def test_check_crafting_multiple(self):
  114. self.i.crafting[0] = Slot(bravo.blocks.items["coal"].slot, 0, 2)
  115. self.i.crafting[2] = Slot(bravo.blocks.items["stick"].slot, 0, 2)
  116. # Force crafting table to be rechecked.
  117. self.i.update_crafted()
  118. # Only checking count of crafted table; the previous test assured that
  119. # the recipe was selected.
  120. self.assertEqual(self.i.crafted[0],
  121. (bravo.blocks.blocks["torch"].slot, 0, 4))
  122. def test_check_crafting_offset(self):
  123. self.i.crafting[1] = Slot(bravo.blocks.items["coal"].slot, 0, 1)
  124. self.i.crafting[3] = Slot(bravo.blocks.items["stick"].slot, 0, 1)
  125. # Force crafting table to be rechecked.
  126. self.i.update_crafted()
  127. self.assertTrue(self.i.recipe)
  128. class TestWorkbenchInternals(unittest.TestCase):
  129. def setUp(self):
  130. self.i = Workbench()
  131. def test_internals(self):
  132. self.assertEqual(self.i.crafted, [None])
  133. self.assertEqual(self.i.crafting, [None] * 9)
  134. class TestCraftingShovel(unittest.TestCase):
  135. """
  136. Test basic crafting functionality.
  137. Assumes that the basic shovel recipe is present and enabled. This recipe
  138. was chosen because shovels broke at one point and we couldn't figure out
  139. why.
  140. """
  141. def setUp(self):
  142. recipes = retrieve_plugins(IRecipe)
  143. if "stone-shovel" not in recipes:
  144. raise unittest.SkipTest("Plugin not present")
  145. self.i = Workbench()
  146. def test_check_crafting(self):
  147. self.i.crafting[0] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 1)
  148. self.i.crafting[3] = Slot(bravo.blocks.items["stick"].slot, 0, 1)
  149. self.i.crafting[6] = Slot(bravo.blocks.items["stick"].slot, 0, 1)
  150. # Force crafting table to be rechecked.
  151. self.i.update_crafted()
  152. self.assertTrue(self.i.recipe)
  153. self.assertEqual(self.i.crafted[0],
  154. (bravo.blocks.items["stone-shovel"].slot, 0, 1))
  155. def test_check_crafting_multiple(self):
  156. self.i.crafting[0] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 2)
  157. self.i.crafting[3] = Slot(bravo.blocks.items["stick"].slot, 0, 2)
  158. self.i.crafting[6] = Slot(bravo.blocks.items["stick"].slot, 0, 2)
  159. # Force crafting table to be rechecked.
  160. self.i.update_crafted()
  161. # Only checking count of crafted table; the previous test assured that
  162. # the recipe was selected.
  163. self.assertEqual(self.i.crafted[0],
  164. (bravo.blocks.items["stone-shovel"].slot, 0, 1))
  165. def test_check_crafting_offset(self):
  166. self.i.crafting[1] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 2)
  167. self.i.crafting[4] = Slot(bravo.blocks.items["stick"].slot, 0, 2)
  168. self.i.crafting[7] = Slot(bravo.blocks.items["stick"].slot, 0, 2)
  169. # Force crafting table to be rechecked.
  170. self.i.update_crafted()
  171. self.assertTrue(self.i.recipe)
  172. class TestCraftingFurnace(unittest.TestCase):
  173. """
  174. Test basic crafting functionality.
  175. Assumes that the basic cobblestone->furnace recipe is present and enabled.
  176. This recipe was chosen because it is the simplest recipe that requires a
  177. 3x3 crafting table.
  178. """
  179. def setUp(self):
  180. recipes = retrieve_plugins(IRecipe)
  181. if "furnace" not in recipes:
  182. raise unittest.SkipTest("Plugin not present")
  183. self.i = Workbench()
  184. def test_check_crafting(self):
  185. self.i.crafting[0] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 1)
  186. self.i.crafting[1] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 1)
  187. self.i.crafting[2] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 1)
  188. self.i.crafting[3] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 1)
  189. self.i.crafting[5] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 1)
  190. self.i.crafting[6] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 1)
  191. self.i.crafting[7] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 1)
  192. self.i.crafting[8] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 1)
  193. # Force crafting table to be rechecked.
  194. self.i.update_crafted()
  195. self.assertTrue(self.i.recipe)
  196. self.assertEqual(self.i.crafted[0],
  197. (bravo.blocks.blocks["furnace"].slot, 0, 1))
  198. def test_check_crafting_multiple(self):
  199. self.i.crafting[0] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 2)
  200. self.i.crafting[1] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 2)
  201. self.i.crafting[2] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 2)
  202. self.i.crafting[3] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 2)
  203. self.i.crafting[5] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 2)
  204. self.i.crafting[6] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 2)
  205. self.i.crafting[7] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 2)
  206. self.i.crafting[8] = Slot(bravo.blocks.blocks["cobblestone"].slot, 0, 2)
  207. # Force crafting table to be rechecked.
  208. self.i.update_crafted()
  209. self.assertEqual(self.i.crafted[0],
  210. (bravo.blocks.blocks["furnace"].slot, 0, 1))
  211. class TestChestSerialization(unittest.TestCase):
  212. def setUp(self):
  213. self.i = ChestStorage()
  214. self.l = [None] * len(self.i)
  215. self.l[0] = 1, 0, 1
  216. self.l[9] = 2, 0, 1
  217. def test_load_from_list(self):
  218. self.i.load_from_list(self.l)
  219. self.assertEqual(self.i.storage[0], (1, 0, 1))
  220. self.assertEqual(self.i.storage[9], (2, 0, 1))
  221. def test_save_to_list(self):
  222. self.i.storage[0] = 1, 0, 1
  223. self.i.storage[9] = 2, 0, 1
  224. m = self.i.save_to_list()
  225. self.assertEqual(m, self.l)