11extends TestSuite
2+ # Grid Inventory Tests
3+ # This script tests the functionality of the GridInventory class.
4+ # It includes tests for adding, removing, splitting, and merging stacks,
5+ # as well as checking for available space and serialization.
6+ # The tests are designed to ensure that the GridInventory behaves correctly
7+ # according to the expected behavior defined in the InventoryDatabase.
28
3- @export var inventory_3x3 : GridInventory
4- @export var inventory_3x3_2 : GridInventory
5- @export var inventory_8x5 : GridInventory
6- @export var inventory_8x1 : GridInventory
7- # Item 1 x 1 (Stackable 16)
89@export var wood : String = "wood"
9- # Item 2 x 2 (Stackable 1)
1010@export var stone_pickaxe : String = "stone_pickaxe"
11- # Item 2 x 2 (Stackable 8)
1211@export var campfire : String = "campfire"
12+ @export var database : InventoryDatabase
1313
1414
1515func init_suite ():
@@ -33,100 +33,124 @@ func init_suite():
3333
3434
3535func test_has_place_for () -> void :
36- # Empty inventory
36+ var inventory_3x3 := GridInventory .new ()
37+ inventory_3x3 .set_size (Vector2i (3 , 3 ))
38+ inventory_3x3 .database = database
39+
3740 assert (inventory_3x3 .has_space_for (wood ))
3841 assert (inventory_3x3 .has_space_for (stone_pickaxe ))
3942
40- # Inventory containing 1x1 item
4143 assert (inventory_3x3 .add (wood ) == 0 )
4244 assert (inventory_3x3 .has_space_for (stone_pickaxe ))
43- #
44- ## Inventory containing 2x2 item
45- ## InventoryGridStacked.set_item_max_stack_size(item_2x2, 1)
45+
4646 assert (inventory_3x3 .add (campfire ) == 0 )
4747 assert (! inventory_3x3 .has_space_for (stone_pickaxe ))
48- #
49- ## Inventory containing 2x2 item with extended max_stack_size
50- ## InventoryGridStacked.set_item_max_stack_size(item_2x2, 10)
51- # assert(inventory_3x3.has_space_for(campfire))
5248
5349 inventory_3x3 .clear ()
54-
5550 assert (inventory_3x3 .stacks .size () == 0 )
51+ inventory_3x3 .free ()
5652
5753
5854func test_add_item_automerge () -> void :
59- ## Inventory containing 2x2 item
55+ var inventory_3x3 := GridInventory .new ()
56+ inventory_3x3 .set_size (Vector2i (3 , 3 ))
57+ inventory_3x3 .database = database
58+
6059 assert (inventory_3x3 .add (stone_pickaxe ) == 0 )
6160 assert (inventory_3x3 .stacks .size () == 1 )
6261
6362 inventory_3x3 .clear ()
64- ## No stack space, no grid space
6563 assert (inventory_3x3 .add (campfire , 8 ) == 0 )
6664 assert (inventory_3x3 .add (campfire , 1 ) == 1 )
67- #
68- ## No stack space but grid space available
6965 assert (inventory_3x3 .add (wood ) == 0 )
7066
71- inventory_3x3 .clear ()
67+ inventory_3x3 .free ()
7268
7369
7470func test_stack_split () -> void :
71+ var inventory_3x3 := GridInventory .new ()
72+ inventory_3x3 .set_size (Vector2i (3 , 3 ))
73+ inventory_3x3 .database = database
74+
7575 assert (inventory_3x3 .add (wood , 2 ) == 0 )
7676 assert (inventory_3x3 .split (0 , 1 ))
7777 assert (inventory_3x3 .stacks .size () == 2 )
7878 assert (inventory_3x3 .contains (wood , 2 ))
79- inventory_3x3 .clear ()
79+ inventory_3x3 .free ()
8080
8181
8282func test_stack_cant_split () -> void :
83+ var inventory_3x3 := GridInventory .new ()
84+ inventory_3x3 .set_size (Vector2i (3 , 3 ))
85+ inventory_3x3 .database = database
86+
8387 assert (inventory_3x3 .add (campfire ) == 0 )
8488 assert (! inventory_3x3 .split (0 , 1 ))
8589 assert (inventory_3x3 .stacks .size () == 1 )
86- inventory_3x3 .clear ()
90+ inventory_3x3 .free ()
8791
8892
8993func test_stack_join () -> void :
94+ var inventory_3x3 := GridInventory .new ()
95+ inventory_3x3 .set_size (Vector2i (3 , 3 ))
96+ inventory_3x3 .database = database
97+
9098 assert (inventory_3x3 .add_on_new_stack (wood ) == 0 )
9199 assert (inventory_3x3 .add_on_new_stack (wood ) == 0 )
92100 assert (inventory_3x3 .stacks .size () == 2 )
93101 assert (inventory_3x3 .transfer_at (1 , inventory_3x3 , 0 , 1 ) == 0 )
94102 assert (inventory_3x3 .stacks .size () == 1 )
95103 assert (inventory_3x3 .stacks [0 ].amount == 2 )
96- inventory_3x3 .clear ()
104+ inventory_3x3 .free ()
97105
98106
99107func test_stack_cant_join () -> void :
108+ var inventory_3x3 := GridInventory .new ()
109+ inventory_3x3 .set_size (Vector2i (3 , 3 ))
110+ inventory_3x3 .database = database
111+
100112 assert (inventory_3x3 .add (wood , 16 ) == 0 )
101113 assert (inventory_3x3 .add (wood , 1 ) == 0 )
102114 assert (inventory_3x3 .stacks .size () == 2 )
103115 assert (inventory_3x3 .transfer_at (1 , inventory_3x3 , 0 , 1 ) == 1 )
104116 assert (inventory_3x3 .stacks .size () == 2 )
105- inventory_3x3 .clear ()
117+ inventory_3x3 .free ()
106118
107119
108120func test_automerge () -> void :
121+ var inventory_3x3 := GridInventory .new ()
122+ inventory_3x3 .set_size (Vector2i (3 , 3 ))
123+ inventory_3x3 .database = database
124+ var inventory_3x3_2 := GridInventory .new ()
125+ inventory_3x3_2 .set_size (Vector2i (3 , 3 ))
126+ inventory_3x3_2 .database = database
127+
109128 assert (inventory_3x3 .add (stone_pickaxe ) == 0 )
110129 assert (inventory_3x3_2 .add (campfire ) == 0 )
111130 assert (inventory_3x3_2 .add (wood ) == 0 )
112131 assert (inventory_3x3 .stacks .size () == 1 )
113132 assert (inventory_3x3_2 .stacks .size () == 2 )
114133
115- # Not enough space
116134 assert (inventory_3x3_2 .transfer (0 , inventory_3x3 , 1 ) == 1 )
117135 assert (inventory_3x3_2 .stacks .size () == 2 )
118136 assert (inventory_3x3 .stacks .size () == 1 )
119137
120- # Enough space
121138 assert (inventory_3x3_2 .transfer (0 , inventory_3x3 , 1 ) == 0 )
122139 assert (inventory_3x3 .stacks .size () == 2 )
123140 assert (inventory_3x3_2 .stacks .size () == 1 )
124141
125- inventory_3x3 .clear ()
126- inventory_3x3_2 .clear ()
142+ inventory_3x3 .free ()
143+ inventory_3x3_2 .free ()
127144
128145
129146func test_autosplitmerge () -> void :
147+ var inventory_3x3 := GridInventory .new ()
148+ inventory_3x3 .set_size (Vector2i (3 , 3 ))
149+ inventory_3x3 .database = database
150+ var inventory_3x3_2 := GridInventory .new ()
151+ inventory_3x3_2 .set_size (Vector2i (3 , 3 ))
152+ inventory_3x3_2 .database = database
153+
130154 assert (inventory_3x3 .add (campfire , 6 ) == 0 )
131155 assert (inventory_3x3_2 .add (campfire , 4 ) == 0 )
132156 assert (inventory_3x3_2 .stacks [0 ].amount == 4 )
@@ -136,30 +160,44 @@ func test_autosplitmerge() -> void:
136160 assert (inventory_3x3 .stacks [0 ].amount == 8 )
137161 assert (inventory_3x3_2 .stacks [0 ].amount == 2 )
138162
139- inventory_3x3 .clear ()
140- inventory_3x3_2 .clear ()
163+ inventory_3x3 .free ()
164+ inventory_3x3_2 .free ()
141165
142166
143167func test_get_stack_at () -> void :
144- inventory_3x3 .clear ()
168+ var inventory_3x3 := GridInventory .new ()
169+ inventory_3x3 .set_size (Vector2i (3 , 3 ))
170+ inventory_3x3 .database = database
171+
145172 assert (inventory_3x3 .add_at_position (Vector2i (1 , 1 ), "wood" , 4 ) == 0 )
146173 assert (inventory_3x3 .add_at_position (Vector2i (2 , 1 ), "stone" , 3 ) == 0 )
147174 var stack = inventory_3x3 .get_stack_at (Vector2i (1 , 1 ))
148175 assert (stack != null )
149176 var index = inventory_3x3 .stacks .find (stack )
150177 assert ( ! (index < 0 or index >= inventory_3x3 .stacks .size ()))
178+ inventory_3x3 .free ()
151179
152180
153181func test_transfer_to_with_stack () -> void :
154- inventory_3x3 .clear ()
182+ var inventory_3x3 := GridInventory .new ()
183+ inventory_3x3 .set_size (Vector2i (3 , 3 ))
184+ inventory_3x3 .database = database
185+
155186 assert (inventory_3x3 .add_at_position (Vector2i (1 , 1 ), "wood" , 4 ) == 0 )
156187 assert (inventory_3x3 .add_at_position (Vector2i (2 , 1 ), "stone" , 3 ) == 0 )
157188 assert (inventory_3x3 .transfer_to (Vector2i (1 , 1 ), inventory_3x3 , Vector2i (2 , 1 ), 4 ) == 0 )
158-
159189 assert (inventory_3x3 .get_stack_at (Vector2i (1 , 1 )).item_id == "stone" )
190+ inventory_3x3 .free ()
160191
161192
162193func test_wrong_stack_type () -> void :
194+ var inventory_3x3 := GridInventory .new ()
195+ inventory_3x3 .set_size (Vector2i (3 , 3 ))
196+ inventory_3x3 .database = database
197+ var inventory_3x3_2 := GridInventory .new ()
198+ inventory_3x3_2 .set_size (Vector2i (3 , 3 ))
199+ inventory_3x3_2 .database = database
200+
163201 assert (inventory_3x3 .add (wood , 1 , { "ok" = "2" }) == 0 )
164202 assert (inventory_3x3_2 .add (wood , 1 , { "teste" = "3" }) == 0 )
165203 assert (inventory_3x3 .stacks .size () == 1 )
@@ -168,11 +206,15 @@ func test_wrong_stack_type() -> void:
168206 assert (inventory_3x3 .stacks .size () == 2 )
169207 assert (inventory_3x3_2 .stacks .size () == 0 )
170208
171- inventory_3x3 .clear ()
172- inventory_3x3_2 .clear ()
209+ inventory_3x3 .free ()
210+ inventory_3x3_2 .free ()
173211
174212
175213func test_clear () -> void :
214+ var inventory_3x3 := GridInventory .new ()
215+ inventory_3x3 .set_size (Vector2i (3 , 3 ))
216+ inventory_3x3 .database = database
217+
176218 assert (inventory_3x3 .add (wood , 1 ) == 0 )
177219 assert (inventory_3x3 .get_quad_tree ().get_first (Vector2i (0 , 0 )) != null )
178220 assert (! inventory_3x3 .get_quad_tree ().is_empty ())
@@ -184,10 +226,14 @@ func test_clear() -> void:
184226 assert (inventory_3x3 .get_quad_tree ().get_first (Vector2i (0 , 0 )) == null )
185227 assert (inventory_3x3 .get_quad_tree ().is_empty ())
186228 assert (inventory_3x3 .stack_positions .size () == 0 )
229+ inventory_3x3 .free ()
187230
188231
189232func test_full () -> void :
190- inventory_3x3 .clear ()
233+ var inventory_3x3 := GridInventory .new ()
234+ inventory_3x3 .set_size (Vector2i (3 , 3 ))
235+ inventory_3x3 .database = database
236+
191237 assert (inventory_3x3 .has_free_place (Vector2i (1 , 1 )))
192238 assert (! inventory_3x3 .is_full ())
193239 assert (inventory_3x3 .add (wood , 16 ) == 0 )
@@ -202,9 +248,17 @@ func test_full() -> void:
202248 assert (inventory_3x3 .add (wood , 16 ) == 0 )
203249 assert (inventory_3x3 .add (wood , 16 ) == 0 )
204250 assert (inventory_3x3 .is_full ())
251+ inventory_3x3 .free ()
205252
206253
207254func test_serialize () -> void :
255+ var inventory_3x3 := GridInventory .new ()
256+ inventory_3x3 .set_size (Vector2i (3 , 3 ))
257+ inventory_3x3 .database = database
258+ var inventory_3x3_2 := GridInventory .new ()
259+ inventory_3x3_2 .set_size (Vector2i (3 , 3 ))
260+ inventory_3x3_2 .database = database
261+
208262 assert (inventory_3x3 .add_at_position (Vector2i (0 , 1 ), wood , 1 ) == 0 )
209263 assert (inventory_3x3 .stack_positions .size () == 1 )
210264 assert (inventory_3x3 .stack_positions [0 ] == Vector2i (0 , 1 ))
@@ -215,28 +269,41 @@ func test_serialize() -> void:
215269 assert (inventory_3x3_2 .contains (wood , 1 ))
216270 assert (inventory_3x3_2 .stack_positions .size () == 1 )
217271 assert (inventory_3x3_2 .get_quad_tree ().get_first (Vector2i (0 , 1 )) != null )
272+ inventory_3x3 .free ()
273+ inventory_3x3_2 .free ()
218274
219275
220276func test_move_workbench () -> void :
221- # Test moving a stack to a workbench
222- inventory_8x1 .clear ()
223- inventory_8x5 .clear ()
277+ var inventory_8x1 := GridInventory .new ()
278+ inventory_8x1 .set_size (Vector2i (8 , 1 ))
279+ inventory_8x1 .database = database
280+ var inventory_8x5 := GridInventory .new ()
281+ inventory_8x5 .set_size (Vector2i (8 , 5 ))
282+ inventory_8x5 .database = database
283+
224284 inventory_8x5 .add ("workbench" , 1 )
225285 var stack_index = inventory_8x5 .get_stack_index_at (Vector2i (0 , 0 ))
226286 if stack_index == - 1 :
287+ inventory_8x1 .free ()
288+ inventory_8x5 .free ()
227289 return
228290 print (stack_index )
229291 var amount = inventory_8x5 .transfer (stack_index , inventory_8x1 , 1 )
230292 assert (amount == 0 )
293+ inventory_8x1 .free ()
294+ inventory_8x5 .free ()
231295
232296
233297func test_has_space_for () -> void :
234- # Test has_space_for with workbench
235- inventory_8x1 .clear ()
236- inventory_8x5 .clear ()
298+ var inventory_8x1 := GridInventory .new ()
299+ inventory_8x1 .set_size (Vector2i (8 , 1 ))
300+ inventory_8x1 .database = database
301+ var inventory_8x5 := GridInventory .new ()
302+ inventory_8x5 .set_size (Vector2i (8 , 5 ))
303+ inventory_8x5 .database = database
237304
238305 assert (! inventory_8x1 .has_space_for ("workbench" ))
239306 assert (inventory_8x5 .has_space_for ("workbench" ))
240307
241- inventory_8x1 .clear ()
242- inventory_8x5 .clear ()
308+ inventory_8x1 .free ()
309+ inventory_8x5 .free ()
0 commit comments