Implemented merging of CommandNode children
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package net.minecraft.commands.tree;
|
||||
|
||||
import net.minecraft.commands.Command;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import static net.minecraft.commands.builder.LiteralArgumentBuilder.literal;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public abstract class AbstractCommandNodeTest {
|
||||
@Mock private Command command;
|
||||
|
||||
protected abstract CommandNode getCommandNode();
|
||||
|
||||
@Test
|
||||
public void testAddChild() throws Exception {
|
||||
CommandNode node = getCommandNode();
|
||||
|
||||
node.addChild(literal("child1").build());
|
||||
node.addChild(literal("child2").build());
|
||||
node.addChild(literal("child1").build());
|
||||
|
||||
assertThat(node.getChildren(), hasSize(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddChildMergesGrandchildren() throws Exception {
|
||||
CommandNode node = getCommandNode();
|
||||
|
||||
node.addChild(literal("child").then(
|
||||
literal("grandchild1")
|
||||
).build());
|
||||
|
||||
node.addChild(literal("child").then(
|
||||
literal("grandchild2")
|
||||
).build());
|
||||
|
||||
assertThat(node.getChildren(), hasSize(1));
|
||||
assertThat(node.getChildren().iterator().next().getChildren(), hasSize(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddChildPreservesCommand() throws Exception {
|
||||
CommandNode node = getCommandNode();
|
||||
|
||||
node.addChild(literal("child").executes(command).build());
|
||||
node.addChild(literal("child").build());
|
||||
|
||||
assertThat(node.getChildren().iterator().next().getCommand(), is(command));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddChildOverwritesCommand() throws Exception {
|
||||
CommandNode node = getCommandNode();
|
||||
|
||||
node.addChild(literal("child").build());
|
||||
node.addChild(literal("child").executes(command).build());
|
||||
|
||||
assertThat(node.getChildren().iterator().next().getCommand(), is(command));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.minecraft.commands.tree;
|
||||
|
||||
import com.google.common.testing.EqualsTester;
|
||||
import net.minecraft.commands.Command;
|
||||
import net.minecraft.commands.context.CommandContextBuilder;
|
||||
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
|
||||
import org.junit.Before;
|
||||
@@ -10,11 +11,17 @@ import static net.minecraft.commands.arguments.IntegerArgumentType.integer;
|
||||
import static net.minecraft.commands.builder.RequiredArgumentBuilder.argument;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
public class ArgumentCommandNodeTest {
|
||||
public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
|
||||
ArgumentCommandNode node;
|
||||
CommandContextBuilder contextBuilder;
|
||||
|
||||
@Override
|
||||
protected CommandNode getCommandNode() {
|
||||
return node;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
node = argument("foo", integer()).build();
|
||||
@@ -44,11 +51,17 @@ public class ArgumentCommandNodeTest {
|
||||
|
||||
@Test
|
||||
public void testEquals() throws Exception {
|
||||
Command command = mock(Command.class);
|
||||
|
||||
new EqualsTester()
|
||||
.addEqualityGroup(
|
||||
argument("foo", integer()).build(),
|
||||
argument("foo", integer()).build()
|
||||
)
|
||||
.addEqualityGroup(
|
||||
argument("foo", integer()).executes(command).build(),
|
||||
argument("foo", integer()).executes(command).build()
|
||||
)
|
||||
.addEqualityGroup(
|
||||
argument("bar", integer(-100, 100)).build(),
|
||||
argument("bar", integer(-100, 100)).build()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.minecraft.commands.tree;
|
||||
|
||||
import com.google.common.testing.EqualsTester;
|
||||
import net.minecraft.commands.Command;
|
||||
import net.minecraft.commands.context.CommandContextBuilder;
|
||||
import net.minecraft.commands.exceptions.IllegalArgumentSyntaxException;
|
||||
import org.junit.Before;
|
||||
@@ -9,11 +10,17 @@ import org.junit.Test;
|
||||
import static net.minecraft.commands.builder.LiteralArgumentBuilder.literal;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
public class LiteralCommandNodeTest {
|
||||
public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
|
||||
LiteralCommandNode node;
|
||||
CommandContextBuilder contextBuilder;
|
||||
|
||||
@Override
|
||||
protected CommandNode getCommandNode() {
|
||||
return node;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
node = literal("foo").build();
|
||||
@@ -42,11 +49,17 @@ public class LiteralCommandNodeTest {
|
||||
|
||||
@Test
|
||||
public void testEquals() throws Exception {
|
||||
Command command = mock(Command.class);
|
||||
|
||||
new EqualsTester()
|
||||
.addEqualityGroup(
|
||||
literal("foo").build(),
|
||||
literal("foo").build()
|
||||
)
|
||||
.addEqualityGroup(
|
||||
literal("bar").executes(command).build(),
|
||||
literal("bar").executes(command).build()
|
||||
)
|
||||
.addEqualityGroup(
|
||||
literal("bar").build(),
|
||||
literal("bar").build()
|
||||
|
||||
@@ -9,9 +9,14 @@ import static net.minecraft.commands.builder.LiteralArgumentBuilder.literal;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class RootCommandNodeTest {
|
||||
public class RootCommandNodeTest extends AbstractCommandNodeTest {
|
||||
RootCommandNode node;
|
||||
|
||||
@Override
|
||||
protected CommandNode getCommandNode() {
|
||||
return node;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
node = new RootCommandNode();
|
||||
@@ -22,6 +27,11 @@ public class RootCommandNodeTest {
|
||||
assertThat(node.parse("foo bar baz", new CommandContextBuilder()), is("foo bar baz"));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void testAddChildNoRoot() throws Exception {
|
||||
node.addChild(new RootCommandNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() throws Exception {
|
||||
new EqualsTester()
|
||||
|
||||
Reference in New Issue
Block a user