From 07a4ba50ec11c1c10d825ee015490a712eb2e7aa Mon Sep 17 00:00:00 2001 From: BuildTools <46540330+willkroboth@users.noreply.github.com> Date: Mon, 17 Jun 2024 04:50:58 -0400 Subject: [PATCH] Add ArgumentListabilityTests --- .../arguments/ArgumentListabilityTests.java | 134 ++++++++++++++++++ .../DuplicateNodeNameExceptionTests.java | 14 ++ 2 files changed, 148 insertions(+) create mode 100644 commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/arguments/ArgumentListabilityTests.java diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/arguments/ArgumentListabilityTests.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/arguments/ArgumentListabilityTests.java new file mode 100644 index 0000000000..86b5ae21f3 --- /dev/null +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/arguments/ArgumentListabilityTests.java @@ -0,0 +1,134 @@ +package dev.jorel.commandapi.test.arguments; + +import com.mojang.brigadier.tree.CommandNode; +import dev.jorel.commandapi.CommandAPICommand; +import dev.jorel.commandapi.arguments.*; +import dev.jorel.commandapi.test.Mut; +import dev.jorel.commandapi.test.TestBase; +import org.bukkit.entity.Player; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Tests for the effects of the {@link AbstractArgument#setListed(boolean)} method. + * Arguments may use different {@link CommandNode} implementation that implement this with separate code. + */ +public class ArgumentListabilityTests extends TestBase { + + /********* + * Setup * + *********/ + + @BeforeEach + public void setUp() { + super.setUp(); + } + + @AfterEach + public void tearDown() { + super.tearDown(); + } + + /********* + * Tests * + *********/ + + @Test + void testUnlistedRequiredArguments() { + Mut results = Mut.of(); + Player sender = server.addPlayer(); + + // Unlisted arguments before or after should not interfere with parsing + new CommandAPICommand("test") + .withArguments( + new StringArgument("number").setListed(false), + new IntegerArgument("number"), + new StringArgument("number").setListed(false) + ) + .executes(info -> { + results.set(info.args().getUnchecked("number")); + }) + .register(); + + assertStoresResult(sender, "test abc 10 def", results, 10); + assertStoresResult(sender, "test 0 1 2", results, 1); + + assertNoMoreResults(results); + } + + @Test + void testUnlistedLiterals() { + Mut results = Mut.of(); + + // Default unlisted literals should not overwrite parsed results + new CommandAPICommand("test") + .withArguments( + new LiteralArgument("number"), + new IntegerArgument("number"), + new LiteralArgument("number") + ) + .executesPlayer(info -> { + results.set(info.args().getUnchecked("number")); + }) + .register(); + + Player player = server.addPlayer(); + + // Unlisted MultiLiterals before or after should not affect the result given + assertStoresResult(player, "test number 0 number", results, 0); + assertStoresResult(player, "test number 10 number", results, 10); + + assertNoMoreResults(results); + } + + @Test + void testUnlistedMultiLiterals() { + Mut results = Mut.of(); + + // This command is valid because the listed arguments do not repeat names + new CommandAPICommand("test") + .withArguments( + new MultiLiteralArgument("literal", "a", "b", "c").setListed(false), + new MultiLiteralArgument("literal", "c", "d", "e"), + new MultiLiteralArgument("literal", "f", "g", "h").setListed(false) + ) + .executesPlayer(info -> { + results.set(info.args().getUnchecked("literal")); + }) + .register(); + + Player player = server.addPlayer(); + + // Unlisted MultiLiterals before or after should not affect the result given + assertStoresResult(player, "test a c f", results, "c"); + assertStoresResult(player, "test b d g", results, "d"); + assertStoresResult(player, "test c e h", results, "e"); + + assertNoMoreResults(results); + } + + @Test + void testUnlistedPreviewableArguments() { + Mut results = Mut.of(); + Player sender = server.addPlayer(); + + // Unlisted arguments before or after should not interfere with parsing + // Though all current Previewable arguments are also Greedy, so you can't + // have an argument after it anyway ¯\_(ツ)_/¯ + new CommandAPICommand("test") + .withArguments( + new IntegerArgument("number"), + new ChatArgument("number").setListed(false) + ) + .executes(info -> { + results.set(info.args().getUnchecked("number")); + }) + .register(); + + assertStoresResult(sender, "test 10 def", results, 10); + assertStoresResult(sender, "test 1 2", results, 1); + + assertNoMoreResults(results); + } +} diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/exceptions/DuplicateNodeNameExceptionTests.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/exceptions/DuplicateNodeNameExceptionTests.java index 58de4dae4b..6aa86b2290 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/exceptions/DuplicateNodeNameExceptionTests.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/exceptions/DuplicateNodeNameExceptionTests.java @@ -3,6 +3,7 @@ import dev.jorel.commandapi.CommandAPICommand; import dev.jorel.commandapi.CommandTree; import dev.jorel.commandapi.arguments.LiteralArgument; +import dev.jorel.commandapi.arguments.MultiLiteralArgument; import dev.jorel.commandapi.arguments.StringArgument; import dev.jorel.commandapi.exceptions.DuplicateNodeNameException; import dev.jorel.commandapi.test.TestBase; @@ -95,6 +96,19 @@ void testCommandAPICommandWithLiteralArguments() { "Duplicate node names for listed arguments are not allowed! Going down the [test alice bob] branch, found alice, which had a duplicated node name", commandWithDuplicateListedLiteralArgumentNames::register ); + + CommandAPICommand command = new CommandAPICommand("test") + .withArguments( + new MultiLiteralArgument("literal", "a", "b", "c"), + new MultiLiteralArgument("literal", "c", "d", "e") + ) + .executesPlayer(P_EXEC); + + assertThrowsWithMessage( + DuplicateNodeNameException.class, + "Duplicate node names for listed arguments are not allowed! Going down the [test literal] branch, found literal, which had a duplicated node name", + command::register + ); } @Test