From d47e761bf7c51712c21bb1fc7a9c8a49fc6866b4 Mon Sep 17 00:00:00 2001 From: TheMode Date: Sat, 2 Jul 2022 02:58:24 +0200 Subject: [PATCH] Allow testing for argument values + some utils Signed-off-by: TheMode --- .../server/command/CommandExecutionTest.java | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/src/test/java/net/minestom/server/command/CommandExecutionTest.java b/src/test/java/net/minestom/server/command/CommandExecutionTest.java index 5c2e11d60..18828ce30 100644 --- a/src/test/java/net/minestom/server/command/CommandExecutionTest.java +++ b/src/test/java/net/minestom/server/command/CommandExecutionTest.java @@ -2,14 +2,18 @@ package net.minestom.server.command; import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.arguments.Argument; -import net.minestom.server.command.builder.arguments.ArgumentType; import org.junit.jupiter.api.Test; +import java.lang.String; import java.util.List; +import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; +import static net.minestom.server.command.builder.arguments.ArgumentType.Boolean; +import static net.minestom.server.command.builder.arguments.ArgumentType.Integer; +import static net.minestom.server.command.builder.arguments.ArgumentType.String; import static org.junit.jupiter.api.Assertions.*; public class CommandExecutionTest { @@ -27,7 +31,7 @@ public class CommandExecutionTest { boolean bool1 = context.get("bool1"); boolean bool2 = context.get("bool2"); bool.set(bool1 && bool2); - }, ArgumentType.Boolean("bool1"), ArgumentType.Boolean("bool2")); + }, Boolean("bool1"), Boolean("bool2")); assertFalse(bool.get()); @@ -70,15 +74,23 @@ public class CommandExecutionTest { @Test public void singleInteger() { - List>> args = List.of( - List.of(ArgumentType.Integer("number")) - ); - assertSyntax(args, "5", ExpectedExecution.FIRST_SYNTAX); - assertSyntax(args, "5 5", ExpectedExecution.DEFAULT); - assertSyntax(args, "", ExpectedExecution.DEFAULT); + List> args = List.of(Integer("number")); + assertSingleSyntax(args, "5", ExpectedExecution.FIRST_SYNTAX, Map.of("number", 5)); + assertSingleSyntax(args, "5 5", ExpectedExecution.DEFAULT); + assertSingleSyntax(args, "", ExpectedExecution.DEFAULT); } - private static void assertSyntax(List>> args, String input, ExpectedExecution expected) { + @Test + public void singleString() { + List> args = List.of(String("string")); + assertSingleSyntax(args, """ + "value" + """, ExpectedExecution.FIRST_SYNTAX, Map.of("string", "value")); + assertSingleSyntax(args, "5 5", ExpectedExecution.DEFAULT); + assertSingleSyntax(args, "", ExpectedExecution.DEFAULT); + } + + private static void assertSyntax(List>> args, String input, ExpectedExecution expectedExecution, Map expectedValues) { final String commandName = "name"; var manager = new CommandManager(); @@ -86,6 +98,8 @@ public class CommandExecutionTest { manager.register(command); AtomicReference result = new AtomicReference<>(); + AtomicReference> values = new AtomicReference<>(); + command.setDefaultExecutor((sender, context) -> { if (!result.compareAndSet(null, ExpectedExecution.DEFAULT)) { fail("Multiple execution: " + result.get()); @@ -99,12 +113,28 @@ public class CommandExecutionTest { if (!result.compareAndSet(null, id)) { fail("Multiple execution: " + result.get()); } + values.set(context.getMap()); }, t.toArray(Argument[]::new)); } final String executeString = commandName + " " + input; manager.executeServerCommand(executeString); - assertEquals(expected, result.get()); + assertEquals(expectedExecution, result.get()); + if (expectedValues != null) { + assertEquals(expectedValues, values.get()); + } + } + + private static void assertSyntax(List>> args, String input, ExpectedExecution expectedExecution) { + assertSyntax(args, input, expectedExecution, null); + } + + private static void assertSingleSyntax(List> args, String input, ExpectedExecution expectedExecution, Map expectedValues) { + assertSyntax(List.of(args), input, expectedExecution, expectedValues); + } + + private static void assertSingleSyntax(List> args, String input, ExpectedExecution expectedExecution) { + assertSingleSyntax(args, input, expectedExecution, null); } enum ExpectedExecution {