From 2ea0f68eb9ac2e51ec47612f6ebca359b547844d Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Tue, 4 Sep 2018 00:02:00 +0100 Subject: [PATCH] Add tests for most command plugins --- .../testing/junit/ApolloTestExtension.kt | 10 ++- game/plugin/cmd/test/AnimateCommandTests.kt | 28 ++++++ game/plugin/cmd/test/BankCommandTests.kt | 27 ++++++ game/plugin/cmd/test/BroadcastCommandTests.kt | 40 +++++++++ game/plugin/cmd/test/ItemCommandTests.kt | 42 +++++++++ game/plugin/cmd/test/LookupCommandTests.kt | 88 +++++++++++++++++++ .../java/org/apollo/game/model/Animation.java | 27 ++++++ 7 files changed, 258 insertions(+), 4 deletions(-) create mode 100644 game/plugin/cmd/test/AnimateCommandTests.kt create mode 100644 game/plugin/cmd/test/BankCommandTests.kt create mode 100644 game/plugin/cmd/test/BroadcastCommandTests.kt create mode 100644 game/plugin/cmd/test/ItemCommandTests.kt create mode 100644 game/plugin/cmd/test/LookupCommandTests.kt diff --git a/game/plugin-testing/src/main/kotlin/org/apollo/game/plugin/testing/junit/ApolloTestExtension.kt b/game/plugin-testing/src/main/kotlin/org/apollo/game/plugin/testing/junit/ApolloTestExtension.kt index c9942e41..d9fbfc51 100644 --- a/game/plugin-testing/src/main/kotlin/org/apollo/game/plugin/testing/junit/ApolloTestExtension.kt +++ b/game/plugin-testing/src/main/kotlin/org/apollo/game/plugin/testing/junit/ApolloTestExtension.kt @@ -79,17 +79,17 @@ class ApolloTestingExtension : createTestDefinitions( callables, companionInstance, ItemDefinition::getId, ItemDefinition::lookup, - ItemDefinition::getDefinitions + ItemDefinition::getDefinitions, ItemDefinition::count ) createTestDefinitions( callables, companionInstance, NpcDefinition::getId, NpcDefinition::lookup, - NpcDefinition::getDefinitions + NpcDefinition::getDefinitions, NpcDefinition::count ) createTestDefinitions( callables, companionInstance, ObjectDefinition::getId, ObjectDefinition::lookup, - ObjectDefinition::getDefinitions + ObjectDefinition::getDefinitions, ObjectDefinition::count ) } @@ -151,7 +151,8 @@ class ApolloTestingExtension : companionObjectInstance: Any?, crossinline idMapper: (D) -> Int, crossinline lookup: (Int) -> D?, - crossinline getAll: () -> Array + crossinline getAll: () -> Array, + crossinline count: () -> Int ) { val testDefinitions = findTestDefinitions(testClassMethods, companionObjectInstance) .associateBy(idMapper) @@ -162,6 +163,7 @@ class ApolloTestingExtension : every { lookup(capture(idSlot)) } answers { testDefinitions[idSlot.captured] } every { getAll() } answers { testDefinitions.values.sortedBy(idMapper).toTypedArray() } + every { count() } answers { _ -> testDefinitions.maxBy { (id, _) -> id }?.key?.let { it + 1 } ?: 0 } } } diff --git a/game/plugin/cmd/test/AnimateCommandTests.kt b/game/plugin/cmd/test/AnimateCommandTests.kt new file mode 100644 index 00000000..a226e1a2 --- /dev/null +++ b/game/plugin/cmd/test/AnimateCommandTests.kt @@ -0,0 +1,28 @@ +import io.mockk.verify +import org.apollo.game.command.Command +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.junit.ApolloTestingExtension +import org.apollo.game.plugin.testing.junit.api.annotations.TestMock +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith + +@ExtendWith(ApolloTestingExtension::class) +class AnimateCommandTests { + + @TestMock + lateinit var world: World + + @TestMock + lateinit var player: Player + + @Test + fun `Plays the animation provided as input`() { + player.privilegeLevel = PrivilegeLevel.MODERATOR + world.commandDispatcher.dispatch(player, Command("animate", arrayOf("1"))) + + verify { player.playAnimation(Animation(1)) } + } +} \ No newline at end of file diff --git a/game/plugin/cmd/test/BankCommandTests.kt b/game/plugin/cmd/test/BankCommandTests.kt new file mode 100644 index 00000000..4ef96051 --- /dev/null +++ b/game/plugin/cmd/test/BankCommandTests.kt @@ -0,0 +1,27 @@ +import io.mockk.verify +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.junit.ApolloTestingExtension +import org.apollo.game.plugin.testing.junit.api.annotations.TestMock +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith + +@ExtendWith(ApolloTestingExtension::class) +class BankCommandTests { + + @TestMock + lateinit var world: World + + @TestMock + lateinit var player: Player + + @Test + fun `Opens bank when used`() { + player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR + world.commandDispatcher.dispatch(player, Command("bank", emptyArray())) + + verify { player.openBank() } + } +} \ No newline at end of file diff --git a/game/plugin/cmd/test/BroadcastCommandTests.kt b/game/plugin/cmd/test/BroadcastCommandTests.kt new file mode 100644 index 00000000..a8bfb9e1 --- /dev/null +++ b/game/plugin/cmd/test/BroadcastCommandTests.kt @@ -0,0 +1,40 @@ +import io.mockk.verify +import io.mockk.verifyOrder +import io.mockk.verifySequence +import org.apollo.cache.def.ItemDefinition +import org.apollo.cache.def.NpcDefinition +import org.apollo.cache.def.ObjectDefinition +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.startsWith +import org.apollo.game.plugin.testing.assertions.strEq +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 +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.Assertions.assertEquals +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith + +@ExtendWith(ApolloTestingExtension::class) +class BroadcastCommandTests { + + @TestMock + lateinit var world: World + + @TestMock + lateinit var player: Player + + @Test + fun `Shows basic information on an item`() { + player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR + world.commandDispatcher.dispatch(player, Command("broadcast", arrayOf("msg"))) + + verify { + player.sendMessage("[Broadcast] Test: msg") + } + } +} \ No newline at end of file diff --git a/game/plugin/cmd/test/ItemCommandTests.kt b/game/plugin/cmd/test/ItemCommandTests.kt new file mode 100644 index 00000000..494b8e3d --- /dev/null +++ b/game/plugin/cmd/test/ItemCommandTests.kt @@ -0,0 +1,42 @@ +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.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 + +@ExtendWith(ApolloTestingExtension::class) +class ItemCommandTests { + + @TestMock + lateinit var world: World + + @TestMock + lateinit var player: Player + + @Test + fun `Defaults to an amount of 1`() { + player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR + world.commandDispatcher.dispatch(player, Command("item", arrayOf("1"))) + + assertEquals(1, player.inventory.getAmount(1)) + } + + @Test + fun `Adds item of specified amount to inventory`() { + player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR + world.commandDispatcher.dispatch(player, Command("item", arrayOf("1", "10"))) + + assertEquals(10, player.inventory.getAmount(1)) + } + + companion object { + @ItemDefinitions + val items = listOf(ItemDefinition(1)) + } +} \ No newline at end of file diff --git a/game/plugin/cmd/test/LookupCommandTests.kt b/game/plugin/cmd/test/LookupCommandTests.kt new file mode 100644 index 00000000..df442a40 --- /dev/null +++ b/game/plugin/cmd/test/LookupCommandTests.kt @@ -0,0 +1,88 @@ +import io.mockk.verify +import io.mockk.verifyOrder +import io.mockk.verifySequence +import org.apollo.cache.def.ItemDefinition +import org.apollo.cache.def.NpcDefinition +import org.apollo.cache.def.ObjectDefinition +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.startsWith +import org.apollo.game.plugin.testing.assertions.strEq +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 +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.Assertions.assertEquals +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith + +@ExtendWith(ApolloTestingExtension::class) +class LookupCommandTests { + + @TestMock + lateinit var world: World + + @TestMock + lateinit var player: Player + + @Test + fun `Shows basic information on an item`() { + player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR + world.commandDispatcher.dispatch(player, Command("iteminfo", arrayOf("1"))) + + verify { + player.sendMessage("Item 1 is called and is not members only.") + player.sendMessage("Its description is ``.") + } + } + + @Test + fun `Shows basic information on an npc`() { + player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR + world.commandDispatcher.dispatch(player, Command("npcinfo", arrayOf("1"))) + + verify { + player.sendMessage("Npc 1 is called and has a combat level of 126.") + player.sendMessage("Its description is ``.") + } + } + + @Test + fun `Shows basic information on an object`() { + player.privilegeLevel = PrivilegeLevel.ADMINISTRATOR + world.commandDispatcher.dispatch(player, Command("objectinfo", arrayOf("1"))) + + verify { + player.sendMessage("Object 1 is called (width=1, length=1).") + player.sendMessage("Its description is ``.") + } + } + + companion object { + @ItemDefinitions + val items = listOf(ItemDefinition(1).apply { + name = "" + description = "" + isMembersOnly = false + }) + + @NpcDefinitions + val npcs = listOf(NpcDefinition(1).apply { + name = "" + combatLevel = 126 + description = "" + }) + + @ObjectDefinitions + val objects = listOf(ObjectDefinition(1).apply { + name = "" + description = "" + width = 1 + length = 1 + }) + } + +} \ No newline at end of file diff --git a/game/src/main/java/org/apollo/game/model/Animation.java b/game/src/main/java/org/apollo/game/model/Animation.java index 57df88a9..0fe52750 100644 --- a/game/src/main/java/org/apollo/game/model/Animation.java +++ b/game/src/main/java/org/apollo/game/model/Animation.java @@ -1,5 +1,8 @@ package org.apollo.game.model; +import com.google.common.base.MoreObjects; +import com.google.common.base.Objects; + /** * Represents an animation. * @@ -60,4 +63,28 @@ public final class Animation { return id; } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Animation animation = (Animation) o; + return delay == animation.delay && id == animation.id; + } + + @Override + public int hashCode() { + return Objects.hashCode(delay, id); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("delay", delay) + .add("id", id) + .toString(); + } } \ No newline at end of file