forked from fogleman/Minecraft
-
Notifications
You must be signed in to change notification settings - Fork 33
/
tests.py
180 lines (159 loc) · 6.67 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Imports, sorted alphabetically.
# Python packages
import random
import unittest
# Third-party packages
# Nothing for now...
# Modules from this project
from crafting import Recipes
import globals as G
from inventory import Inventory
from items import ItemStack
__all__ = (
'InventoryTests', 'CraftingTests',
)
class InventoryTests(unittest.TestCase):
def test_init(self):
for size in [0, 9, random.randint(3, 100)]:
inv = Inventory(slot_count=size)
self.assertEqual(inv.slot_count, size)
self.assertEqual(inv.slots, [None] * size)
def test_find_empty_slot(self):
for size in [0, 9, random.randint(3, 100)]:
inv = Inventory(slot_count=size).find_empty_slot()
self.assertEqual(inv, 0 if size > 0 else -1)
def test_add_1(self):
for size in [0, 9, random.randint(3, 100)]:
inv = Inventory(slot_count=size)
item = random.choice(list(G.ITEMS_DIR.keys()))
block = random.choice(list(G.BLOCKS_DIR.keys()))
result = inv.add_item(item)
result2 = inv.add_item(block)
if size == 0:
self.assertFalse(result)
self.assertFalse(result2)
else:
self.assertTrue(result)
self.assertTrue(result2)
foundItem = False
foundBlock = False
for slot in inv.slots:
if slot:
if slot.type == item and slot.amount == 1:
foundItem = True
elif slot.type == block and slot.amount == 1:
foundBlock = True
self.assertTrue(foundItem)
self.assertTrue(foundBlock)
self.assertEqual(result, result2)
def test_add_2(self):
inv = Inventory(slot_count=20)
block = random.choice(list(G.BLOCKS_DIR.keys()))
max_items = G.BLOCKS_DIR[block].max_stack_size * 20
for i in range(0, max_items):
self.assertTrue(inv.add_item(block))
item = random.choice(list(G.ITEMS_DIR.keys()))
inv2 = Inventory(slot_count=20)
max_items2 = G.ITEMS_DIR[item].max_stack_size * 20
for i in range(0, max_items2):
self.assertTrue(inv2.add_item(item))
self.assertNotIn(None, inv.slots)
self.assertNotIn(None, inv2.slots)
for slot in inv.slots:
self.assertEqual(slot.type, block)
self.assertEqual(slot.amount, G.BLOCKS_DIR[block].max_stack_size)
for slot in inv2.slots:
self.assertEqual(slot.type, item)
self.assertEqual(slot.amount, G.ITEMS_DIR[item].max_stack_size)
def test_remove(self):
inv = Inventory(slot_count=20)
block = random.choice(list(G.BLOCKS_DIR.keys()))
max_items = G.BLOCKS_DIR[block].max_stack_size * 20
for i in range(0, max_items):
self.assertTrue(inv.add_item(block))
self.assertFalse(inv.remove_item(block, quantity=0))
for i in range(0, 20):
self.assertTrue(inv.remove_item(block, quantity=G.BLOCKS_DIR[block].max_stack_size))
self.assertEqual(inv.slots, [None] * 20)
for i in range(0, max_items):
self.assertTrue(inv.add_item(block))
for i in range(0, 20):
self.assertTrue(inv.remove_by_index(i, quantity=G.BLOCKS_DIR[block].max_stack_size))
self.assertEqual(inv.slots, [None] * 20)
for i in range(0, 20):
inv.slots[i] = ItemStack(block, amount=1)
inv.slots[i].change_amount(-1)
inv.remove_unnecessary_stacks()
self.assertEqual(inv.slots, [None] * 20)
class CraftingTests(unittest.TestCase):
current_block_id = 0
def generate_random_recipe(self, characters='#@'):
recipe = []
recipe2 = []
recipe3 = []
ingre = {}
for character in characters:
ingre[character] = list(G.BLOCKS_DIR.values())[self.current_block_id]
self.current_block_id += 1
if self.current_block_id >= len(list(G.BLOCKS_DIR.values())):
self.current_block_id = 0
for i in range(0, 3):
recipe.append(''.join(random.choice(characters) for x in range(3)))
recipe2.append([])
for character in recipe[i]:
recipe2[i].append(ingre[character])
recipe3.append(ingre[character])
return recipe, ingre, ItemStack(random.choice(list(G.BLOCKS_DIR.values())).id, amount=random.randint(1, 20)), recipe2, recipe3
def test_add_1(self, characters='#@'):
self.recipes = Recipes()
recipes = []
ingres = []
outputs = []
for i in range(0, 50):
recipe, ingre, output, recipe2, recipe3 = self.generate_random_recipe(characters=characters)
recipes.append(recipe2)
ingres.append(ingre)
outputs.append(output)
self.recipes.add_recipe(recipe, ingre, output)
self.assertEqual(self.recipes.nr_recipes, 50)
for i, recipe in enumerate(recipes):
self.assertEqual(self.recipes.craft(recipe), outputs[i])
def test_add_2(self, characters='#@'):
self.recipes = Recipes()
recipes = []
ingres = []
outputs = []
for i in range(0, 25):
recipe, ingre, output, recipe2, recipe3 = self.generate_random_recipe(characters=characters)
recipes.append(recipe2)
ingres.append(ingre)
outputs.append(output)
self.recipes.add_shapeless_recipe(recipe3, output)
for i, recipe in enumerate(recipes):
self.assertEqual(self.recipes.craft(recipe), outputs[i])
def test_add_3(self):
self.recipes = Recipes()
recipes = []
ingres = []
outputs = []
for i in range(0, 25):
shapeless = random.choice([True, False])
if shapeless:
recipe, ingre, output, recipe2, recipe3 = self.generate_random_recipe()
else:
recipe, ingre, output, recipe2, recipe3 = self.generate_random_recipe(characters='12')
recipes.append(recipe2)
ingres.append(ingre)
outputs.append(output)
if shapeless:
self.recipes.add_shapeless_recipe(recipe3, output)
else:
self.recipes.add_recipe(recipe, ingre, output)
for i, recipe in enumerate(recipes):
self.assertEqual(self.recipes.craft(recipe), outputs[i])
def test_add_4(self):
self.test_add_1(characters='!@#$123456789')
def test_add_5(self):
self.test_add_2(characters='!@#$123456789')
if __name__ == '__main__':
unittest.main()