-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
InventoryTest.kt
331 lines (236 loc) · 9.46 KB
/
InventoryTest.kt
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* FXGL - JavaFX Game Library. The MIT License (MIT).
* Copyright (c) AlmasB (almaslvl@gmail.com).
* See LICENSE for details.
*/
@file:Suppress("JAVA_MODULE_DOES_NOT_DEPEND_ON_MODULE")
package com.almasb.fxgl.inventory
import javafx.collections.FXCollections
import javafx.scene.shape.Rectangle
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
/**
*
* @author Almas Baimagambetov (almaslvl@gmail.com)
*/
class InventoryTest {
private lateinit var inventory: Inventory<String>
@BeforeEach
fun setUp() {
inventory = Inventory(25)
}
@Test
fun `Inventory capacity`() {
assertThat(inventory.capacity, `is`(25))
assertFalse(inventory.isFull)
repeat(24) { index ->
inventory.add("$index")
}
assertFalse(inventory.isFull)
inventory.add("100")
assertTrue(inventory.isFull)
inventory.remove("100")
assertFalse(inventory.isFull)
}
@Test
fun `Add new and remove items`() {
inventory.add("Hello")
assertThat(inventory.size, `is`(1))
assertThat(inventory.getItemQuantity("Hello"), `is`(1))
// we are adding the same thing (as defined by equals()), so size remains the same
// but quantity should change
inventory.add("Hello")
assertThat(inventory.size, `is`(1))
assertThat(inventory.getItemQuantity("Hello"), `is`(2))
inventory.add("Hello", quantity = 30)
assertThat(inventory.size, `is`(1))
assertThat(inventory.getItemQuantity("Hello"), `is`(32))
inventory.incrementQuantity("Hello", -31)
assertThat(inventory.size, `is`(1))
assertThat(inventory.getItemQuantity("Hello"), `is`(1))
inventory.remove("Hello")
assertThat(inventory.size, `is`(0))
}
@Test
fun `ItemData is generated for each new item`() {
assertTrue(inventory.allData.isEmpty())
inventory.add("Hello")
assertThat(inventory.allData.size, `is`(1))
val itemData = inventory.allData["Hello"]!!
assertThat(itemData.userItem, `is`("Hello"))
assertThat(itemData.quantity, `is`(1))
val view = Rectangle()
inventory.add("Hi", "name", "description", view, 3)
assertThat(inventory.allData.size, `is`(2))
val itemData2 = inventory.allData["Hi"]!!
assertThat(itemData2.userItem, `is`("Hi"))
assertThat(itemData2.name, `is`("name"))
assertThat(itemData2.description, `is`("description"))
assertThat(itemData2.view, `is`(view))
assertThat(itemData2.quantity, `is`(3))
assertThat(itemData2.nameProperty().value, `is`("name"))
assertThat(itemData2.descriptionProperty().value, `is`("description"))
assertThat(itemData2.viewProperty().value, `is`(view))
assertThat(itemData2.quantityProperty().value, `is`(3))
}
@Test
fun `Auto-remove items if quantity is 0`() {
inventory.add("Hello", quantity = 2)
inventory.incrementQuantity("Hello", -2)
assertThat(inventory.itemsProperty().size, `is`(0))
}
@Test
fun `Cannot add items beyond capacity`() {
inventory = Inventory(2)
assertTrue(inventory.add("Hi1"))
assertTrue(inventory.add("Hi2"))
assertFalse(inventory.add("Hi3"))
}
@Test
fun `Cannot increment quantity if item not present`() {
assertFalse(inventory.incrementQuantity("bla-bla", 3))
}
@Test
fun `Cannot increment by negative quantity if item does not have enough quantity`() {
inventory.add("Hello", quantity = 3)
assertFalse(inventory.incrementQuantity("Hello", -5))
}
@Test
fun `New items are created when adding quantity of items past stackMax`() {
inventory.add("Hello", ItemConfig(maxStackQuantity = 3), quantity = 7)
// Should create 3 stacks of quantities: [3, 3, 1]
assertThat(inventory.size, `is`(3))
// Should create 5 stacks of quantities: [3, 3, 3, 3, 2]
inventory.add("Hello", quantity = 7)
assertThat(inventory.size, `is`(5))
}
@Test
fun `New items are created when incrementing quantity past stackMax`() {
inventory.add("Hello", ItemConfig(maxStackQuantity = 3), quantity = 3)
// Should create stacks: [3, 3, 3, 1]
inventory.incrementQuantity("Hello", 7)
assertThat(inventory.size, `is`(4))
// Should increment stacks: [3, 3, 3, 3, 3]
inventory.incrementQuantity("Hello", 5)
assertThat(inventory.size, `is`(5))
}
@Test
fun `getItemQuantity returns total quantity of all item's stacks`() {
// Stacks should be: [3, 3, 3, 3, 3]
inventory.add("Hello", ItemConfig(maxStackQuantity = 3), quantity = 15)
assertThat(inventory.size, `is`(5))
assertTrue(inventory.incrementQuantity("Hello", -10))
// Stacks should be: [3, 2]
assertThat(inventory.size, `is`(2))
assertThat(inventory.getItemQuantity("Hello"), `is`(5))
//Stacks should be: [3, 3, 3, 3, 3, 1]
inventory.incrementQuantity("Hello", 11)
assertThat(inventory.size, `is`(6))
assertThat(inventory.getItemQuantity("Hello"), `is` (16))
}
@Test
fun `Stacks are removed when incrementing quantity`() {
// Stacks should be: [3, 3, 3, 3, 3]
inventory.add("Hello", ItemConfig(maxStackQuantity = 3), quantity = 15)
// Stacks should be: [3, 2]
assertTrue(inventory.incrementQuantity("Hello", -10))
assertThat(inventory.size, `is`(2))
assertThat(inventory.getItemQuantity("Hello"), `is` (5))
// Item should be removed entirely
assertTrue(inventory.incrementQuantity("Hello", -5))
assertThat(inventory.size, `is`(0))
assertFalse(inventory.hasItem("Hello"))
}
//
// @Test
// fun `Cannot change stackMax to higher than greatest item quantity`() {
// inventory.setStackMax(5)
//
// inventory.add("Hello", quantity = 5)
//
// assertThat(inventory.size, `is`(1))
//
// assertFalse(inventory.setStackMax(4))
//
// assertThat(inventory.getStackMax(), `is`(5))
// }
@Test
fun `Cannot increment quantity past inventory capacity`() {
inventory = Inventory(5)
assertFalse(inventory.add("Hello", ItemConfig(maxStackQuantity = 3), quantity = 16))
assertTrue(inventory.add("Hello", ItemConfig(maxStackQuantity = 3), quantity = 15))
// Stacks: [3, 3, 3, 3, 3]
assertTrue(inventory.incrementQuantity("Hello", -5))
// Stacks: [3, 3, 3, 1]
assertFalse(inventory.incrementQuantity("Hello", 6))
assertThat(inventory.getItemQuantity("Hello"), `is`(10))
assertFalse(inventory.incrementQuantity("Hello", -11))
assertTrue(inventory.incrementQuantity("Hello", -10))
}
@Test
fun `Cannot add a new item with 0 or negative amount`() {
inventory = Inventory(5)
assertFalse(inventory.add("Hello", quantity = 0))
assertFalse(inventory.add("Hello", quantity = -1))
}
@Test
fun `Cannot increment qty of existing item by 0`() {
inventory = Inventory(5)
inventory.add("Hello")
assertFalse(inventory.incrementQuantity("Hello", 0))
}
@Test
fun `Transfer from another inventory`() {
inventory = Inventory(5)
inventory.add("Hello", quantity = 5)
val other = Inventory<String>(2)
var result = other.transferFrom(inventory, "Hello", 4)
assertTrue(result)
assertThat(other.getItemQuantity("Hello"), `is`(4))
assertThat(inventory.getItemQuantity("Hello"), `is`(1))
// quantity only 1, so cannot transfer
result = other.transferFrom(inventory, "Hello", 4)
assertFalse(result)
// fill the inventory
other.add("Hi")
assertTrue(other.isFull)
// but we can still transfer from other since "Hello" stack exists and not limited
result = other.transferFrom(inventory, "Hello", 1)
assertTrue(result)
assertTrue(!inventory.hasItem("Hello"))
}
@Test
fun `Transfer all from inventory`() {
inventory = Inventory(5)
inventory.add("Hello", quantity = 5)
inventory.add("Hi", quantity = 3)
val other = Inventory<String>(5)
assertTrue(other.transferAllFrom(inventory))
assertThat(other.getItemQuantity("Hello"), `is`(5))
assertThat(other.getItemQuantity("Hi"), `is`(3))
val other2 = Inventory<String>(1)
// true because [other2] only has 1 space, so partially succeeds
assertTrue(other2.transferAllFrom(other))
assertThat(other2.getItemQuantity("Hello"), `is`(5))
assertThat(other2.getItemQuantity("Hi"), `is`(0))
// false because [other2] now has no space, so op fails
assertFalse(other2.transferAllFrom(other))
}
@Test
fun `Transfer from inventory does not work if no item in inventory`() {
inventory = Inventory(5)
val other = Inventory<String>(2)
assertFalse(other.transferFrom(inventory, "Hello"))
}
@Test
fun `Transfer from inventory does not work if inventory is full`() {
inventory = Inventory(5)
inventory.add("Hello")
val other = Inventory<String>(0)
assertFalse(other.transferFrom(inventory, "Hello"))
}
}