From dd66a90d6b022a3cfa4c799ffd180da1feb0c623 Mon Sep 17 00:00:00 2001 From: NeunEinser Date: Thu, 25 Oct 2018 19:41:58 +0200 Subject: [PATCH] Added some tests --- .../mojang/brigadier/tree/ArgumentCommandNode.java | 5 +++++ .../java/com/mojang/brigadier/tree/CommandNode.java | 2 +- .../com/mojang/brigadier/CommandDispatcherTest.java | 10 ++++++++++ .../builder/LiteralArgumentBuilderTest.java | 1 + .../builder/RequiredArgumentBuilderTest.java | 12 ++++++++++++ .../brigadier/tree/ArgumentCommandNodeTest.java | 11 +++++++++++ 6 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/mojang/brigadier/tree/ArgumentCommandNode.java b/src/main/java/com/mojang/brigadier/tree/ArgumentCommandNode.java index 5daab40..05d8d40 100644 --- a/src/main/java/com/mojang/brigadier/tree/ArgumentCommandNode.java +++ b/src/main/java/com/mojang/brigadier/tree/ArgumentCommandNode.java @@ -45,6 +45,10 @@ public class ArgumentCommandNode extends CommandNode { return type; } + public T getDefaultValue() { + return defaultValue; + } + @Override public String getName() { return name; @@ -110,6 +114,7 @@ public class ArgumentCommandNode extends CommandNode { 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); } diff --git a/src/main/java/com/mojang/brigadier/tree/CommandNode.java b/src/main/java/com/mojang/brigadier/tree/CommandNode.java index 124360c..8320c74 100644 --- a/src/main/java/com/mojang/brigadier/tree/CommandNode.java +++ b/src/main/java/com/mojang/brigadier/tree/CommandNode.java @@ -141,7 +141,7 @@ public abstract class CommandNode implements Comparable> { 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 diff --git a/src/test/java/com/mojang/brigadier/CommandDispatcherTest.java b/src/test/java/com/mojang/brigadier/CommandDispatcherTest.java index f2bab4a..ff8127f 100644 --- a/src/test/java/com/mojang/brigadier/CommandDispatcherTest.java +++ b/src/test/java/com/mojang/brigadier/CommandDispatcherTest.java @@ -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 { diff --git a/src/test/java/com/mojang/brigadier/builder/LiteralArgumentBuilderTest.java b/src/test/java/com/mojang/brigadier/builder/LiteralArgumentBuilderTest.java index 1c002f6..4bcea50 100644 --- a/src/test/java/com/mojang/brigadier/builder/LiteralArgumentBuilderTest.java +++ b/src/test/java/com/mojang/brigadier/builder/LiteralArgumentBuilderTest.java @@ -31,6 +31,7 @@ public class LiteralArgumentBuilderTest { final LiteralCommandNode node = builder.build(); assertThat(node.getLiteral(), is("foo")); + assertThat(node.isDefaultNode(), is(false)); } @Test diff --git a/src/test/java/com/mojang/brigadier/builder/RequiredArgumentBuilderTest.java b/src/test/java/com/mojang/brigadier/builder/RequiredArgumentBuilderTest.java index 6b5cc40..d57ca55 100644 --- a/src/test/java/com/mojang/brigadier/builder/RequiredArgumentBuilderTest.java +++ b/src/test/java/com/mojang/brigadier/builder/RequiredArgumentBuilderTest.java @@ -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 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 diff --git a/src/test/java/com/mojang/brigadier/tree/ArgumentCommandNodeTest.java b/src/test/java/com/mojang/brigadier/tree/ArgumentCommandNodeTest.java index f8c28d9..5e9c5e3 100644 --- a/src/test/java/com/mojang/brigadier/tree/ArgumentCommandNodeTest.java +++ b/src/test/java/com/mojang/brigadier/tree/ArgumentCommandNodeTest.java @@ -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 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(""));