mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-06 08:40:03 +00:00
Update plugin test framework to junit5
Updates the testing infrastructure to use the latest relesae of junit and leverages the new extension mechanism to create an easy to use testing framework. Also adds additional test coverage for several plugins.
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
|
||||
import io.mockk.every
|
||||
import io.mockk.spyk
|
||||
import io.mockk.staticMockk
|
||||
import io.mockk.verify
|
||||
import org.apollo.cache.def.ItemDefinition
|
||||
import org.apollo.game.model.World
|
||||
import org.apollo.game.model.entity.Player
|
||||
import org.apollo.game.model.entity.Skill
|
||||
import org.apollo.game.plugin.api.expireObject
|
||||
import org.apollo.game.plugin.skills.mining.Ore
|
||||
import org.apollo.game.plugin.skills.mining.Pickaxe
|
||||
import org.apollo.game.plugin.skills.mining.TIN_OBJECTS
|
||||
import org.apollo.game.plugin.testing.assertions.after
|
||||
import org.apollo.game.plugin.testing.assertions.verifyAfter
|
||||
import org.apollo.game.plugin.testing.junit.ApolloTestingExtension
|
||||
import org.apollo.game.plugin.testing.junit.api.ActionCapture
|
||||
import org.apollo.game.plugin.testing.junit.api.annotations.ItemDefinitions
|
||||
import org.apollo.game.plugin.testing.junit.api.annotations.TestMock
|
||||
import org.apollo.game.plugin.testing.junit.api.interactions.spawnObject
|
||||
import org.apollo.game.plugin.testing.assertions.contains
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.extension.ExtendWith
|
||||
|
||||
@ExtendWith(ApolloTestingExtension::class)
|
||||
class MiningActionTests {
|
||||
private val TIN_OBJ_IDS = TIN_OBJECTS.entries.first()
|
||||
|
||||
@ItemDefinitions
|
||||
fun ores() = Ore.values()
|
||||
.map { ItemDefinition(it.id).also { it.name = "<ore_type>" } }
|
||||
|
||||
@ItemDefinitions
|
||||
fun pickaxes() = listOf(ItemDefinition(Pickaxe.BRONZE.id))
|
||||
|
||||
@TestMock
|
||||
lateinit var world: World
|
||||
|
||||
@TestMock
|
||||
lateinit var player: Player
|
||||
|
||||
@TestMock
|
||||
lateinit var action: ActionCapture
|
||||
|
||||
@Test
|
||||
fun `Attempting to mine a rock we don't have the skill to should send the player a message`() {
|
||||
val obj = world.spawnObject(1, player.position)
|
||||
val target = spyk(MiningTarget(obj.id, obj.position, Ore.TIN))
|
||||
|
||||
every { target.skillRequirementsMet(player) } returns false
|
||||
player.startAction(MiningAction(player, Pickaxe.BRONZE, target))
|
||||
verifyAfter(action.complete()) { player.sendMessage(contains("do not have the required level")) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Mining a rock we have the skill to mine should eventually reward ore and experience`() {
|
||||
val (tinId, expiredTinId) = TIN_OBJ_IDS
|
||||
val obj = world.spawnObject(tinId, player.position)
|
||||
val target = spyk(MiningTarget(obj.id, obj.position, Ore.TIN))
|
||||
staticMockk("org.apollo.game.plugin.api.WorldKt").mock()
|
||||
|
||||
every { target.skillRequirementsMet(player) } returns true
|
||||
every { target.isSuccessful(player, any()) } returns true
|
||||
every { world.expireObject(obj, any(), any()) } answers {}
|
||||
|
||||
player.skillSet.setCurrentLevel(Skill.MINING, Ore.TIN.level)
|
||||
player.startAction(MiningAction(player, Pickaxe.BRONZE, target))
|
||||
|
||||
verifyAfter(action.ticks(1)) { player.sendMessage(contains("You swing your pick")) }
|
||||
after(action.complete()) {
|
||||
verify { player.sendMessage("You manage to mine some <ore_type>") }
|
||||
verify { world.expireObject(obj, expiredTinId, Ore.TIN.respawn) }
|
||||
|
||||
assertTrue(player.inventory.contains(Ore.TIN.id))
|
||||
assertEquals(player.skillSet.getExperience(Skill.MINING), Ore.TIN.exp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import org.apollo.cache.def.ItemDefinition
|
||||
import org.apollo.game.model.entity.Player
|
||||
import org.apollo.game.model.entity.Skill
|
||||
import org.apollo.game.plugin.skills.mining.Pickaxe
|
||||
import org.apollo.game.plugin.testing.junit.ApolloTestingExtension
|
||||
import org.apollo.game.plugin.testing.junit.api.annotations.ItemDefinitions
|
||||
import org.apollo.game.plugin.testing.junit.api.annotations.TestMock
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.extension.ExtendWith
|
||||
import org.junit.jupiter.params.ParameterizedTest
|
||||
import org.junit.jupiter.params.provider.EnumSource
|
||||
|
||||
@ExtendWith(ApolloTestingExtension::class)
|
||||
class PickaxeTests {
|
||||
|
||||
@ItemDefinitions
|
||||
fun pickaxes() = Pickaxe.values().map {
|
||||
ItemDefinition(it.id).apply { isStackable = false }
|
||||
}
|
||||
|
||||
@TestMock
|
||||
lateinit var player: Player
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(Pickaxe::class)
|
||||
fun `No pickaxe is chosen if none are available`(pickaxe: Pickaxe) {
|
||||
player.skillSet.setCurrentLevel(Skill.MINING, pickaxe.level)
|
||||
|
||||
assertEquals(null, Pickaxe.bestFor(player))
|
||||
}
|
||||
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(Pickaxe::class)
|
||||
fun `The highest level pickaxe is chosen when available`(pickaxe: Pickaxe) {
|
||||
player.skillSet.setCurrentLevel(Skill.MINING, pickaxe.level)
|
||||
player.inventory.add(pickaxe.id)
|
||||
|
||||
assertEquals(pickaxe, Pickaxe.bestFor(player))
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(Pickaxe::class)
|
||||
fun `Only pickaxes the player has are chosen`(pickaxe: Pickaxe) {
|
||||
player.skillSet.setCurrentLevel(Skill.MINING, pickaxe.level)
|
||||
player.inventory.add(Pickaxe.BRONZE.id)
|
||||
|
||||
assertEquals(Pickaxe.BRONZE, Pickaxe.bestFor(player))
|
||||
}
|
||||
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(value = Pickaxe::class)
|
||||
fun `Pickaxes can be chosen from equipment as well as inventory`(pickaxe: Pickaxe) {
|
||||
player.skillSet.setCurrentLevel(Skill.MINING, pickaxe.level)
|
||||
player.inventory.add(pickaxe.id)
|
||||
|
||||
assertEquals(pickaxe, Pickaxe.bestFor(player))
|
||||
}
|
||||
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(value = Pickaxe::class)
|
||||
fun `Pickaxes with a level requirement higher than the player's are ignored`(pickaxe: Pickaxe) {
|
||||
player.skillSet.setCurrentLevel(Skill.MINING, pickaxe.level)
|
||||
player.inventory.add(pickaxe.id)
|
||||
|
||||
Pickaxe.values()
|
||||
.filter { it.level > pickaxe.level }
|
||||
.forEach { player.inventory.add(it.id) }
|
||||
|
||||
assertEquals(pickaxe, Pickaxe.bestFor(player))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
|
||||
import org.apollo.cache.def.ItemDefinition
|
||||
import org.apollo.game.model.entity.Player
|
||||
import org.apollo.game.plugin.skills.mining.Ore
|
||||
import org.apollo.game.plugin.testing.assertions.verifyAfter
|
||||
import org.apollo.game.plugin.testing.junit.ApolloTestingExtension
|
||||
import org.apollo.game.plugin.testing.junit.api.ActionCapture
|
||||
import org.apollo.game.plugin.testing.junit.api.annotations.ItemDefinitions
|
||||
import org.apollo.game.plugin.testing.junit.api.annotations.TestMock
|
||||
import org.apollo.game.plugin.testing.junit.api.interactions.interactWithObject
|
||||
import org.apollo.game.plugin.testing.assertions.contains
|
||||
import org.junit.jupiter.api.extension.ExtendWith
|
||||
import org.junit.jupiter.params.ParameterizedTest
|
||||
import org.junit.jupiter.params.provider.ArgumentsSource
|
||||
|
||||
@ExtendWith(ApolloTestingExtension::class)
|
||||
class ProspectingTests {
|
||||
|
||||
@ItemDefinitions
|
||||
fun ores() = Ore.values().map {
|
||||
ItemDefinition(it.id).also { it.name = "<ore_type>" }
|
||||
}
|
||||
|
||||
@TestMock
|
||||
lateinit var player: Player
|
||||
|
||||
@TestMock
|
||||
lateinit var action: ActionCapture
|
||||
|
||||
@ParameterizedTest
|
||||
@ArgumentsSource(MiningTestDataProvider::class)
|
||||
fun `Prospecting a rock should reveal the type of ore it contains`(data: MiningTestData) {
|
||||
player.interactWithObject(data.rockId, 2)
|
||||
|
||||
verifyAfter(action.ticks(1)) { player.sendMessage(contains("examine the rock")) }
|
||||
verifyAfter(action.complete()) { player.sendMessage(contains("This rock contains <ore_type>")) }
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ArgumentsSource(MiningTestDataProvider::class)
|
||||
fun `Prospecting an expired rock should reveal it contains no ore`(data: MiningTestData) {
|
||||
player.interactWithObject(data.expiredRockId, 2)
|
||||
|
||||
verifyAfter(action.complete()) { player.sendMessage(contains("no ore available in this rock")) }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import org.apollo.game.plugin.skills.mining.Ore
|
||||
import org.junit.jupiter.api.extension.ExtensionContext
|
||||
import org.junit.jupiter.params.provider.Arguments
|
||||
import org.junit.jupiter.params.provider.ArgumentsProvider
|
||||
import java.util.stream.Stream
|
||||
|
||||
data class MiningTestData(val rockId: Int, val expiredRockId: Int, val ore: Ore)
|
||||
|
||||
fun miningTestData(): Collection<MiningTestData> = Ore.values()
|
||||
.flatMap { ore -> ore.objects.map { MiningTestData(it.key, it.value, ore) } }
|
||||
.toList()
|
||||
|
||||
class MiningTestDataProvider : ArgumentsProvider {
|
||||
override fun provideArguments(context: ExtensionContext?): Stream<out Arguments> {
|
||||
return miningTestData().map { Arguments { arrayOf(it) } }.stream()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user