Fixed an exception thrown when buying from player stock

Fix issue where malformed stock could shift following shop items
This commit is contained in:
Ceikry
2022-05-02 10:22:36 +00:00
committed by Ryan
parent a5f3f93048
commit 8438ecdbd7
4 changed files with 57 additions and 13 deletions
+27
View File
@@ -4,6 +4,8 @@ import org.junit.Assert
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import rs09.game.content.global.shops.Shop
import rs09.game.content.global.shops.Shops
import rs09.game.system.config.ShopParser
class ShopTests {
val testPlayer = TestUtils.getMockPlayer("test")
@@ -114,4 +116,29 @@ class ShopTests {
general.restock()
}
}
@Test fun playerStockShouldNeverBeNull() {
Assertions.assertNotNull(general.playerStock)
}
@Test fun shouldAllowBuyingFromPlayerStockOnMultipleRows() {
for(i in 0 until 100) {
general.playerStock.add(Item(i + 3100, 1)) //make sure we populate several rows of items
}
testPlayer.inventory.add(Item(995, 100000))
testPlayer.setAttribute("shop-cont", general.getContainer(testPlayer))
testPlayer.setAttribute("shop-main", false)
var status: Shop.TransactionStatus = Shop.TransactionStatus.Success()
Assertions.assertDoesNotThrow({
status = general.buy(testPlayer, 36, 1)
}, "Error selling to shop: ${general.playerStock}")
Assertions.assertEquals(true, status is Shop.TransactionStatus.Success)
}
@Test fun invalidStockJsonShouldNotCauseItemShift() {
val invalidJson = "{1277,10,100}-{1277,10,100}-{1279,10,100}"
val stock = Shops.parseStock(invalidJson, -1)
Assertions.assertEquals(2, stock.size)
Assertions.assertEquals(20, stock[0].amount)
}
}