Changed how parsing is done, performed by dispatcher and can now look as deep as needed
This commit is contained in:
@@ -8,8 +8,11 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import static net.minecraft.commands.arguments.IntegerArgumentType.integer;
|
||||
import static net.minecraft.commands.builder.LiteralArgumentBuilder.literal;
|
||||
import static net.minecraft.commands.builder.RequiredArgumentBuilder.argument;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
@@ -22,12 +25,6 @@ public class CommandDispatcherTest {
|
||||
subject = new CommandDispatcher();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testDuplicateCommand() throws Exception {
|
||||
subject.register(literal("foo").executes(command));
|
||||
subject.register(literal("foo").executes(command));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateAndExecuteCommand() throws Exception {
|
||||
subject.register(literal("foo").executes(command));
|
||||
@@ -40,4 +37,35 @@ public class CommandDispatcherTest {
|
||||
public void testExecuteUnknownCommand() throws Exception {
|
||||
subject.execute("foo");
|
||||
}
|
||||
|
||||
@Test(expected = UnknownCommandException.class)
|
||||
public void testExecuteUnknownSubcommand() throws Exception {
|
||||
subject.register(literal("foo").executes(command));
|
||||
subject.execute("foo bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteSubcommand() throws Exception {
|
||||
Command subCommand = mock(Command.class);
|
||||
|
||||
subject.register(literal("foo").then(
|
||||
literal("a")
|
||||
).then(
|
||||
literal("b").executes(subCommand)
|
||||
).then(
|
||||
literal("c")
|
||||
).executes(command));
|
||||
|
||||
subject.execute("foo b");
|
||||
verify(subCommand).run(any(CommandContext.class));
|
||||
}
|
||||
|
||||
@Test(expected = UnknownCommandException.class)
|
||||
public void testExecuteInvalidSubcommand() throws Exception {
|
||||
subject.register(literal("foo").then(
|
||||
argument("bar", integer())
|
||||
).executes(command));
|
||||
|
||||
subject.execute("foo bar");
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.minecraft.commands.tree;
|
||||
|
||||
import net.minecraft.commands.context.CommandContextBuilder;
|
||||
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -11,40 +12,32 @@ import static org.junit.Assert.assertThat;
|
||||
|
||||
public class ArgumentCommandNodeTest {
|
||||
ArgumentCommandNode node;
|
||||
CommandContextBuilder contextBuilder;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
node = argument("foo", integer()).build();
|
||||
contextBuilder = new CommandContextBuilder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParse() throws Exception {
|
||||
assertThat((ArgumentCommandNode) node.parse("123"), is(node));
|
||||
assertThat(node.parse("123 456", contextBuilder), is("456"));
|
||||
|
||||
assertThat(contextBuilder.getArguments().containsKey("foo"), is(true));
|
||||
assertThat(contextBuilder.getArguments().get("foo").getResult(), is((Object) 123));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseExact() throws Exception {
|
||||
assertThat(node.parse("123", contextBuilder), is(""));
|
||||
|
||||
assertThat(contextBuilder.getArguments().containsKey("foo"), is(true));
|
||||
assertThat(contextBuilder.getArguments().get("foo").getResult(), is((Object) 123));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentSyntaxException.class)
|
||||
public void testParseInvalid() throws Exception {
|
||||
node.parse("bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseChild() throws Exception {
|
||||
CommandNode child = argument("bar", integer()).build();
|
||||
|
||||
node.addChild(child);
|
||||
|
||||
assertThat(node.parse("123 123"), is(child));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentSyntaxException.class)
|
||||
public void testParseInvalidChild() throws Exception {
|
||||
node.addChild(argument("bar", integer()).build());
|
||||
|
||||
node.parse("123 bar");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentSyntaxException.class)
|
||||
public void testParseNoChildren() throws Exception {
|
||||
node.parse("123 123");
|
||||
node.parse("foo", contextBuilder);
|
||||
}
|
||||
}
|
||||
@@ -1,51 +1,36 @@
|
||||
package net.minecraft.commands.tree;
|
||||
|
||||
import net.minecraft.commands.context.CommandContextBuilder;
|
||||
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static net.minecraft.commands.arguments.IntegerArgumentType.integer;
|
||||
import static net.minecraft.commands.builder.LiteralArgumentBuilder.literal;
|
||||
import static net.minecraft.commands.builder.RequiredArgumentBuilder.argument;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class LiteralCommandNodeTest {
|
||||
LiteralCommandNode node;
|
||||
CommandContextBuilder contextBuilder;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
node = literal("foo").build();
|
||||
contextBuilder = new CommandContextBuilder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParse() throws Exception {
|
||||
assertThat((LiteralCommandNode) node.parse("foo"), is(node));
|
||||
assertThat(node.parse("foo bar", contextBuilder), is("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseExact() throws Exception {
|
||||
assertThat(node.parse("foo", contextBuilder), is(""));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentSyntaxException.class)
|
||||
public void testParseInvalid() throws Exception {
|
||||
node.parse("bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseChild() throws Exception {
|
||||
CommandNode child = argument("bar", integer()).build();
|
||||
|
||||
node.addChild(child);
|
||||
|
||||
assertThat(node.parse("foo 123"), is(child));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentSyntaxException.class)
|
||||
public void testParseInvalidChild() throws Exception {
|
||||
node.addChild(argument("bar", integer()).build());
|
||||
|
||||
node.parse("foo bar");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentSyntaxException.class)
|
||||
public void testParseNoChildren() throws Exception {
|
||||
node.parse("foo 123");
|
||||
node.parse("bar", contextBuilder);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package net.minecraft.commands.tree;
|
||||
|
||||
import net.minecraft.commands.context.CommandContextBuilder;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class RootCommandNodeTest {
|
||||
RootCommandNode node;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
node = new RootCommandNode();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParse() throws Exception {
|
||||
assertThat(node.parse("foo bar baz", new CommandContextBuilder()), is("foo bar baz"));
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user