From fc1d6f46faa880bc3ca7e2a67625ab860bf1c49c Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Tue, 4 Sep 2018 02:35:35 +0100 Subject: [PATCH] Add additional command test coverage --- .../junit/api/annotations/stubAnnotations.kt | 1 + .../testing/junit/stubs/PlayerStubInfo.kt | 2 + game/plugin/cmd/src/teleport-cmd.plugin.kts | 20 ++++-- game/plugin/cmd/test/AnimateCommandTests.kt | 12 ++++ game/plugin/cmd/test/ItemCommandTests.kt | 15 ++++ game/plugin/cmd/test/LookupCommandTests.kt | 14 ++++ game/plugin/cmd/test/SkillCommandTests.kt | 30 ++++++++ game/plugin/cmd/test/TeleportCommandTests.kt | 70 +++++++++++++++++++ .../cmd/test/TeleportToPlayerCommandTests.kt | 53 ++++++++++++++ gradle/config/checkstyle.xml | 0 10 files changed, 210 insertions(+), 7 deletions(-) create mode 100644 game/plugin/cmd/test/SkillCommandTests.kt create mode 100644 game/plugin/cmd/test/TeleportCommandTests.kt create mode 100644 game/plugin/cmd/test/TeleportToPlayerCommandTests.kt create mode 100644 gradle/config/checkstyle.xml diff --git a/game/plugin-testing/src/main/kotlin/org/apollo/game/plugin/testing/junit/api/annotations/stubAnnotations.kt b/game/plugin-testing/src/main/kotlin/org/apollo/game/plugin/testing/junit/api/annotations/stubAnnotations.kt index 9166616f..2acf1636 100644 --- a/game/plugin-testing/src/main/kotlin/org/apollo/game/plugin/testing/junit/api/annotations/stubAnnotations.kt +++ b/game/plugin-testing/src/main/kotlin/org/apollo/game/plugin/testing/junit/api/annotations/stubAnnotations.kt @@ -2,6 +2,7 @@ package org.apollo.game.plugin.testing.junit.api.annotations annotation class Id(val value: Int) annotation class Pos(val x: Int, val y: Int, val height: Int = 0) +annotation class Name(val value: String) @Target(AnnotationTarget.FIELD) @Retention(AnnotationRetention.RUNTIME) diff --git a/game/plugin-testing/src/main/kotlin/org/apollo/game/plugin/testing/junit/stubs/PlayerStubInfo.kt b/game/plugin-testing/src/main/kotlin/org/apollo/game/plugin/testing/junit/stubs/PlayerStubInfo.kt index 95f4404a..3aae860c 100644 --- a/game/plugin-testing/src/main/kotlin/org/apollo/game/plugin/testing/junit/stubs/PlayerStubInfo.kt +++ b/game/plugin-testing/src/main/kotlin/org/apollo/game/plugin/testing/junit/stubs/PlayerStubInfo.kt @@ -1,6 +1,7 @@ package org.apollo.game.plugin.testing.junit.stubs 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 class PlayerStubInfo { @@ -10,6 +11,7 @@ class PlayerStubInfo { annotations.forEach { when (it) { + is Name -> info.name = it.value is Pos -> info.position = Position(it.x, it.y, it.height) } } diff --git a/game/plugin/cmd/src/teleport-cmd.plugin.kts b/game/plugin/cmd/src/teleport-cmd.plugin.kts index d51a964f..4cc6c8ad 100644 --- a/game/plugin/cmd/src/teleport-cmd.plugin.kts +++ b/game/plugin/cmd/src/teleport-cmd.plugin.kts @@ -30,6 +30,7 @@ on_command("tele", PrivilegeLevel.ADMINISTRATOR) if (results.isEmpty()) { player.sendMessage("No destinations matching '$query'.") + player.sendMessage(invalidSyntax) return@then } else if (results.size > 1) { player.sendMessage("Ambiguous query '$query' (could be $results). Please disambiguate.") @@ -84,15 +85,20 @@ on_command("teleto", PrivilegeLevel.ADMINISTRATOR) if (arguments.size == 1) { val playerName = arguments[0] - val foundPlayer = player.world.getPlayer(playerName) + try { + val foundPlayer = player.world.getPlayer(playerName) - if (foundPlayer == null) { - player.sendMessage("Player $playerName is currently offline or does not exist.") - return@then + if (foundPlayer == null) { + player.sendMessage("Player $playerName is currently offline or does not exist.") + 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 { player.sendMessage(invalidSyntax) } diff --git a/game/plugin/cmd/test/AnimateCommandTests.kt b/game/plugin/cmd/test/AnimateCommandTests.kt index a226e1a2..c52e72ba 100644 --- a/game/plugin/cmd/test/AnimateCommandTests.kt +++ b/game/plugin/cmd/test/AnimateCommandTests.kt @@ -4,6 +4,7 @@ import org.apollo.game.model.Animation 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.Test @@ -25,4 +26,15 @@ class AnimateCommandTests { verify { player.playAnimation(Animation(1)) } } + + @Test + fun `Help message sent on invalid syntax`() { + player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR + world.commandDispatcher.dispatch(player, Command("animate", arrayOf(""))) + + verify { + player.sendMessage(contains("Invalid syntax")) + } + } + } \ No newline at end of file diff --git a/game/plugin/cmd/test/ItemCommandTests.kt b/game/plugin/cmd/test/ItemCommandTests.kt index 494b8e3d..c9884105 100644 --- a/game/plugin/cmd/test/ItemCommandTests.kt +++ b/game/plugin/cmd/test/ItemCommandTests.kt @@ -1,14 +1,18 @@ +import io.mockk.verify import org.apollo.cache.def.ItemDefinition 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.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.ItemDefinitions 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 ItemCommandTests { @@ -35,6 +39,17 @@ class ItemCommandTests { assertEquals(10, player.inventory.getAmount(1)) } + @ParameterizedTest(name = "::item {0}") + @ValueSource(strings = ["", "1 ", " 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 { @ItemDefinitions val items = listOf(ItemDefinition(1)) diff --git a/game/plugin/cmd/test/LookupCommandTests.kt b/game/plugin/cmd/test/LookupCommandTests.kt index 0372d9c3..73208f8d 100644 --- a/game/plugin/cmd/test/LookupCommandTests.kt +++ b/game/plugin/cmd/test/LookupCommandTests.kt @@ -6,6 +6,7 @@ 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.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.ItemDefinitions 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.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 LookupCommandTests { @@ -56,6 +59,17 @@ class LookupCommandTests { } } + @ParameterizedTest(name = "::{0} ") + @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(""))) + + verify { + player.sendMessage(contains("Invalid syntax")) + } + } + companion object { @ItemDefinitions val items = listOf(ItemDefinition(1).apply { diff --git a/game/plugin/cmd/test/SkillCommandTests.kt b/game/plugin/cmd/test/SkillCommandTests.kt new file mode 100644 index 00000000..3dd28af5 --- /dev/null +++ b/game/plugin/cmd/test/SkillCommandTests.kt @@ -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)) + } + } +} \ No newline at end of file diff --git a/game/plugin/cmd/test/TeleportCommandTests.kt b/game/plugin/cmd/test/TeleportCommandTests.kt new file mode 100644 index 00000000..16571fd1 --- /dev/null +++ b/game/plugin/cmd/test/TeleportCommandTests.kt @@ -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 ", + "1 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")) + } + } + +} \ No newline at end of file diff --git a/game/plugin/cmd/test/TeleportToPlayerCommandTests.kt b/game/plugin/cmd/test/TeleportToPlayerCommandTests.kt new file mode 100644 index 00000000..6b4e2864 --- /dev/null +++ b/game/plugin/cmd/test/TeleportToPlayerCommandTests.kt @@ -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 = [ + "" + ]) + 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")) + } + } + +} \ No newline at end of file diff --git a/gradle/config/checkstyle.xml b/gradle/config/checkstyle.xml new file mode 100644 index 00000000..e69de29b