This commit is contained in:
commit
7c322707ba
51 changed files with 1488 additions and 0 deletions
|
|
@ -0,0 +1,6 @@
|
|||
package site.lab0x13.scrow.commands;
|
||||
|
||||
import site.lab0x13.scrow.commands.model.AbstractCommandContext;
|
||||
|
||||
public class CommandContextImpl extends AbstractCommandContext<CommandContextImpl> {
|
||||
}
|
||||
108
common/tests/site/lab0x13/scrow/commands/CommandHandlerTest.java
Normal file
108
common/tests/site/lab0x13/scrow/commands/CommandHandlerTest.java
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
package site.lab0x13.scrow.commands;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import site.lab0x13.scrow.commands.model.argument.Argument;
|
||||
import site.lab0x13.scrow.commands.model.argument.types.IntegerArgumentType;
|
||||
import site.lab0x13.scrow.commands.model.argument.types.StringArgumentType;
|
||||
import site.lab0x13.scrow.commands.model.literal.Literal;
|
||||
import site.lab0x13.scrow.commands.model.literal.MessageStyle;
|
||||
import site.lab0x13.scrow.commands.model.literal.RootLiteral;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.util.Collections;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class CommandHandlerTest {
|
||||
|
||||
private static final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
||||
private static final PrintStream originalOut = System.out;
|
||||
|
||||
private final CommandHandler<CommandContextImpl> commandHandler = new CommandHandler<>(new TestPlatform(Collections.emptyList()));
|
||||
private final Argument<CommandContextImpl, String> playerArg =
|
||||
Argument.builder("player", new StringArgumentType<CommandContextImpl>())
|
||||
.build();
|
||||
private final Argument<CommandContextImpl, Integer> amountArg =
|
||||
Argument.builder("amount", new IntegerArgumentType<CommandContextImpl>())
|
||||
.build();
|
||||
private final RootLiteral<CommandContextImpl> rootLiteral = Literal.<CommandContextImpl>builder("coins")
|
||||
.withSyncExecutor(_ -> {
|
||||
System.out.println("default executor");
|
||||
})
|
||||
.withSubLiteral(Literal.<CommandContextImpl>builder("get")
|
||||
.withArgument(playerArg)
|
||||
.withSyncExecutor(ctx -> {
|
||||
var player = ctx.getArg(playerArg);
|
||||
System.out.println("get executor: player=" + player);
|
||||
})
|
||||
.build()
|
||||
)
|
||||
.withSubLiteral(Literal.<CommandContextImpl>builder("set")
|
||||
.withArgument(playerArg)
|
||||
.withArgument(amountArg)
|
||||
.withSyncExecutor(ctx -> {
|
||||
var player = ctx.getArg(playerArg);
|
||||
var amount = ctx.getArg(amountArg);
|
||||
System.out.println("set executor: player=" + player + " amount=" + amount);
|
||||
})
|
||||
.build()
|
||||
)
|
||||
.asRootLiteral(MessageStyle.PLAIN);
|
||||
|
||||
@BeforeAll
|
||||
static void init() {
|
||||
System.setOut(new PrintStream(outContent));
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void cleanup() {
|
||||
System.setOut(originalOut);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void resetOut() {
|
||||
outContent.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDefaultExecutor() {
|
||||
commandHandler.invoke(rootLiteral, new CommandContextImpl(), Stream.of());
|
||||
assertEquals("default executor\n", outContent.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSubLiteralNoArgs() {
|
||||
commandHandler.invoke(rootLiteral, new CommandContextImpl(), Stream.of("get", "foo"));
|
||||
assertEquals("get executor: player=foo\n", outContent.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSubLiteralWithArgs() {
|
||||
commandHandler.invoke(rootLiteral, new CommandContextImpl(), Stream.of("set", "foo", "10"));
|
||||
assertEquals("set executor: player=foo amount=10\n", outContent.toString());
|
||||
outContent.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFailOnBadLiteral() {
|
||||
commandHandler.invoke(rootLiteral, new CommandContextImpl(), Stream.of("BAD_LITERAL"));
|
||||
assertEquals("Unknown literal \"BAD_LITERAL\".\n", outContent.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFailOnBadArgument() {
|
||||
commandHandler.invoke(rootLiteral, new CommandContextImpl(), Stream.of("set", "foo", "bar"));
|
||||
assertEquals("Not a number.\n", outContent.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFailOnMissingArgument() {
|
||||
commandHandler.invoke(rootLiteral, new CommandContextImpl(), Stream.of("set"));
|
||||
assertEquals("Argument expected.\n", outContent.toString());
|
||||
}
|
||||
}
|
||||
27
common/tests/site/lab0x13/scrow/commands/TestPlatform.java
Normal file
27
common/tests/site/lab0x13/scrow/commands/TestPlatform.java
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package site.lab0x13.scrow.commands;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TestPlatform implements PlatformAdapter<CommandContextImpl> {
|
||||
|
||||
private final PlainTextComponentSerializer plainText = PlainTextComponentSerializer.plainText();
|
||||
|
||||
private final List<String> permissions;
|
||||
|
||||
public TestPlatform(List<String> permissions) {
|
||||
this.permissions = permissions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkPermission(CommandContextImpl ctx, String permission) {
|
||||
return permissions.contains(permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(CommandContextImpl ctx, Component message) {
|
||||
System.out.println(plainText.serialize(message));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue