Skip to content

Commit

Permalink
Add some tests and add context to brigadier-executionHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
TBlueF committed Oct 12, 2024
1 parent 8090229 commit 7ede731
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public int run(CommandContext<D> context) throws CommandSyntaxException {
.max(Comparator.comparing(ParseMatch::getPriority))
.orElseThrow(IllegalStateException::new);

return executionHandler.handleExecution(executable.execute());
return executionHandler.handleExecution(executable.getContext(), executable.execute());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

public interface CommandExecutionHandler<C, T> {

int handleExecution(@Nullable T result) throws CommandSyntaxException;
int handleExecution(C context, @Nullable T result) throws CommandSyntaxException;

int handleParseFailure(ParseResult<C, T> parseResult) throws CommandSyntaxException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public class DefaultExecutionHandler<C, T> implements CommandExecutionHandler<C, T> {

@Override
public int handleExecution(T result) {
public int handleExecution(C context, T result) {
if (result instanceof Number)
return ((Number) result).intValue();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import de.bluecolored.bluecommands.parsers.ArgumentParser;

import java.util.Collection;
import java.util.List;

public class ArgumentCommand<C, T> extends Command<C, T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public Collection<Command<C, T>> getSubCommands() {
return subCommands;
}

public ParseResult<C, T> parse(C context, String input) {
return parse(context, new InputReader(input));
}

public ParseResult<C, T> parse(C context, InputReader input) {
ParseData<C, T> stack = new ParseData<>(context, input);
parse(stack);
Expand Down Expand Up @@ -114,6 +118,7 @@ private boolean isSubTreeOptional() {
return true;
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean isTreeOptional() {
long now = System.currentTimeMillis();
if (lastTreeOptionalTime < now - 1000) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface ParserType {

Class<? extends ArgumentParser<?, ?>> value();
@SuppressWarnings("rawtypes")
Class<? extends ArgumentParser> value();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package de.bluecolored.bluecommands;

import de.bluecolored.bluecommands.annotations.Argument;
import de.bluecolored.bluecommands.annotations.Command;
import de.bluecolored.bluecommands.annotations.ParserType;
import de.bluecolored.bluecommands.parsers.ArgumentParser;
import de.bluecolored.bluecommands.parsers.SimpleArgumentParser;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;

public class BlueCommandsTest {

private de.bluecolored.bluecommands.Command<?, ?> commands;

@BeforeEach
public void init() {
BlueCommands<?> blueCommands = new BlueCommands<>();
commands = blueCommands.createCommand(this);
}

@Test
public void testBasicCommand() {
var result = commands.parse(null, "test arg1 arg2");
assertEquals(1, result.getMatches().size());

var match = result.getMatches().iterator().next();
assertNull(match.getContext());
var expected = new HashMap<>();
expected.put("with", "arg1");
expected.put("some", null);
expected.put("arguments", "arg2");
assertEquals(expected, match.getArguments());
assertEquals(4, match.getCommandStack().size());

var stack1 = match.getCommandStack().get(0);
assertEquals(0, stack1.getPosition());
assertInstanceOf(LiteralCommand.class, stack1.getCommand());
assertNull(stack1.getValue());

var stack2 = match.getCommandStack().get(1);
assertEquals(5, stack2.getPosition());
assertInstanceOf(ArgumentCommand.class, stack2.getCommand());
assertEquals("arg1", stack2.getValue());

var stack3 = match.getCommandStack().get(2);
assertEquals(10, stack3.getPosition());
assertInstanceOf(ArgumentCommand.class, stack3.getCommand());
assertNull(stack3.getValue());

var stack4 = match.getCommandStack().get(3);
assertEquals(10, stack4.getPosition());
assertInstanceOf(ArgumentCommand.class, stack4.getCommand());
assertEquals("arg2", stack4.getValue());
}

@Test
public void testSuggestionWithTrailingSpaceStart() {
var result = commands.parse(null, "test ");
assertEquals(Set.of(
"suggestion1_1", "suggestion1_2",
"suggestion4_1", "suggestion4_2"
), allSuggestions(result));
}

@Test
public void testSuggestionWithoutTrailingSpaceBeforeOptional() {
var result = commands.parse(null, "test arg1");
assertEquals(Set.of(
"suggestion1_1", "suggestion1_2",
"suggestion4_1", "suggestion4_2"
), allSuggestions(result));
}

@Test
public void testSuggestionWithTrailingSpaceOptional() {
var result = commands.parse(null, "test arg1 ");
assertEquals(Set.of(
"suggestion2_1", "suggestion2_2",
"suggestion3_1", "suggestion3_2"
), allSuggestions(result));
}

@Test
public void testSuggestionWithoutTrailingSpace() {
var result = commands.parse(null, "test arg1 arg2");
assertEquals(Set.of(
"suggestion2_1", "suggestion2_2",
"suggestion3_1", "suggestion3_2"
), allSuggestions(result));
}

@Test
public void testSuggestionWithTrailingSpace() {
var result = commands.parse(null, "test arg1 arg2 ");
assertEquals(Set.of(
"suggestion3_1", "suggestion3_2"
), allSuggestions(result));
}

private static Set<String> allSuggestions(ParseResult<?, ?> result) {
return result.getFailures().stream()
.map(ParseFailure::getSuggestions)
.flatMap(List::stream)
.map(Suggestion::getString)
.collect(Collectors.toSet());
}

@Command("test <with> [some] <arguments>")
public void testWithArgs(
@ParserType(StringWithSuggestions.class) @Argument("with") String with,
@ParserType(StringWithSuggestions2.class) @Argument("some") String some,
@ParserType(StringWithSuggestions3.class) @Argument("arguments") String arguments
) {}

@Command("test <single-argument>")
public void testWithArgs(
@ParserType(StringWithSuggestions4.class) @Argument("single-argument") String singleArgument
) {}

public static class StringWithSuggestions<C> extends SimpleArgumentParser<C, String> implements ArgumentParser<C, String> {

public StringWithSuggestions() {
super(true, false);
}

@Override
public String parse(C context, String string) throws CommandParseException {
return string;
}

@Override
public List<Suggestion> suggest(C context, InputReader input) {
return List.of(
new SimpleSuggestion("suggestion1_1"),
new SimpleSuggestion("suggestion1_2")
);
}

}

public static class StringWithSuggestions2<C> extends SimpleArgumentParser<C, String> {

public StringWithSuggestions2() {
super(true, false);
}

@Override
public String parse(C context, String string) throws CommandParseException {
return string;
}

@Override
public List<Suggestion> suggest(C context, InputReader input) {
return List.of(
new SimpleSuggestion("suggestion2_1"),
new SimpleSuggestion("suggestion2_2")
);
}

}

public static class StringWithSuggestions3<C> extends SimpleArgumentParser<C, String> {

public StringWithSuggestions3() {
super(true, false);
}

@Override
public String parse(C context, String string) throws CommandParseException {
return string;
}

@Override
public List<Suggestion> suggest(C context, InputReader input) {
return List.of(
new SimpleSuggestion("suggestion3_1"),
new SimpleSuggestion("suggestion3_2")
);
}

}

public static class StringWithSuggestions4<C> extends SimpleArgumentParser<C, String> {

public StringWithSuggestions4() {
super(true, false);
}

@Override
public String parse(C context, String string) throws CommandParseException {
return string;
}

@Override
public List<Suggestion> suggest(C context, InputReader input) {
return List.of(
new SimpleSuggestion("suggestion4_1"),
new SimpleSuggestion("suggestion4_2")
);
}

}

}

0 comments on commit 7ede731

Please sign in to comment.