Added some tests

This commit is contained in:
NeunEinser 2018-10-25 19:41:58 +02:00
parent 93867b16d2
commit dd66a90d6b
6 changed files with 40 additions and 1 deletions

View file

@ -45,6 +45,10 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
return type;
}
public T getDefaultValue() {
return defaultValue;
}
@Override
public String getName() {
return name;
@ -110,6 +114,7 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
if (!name.equals(that.name)) return false;
if (!type.equals(that.type)) return false;
if (defaultValue != null ? !defaultValue.equals(that.defaultValue) : that.defaultValue != null) return false;
return super.equals(o);
}

View file

@ -141,7 +141,7 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
if (!children.equals(that.children)) return false;
if (command != null ? !command.equals(that.command) : that.command != null) return false;
return true;
return isDefaultNode == that.isDefaultNode;
}
@Override

View file

@ -15,6 +15,7 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.defaultLiteral;
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
import static org.hamcrest.Matchers.equalTo;
@ -70,6 +71,15 @@ public class CommandDispatcherTest {
verify(command).run(any(CommandContext.class));
}
@SuppressWarnings("unchecked")
@Test
public void testCreateAndExecuteDefaultCommand() throws Exception {
subject.register(literal("foo").then(defaultLiteral("bar").executes(command)));
assertThat(subject.execute("foo", source), is(42));
verify(command).run(any(CommandContext.class));
}
@SuppressWarnings("unchecked")
@Test
public void testCreateAndMergeCommands() throws Exception {

View file

@ -31,6 +31,7 @@ public class LiteralArgumentBuilderTest {
final LiteralCommandNode<Object> node = builder.build();
assertThat(node.getLiteral(), is("foo"));
assertThat(node.isDefaultNode(), is(false));
}
@Test

View file

@ -14,6 +14,7 @@ import org.mockito.runners.MockitoJUnitRunner;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.defaultArgument;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
@ -38,6 +39,17 @@ public class RequiredArgumentBuilderTest {
assertThat(node.getName(), is("foo"));
assertThat(node.getType(), is(type));
assertThat(node.isDefaultNode(), is(false));
}
@Test
public void testBuildDefaultNode() throws Exception {
final ArgumentCommandNode<Object, Integer> node = defaultArgument("foo", type, 42).build();
assertThat(node.getName(), is("foo"));
assertThat(node.getType(), is(type));
assertThat(node.isDefaultNode(), is(true));
assertThat(node.getDefaultValue(), is(42));
}
@Test

View file

@ -16,6 +16,7 @@ import org.junit.Test;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.defaultArgument;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
@ -44,6 +45,16 @@ public class ArgumentCommandNodeTest extends CommandNodeTest {
assertThat(contextBuilder.getArguments().get("foo").getResult(), is(123));
}
@Test
public void testParseDefaultNode() throws Exception {
final StringReader reader = new StringReader("");
final ArgumentCommandNode<Object, Integer> node = defaultArgument("foo", integer(), 42).build();
node.parse(reader, contextBuilder);
assertThat(contextBuilder.getArguments().containsKey("foo"), is(true));
assertThat(contextBuilder.getArguments().get("foo").getResult(), is(42));
}
@Test
public void testUsage() throws Exception {
assertThat(node.getUsageText(), is("<foo>"));