Fixed from failing prematurely on partially matching literals

This commit is contained in:
Nathan Adams
2017-08-07 09:24:14 +02:00
parent 2d7644dce3
commit 78f4187908
2 changed files with 17 additions and 3 deletions
@@ -94,6 +94,11 @@ public class CommandDispatcher<S> {
final int cursor = reader.getCursor(); final int cursor = reader.getCursor();
try { try {
child.parse(reader, context); child.parse(reader, context);
if (reader.canRead()) {
if (reader.peek() != ARGUMENT_SEPARATOR_CHAR) {
throw ERROR_EXPECTED_ARGUMENT_SEPARATOR.createWithContext(reader);
}
}
} catch (final CommandException ex) { } catch (final CommandException ex) {
errors.put(child, ex); errors.put(child, ex);
reader.setCursor(cursor); reader.setCursor(cursor);
@@ -102,9 +107,6 @@ public class CommandDispatcher<S> {
context.withCommand(child.getCommand()); context.withCommand(child.getCommand());
if (reader.canRead()) { if (reader.canRead()) {
if (reader.peek() != ARGUMENT_SEPARATOR_CHAR) {
throw ERROR_EXPECTED_ARGUMENT_SEPARATOR.createWithContext(reader);
}
reader.skip(); reader.skip();
if (child.getRedirect() != null) { if (child.getRedirect() != null) {
return parseNodes(child.getRedirect(), reader, context.redirect()); return parseNodes(child.getRedirect(), reader, context.redirect());
@@ -22,6 +22,7 @@ import static org.junit.Assert.fail;
import static org.mockito.Matchers.notNull; import static org.mockito.Matchers.notNull;
import static org.mockito.Mockito.any; import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -207,6 +208,17 @@ public class CommandDispatcherTest {
} }
} }
@Test
public void testExecute_invalidOther() throws Exception {
final Command<Object> wrongCommand = mock(Command.class);
subject.register(literal("w").executes(wrongCommand));
subject.register(literal("world").executes(command));
assertThat(subject.execute("world", source), is(42));
verify(wrongCommand, never()).run(any());
verify(command).run(any());
}
@Test @Test
public void parse_noSpaceSeparator() throws Exception { public void parse_noSpaceSeparator() throws Exception {
subject.register(literal("foo").then(argument("bar", integer()).executes(command))); subject.register(literal("foo").then(argument("bar", integer()).executes(command)));