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:
Gary Tierney
2018-08-19 22:28:41 +01:00
parent e255bd195e
commit 248a7d97d9
56 changed files with 1327 additions and 463 deletions
+17 -16
View File
@@ -9,12 +9,12 @@ import org.apollo.game.plugin.skills.fishing.FishingTool.*
/**
* A fish that can be gathered using the fishing skill.
*/
enum class Fish(val id: Int, val level: Int, val experience: Double) {
SHRIMP(id = 317, level = 1, experience = 10.0),
enum class Fish(val id: Int, val level: Int, val experience: Double, catchSuffix: String? = null) {
SHRIMPS(id = 317, level = 1, experience = 10.0, catchSuffix = "some shrimp."),
SARDINE(id = 327, level = 5, experience = 20.0),
MACKEREL(id = 353, level = 16, experience = 20.0),
HERRING(id = 345, level = 10, experience = 30.0),
ANCHOVY(id = 321, level = 15, experience = 40.0),
ANCHOVIES(id = 321, level = 15, experience = 40.0, catchSuffix = "some anchovies."),
TROUT(id = 335, level = 20, experience = 50.0),
COD(id = 341, level = 23, experience = 45.0),
PIKE(id = 349, level = 25, experience = 60.0),
@@ -23,13 +23,14 @@ enum class Fish(val id: Int, val level: Int, val experience: Double) {
LOBSTER(id = 377, level = 40, experience = 90.0),
BASS(id = 363, level = 46, experience = 100.0),
SWORDFISH(id = 371, level = 50, experience = 100.0),
SHARK(id = 383, level = 76, experience = 110.0);
SHARK(id = 383, level = 76, experience = 110.0, catchSuffix = "a shark!");
/**
* The name of this fish, formatted so it can be inserted into a message.
*/
val formattedName = Definitions.item(id)!!.name.toLowerCase()
// TODO this leads to incorrect messages, e.g. 'You catch a raw shrimps'.
val catchMessage = "You catch ${catchSuffix ?: "a ${catchName()}."}"
private fun catchName() = Definitions.item(id)!!.name.toLowerCase().removePrefix("raw ")
}
@@ -69,16 +70,7 @@ enum class FishingSpot(val npc: Int, private val first: Option, private val seco
ROD(309, Option.of(FLY_FISHING_ROD, TROUT, SALMON), Option.of(FISHING_ROD, PIKE)),
CAGE_HARPOON(312, Option.of(LOBSTER_CAGE, LOBSTER), Option.of(HARPOON, TUNA, SWORDFISH)),
NET_HARPOON(313, Option.of(BIG_NET, MACKEREL, COD), Option.of(HARPOON, BASS, SHARK)),
NET_ROD(316, Option.of(SMALL_NET, SHRIMP, ANCHOVY), Option.of(FISHING_ROD, SARDINE, HERRING));
companion object {
private val FISHING_SPOTS = FishingSpot.values().associateBy({ it.npc }, { it })
/**
* Returns the [FishingSpot] with the specified [id], or `null` if the spot does not exist.
*/
fun lookup(id: Int): FishingSpot? = FISHING_SPOTS[id]
}
NET_ROD(316, Option.of(SMALL_NET, SHRIMPS, ANCHOVIES), Option.of(FISHING_ROD, SARDINE, HERRING));
/**
* Returns the [FishingSpot.Option] associated with the specified action id.
@@ -159,4 +151,13 @@ enum class FishingSpot(val npc: Int, private val first: Option, private val seco
}
companion object {
private val FISHING_SPOTS = FishingSpot.values().associateBy(FishingSpot::npc)
/**
* Returns the [FishingSpot] with the specified [id], or `null` if the spot does not exist.
*/
fun lookup(id: Int): FishingSpot? = FISHING_SPOTS[id]
}
}
@@ -57,7 +57,7 @@ class FishingAction(player: Player, position: Position, val option: FishingSpot.
}
mob.inventory.add(fish.id)
mob.sendMessage("You catch a ${fish.formattedName}.")
mob.sendMessage(fish.catchMessage)
mob.fishing.experience += fish.experience
if (mob.inventory.freeSlots() == 0) {
+1 -1
View File
@@ -10,5 +10,5 @@ plugin {
"tlf30"
]
dependencies = ["api"]
dependencies = ["api", "util:lookup"]
}
+1 -1
View File
@@ -8,6 +8,6 @@ enum class Gem(val id: Int) { // TODO add gem drop chances
companion object {
private val GEMS = Gem.values().associateBy({ it.id }, { it })
fun lookup(id: Int): Gem? = GEMS[id]
operator fun get(id: Int): Gem? = GEMS[id]
}
}
+146
View File
@@ -0,0 +1,146 @@
import org.apollo.game.action.ActionBlock
import org.apollo.game.action.AsyncDistancedAction
import org.apollo.game.message.impl.ObjectActionMessage
import org.apollo.game.model.Position
import org.apollo.game.model.World
import org.apollo.game.model.entity.Player
import org.apollo.game.model.entity.obj.GameObject
import org.apollo.game.plugin.api.*
import org.apollo.game.plugin.skills.mining.Ore
import org.apollo.game.plugin.skills.mining.Pickaxe
import org.apollo.net.message.Message
import java.util.*
class MiningAction(
player: Player,
private val tool: Pickaxe,
private val target: MiningTarget
) : AsyncDistancedAction<Player>(PULSES, true, player, target.position, ORE_SIZE) {
companion object {
private const val PULSES = 0
private const val ORE_SIZE = 1
/**
* Starts a [MiningAction] for the specified [Player], terminating the [Message] that triggered it.
*/
fun start(message: ObjectActionMessage, player: Player, ore: Ore) {
val pickaxe = Pickaxe.bestFor(player)
if (pickaxe == null) {
player.sendMessage("You do not have a pickaxe for which you have the level to use.")
} else {
val target = MiningTarget(message.id, message.position, ore)
val action = MiningAction(player, pickaxe, target)
player.startAction(action)
}
message.terminate()
}
}
override fun action(): ActionBlock = {
mob.turnTo(position)
if (!target.skillRequirementsMet(mob)) {
mob.sendMessage("You do not have the required level to mine this rock.")
stop()
}
mob.sendMessage("You swing your pick at the rock.")
mob.playAnimation(tool.animation)
wait(tool.pulses)
if (!target.isValid(mob.world)) {
stop()
}
val successChance = rand(100)
if (target.isSuccessful(mob, successChance)) {
if (mob.inventory.freeSlots() == 0) {
mob.inventory.forceCapacityExceeded()
stop()
}
if (target.reward(mob)) {
mob.sendMessage("You manage to mine some ${target.oreName()}")
target.deplete(mob.world)
stop()
}
}
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as MiningAction
return mob == other.mob && target == other.target
}
override fun hashCode(): Int = Objects.hash(mob, target)
}
data class MiningTarget(val objectId: Int, val position: Position, val ore: Ore) {
/**
* Get the [GameObject] represented by this target.
*
* @todo: api: shouldn't be as verbose
*/
private fun getObject(world: World): GameObject? {
val region = world.regionRepository.fromPosition(position)
return region.findObject(position, objectId).orElse(null)
}
/**
* Deplete this mining resource from the [World], and schedule it to be respawned
* in a number of ticks specified by the [Ore].
*/
fun deplete(world: World) {
world.expireObject(getObject(world)!!, ore.objects[objectId]!!, ore.respawn)
}
/**
* Check if the [Player] was successful in mining this ore with a random success [chance] value between 0 and 100.
*/
fun isSuccessful(mob: Player, chance: Int): Boolean {
val percent = (ore.chance * mob.mining.current + ore.chanceOffset) * 100
return chance < percent
}
/**
* Check if this target is still valid in the [World] (i.e. has not been [deplete]d).
*/
fun isValid(world: World) = getObject(world) != null
/**
* Get the normalized name of the [Ore] represented by this target.
*/
fun oreName() = Definitions.item(ore.id)!!.name.toLowerCase()
/**
* Reward a [player] with experience and ore if they have the inventory capacity to take a new ore.
*/
fun reward(player: Player): Boolean {
val hasInventorySpace = player.inventory.add(ore.id)
if (hasInventorySpace) {
player.mining.experience += ore.exp
}
return hasInventorySpace
}
/**
* Check if the [mob] has met the skill requirements to mine te [Ore] represented by
* this [MiningTarget].
*/
fun skillRequirementsMet(mob: Player) = mob.mining.current < ore.level
}
@@ -1,19 +1,5 @@
import org.apollo.game.action.ActionBlock
import org.apollo.game.action.AsyncDistancedAction
import org.apollo.game.message.impl.ObjectActionMessage
import org.apollo.game.model.Position
import org.apollo.game.model.World
import org.apollo.game.model.entity.Player
import org.apollo.game.model.entity.obj.GameObject
import org.apollo.game.plugin.api.Definitions
import org.apollo.game.plugin.api.expireObject
import org.apollo.game.plugin.api.findObject
import org.apollo.game.plugin.api.mining
import org.apollo.game.plugin.api.rand
import org.apollo.game.plugin.skills.mining.Ore
import org.apollo.game.plugin.skills.mining.Pickaxe
import org.apollo.net.message.Message
import java.util.Objects
on { ObjectActionMessage::class }
.where { option == Actions.MINING }
@@ -35,99 +21,6 @@ on { ObjectActionMessage::class }
}
}
class MiningAction(
player: Player,
private val tool: Pickaxe,
private val target: MiningTarget
) : AsyncDistancedAction<Player>(PULSES, true, player, target.position, ORE_SIZE) {
companion object {
private const val PULSES = 0
private const val ORE_SIZE = 1
/**
* Starts a [MiningAction] for the specified [Player], terminating the [Message] that triggered it.
*/
fun start(message: ObjectActionMessage, player: Player, ore: Ore) {
val pickaxe = Pickaxe.bestFor(player)
if (pickaxe == null) {
player.sendMessage("You do not have a pickaxe for which you have the level to use.")
} else {
val target = MiningTarget(message.id, message.position, ore)
val action = MiningAction(player, pickaxe, target)
player.startAction(action)
}
message.terminate()
}
}
override fun action(): ActionBlock = {
mob.turnTo(position)
val level = mob.mining.current
if (level < target.ore.level) {
mob.sendMessage("You do not have the required level to mine this rock.")
stop()
}
mob.sendMessage("You swing your pick at the rock.")
mob.playAnimation(tool.animation)
wait(tool.pulses)
val obj = target.getObject(mob.world)
if (obj == null) {
stop()
}
if (target.isSuccessful(mob)) {
if (mob.inventory.freeSlots() == 0) {
mob.inventory.forceCapacityExceeded()
stop()
}
if (mob.inventory.add(target.ore.id)) {
val oreName = Definitions.item(target.ore.id)!!.name.toLowerCase()
mob.sendMessage("You manage to mine some $oreName")
mob.mining.experience += target.ore.exp
mob.world.expireObject(obj!!, target.ore.objects[target.objectId]!!, target.ore.respawn)
stop()
}
}
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as MiningAction
return mob == other.mob && target == other.target
}
override fun hashCode(): Int = Objects.hash(mob, target)
}
data class MiningTarget(val objectId: Int, val position: Position, val ore: Ore) {
fun getObject(world: World): GameObject? {
val region = world.regionRepository.fromPosition(position)
return region.findObject(position, objectId).orElse(null)
}
fun isSuccessful(mob: Player): Boolean {
val offset = if (ore.chanceOffset) 1 else 0
val percent = (ore.chance * mob.mining.current + offset) * 100
return rand(100) < percent
}
}
private object Actions {
const val MINING = 1
const val PROSPECTING = 2
+5 -5
View File
@@ -17,12 +17,12 @@ enum class Ore(
val exp: Double,
val respawn: Int,
val chance: Double,
val chanceOffset: Boolean = false
val chanceOffset: Double = 0.0
) {
CLAY(CLAY_OBJECTS, id = 434, level = 1, exp = 5.0, respawn = 1, chance = 0.0085, chanceOffset = true),
COPPER(COPPER_OBJECTS, id = 436, level = 1, exp = 17.5, respawn = 4, chance = 0.0085, chanceOffset = true),
TIN(TIN_OBJECTS, id = 438, level = 1, exp = 17.5, respawn = 4, chance = 0.0085, chanceOffset = true),
IRON(IRON_OBJECTS, id = 440, level = 15, exp = 35.0, respawn = 9, chance = 0.0085, chanceOffset = true),
CLAY(CLAY_OBJECTS, id = 434, level = 1, exp = 5.0, respawn = 1, chance = 0.0085, chanceOffset = 0.45),
COPPER(COPPER_OBJECTS, id = 436, level = 1, exp = 17.5, respawn = 4, chance = 0.0085, chanceOffset = 0.45),
TIN(TIN_OBJECTS, id = 438, level = 1, exp = 17.5, respawn = 4, chance = 0.0085, chanceOffset = 0.45),
IRON(IRON_OBJECTS, id = 440, level = 15, exp = 35.0, respawn = 9, chance = 0.0085, chanceOffset = 0.45),
COAL(COAL_OBJECTS, id = 453, level = 30, exp = 50.0, respawn = 50, chance = 0.004),
GOLD(GOLD_OBJECTS, id = 444, level = 40, exp = 65.0, respawn = 100, chance = 0.003),
SILVER(SILVER_OBJECTS, id = 442, level = 20, exp = 40.0, respawn = 100, chance = 0.0085),
+1 -1
View File
@@ -7,7 +7,7 @@ import org.apollo.game.plugin.api.mining
enum class Pickaxe(val id: Int, val level: Int, animation: Int, val pulses: Int) {
BRONZE(id = 1265, level = 1, animation = 625, pulses = 8),
ITRON(id = 1267, level = 1, animation = 626, pulses = 7),
STEEL(id = 1269, level = 1, animation = 627, pulses = 6),
STEEL(id = 1269, level = 6, animation = 627, pulses = 6),
MITHRIL(id = 1273, level = 21, animation = 629, pulses = 5),
ADAMANT(id = 1271, level = 31, animation = 628, pulses = 4),
RUNE(id = 1275, level = 41, animation = 624, pulses = 3);
@@ -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()
}
}
@@ -25,7 +25,7 @@ class BuryBoneAction(
}
companion object {
private val BURY_BONE_ANIMATION = Animation(827)
public val BURY_BONE_ANIMATION = Animation(827)
internal const val BURY_OPTION = 1
}
@@ -34,4 +34,4 @@ on { ItemOptionMessage::class }
player.startAction(BuryBoneAction(player, slot, bone))
terminate()
}
}
@@ -0,0 +1,72 @@
import BuryBoneAction.Companion.BURY_BONE_ANIMATION
import io.mockk.verify
import org.apollo.cache.def.ItemDefinition
import org.apollo.game.model.entity.Player
import org.apollo.game.plugin.api.prayer
import org.apollo.game.plugin.testing.assertions.after
import org.apollo.game.plugin.testing.assertions.startsWith
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.interactWithItem
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 BuryBoneTests {
@TestMock
lateinit var player: Player
@TestMock
lateinit var action: ActionCapture
@ItemDefinitions
fun bones(): Collection<ItemDefinition> {
return Bone.values().map { ItemDefinition(it.id) }
}
@ParameterizedTest
@EnumSource(value = Bone::class)
fun `Burying a bone should send a message`(bone: Bone) {
player.inventory.add(bone.id)
player.interactWithItem(bone.id, option = 1)
verifyAfter(action.ticks(1), "message is sent") {
player.sendMessage(startsWith("You dig a hole"))
}
}
@ParameterizedTest
@EnumSource(value = Bone::class)
fun `Burying a bone should play an animation`(bone: Bone) {
player.inventory.add(bone.id)
player.interactWithItem(bone.id, option = 1)
verifyAfter(action.ticks(1), "animation is played") {
player.playAnimation(eq(BURY_BONE_ANIMATION))
}
}
@ParameterizedTest
@EnumSource(value = Bone::class)
fun `Burying a bone should give the player experience`(bone: Bone) {
player.inventory.add(bone.id)
player.interactWithItem(bone.id, option = 1)
action.ticks(1)
after(action.complete(), "experience is granted after bone burial") {
verify { player.sendMessage(startsWith("You bury the bones")) }
assertEquals(bone.xp, player.prayer.experience)
assertEquals(player.inventory.getAmount(bone.id), 0)
}
}
}
@@ -90,10 +90,7 @@ class WoodcuttingAction(
wait(tool.pulses)
// Check that the object exists in the world
val obj = target.getObject(mob.world)
if (obj == null) {
stop()
}
val obj = target.getObject(mob.world) ?: stop()
if (mob.inventory.add(target.tree.id)) {
val logName = Definitions.item(target.tree.id)!!.name.toLowerCase()
@@ -105,7 +102,7 @@ class WoodcuttingAction(
// respawn time: http://runescape.wikia.com/wiki/Trees
val respawn = TimeUnit.SECONDS.toMillis(MINIMUM_RESPAWN_TIME + rand(150)) / GameConstants.PULSE_DELAY
mob.world.expireObject(obj!!, target.tree.stump, respawn.toInt())
mob.world.expireObject(obj, target.tree.stump, respawn.toInt())
stop()
}
}
@@ -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))
}
}
}