Add more unit tests for shops and quest related architecture
Fixed bug where quests could be repeatedly finished Fixed bug where ironman status wasn't checked when buying overstocked items Fixed bug where selling multiple items at a time that weren't listed in a shop would not succeed Fixed bug where shop restocking would sometimes interrupt if a shop stock item was in a null slot
This commit is contained in:
@@ -13,8 +13,8 @@ import rs09.game.node.entity.skill.slayer.SlayerManager
|
||||
import rs09.game.system.SystemLogger
|
||||
|
||||
object APITests {
|
||||
val testPlayer = Player(PlayerDetails("test", "testing"))
|
||||
val testPlayer2 = Player(PlayerDetails("test2", "testing"))
|
||||
val testPlayer = TestUtils.getMockPlayer("test")
|
||||
val testPlayer2 = TestUtils.getMockPlayer("test2")
|
||||
|
||||
@Test fun testIfaceSettings(){
|
||||
var builder = IfaceSettingsBuilder()
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import core.game.node.entity.player.link.quest.Quest
|
||||
import core.game.node.entity.player.link.quest.QuestRepository
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class QuestTests {
|
||||
val testPlayer = TestUtils.getMockPlayer("test")
|
||||
class TestQuest : Quest("Test Quest", 0, 0, 1, 1, 0, 1, 2) {
|
||||
override fun newInstance(`object`: Any?): Quest {
|
||||
return this
|
||||
}
|
||||
}
|
||||
val testQuest = TestQuest()
|
||||
|
||||
@Test fun getIndexShouldNotThrowException() {
|
||||
Assertions.assertDoesNotThrow {
|
||||
testQuest.index
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun registerShouldMakeQuestImmediatelyAvailable() {
|
||||
QuestRepository.register(testQuest)
|
||||
Assertions.assertNotNull(QuestRepository.getQuests()[testQuest.name])
|
||||
}
|
||||
|
||||
@Test fun registerShouldMakeQuestImmediatelyAvailableToInstances() {
|
||||
QuestRepository.register(testQuest)
|
||||
val instance = QuestRepository(testPlayer)
|
||||
Assertions.assertNotNull(instance.getQuest(testQuest.name))
|
||||
}
|
||||
|
||||
@Test fun getStageOnUnstartedQuestShouldNotThrowException() {
|
||||
QuestRepository.register(testQuest)
|
||||
val instance = QuestRepository(testPlayer)
|
||||
Assertions.assertDoesNotThrow {
|
||||
instance.getStage(testQuest)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun setStageOnUnstartedQuestShouldNotThrowException() {
|
||||
QuestRepository.register(testQuest)
|
||||
val instance = QuestRepository(testPlayer)
|
||||
Assertions.assertDoesNotThrow {
|
||||
instance.setStage(testQuest, 10)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun completeQuestShouldThrowExceptionIfAlreadyComplete() {
|
||||
Assertions.assertThrows(IllegalStateException::class.java, {
|
||||
QuestRepository.register(testQuest)
|
||||
val repo = QuestRepository(testPlayer)
|
||||
repo.getQuest("Test Quest").finish(testPlayer)
|
||||
repo.getQuest("Test Quest").finish(testPlayer)
|
||||
}, "Quest completed twice without throwing an exception or threw wrong exception!")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
import core.game.node.entity.player.link.IronmanMode
|
||||
import core.game.node.item.Item
|
||||
import org.junit.Assert
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import org.junit.jupiter.api.Test
|
||||
import rs09.game.content.global.shops.Shop
|
||||
|
||||
class ShopTests {
|
||||
val testPlayer = TestUtils.getMockPlayer("test")
|
||||
val testIronman = TestUtils.getMockPlayer("test2", IronmanMode.STANDARD)
|
||||
val nonGeneral = TestUtils.getMockShop("Not General", false, Item(4151, 1))
|
||||
val general = TestUtils.getMockShop("General", true, Item(4151, 1))
|
||||
|
||||
@Test fun shouldSellItemToStore() {
|
||||
testPlayer.inventory.add(Item(4151, 1))
|
||||
testPlayer.setAttribute("shop-cont", general.getContainer(testPlayer))
|
||||
val status = general.sell(testPlayer, 0, 1)
|
||||
Assertions.assertEquals(true, status is Shop.TransactionStatus.Success, "Transaction failed: ${if(status is Shop.TransactionStatus.Failure) status.reason else ""}")
|
||||
}
|
||||
|
||||
@Test fun shouldNotSellUnstockedItemToStandardStore() {
|
||||
testPlayer.inventory.add(Item(1, 1))
|
||||
testPlayer.setAttribute("shop-cont", nonGeneral.getContainer(testPlayer))
|
||||
val status = nonGeneral.sell(testPlayer, 0, 1)
|
||||
Assertions.assertEquals(true, status is Shop.TransactionStatus.Failure)
|
||||
}
|
||||
|
||||
@Test fun shouldSellUnstockedItemToGeneralStore() {
|
||||
testPlayer.inventory.add(Item(1, 1))
|
||||
testPlayer.setAttribute("shop-cont", general.getContainer(testPlayer))
|
||||
val status = general.sell(testPlayer, 0, 1)
|
||||
Assertions.assertEquals(true, status is Shop.TransactionStatus.Success, "Transaction failure: ${if(status is Shop.TransactionStatus.Failure) status.reason else ""}")
|
||||
}
|
||||
|
||||
@Test fun shouldSellUnstockedItemToGeneralStoreAsIronman() {
|
||||
testIronman.inventory.add(Item(1, 1))
|
||||
testIronman.setAttribute("shop-cont", general.getContainer(testPlayer))
|
||||
val status = general.sell(testIronman, 0, 1)
|
||||
Assertions.assertEquals(true, status is Shop.TransactionStatus.Success, "Transaction failure: ${if(status is Shop.TransactionStatus.Failure) status.reason else ""}")
|
||||
}
|
||||
|
||||
@Test fun shouldSellStackOfUnstockedItemsToPlayerStock() {
|
||||
testPlayer.inventory.add(Item(1, 20))
|
||||
testPlayer.setAttribute("shop-cont", general.getContainer(testPlayer))
|
||||
val status = general.sell(testPlayer, 0, 20)
|
||||
Assertions.assertEquals(true, status is Shop.TransactionStatus.Success, "Transaction failure: ${if(status is Shop.TransactionStatus.Failure) status.reason else ""}")
|
||||
}
|
||||
|
||||
@Test fun shouldPutSoldUnstockedItemsInPlayerStock() {
|
||||
testPlayer.inventory.add(Item(2,1))
|
||||
testPlayer.setAttribute("shop-cont", general.getContainer(testPlayer))
|
||||
val status = general.sell(testPlayer, 0, 1)
|
||||
Assertions.assertEquals(true, status is Shop.TransactionStatus.Success, "Transaction failure: ${if(status is Shop.TransactionStatus.Failure) status.reason else ""}")
|
||||
Assertions.assertEquals(1, general.playerStock.getAmount(2))
|
||||
Assertions.assertEquals(0, general.getContainer(testPlayer).getAmount(2))
|
||||
}
|
||||
|
||||
@Test fun shouldAllowStandardPlayerToBuy() {
|
||||
testPlayer.inventory.add(Item(995, 100000))
|
||||
testPlayer.setAttribute("shop-cont", general.getContainer(testPlayer))
|
||||
testPlayer.setAttribute("shop-main", true)
|
||||
val status = general.buy(testPlayer, 0, 1)
|
||||
Assertions.assertEquals(true, status is Shop.TransactionStatus.Success, "Transaction failure: ${if(status is Shop.TransactionStatus.Failure) status.reason else ""}")
|
||||
}
|
||||
|
||||
@Test fun shouldAllowStandardPlayerToBuyOverstock() {
|
||||
testPlayer.inventory.add(Item(995, 100000))
|
||||
testPlayer.setAttribute("shop-cont", general.getContainer(testPlayer))
|
||||
testPlayer.setAttribute("shop-main", true)
|
||||
general.getContainer(testPlayer).add(Item(4151, 100))
|
||||
val status = general.buy(testPlayer, 0, 1)
|
||||
Assertions.assertEquals(true, status is Shop.TransactionStatus.Success, "Transaction failure: ${if(status is Shop.TransactionStatus.Failure) status.reason else ""}")
|
||||
}
|
||||
|
||||
@Test fun shouldAllowStandardPlayerToBuyPlayerStock() {
|
||||
testPlayer.inventory.add(Item(995, 100000))
|
||||
testPlayer.setAttribute("shop-cont", general.getContainer(testPlayer))
|
||||
testPlayer.setAttribute("shop-main", false)
|
||||
general.playerStock.add(Item(4151, 100))
|
||||
val status = general.buy(testPlayer, 0, 1)
|
||||
Assertions.assertEquals(true, status is Shop.TransactionStatus.Success, "Transaction failure: ${if(status is Shop.TransactionStatus.Failure) status.reason else ""}")
|
||||
}
|
||||
|
||||
@Test fun shouldNotAllowIronmanToBuyOverstock() {
|
||||
testIronman.inventory.add(Item(995, 100000))
|
||||
testIronman.setAttribute("shop-cont", general.getContainer(testIronman))
|
||||
testIronman.setAttribute("shop-main", true)
|
||||
general.getContainer(testIronman).add(Item(4151, 100))
|
||||
val status = general.buy(testIronman, 0, 1)
|
||||
Assertions.assertEquals(true, status is Shop.TransactionStatus.Failure)
|
||||
}
|
||||
|
||||
@Test fun shouldNotAllowIronmanToBuyPlayerStock() {
|
||||
testIronman.inventory.add(Item(995, 100000))
|
||||
testIronman.setAttribute("shop-cont", general.playerStock)
|
||||
testIronman.setAttribute("shop-main", false)
|
||||
general.playerStock.add(Item(4151, 1))
|
||||
val status = general.buy(testIronman, 0, 1)
|
||||
Assertions.assertEquals(true, status is Shop.TransactionStatus.Failure)
|
||||
}
|
||||
|
||||
@Test fun openShopShouldNotThrowException() {
|
||||
Assertions.assertDoesNotThrow {
|
||||
general.openFor(testPlayer)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun shouldNotThrowExceptionWhenRestockingStockWithNullSlot() {
|
||||
Assertions.assertDoesNotThrow {
|
||||
general.getContainer(testPlayer).add(Item(1, 100))
|
||||
general.getContainer(testPlayer).add(Item(2, 100))
|
||||
general.getContainer(testPlayer).replace(null, 0) //replace item in slot 0 with null
|
||||
for ((k,_) in general.stockInstances) general.needsUpdate[k] = true
|
||||
general.restock()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import core.game.node.entity.player.Player
|
||||
import core.game.node.entity.player.info.PlayerDetails
|
||||
import core.game.node.entity.player.link.IronmanMode
|
||||
import core.game.node.item.Item
|
||||
import rs09.game.ai.ArtificialSession
|
||||
import rs09.game.content.global.shops.Shop
|
||||
import rs09.game.content.global.shops.ShopItem
|
||||
|
||||
object TestUtils {
|
||||
fun getMockPlayer(name: String, ironman: IronmanMode = IronmanMode.NONE): Player {
|
||||
val p = Player(PlayerDetails(name, name))
|
||||
p.details.session = ArtificialSession.getSingleton()
|
||||
p.ironmanManager.mode = ironman
|
||||
return p
|
||||
}
|
||||
|
||||
fun getMockShop(name: String, general: Boolean, vararg stock: Item) : Shop {
|
||||
return Shop(name, stock.map { ShopItem(it.id, it.amount, 100) }.toTypedArray(), general)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user