From 912eccbbfb84f63029d37163eaa429ad28a6682c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berke=20Ak=C3=A7en?= Date: Tue, 23 Jan 2024 13:14:21 +0300 Subject: [PATCH] Added tests for custom parameters --- .../test/CommandRegistrationTest.java | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/test/java/me/despical/commandframework/test/CommandRegistrationTest.java b/src/test/java/me/despical/commandframework/test/CommandRegistrationTest.java index 10f1a5d..4a7fabc 100644 --- a/src/test/java/me/despical/commandframework/test/CommandRegistrationTest.java +++ b/src/test/java/me/despical/commandframework/test/CommandRegistrationTest.java @@ -48,7 +48,7 @@ public void setUp() { @Test void testCommandRegistration() { CommandFramework commandFramework = createCommandFramework(); - assertEquals(2, commandFramework.getCommands().size()); + assertEquals(3, commandFramework.getCommands().size()); } /** @@ -82,7 +82,12 @@ void testCommandExecutionByPlayer() { player.performCommand("secondAlias"); player.assertSaid("§cRequired argument length is less or greater than needed!"); + // no command arguments player.performCommand("nocommandargs"); + + // custom parameters + player.performCommand("customargs test"); + player.assertSaid("First parameter is test"); } @AfterEach @@ -97,6 +102,7 @@ public void tearDown() { private CommandFramework createCommandFramework() { CommandFramework commandFramework = new CommandFrameworkMock(plugin); commandFramework.registerCommands(new ExampleCommand()); + commandFramework.addCustomParameter(String.class, arguments -> arguments.getArgument(0)); return commandFramework; } @@ -130,9 +136,22 @@ public void noCommandArgsTest() { Logger.getLogger(this.getClass().getSimpleName()).info("This command is annotated with @NoCommandArguments to run without required parameters."); } + @Command( + name = "customargs", + min = 1 + ) + // Do not forget to annotate with @CustomParameters; otherwise, the method won't be registered. + @CustomParameters + public void customParamCommand(String firstParameter, CommandArguments arguments) { + CommandSender sender = arguments.getSender(); + // Check if arguments are empty; otherwise, firstParameter will return null. + // CommandArguments parameter can be added to anywhere in method as a parameter. + sender.sendMessage("First parameter is " + firstParameter); + } + @Completer( - name = "example" - , aliases = {"firstAlias", "secondAlias"} + name = "example", + aliases = {"firstAlias", "secondAlias"} ) public List exampleCommandCompletion(CommandArguments arguments) { return Arrays.asList("first", "second", "third");