Test sender conversion for players

Also refactored stuff I deprecated
This commit is contained in:
Norbi Peti 2024-03-16 01:24:43 +01:00
parent a39539ea6e
commit 7996426d63
3 changed files with 61 additions and 19 deletions

View file

@ -8,6 +8,7 @@ import buttondevteam.lib.chat.ICommand2MC
import buttondevteam.lib.player.ChromaGamerBase
import buttondevteam.lib.player.TBMCPlayer
import org.bukkit.OfflinePlayer
import org.bukkit.entity.Player
abstract class Command2MCCommands {
@CommandClass(helpText = ["Test command", "Used for testing"])
@ -117,6 +118,11 @@ abstract class Command2MCCommands {
fun def(sender: OfflinePlayer, testInt: Int?, testLong: Long?, testDouble: Double?, testDouble2: Double) {
testCommandReceived = "$testInt $testLong $testDouble $testDouble2 ${sender.name}"
}
@Command2.Subcommand
fun fail(sender: Player) {
testCommandReceived = sender.name
}
}
@CommandClass
@ -132,6 +138,16 @@ abstract class Command2MCCommands {
}
}
@CommandClass
object TestSenderConversionCommand : ICommand2MC(), ITestCommand2MC {
override var testCommandReceived: String? = null
@Command2.Subcommand
fun def(sender: Player) {
testCommandReceived = sender.name
}
}
interface ITestCommand2MC {
var testCommandReceived: String?
}

View file

@ -9,6 +9,7 @@ import buttondevteam.lib.chat.commands.CommandUtils.coreExecutable
import buttondevteam.lib.chat.test.Command2MCCommands.*
import buttondevteam.lib.player.ChromaGamerBase
import buttondevteam.lib.player.TBMCPlayer
import buttondevteam.lib.player.TBMCPlayerBase
import org.junit.jupiter.api.MethodOrderer
import org.junit.jupiter.api.Order
import org.junit.jupiter.api.Test
@ -55,14 +56,13 @@ class Command2MCTest {
assertEquals(Command2MCSender::class.java, coreExecutable?.data?.senderType, "The sender's type doesn't seem to be stored correctly")
NoArgTestCommand.register()
val errCmd = ErroringTestCommand
assertEquals("No sender parameter for method '${errCmd::class.java.getMethod("def")}'", assertFails { errCmd.register() }.message)
ErroringTestCommand.registerAssertFail("No sender parameter for method '${ErroringTestCommand::class.java.getMethod("def")}'")
MultiArgTestCommand.register()
TestNoMainCommand1.register()
TestNoMainCommand2.register()
TestParamsCommand.register()
assertEquals("There are no subcommands defined in the command class TestEmptyCommand!", assertFails { TestEmptyCommand.register() }.message)
TestEmptyCommand.registerAssertFail("There are no subcommands defined in the command class TestEmptyCommand!")
}
@Test
@ -90,7 +90,8 @@ class Command2MCTest {
"/test errortest\n" +
"/test playerfail\n" +
"/test plugin\n" +
"/testparams", ButtonPlugin.command2MC.getCommandList(sender).joinToString("\n")
"/testparams\n" +
"/testparams fail", ButtonPlugin.command2MC.getCommandList(sender).joinToString("\n")
)
}
@ -109,6 +110,15 @@ class Command2MCTest {
@Test
@Order(5)
fun testSenderConversion() {
TestSenderConversionCommand.register()
val sender = createPlayer()
sender.assertCommand("/test something", TestCommand, "something")
sender.assertCommand("/testsenderconversion", TestSenderConversionCommand, "Player2")
}
@Test
@Order(6)
fun testHasPermission() {
}
@ -118,6 +128,12 @@ class Command2MCTest {
return TestCommand2MCSender(user)
}
private fun createPlayer(): TestCommand2MCSender {
val player = MockBukkit.getMock().addPlayer()
val user = TBMCPlayerBase.getPlayer(player.uniqueId, TBMCPlayer::class.java)
return TestCommand2MCSender(user)
}
/**
* Tests parameter conversion, help text and errors.
*/
@ -127,13 +143,16 @@ class Command2MCTest {
sender.assertCrashingCommand("/test playerfail TestPlayer") { it.cause?.message == "No suitable converter found for class buttondevteam.lib.player.TBMCPlayer param1" }
sender.assertCommandUserError("/test plugin asd", "§cError: §cNo Chroma plugin found by that name.")
sender.assertCrashingCommand("/test errortest") { it.cause?.cause?.message === "Hmm" }
assertEquals(
"Test command\n" +
"Used for testing\n" +
"§6---- Subcommands ----\n" +
"/test errortest\n" +
"/test playerfail\n" +
"/test plugin", sender.runCommandWithReceive("/test")
sender.assertCommandReceiveMessage(
"/test",
"""
Test command
Used for testing
§6---- Subcommands ----
/test errortest
/test playerfail
/test plugin
""".trimIndent()
)
}
@ -164,8 +183,9 @@ class Command2MCTest {
*/
private fun testTestParamsCommand(sender: TestCommand2MCSender) {
sender.assertCommand("/testparams 12 34 56 78", TestParamsCommand, "12 34 56.0 78.0 Player0")
assertEquals("§cExpected integer at position 11: ...estparams <--[HERE]", sender.runCommandWithReceive("/testparams asd 34 56 78"))
sender.assertCommandReceiveMessage("/testparams asd 34 56 78", "§cExpected integer at position 11: ...estparams <--[HERE]\n§6---- Subcommands ----\n/testparams fail")
// TODO: Change test when usage help is added
sender.assertCommandUserError("/testparams fail", "§cYou need to be a player to use this command.")
}
/**
@ -174,10 +194,12 @@ class Command2MCTest {
private fun testSomeCommand(sender: TestCommand2MCSender) {
sender.assertCommand("/some test cmd", TestNoMainCommand1, "TestPlayer")
sender.assertCommand("/some another cmd", TestNoMainCommand2, "TestPlayer")
assertEquals(
"§6---- Subcommands ----\n" +
"/some another cmd\n" +
"/some test cmd", sender.runCommandWithReceive("/some")
sender.assertCommandReceiveMessage(
"/some", """
§6---- Subcommands ----
/some another cmd
/some test cmd
""".trimIndent()
)
}
@ -185,6 +207,13 @@ class Command2MCTest {
MainPlugin.instance.registerCommand(this)
}
/**
* Attempts to register a command, expecting the given error (exception) message.
*/
private fun ICommand2MC.registerAssertFail(expected: String) {
assertEquals(expected, assertFails { this.register() }.message)
}
companion object {
private var initialized = false
}

View file

@ -1,3 +0,0 @@
package buttondevteam.lib.chat.test
class TestConvertedUser(val name: String)