mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-07 08:39:17 +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,72 @@
|
||||
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.woodcutting.Axe
|
||||
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 AxeTests {
|
||||
|
||||
@ItemDefinitions
|
||||
fun axes() = Axe.values().map {
|
||||
ItemDefinition(it.id).apply { isStackable = false }
|
||||
}
|
||||
|
||||
@TestMock
|
||||
lateinit var player: Player
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(Axe::class)
|
||||
fun `No axe is chosen if none are available`(axe: Axe) {
|
||||
player.skillSet.setCurrentLevel(Skill.WOODCUTTING, axe.level)
|
||||
|
||||
assertEquals(null, Axe.bestFor(player))
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(Axe::class)
|
||||
fun `The highest level axe is chosen when available`(axe: Axe) {
|
||||
player.skillSet.setCurrentLevel(Skill.WOODCUTTING, axe.level)
|
||||
player.inventory.add(axe.id)
|
||||
|
||||
assertEquals(axe, Axe.bestFor(player))
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(Axe::class)
|
||||
fun `Only axes the player has are chosen`(axe: Axe) {
|
||||
player.skillSet.setCurrentLevel(Skill.WOODCUTTING, axe.level)
|
||||
player.inventory.add(Axe.BRONZE.id)
|
||||
|
||||
assertEquals(Axe.BRONZE, Axe.bestFor(player))
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(Axe::class)
|
||||
fun `Axes can be chosen from equipment as well as inventory`(axe: Axe) {
|
||||
player.skillSet.setCurrentLevel(Skill.WOODCUTTING, axe.level)
|
||||
player.inventory.add(axe.id)
|
||||
|
||||
assertEquals(axe, Axe.bestFor(player))
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(Axe::class)
|
||||
fun `Axes with a level requirement higher than the player's are ignored`(axe: Axe) {
|
||||
player.skillSet.setCurrentLevel(Skill.WOODCUTTING, axe.level)
|
||||
player.inventory.add(axe.id)
|
||||
|
||||
Axe.values()
|
||||
.filter { it.level > axe.level }
|
||||
.forEach { player.inventory.add(it.id) }
|
||||
|
||||
assertEquals(axe, Axe.bestFor(player))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import org.apollo.game.plugin.skills.woodcutting.Tree
|
||||
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 WoodcuttingTestData(val treeId: Int, val stumpId: Int, val tree: Tree)
|
||||
|
||||
fun woodcuttingTestData(): Collection<WoodcuttingTestData> = Tree.values()
|
||||
.flatMap { tree -> tree.objects.map { WoodcuttingTestData(it, tree.stump, tree) } }
|
||||
.toList()
|
||||
|
||||
class WoodcuttingTestDataProvider : ArgumentsProvider {
|
||||
override fun provideArguments(context: ExtensionContext?): Stream<out Arguments> {
|
||||
return woodcuttingTestData().map { Arguments { arrayOf(it) } }.stream()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
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.woodcutting.Axe
|
||||
import org.apollo.game.plugin.testing.assertions.after
|
||||
import org.apollo.game.plugin.testing.assertions.contains
|
||||
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.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assumptions.assumeTrue
|
||||
import org.junit.jupiter.api.Disabled
|
||||
import org.junit.jupiter.api.extension.ExtendWith
|
||||
import org.junit.jupiter.params.ParameterizedTest
|
||||
import org.junit.jupiter.params.provider.ArgumentsSource
|
||||
import java.util.*
|
||||
|
||||
@ExtendWith(ApolloTestingExtension::class)
|
||||
class WoodcuttingTests {
|
||||
|
||||
@ItemDefinitions
|
||||
fun logs() = woodcuttingTestData().map {
|
||||
ItemDefinition(it.tree.id).also { it.name = "<tree_type>" }
|
||||
}
|
||||
|
||||
@ItemDefinitions
|
||||
fun tools() = listOf(ItemDefinition(Axe.BRONZE.id))
|
||||
|
||||
@TestMock
|
||||
lateinit var action: ActionCapture
|
||||
|
||||
@TestMock
|
||||
lateinit var player: Player
|
||||
|
||||
@ParameterizedTest
|
||||
@ArgumentsSource(WoodcuttingTestDataProvider::class)
|
||||
fun `Attempting to cut a tree when the player has no axe should send a message`(data: WoodcuttingTestData) {
|
||||
player.interactWithObject(data.treeId, 1)
|
||||
|
||||
verify { player.sendMessage(contains("do not have an axe")) }
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ArgumentsSource(WoodcuttingTestDataProvider::class)
|
||||
fun `Attempting to cut a tree when the player is too low levelled should send a message`(data: WoodcuttingTestData) {
|
||||
assumeTrue(data.tree.level > 1, "Normal trees are covered by axe requirements")
|
||||
|
||||
player.inventory.add(Axe.BRONZE.id)
|
||||
player.skillSet.setCurrentLevel(Skill.WOODCUTTING, data.tree.level - 1)
|
||||
|
||||
player.interactWithObject(data.treeId, 1)
|
||||
|
||||
verifyAfter(action.complete()) { player.sendMessage(contains("do not have the required level")) }
|
||||
}
|
||||
|
||||
@Disabled("Mocking constructors is not supported in mockk. Update WoodcuttingAction to pass a chance value")
|
||||
@ParameterizedTest
|
||||
@ArgumentsSource(WoodcuttingTestDataProvider::class)
|
||||
fun `Cutting a tree we have the skill to cut should eventually reward logs and experience`(
|
||||
data: WoodcuttingTestData
|
||||
) {
|
||||
// Mock RNG instances used by mining internally to determine success
|
||||
// @todo - improve this so we don't have to mock Random
|
||||
val rng = mockk<Random>()
|
||||
every { rng.nextInt(100) } answers { 0 }
|
||||
|
||||
player.inventory.add(Axe.BRONZE.id)
|
||||
player.skillSet.setCurrentLevel(Skill.WOODCUTTING, data.tree.level)
|
||||
|
||||
player.interactWithObject(data.treeId, 1)
|
||||
|
||||
verifyAfter(action.ticks(1)) { player.sendMessage(contains("You swing your axe")) }
|
||||
|
||||
after(action.ticks(Axe.BRONZE.pulses)) {
|
||||
// @todo - cummulative ticks() calls?
|
||||
verify { player.sendMessage("You manage to cut some <tree_type>") }
|
||||
assertEquals(data.tree.exp, player.skillSet.getExperience(Skill.WOODCUTTING))
|
||||
assertEquals(1, player.inventory.getAmount(data.tree.id))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user