Add additional command test coverage

This commit is contained in:
Gary Tierney
2018-09-04 02:35:35 +01:00
parent 21fea83c8e
commit fc1d6f46fa
10 changed files with 210 additions and 7 deletions
@@ -2,6 +2,7 @@ package org.apollo.game.plugin.testing.junit.api.annotations
annotation class Id(val value: Int) annotation class Id(val value: Int)
annotation class Pos(val x: Int, val y: Int, val height: Int = 0) annotation class Pos(val x: Int, val y: Int, val height: Int = 0)
annotation class Name(val value: String)
@Target(AnnotationTarget.FIELD) @Target(AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME) @Retention(AnnotationRetention.RUNTIME)
@@ -1,6 +1,7 @@
package org.apollo.game.plugin.testing.junit.stubs package org.apollo.game.plugin.testing.junit.stubs
import org.apollo.game.model.Position import org.apollo.game.model.Position
import org.apollo.game.plugin.testing.junit.api.annotations.Name
import org.apollo.game.plugin.testing.junit.api.annotations.Pos import org.apollo.game.plugin.testing.junit.api.annotations.Pos
class PlayerStubInfo { class PlayerStubInfo {
@@ -10,6 +11,7 @@ class PlayerStubInfo {
annotations.forEach { annotations.forEach {
when (it) { when (it) {
is Name -> info.name = it.value
is Pos -> info.position = Position(it.x, it.y, it.height) is Pos -> info.position = Position(it.x, it.y, it.height)
} }
} }
+13 -7
View File
@@ -30,6 +30,7 @@ on_command("tele", PrivilegeLevel.ADMINISTRATOR)
if (results.isEmpty()) { if (results.isEmpty()) {
player.sendMessage("No destinations matching '$query'.") player.sendMessage("No destinations matching '$query'.")
player.sendMessage(invalidSyntax)
return@then return@then
} else if (results.size > 1) { } else if (results.size > 1) {
player.sendMessage("Ambiguous query '$query' (could be $results). Please disambiguate.") player.sendMessage("Ambiguous query '$query' (could be $results). Please disambiguate.")
@@ -84,15 +85,20 @@ on_command("teleto", PrivilegeLevel.ADMINISTRATOR)
if (arguments.size == 1) { if (arguments.size == 1) {
val playerName = arguments[0] val playerName = arguments[0]
val foundPlayer = player.world.getPlayer(playerName) try {
val foundPlayer = player.world.getPlayer(playerName)
if (foundPlayer == null) { if (foundPlayer == null) {
player.sendMessage("Player $playerName is currently offline or does not exist.") player.sendMessage("Player $playerName is currently offline or does not exist.")
return@then return@then
}
player.teleport(foundPlayer.position)
player.sendMessage("You have teleported to player $playerName.")
} catch (_: Exception) {
// Invalid player name syntax
player.sendMessage(invalidSyntax)
} }
player.teleport(foundPlayer.position)
player.sendMessage("You have teleported to player $playerName.")
} else { } else {
player.sendMessage(invalidSyntax) player.sendMessage(invalidSyntax)
} }
@@ -4,6 +4,7 @@ import org.apollo.game.model.Animation
import org.apollo.game.model.World import org.apollo.game.model.World
import org.apollo.game.model.entity.Player import org.apollo.game.model.entity.Player
import org.apollo.game.model.entity.setting.PrivilegeLevel import org.apollo.game.model.entity.setting.PrivilegeLevel
import org.apollo.game.plugin.testing.assertions.contains
import org.apollo.game.plugin.testing.junit.ApolloTestingExtension import org.apollo.game.plugin.testing.junit.ApolloTestingExtension
import org.apollo.game.plugin.testing.junit.api.annotations.TestMock import org.apollo.game.plugin.testing.junit.api.annotations.TestMock
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
@@ -25,4 +26,15 @@ class AnimateCommandTests {
verify { player.playAnimation(Animation(1)) } verify { player.playAnimation(Animation(1)) }
} }
@Test
fun `Help message sent on invalid syntax`() {
player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR
world.commandDispatcher.dispatch(player, Command("animate", arrayOf("<garbage>")))
verify {
player.sendMessage(contains("Invalid syntax"))
}
}
} }
+15
View File
@@ -1,14 +1,18 @@
import io.mockk.verify
import org.apollo.cache.def.ItemDefinition import org.apollo.cache.def.ItemDefinition
import org.apollo.game.command.Command import org.apollo.game.command.Command
import org.apollo.game.model.World import org.apollo.game.model.World
import org.apollo.game.model.entity.Player import org.apollo.game.model.entity.Player
import org.apollo.game.model.entity.setting.PrivilegeLevel import org.apollo.game.model.entity.setting.PrivilegeLevel
import org.apollo.game.plugin.testing.assertions.contains
import org.apollo.game.plugin.testing.junit.ApolloTestingExtension 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.ItemDefinitions
import org.apollo.game.plugin.testing.junit.api.annotations.TestMock import org.apollo.game.plugin.testing.junit.api.annotations.TestMock
import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
@ExtendWith(ApolloTestingExtension::class) @ExtendWith(ApolloTestingExtension::class)
class ItemCommandTests { class ItemCommandTests {
@@ -35,6 +39,17 @@ class ItemCommandTests {
assertEquals(10, player.inventory.getAmount(1)) assertEquals(10, player.inventory.getAmount(1))
} }
@ParameterizedTest(name = "::item {0}")
@ValueSource(strings = ["<garbage>", "1 <garbage>", "<garbage> 1"])
fun `Help message sent on invalid syntax`(args: String) {
player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR
world.commandDispatcher.dispatch(player, Command("item", args.split(" ").toTypedArray()))
verify {
player.sendMessage(contains("Invalid syntax"))
}
}
companion object { companion object {
@ItemDefinitions @ItemDefinitions
val items = listOf(ItemDefinition(1)) val items = listOf(ItemDefinition(1))
@@ -6,6 +6,7 @@ import org.apollo.game.command.Command
import org.apollo.game.model.World import org.apollo.game.model.World
import org.apollo.game.model.entity.Player import org.apollo.game.model.entity.Player
import org.apollo.game.model.entity.setting.PrivilegeLevel import org.apollo.game.model.entity.setting.PrivilegeLevel
import org.apollo.game.plugin.testing.assertions.contains
import org.apollo.game.plugin.testing.junit.ApolloTestingExtension 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.ItemDefinitions
import org.apollo.game.plugin.testing.junit.api.annotations.NpcDefinitions import org.apollo.game.plugin.testing.junit.api.annotations.NpcDefinitions
@@ -13,6 +14,8 @@ import org.apollo.game.plugin.testing.junit.api.annotations.ObjectDefinitions
import org.apollo.game.plugin.testing.junit.api.annotations.TestMock import org.apollo.game.plugin.testing.junit.api.annotations.TestMock
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
@ExtendWith(ApolloTestingExtension::class) @ExtendWith(ApolloTestingExtension::class)
class LookupCommandTests { class LookupCommandTests {
@@ -56,6 +59,17 @@ class LookupCommandTests {
} }
} }
@ParameterizedTest(name = "::{0} <garbage>")
@ValueSource(strings = ["npcinfo", "iteminfo", "objectinfo"])
fun `Help message sent on invalid syntax`(command: String) {
player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR
world.commandDispatcher.dispatch(player, Command(command, arrayOf("<garbage>")))
verify {
player.sendMessage(contains("Invalid syntax"))
}
}
companion object { companion object {
@ItemDefinitions @ItemDefinitions
val items = listOf(ItemDefinition(1).apply { val items = listOf(ItemDefinition(1).apply {
+30
View File
@@ -0,0 +1,30 @@
import org.apollo.game.command.Command
import org.apollo.game.model.World
import org.apollo.game.model.entity.Player
import org.apollo.game.model.entity.Skill
import org.apollo.game.model.entity.setting.PrivilegeLevel
import org.apollo.game.plugin.testing.junit.ApolloTestingExtension
import org.apollo.game.plugin.testing.junit.api.annotations.TestMock
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
@ExtendWith(ApolloTestingExtension::class)
class SkillCommandTests {
@TestMock
lateinit var world: World
@TestMock
lateinit var player: Player
@Test
fun `Max stats to 99`() {
player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR
world.commandDispatcher.dispatch(player, Command("max", emptyArray()))
for (stat in 0 until Skill.RUNECRAFT) {
assertEquals(99, player.skillSet.getCurrentLevel(99))
}
}
}
@@ -0,0 +1,70 @@
import io.mockk.verify
import org.apollo.game.command.Command
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.setting.PrivilegeLevel
import org.apollo.game.plugin.testing.assertions.contains
import org.apollo.game.plugin.testing.junit.ApolloTestingExtension
import org.apollo.game.plugin.testing.junit.api.annotations.TestMock
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
@ExtendWith(ApolloTestingExtension::class)
class TeleportCommandTests {
@TestMock
lateinit var world: World
@TestMock
lateinit var player: Player
@Test
fun `Teleport to given coordinates`() {
player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR
world.commandDispatcher.dispatch(player, Command("tele", arrayOf("1", "2", "0")))
assertEquals(Position(1, 2, 0), player.position)
}
@Test
fun `Teleport to given coordinates on players plane when no plane given`() {
player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR
player.position = Position(1, 1, 1)
world.commandDispatcher.dispatch(player, Command("tele", arrayOf("1", "2")))
assertEquals(Position(1, 2, 1), player.position)
}
@Test
fun `Shows current position information`() {
player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR
player.position = Position(1, 2, 3)
world.commandDispatcher.dispatch(player, Command("pos", emptyArray()))
verify {
player.sendMessage(contains("1, 2, 3"))
}
}
@ParameterizedTest(name = "::tele {0}")
@ValueSource(strings = [
"1 2 <garbage>",
"1 <garbage> 2",
"1",
"1 2 3 4"
])
fun `Help message sent on invalid syntax`(args: String) {
player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR
world.commandDispatcher.dispatch(player, Command("tele", args.split(" ").toTypedArray()))
verify {
player.sendMessage(contains("Invalid syntax"))
}
}
}
@@ -0,0 +1,53 @@
import io.mockk.verify
import org.apollo.game.command.Command
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.setting.PrivilegeLevel
import org.apollo.game.plugin.testing.assertions.contains
import org.apollo.game.plugin.testing.junit.ApolloTestingExtension
import org.apollo.game.plugin.testing.junit.api.annotations.Name
import org.apollo.game.plugin.testing.junit.api.annotations.TestMock
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
@ExtendWith(ApolloTestingExtension::class)
class TeleportToPlayerCommandTests {
@TestMock
lateinit var world: World
@TestMock
lateinit var player: Player
@TestMock
@Name("player_two")
lateinit var secondPlayer: Player
@Test
fun `Teleport to given player`() {
player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR
player.position = Position(300, 300)
secondPlayer.position = Position(1, 2)
world.commandDispatcher.dispatch(player, Command("teleto", arrayOf("player_two")))
assertEquals(secondPlayer.position, player.position)
}
@ParameterizedTest(name = "::teleto {0}")
@ValueSource(strings = [
"<garbage_invalid_playername>"
])
fun `Help message sent on invalid syntax`(args: String) {
player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR
world.commandDispatcher.dispatch(player, Command("teleto", args.split(" ").toTypedArray()))
verify {
player.sendMessage(contains("Invalid syntax"))
}
}
}
View File