Made node parsing also use readers
This commit is contained in:
@@ -23,6 +23,7 @@ import java.util.stream.Collectors;
|
|||||||
public class CommandDispatcher<S> {
|
public class CommandDispatcher<S> {
|
||||||
public static final SimpleCommandExceptionType ERROR_UNKNOWN_COMMAND = new SimpleCommandExceptionType("command.unknown.command", "Unknown command");
|
public static final SimpleCommandExceptionType ERROR_UNKNOWN_COMMAND = new SimpleCommandExceptionType("command.unknown.command", "Unknown command");
|
||||||
public static final ParameterizedCommandExceptionType ERROR_UNKNOWN_ARGUMENT = new ParameterizedCommandExceptionType("command.unknown.argument", "Incorrect argument for command, couldn't parse: ${argument}", "argument");
|
public static final ParameterizedCommandExceptionType ERROR_UNKNOWN_ARGUMENT = new ParameterizedCommandExceptionType("command.unknown.argument", "Incorrect argument for command, couldn't parse: ${argument}", "argument");
|
||||||
|
public static final SimpleCommandExceptionType ERROR_EXPECTED_ARGUMENT_SEPARATOR = new SimpleCommandExceptionType("command.expected.separator", "Expected whitespace to end one argument, but found trailing data");
|
||||||
|
|
||||||
public static final String ARGUMENT_SEPARATOR = " ";
|
public static final String ARGUMENT_SEPARATOR = " ";
|
||||||
public static final char ARGUMENT_SEPARATOR_CHAR = ' ';
|
public static final char ARGUMENT_SEPARATOR_CHAR = ' ';
|
||||||
@@ -69,10 +70,11 @@ public class CommandDispatcher<S> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ParseResults<S> parse(String command, S source) throws CommandException {
|
public ParseResults<S> parse(String command, S source) throws CommandException {
|
||||||
return parseNodes(root, command, new CommandContextBuilder<>(this, source));
|
StringReader reader = new StringReader(command);
|
||||||
|
return parseNodes(root, reader, new CommandContextBuilder<>(this, source));
|
||||||
}
|
}
|
||||||
|
|
||||||
private ParseResults<S> parseNodes(CommandNode<S> node, String command, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
private ParseResults<S> parseNodes(CommandNode<S> node, StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||||
final S source = contextBuilder.getSource();
|
final S source = contextBuilder.getSource();
|
||||||
Map<CommandNode<S>, CommandException> errors = Maps.newHashMap();
|
Map<CommandNode<S>, CommandException> errors = Maps.newHashMap();
|
||||||
|
|
||||||
@@ -81,23 +83,28 @@ public class CommandDispatcher<S> {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
CommandContextBuilder<S> context = contextBuilder.copy();
|
CommandContextBuilder<S> context = contextBuilder.copy();
|
||||||
String remaining;
|
int cursor = reader.getCursor();
|
||||||
try {
|
try {
|
||||||
remaining = child.parse(command, context);
|
child.parse(reader, context);
|
||||||
} catch (CommandException ex) {
|
} catch (CommandException ex) {
|
||||||
errors.put(child, ex);
|
errors.put(child, ex);
|
||||||
|
reader.setCursor(cursor);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
context.withCommand(child.getCommand());
|
context.withCommand(child.getCommand());
|
||||||
if (remaining.isEmpty()) {
|
if (reader.canRead()) {
|
||||||
return new ParseResults<>(context);
|
if (reader.peek() != ARGUMENT_SEPARATOR_CHAR) {
|
||||||
|
throw ERROR_EXPECTED_ARGUMENT_SEPARATOR.create();
|
||||||
|
}
|
||||||
|
reader.skip();
|
||||||
|
return parseNodes(child, reader, context);
|
||||||
} else {
|
} else {
|
||||||
return parseNodes(child, remaining.substring(1), context);
|
return new ParseResults<>(context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ParseResults<>(contextBuilder, command, errors);
|
return new ParseResults<>(contextBuilder, reader.getRemaining(), errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] getAllUsage(CommandNode<S> node, S source) {
|
public String[] getAllUsage(CommandNode<S> node, S source) {
|
||||||
@@ -184,22 +191,28 @@ public class CommandDispatcher<S> {
|
|||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<String> findSuggestions(CommandNode<S> node, String command, CommandContextBuilder<S> contextBuilder, Set<String> result) {
|
private Set<String> findSuggestions(CommandNode<S> node, StringReader reader, CommandContextBuilder<S> contextBuilder, Set<String> result) {
|
||||||
final S source = contextBuilder.getSource();
|
final S source = contextBuilder.getSource();
|
||||||
for (CommandNode<S> child : node.getChildren()) {
|
for (CommandNode<S> child : node.getChildren()) {
|
||||||
if (!child.canUse(source)) {
|
if (!child.canUse(source)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
CommandContextBuilder<S> context = contextBuilder.copy();
|
CommandContextBuilder<S> context = contextBuilder.copy();
|
||||||
|
int cursor = reader.getCursor();
|
||||||
try {
|
try {
|
||||||
String remaining = child.parse(command, context);
|
child.parse(reader, context);
|
||||||
if (remaining.isEmpty()) {
|
if (reader.canRead()) {
|
||||||
child.listSuggestions(command, result, context);
|
if (reader.peek() == ARGUMENT_SEPARATOR_CHAR) {
|
||||||
|
reader.skip();
|
||||||
|
return findSuggestions(child, reader, context, result);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return findSuggestions(child, remaining.substring(1), context, result);
|
reader.setCursor(cursor);
|
||||||
|
child.listSuggestions(reader.getRemaining(), result, context);
|
||||||
}
|
}
|
||||||
} catch (CommandException e) {
|
} catch (CommandException e) {
|
||||||
child.listSuggestions(command, result, context);
|
reader.setCursor(cursor);
|
||||||
|
child.listSuggestions(reader.getRemaining(), result, context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,7 +220,8 @@ public class CommandDispatcher<S> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String[] getCompletionSuggestions(String command, S source) {
|
public String[] getCompletionSuggestions(String command, S source) {
|
||||||
final Set<String> nodes = findSuggestions(root, command, new CommandContextBuilder<>(this, source), Sets.newLinkedHashSet());
|
StringReader reader = new StringReader(command);
|
||||||
|
final Set<String> nodes = findSuggestions(root, reader, new CommandContextBuilder<>(this, source), Sets.newLinkedHashSet());
|
||||||
|
|
||||||
return nodes.toArray(new String[nodes.size()]);
|
return nodes.toArray(new String[nodes.size()]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,13 +8,6 @@ import com.mojang.brigadier.exceptions.CommandException;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public interface ArgumentType<T> {
|
public interface ArgumentType<T> {
|
||||||
@Deprecated
|
|
||||||
default <S> ParsedArgument<S, T> parse(String command, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
|
||||||
StringReader reader = new StringReader(command);
|
|
||||||
T result = parse(reader, contextBuilder);
|
|
||||||
return new ParsedArgument<>(reader.getRead(), result);
|
|
||||||
}
|
|
||||||
|
|
||||||
<S> T parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException;
|
<S> T parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException;
|
||||||
|
|
||||||
default <S> void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder) {}
|
default <S> void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder) {}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.mojang.brigadier.tree;
|
package com.mojang.brigadier.tree;
|
||||||
|
|
||||||
import com.mojang.brigadier.Command;
|
import com.mojang.brigadier.Command;
|
||||||
|
import com.mojang.brigadier.StringReader;
|
||||||
import com.mojang.brigadier.arguments.ArgumentType;
|
import com.mojang.brigadier.arguments.ArgumentType;
|
||||||
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
||||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||||
@@ -50,18 +51,13 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String parse(String command, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
public void parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||||
ParsedArgument<S, T> parsed = type.parse(command, contextBuilder);
|
int start = reader.getCursor();
|
||||||
int start = parsed.getRaw().length();
|
T result = type.parse(reader, contextBuilder);
|
||||||
|
ParsedArgument<S, T> parsed = new ParsedArgument<>(reader.getString().substring(start, reader.getCursor()), result);
|
||||||
|
|
||||||
contextBuilder.withArgument(name, parsed);
|
contextBuilder.withArgument(name, parsed);
|
||||||
contextBuilder.withNode(this, parsed.getRaw());
|
contextBuilder.withNode(this, parsed.getRaw());
|
||||||
|
|
||||||
if (command.length() > start) {
|
|
||||||
return command.substring(start);
|
|
||||||
} else {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.mojang.brigadier.tree;
|
|||||||
import com.google.common.collect.ComparisonChain;
|
import com.google.common.collect.ComparisonChain;
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import com.mojang.brigadier.Command;
|
import com.mojang.brigadier.Command;
|
||||||
|
import com.mojang.brigadier.StringReader;
|
||||||
import com.mojang.brigadier.builder.ArgumentBuilder;
|
import com.mojang.brigadier.builder.ArgumentBuilder;
|
||||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||||
import com.mojang.brigadier.exceptions.CommandException;
|
import com.mojang.brigadier.exceptions.CommandException;
|
||||||
@@ -79,7 +80,7 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
|
|||||||
|
|
||||||
public abstract String getUsageText();
|
public abstract String getUsageText();
|
||||||
|
|
||||||
public abstract String parse(String command, CommandContextBuilder<S> contextBuilder) throws CommandException;
|
public abstract void parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException;
|
||||||
|
|
||||||
public abstract void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder);
|
public abstract void listSuggestions(String command, Set<String> output, CommandContextBuilder<S> contextBuilder);
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.mojang.brigadier.tree;
|
|||||||
|
|
||||||
import com.mojang.brigadier.Command;
|
import com.mojang.brigadier.Command;
|
||||||
import com.mojang.brigadier.CommandDispatcher;
|
import com.mojang.brigadier.CommandDispatcher;
|
||||||
|
import com.mojang.brigadier.StringReader;
|
||||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||||
import com.mojang.brigadier.exceptions.CommandException;
|
import com.mojang.brigadier.exceptions.CommandException;
|
||||||
@@ -30,16 +31,16 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String parse(String command, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
public void parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||||
String expected = literal + (command.length() > literal.length() ? CommandDispatcher.ARGUMENT_SEPARATOR : "");
|
for (int i = 0; i < literal.length(); i++) {
|
||||||
|
if (reader.canRead() && reader.peek() == literal.charAt(i)) {
|
||||||
if (!command.startsWith(expected)) {
|
reader.skip();
|
||||||
throw ERROR_INCORRECT_LITERAL.create(literal);
|
} else {
|
||||||
|
throw ERROR_INCORRECT_LITERAL.create(literal);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
contextBuilder.withNode(this, literal);
|
contextBuilder.withNode(this, literal);
|
||||||
int start = literal.length();
|
|
||||||
return command.substring(start);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.mojang.brigadier.tree;
|
package com.mojang.brigadier.tree;
|
||||||
|
|
||||||
|
import com.mojang.brigadier.StringReader;
|
||||||
import com.mojang.brigadier.builder.ArgumentBuilder;
|
import com.mojang.brigadier.builder.ArgumentBuilder;
|
||||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||||
import com.mojang.brigadier.exceptions.CommandException;
|
import com.mojang.brigadier.exceptions.CommandException;
|
||||||
@@ -22,8 +23,7 @@ public class RootCommandNode<S> extends CommandNode<S> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String parse(String command, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
public void parse(StringReader reader, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||||
return command;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -10,17 +10,12 @@ import org.junit.runner.RunWith;
|
|||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.runners.MockitoJUnitRunner;
|
import org.mockito.runners.MockitoJUnitRunner;
|
||||||
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
|
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
|
||||||
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
|
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
|
||||||
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
|
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
import static org.hamcrest.Matchers.not;
|
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class CommandContextTest {
|
public class CommandContextTest {
|
||||||
@@ -42,13 +37,13 @@ public class CommandContextTest {
|
|||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testGetArgument_wrongType() throws Exception {
|
public void testGetArgument_wrongType() throws Exception {
|
||||||
CommandContext<Object> context = builder.withArgument("foo", integer().parse("123", new CommandContextBuilder<>(dispatcher, source))).build();
|
CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>("123", 123)).build();
|
||||||
context.getArgument("foo", String.class);
|
context.getArgument("foo", String.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetArgument() throws Exception {
|
public void testGetArgument() throws Exception {
|
||||||
CommandContext<Object> context = builder.withArgument("foo", integer().parse("123", new CommandContextBuilder<>(dispatcher, source))).build();
|
CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>("123", 123)).build();
|
||||||
assertThat(context.getArgument("foo", int.class), is(123));
|
assertThat(context.getArgument("foo", int.class), is(123));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,7 +65,7 @@ public class CommandContextTest {
|
|||||||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, otherSource).build(), new CommandContextBuilder<>(dispatcher, otherSource).build())
|
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, otherSource).build(), new CommandContextBuilder<>(dispatcher, otherSource).build())
|
||||||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).withCommand(command).build(), new CommandContextBuilder<>(dispatcher, source).withCommand(command).build())
|
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).withCommand(command).build(), new CommandContextBuilder<>(dispatcher, source).withCommand(command).build())
|
||||||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).withCommand(otherCommand).build(), new CommandContextBuilder<>(dispatcher, source).withCommand(otherCommand).build())
|
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).withCommand(otherCommand).build(), new CommandContextBuilder<>(dispatcher, source).withCommand(otherCommand).build())
|
||||||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).withArgument("foo", integer().parse("123", new CommandContextBuilder<>(dispatcher, source))).build(), new CommandContextBuilder<>(dispatcher, source).withArgument("foo", integer().parse("123", new CommandContextBuilder<>(dispatcher, source))).build())
|
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).withArgument("foo", new ParsedArgument<>("123", 123)).build(), new CommandContextBuilder<>(dispatcher, source).withArgument("foo", new ParsedArgument<>("123", 123)).build())
|
||||||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).withNode(node, "foo").withNode(otherNode, "bar").build(), new CommandContextBuilder<>(dispatcher, source).withNode(node, "foo").withNode(otherNode, "bar").build())
|
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).withNode(node, "foo").withNode(otherNode, "bar").build(), new CommandContextBuilder<>(dispatcher, source).withNode(node, "foo").withNode(otherNode, "bar").build())
|
||||||
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).withNode(otherNode, "bar").withNode(node, "foo").build(), new CommandContextBuilder<>(dispatcher, source).withNode(otherNode, "bar").withNode(node, "foo").build())
|
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source).withNode(otherNode, "bar").withNode(node, "foo").build(), new CommandContextBuilder<>(dispatcher, source).withNode(otherNode, "bar").withNode(node, "foo").build())
|
||||||
.testEquals();
|
.testEquals();
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.google.common.collect.Sets;
|
|||||||
import com.google.common.testing.EqualsTester;
|
import com.google.common.testing.EqualsTester;
|
||||||
import com.mojang.brigadier.Command;
|
import com.mojang.brigadier.Command;
|
||||||
import com.mojang.brigadier.CommandDispatcher;
|
import com.mojang.brigadier.CommandDispatcher;
|
||||||
|
import com.mojang.brigadier.StringReader;
|
||||||
import com.mojang.brigadier.arguments.ArgumentType;
|
import com.mojang.brigadier.arguments.ArgumentType;
|
||||||
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
||||||
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
||||||
@@ -43,15 +44,8 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParse() throws Exception {
|
public void testParse() throws Exception {
|
||||||
assertThat(node.parse("123 456", contextBuilder), is(" 456"));
|
StringReader reader = new StringReader("123 456");
|
||||||
|
node.parse(reader, contextBuilder);
|
||||||
assertThat(contextBuilder.getArguments().containsKey("foo"), is(true));
|
|
||||||
assertThat(contextBuilder.getArguments().get("foo").getResult(), is(123));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseExact() throws Exception {
|
|
||||||
assertThat(node.parse("123", contextBuilder), is(""));
|
|
||||||
|
|
||||||
assertThat(contextBuilder.getArguments().containsKey("foo"), is(true));
|
assertThat(contextBuilder.getArguments().containsKey("foo"), is(true));
|
||||||
assertThat(contextBuilder.getArguments().get("foo").getResult(), is(123));
|
assertThat(contextBuilder.getArguments().get("foo").getResult(), is(123));
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.google.common.collect.Sets;
|
|||||||
import com.google.common.testing.EqualsTester;
|
import com.google.common.testing.EqualsTester;
|
||||||
import com.mojang.brigadier.Command;
|
import com.mojang.brigadier.Command;
|
||||||
import com.mojang.brigadier.CommandDispatcher;
|
import com.mojang.brigadier.CommandDispatcher;
|
||||||
|
import com.mojang.brigadier.StringReader;
|
||||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||||
import com.mojang.brigadier.exceptions.CommandException;
|
import com.mojang.brigadier.exceptions.CommandException;
|
||||||
@@ -39,29 +40,31 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParse() throws Exception {
|
public void testParse() throws Exception {
|
||||||
assertThat(node.parse("foo bar", contextBuilder), is(" bar"));
|
StringReader reader = new StringReader("foo bar");
|
||||||
|
node.parse(reader, contextBuilder);
|
||||||
|
assertThat(reader.getRemaining(), equalTo(" bar"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParseExact() throws Exception {
|
public void testParseExact() throws Exception {
|
||||||
assertThat(node.parse("foo", contextBuilder), is(""));
|
StringReader reader = new StringReader("foo");
|
||||||
|
node.parse(reader, contextBuilder);
|
||||||
|
assertThat(reader.getRemaining(), equalTo(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParseSimilar() throws Exception {
|
public void testParseSimilar() throws Exception {
|
||||||
try {
|
StringReader reader = new StringReader("foobar");
|
||||||
node.parse("foobar", contextBuilder);
|
node.parse(reader, contextBuilder);
|
||||||
fail();
|
assertThat(reader.getRemaining(), equalTo("bar"));
|
||||||
} catch (CommandException ex) {
|
// This should succeed, because it's the responsibility of the dispatcher to realize there's trailing text
|
||||||
assertThat(ex.getType(), is(LiteralCommandNode.ERROR_INCORRECT_LITERAL));
|
|
||||||
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("expected", "foo")));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParseInvalid() throws Exception {
|
public void testParseInvalid() throws Exception {
|
||||||
|
StringReader reader = new StringReader("bar");
|
||||||
try {
|
try {
|
||||||
node.parse("bar", contextBuilder);
|
node.parse(reader, contextBuilder);
|
||||||
fail();
|
fail();
|
||||||
} catch (CommandException ex) {
|
} catch (CommandException ex) {
|
||||||
assertThat(ex.getType(), is(LiteralCommandNode.ERROR_INCORRECT_LITERAL));
|
assertThat(ex.getType(), is(LiteralCommandNode.ERROR_INCORRECT_LITERAL));
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.mojang.brigadier.tree;
|
|||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
import com.google.common.testing.EqualsTester;
|
import com.google.common.testing.EqualsTester;
|
||||||
import com.mojang.brigadier.CommandDispatcher;
|
import com.mojang.brigadier.CommandDispatcher;
|
||||||
|
import com.mojang.brigadier.StringReader;
|
||||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -30,7 +31,9 @@ public class RootCommandNodeTest extends AbstractCommandNodeTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParse() throws Exception {
|
public void testParse() throws Exception {
|
||||||
assertThat(node.parse("foo bar baz", new CommandContextBuilder<>(new CommandDispatcher<>(), new Object())), is("foo bar baz"));
|
StringReader reader = new StringReader("hello world");
|
||||||
|
node.parse(reader, new CommandContextBuilder<>(new CommandDispatcher<>(), new Object()));
|
||||||
|
assertThat(reader.getCursor(), is(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = UnsupportedOperationException.class)
|
@Test(expected = UnsupportedOperationException.class)
|
||||||
|
|||||||
Reference in New Issue
Block a user