mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-12 18:31:41 +01:00
Separate syntax test in single & multi variants
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
793561e0cb
commit
ab5734334c
@ -1,147 +0,0 @@
|
||||
package net.minestom.server.command;
|
||||
|
||||
import net.minestom.server.command.builder.Command;
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
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 {
|
||||
|
||||
@Test
|
||||
public void testAndCommand() {
|
||||
var manager = new CommandManager();
|
||||
|
||||
var command = new Command("and");
|
||||
manager.register(command);
|
||||
|
||||
var bool = new AtomicBoolean(false);
|
||||
|
||||
command.addSyntax((sender, context) -> {
|
||||
boolean bool1 = context.get("bool1");
|
||||
boolean bool2 = context.get("bool2");
|
||||
bool.set(bool1 && bool2);
|
||||
}, Boolean("bool1"), Boolean("bool2"));
|
||||
|
||||
assertFalse(bool.get());
|
||||
|
||||
manager.executeServerCommand("and true true");
|
||||
assertTrue(bool.get());
|
||||
|
||||
manager.executeServerCommand("and false true");
|
||||
assertFalse(bool.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConditionalMessage() {
|
||||
var manager = new CommandManager();
|
||||
|
||||
var command = new Command("allowed", "disallowed");
|
||||
manager.register(command);
|
||||
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
|
||||
command.setCondition((sender, commandString) -> {
|
||||
if (commandString == null) {
|
||||
return true;
|
||||
}
|
||||
var result = commandString.startsWith("allowed");
|
||||
counter.set(result ? 1 : 0);
|
||||
return result;
|
||||
});
|
||||
|
||||
command.setDefaultExecutor((sender, context) -> {
|
||||
});
|
||||
|
||||
assertEquals(0, counter.get());
|
||||
|
||||
manager.executeServerCommand("allowed");
|
||||
assertEquals(1, counter.get());
|
||||
|
||||
manager.executeServerCommand("disallowed");
|
||||
assertEquals(0, counter.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleInteger() {
|
||||
List<Argument<?>> 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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleString() {
|
||||
List<Argument<?>> 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<List<Argument<?>>> args, String input, ExpectedExecution expectedExecution, Map<String, Object> expectedValues) {
|
||||
final String commandName = "name";
|
||||
|
||||
var manager = new CommandManager();
|
||||
var command = new Command(commandName);
|
||||
manager.register(command);
|
||||
|
||||
AtomicReference<ExpectedExecution> result = new AtomicReference<>();
|
||||
AtomicReference<Map<String, Object>> values = new AtomicReference<>();
|
||||
|
||||
command.setDefaultExecutor((sender, context) -> {
|
||||
if (!result.compareAndSet(null, ExpectedExecution.DEFAULT)) {
|
||||
fail("Multiple execution: " + result.get());
|
||||
}
|
||||
});
|
||||
|
||||
int i = ExpectedExecution.FIRST_SYNTAX.ordinal();
|
||||
for (List<Argument<?>> t : args) {
|
||||
ExpectedExecution id = ExpectedExecution.values()[i++];
|
||||
command.addSyntax((sender, context) -> {
|
||||
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(expectedExecution, result.get());
|
||||
if (expectedValues != null) {
|
||||
assertEquals(expectedValues, values.get());
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertSyntax(List<List<Argument<?>>> args, String input, ExpectedExecution expectedExecution) {
|
||||
assertSyntax(args, input, expectedExecution, null);
|
||||
}
|
||||
|
||||
private static void assertSingleSyntax(List<Argument<?>> args, String input, ExpectedExecution expectedExecution, Map<String, Object> expectedValues) {
|
||||
assertSyntax(List.of(args), input, expectedExecution, expectedValues);
|
||||
}
|
||||
|
||||
private static void assertSingleSyntax(List<Argument<?>> args, String input, ExpectedExecution expectedExecution) {
|
||||
assertSingleSyntax(args, input, expectedExecution, null);
|
||||
}
|
||||
|
||||
enum ExpectedExecution {
|
||||
DEFAULT,
|
||||
|
||||
FIRST_SYNTAX,
|
||||
SECOND_SYNTAX,
|
||||
THIRD_SYNTAX
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package net.minestom.server.command;
|
||||
|
||||
import net.minestom.server.command.builder.Command;
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.String;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static net.minestom.server.command.builder.arguments.ArgumentType.Float;
|
||||
import static net.minestom.server.command.builder.arguments.ArgumentType.Integer;
|
||||
import static net.minestom.server.command.builder.arguments.ArgumentType.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
public class CommandSyntaxMultiTest {
|
||||
|
||||
@Test
|
||||
public void integerFloat() {
|
||||
List<List<Argument<?>>> args = List.of(
|
||||
List.of(Literal("integer"), Integer("number")),
|
||||
List.of(Literal("float"), Float("number"))
|
||||
);
|
||||
assertSyntax(args, "integer 5", ExpectedExecution.FIRST_SYNTAX, Map.of("integer", "integer", "number", 5));
|
||||
assertSyntax(args, "float 5.5", ExpectedExecution.SECOND_SYNTAX, Map.of("float", "float", "number", 5.5f));
|
||||
}
|
||||
|
||||
private static void assertSyntax(List<List<Argument<?>>> args, String input, ExpectedExecution expectedExecution, Map<String, Object> expectedValues) {
|
||||
final String commandName = "name";
|
||||
|
||||
var manager = new CommandManager();
|
||||
var command = new Command(commandName);
|
||||
manager.register(command);
|
||||
|
||||
AtomicReference<ExpectedExecution> result = new AtomicReference<>();
|
||||
AtomicReference<Map<String, Object>> values = new AtomicReference<>();
|
||||
|
||||
command.setDefaultExecutor((sender, context) -> {
|
||||
if (!result.compareAndSet(null, ExpectedExecution.DEFAULT)) {
|
||||
fail("Multiple execution: " + result.get());
|
||||
}
|
||||
});
|
||||
|
||||
int i = ExpectedExecution.FIRST_SYNTAX.ordinal();
|
||||
for (List<Argument<?>> t : args) {
|
||||
ExpectedExecution id = ExpectedExecution.values()[i++];
|
||||
command.addSyntax((sender, context) -> {
|
||||
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(expectedExecution, result.get());
|
||||
if (expectedValues != null) {
|
||||
assertEquals(expectedValues, values.get());
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertSyntax(List<List<Argument<?>>> args, String input, ExpectedExecution expectedExecution) {
|
||||
assertSyntax(args, input, expectedExecution, null);
|
||||
}
|
||||
|
||||
enum ExpectedExecution {
|
||||
DEFAULT,
|
||||
|
||||
FIRST_SYNTAX,
|
||||
SECOND_SYNTAX,
|
||||
THIRD_SYNTAX
|
||||
}
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
package net.minestom.server.command;
|
||||
|
||||
import net.minestom.server.command.builder.Command;
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
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.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
public class CommandSyntaxSingleTest {
|
||||
@Test
|
||||
public void singleInteger() {
|
||||
List<Argument<?>> args = List.of(Integer("number"));
|
||||
assertSyntax(args, "5", ExpectedExecution.SYNTAX, Map.of("number", 5));
|
||||
assertSyntax(args, "5 5", ExpectedExecution.DEFAULT);
|
||||
assertSyntax(args, "", ExpectedExecution.DEFAULT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleIntegerInteger() {
|
||||
List<Argument<?>> args = List.of(Integer("number"), Integer("number2"));
|
||||
assertSyntax(args, "5", ExpectedExecution.DEFAULT);
|
||||
assertSyntax(args, "5 6", ExpectedExecution.SYNTAX, Map.of("number", 5, "number2", 6));
|
||||
assertSyntax(args, "", ExpectedExecution.DEFAULT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleString() {
|
||||
List<Argument<?>> args = List.of(String("string"));
|
||||
assertSyntax(args, """
|
||||
"value"
|
||||
""", ExpectedExecution.SYNTAX, Map.of("string", "value"));
|
||||
assertSyntax(args, "5 5", ExpectedExecution.DEFAULT);
|
||||
assertSyntax(args, "", ExpectedExecution.DEFAULT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleStringString() {
|
||||
List<Argument<?>> args = List.of(String("string"), String("string2"));
|
||||
assertSyntax(args, "test", ExpectedExecution.DEFAULT);
|
||||
assertSyntax(args, """
|
||||
"first" "second"
|
||||
""", ExpectedExecution.SYNTAX, Map.of("string", "first", "string2", "second"));
|
||||
assertSyntax(args, """
|
||||
"unescaped" "esc\\"aped"
|
||||
""", ExpectedExecution.SYNTAX, Map.of("string", "unescaped", "string2", "esc\"aped"));
|
||||
assertSyntax(args, "5 5", ExpectedExecution.SYNTAX);
|
||||
assertSyntax(args, "", ExpectedExecution.DEFAULT);
|
||||
}
|
||||
|
||||
private static void assertSyntax(List<Argument<?>> args, String input, ExpectedExecution expectedExecution, Map<String, Object> expectedValues) {
|
||||
final String commandName = "name";
|
||||
|
||||
var manager = new CommandManager();
|
||||
var command = new Command(commandName);
|
||||
manager.register(command);
|
||||
|
||||
AtomicReference<ExpectedExecution> result = new AtomicReference<>();
|
||||
AtomicReference<Map<String, Object>> values = new AtomicReference<>();
|
||||
|
||||
command.setDefaultExecutor((sender, context) -> {
|
||||
if (!result.compareAndSet(null, ExpectedExecution.DEFAULT)) {
|
||||
fail("Multiple execution: " + result.get());
|
||||
}
|
||||
});
|
||||
|
||||
command.addSyntax((sender, context) -> {
|
||||
if (!result.compareAndSet(null, ExpectedExecution.SYNTAX)) {
|
||||
fail("Multiple execution: " + result.get());
|
||||
}
|
||||
values.set(context.getMap());
|
||||
}, args.toArray(Argument[]::new));
|
||||
|
||||
final String executeString = commandName + " " + input;
|
||||
manager.executeServerCommand(executeString);
|
||||
assertEquals(expectedExecution, result.get());
|
||||
if (expectedValues != null) {
|
||||
assertEquals(expectedValues, values.get());
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertSyntax(List<Argument<?>> args, String input, ExpectedExecution expectedExecution) {
|
||||
assertSyntax(args, input, expectedExecution, null);
|
||||
}
|
||||
|
||||
enum ExpectedExecution {
|
||||
DEFAULT,
|
||||
SYNTAX
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user