Changed tests to ensure exceptions are of right type & right details

This commit is contained in:
Nathan Adams
2014-10-03 12:45:19 +02:00
parent ef4199e824
commit b3703db535
4 changed files with 72 additions and 17 deletions
@@ -30,7 +30,7 @@ public class LiteralCommandNode extends CommandNode {
String expected = literal + (command.length() > literal.length() ? CommandDispatcher.ARGUMENT_SEPARATOR : ""); String expected = literal + (command.length() > literal.length() ? CommandDispatcher.ARGUMENT_SEPARATOR : "");
if (!command.startsWith(expected)) { if (!command.startsWith(expected)) {
throw ERROR_INCORRECT_LITERAL.create(expected); throw ERROR_INCORRECT_LITERAL.create(literal);
} }
int start = expected.length(); int start = expected.length();
@@ -1,20 +1,27 @@
package com.mojang.brigadier; package com.mojang.brigadier;
import com.google.common.collect.ImmutableMap;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandException; import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.exceptions.CommandExceptionType;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import java.util.Collections;
import java.util.Map;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal; import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument; import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.*;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class CommandDispatcherTest { public class CommandDispatcherTest {
@@ -77,15 +84,28 @@ public class CommandDispatcherTest {
verify(three).run(any(CommandContext.class)); verify(three).run(any(CommandContext.class));
} }
@Test(expected = CommandException.class) @Test
public void testExecuteUnknownCommand() throws Exception { public void testExecuteUnknownCommand() throws Exception {
try {
subject.execute("foo", source); subject.execute("foo", source);
fail();
} catch (CommandException ex) {
assertThat(ex.getType(), is((CommandExceptionType) CommandDispatcher.ERROR_UNKNOWN_COMMAND));
assertThat(ex.getData(), is(Collections.<String, Object>emptyMap()));
}
} }
@Test(expected = CommandException.class) @Test
public void testExecuteUnknownSubcommand() throws Exception { public void testExecuteUnknownSubcommand() throws Exception {
subject.register(literal("foo").executes(command)); subject.register(literal("foo").executes(command));
try {
subject.execute("foo bar", source); subject.execute("foo bar", source);
fail();
} catch (CommandException ex) {
assertThat(ex.getType(), is((CommandExceptionType) CommandDispatcher.ERROR_UNKNOWN_COMMAND));
assertThat(ex.getData(), is(Collections.<String, Object>emptyMap()));
}
} }
@Test @Test
@@ -104,12 +124,18 @@ public class CommandDispatcherTest {
verify(subCommand).run(any(CommandContext.class)); verify(subCommand).run(any(CommandContext.class));
} }
@Test(expected = CommandException.class) @Test
public void testExecuteInvalidSubcommand() throws Exception { public void testExecuteInvalidSubcommand() throws Exception {
subject.register(literal("foo").then( subject.register(literal("foo").then(
argument("bar", integer()) argument("bar", integer())
).executes(command)); ).executes(command));
try {
subject.execute("foo bar", source); subject.execute("foo bar", source);
fail();
} catch (CommandException ex) {
assertThat(ex.getType(), is((CommandExceptionType) IntegerArgumentType.ERROR_NOT_A_NUMBER));
assertThat(ex.getData(), is((Map<String, Object>) ImmutableMap.<String, Object>of("found", "bar")));
}
} }
} }
@@ -1,20 +1,26 @@
package com.mojang.brigadier.tree; package com.mojang.brigadier.tree;
import com.google.common.collect.ImmutableMap;
import com.google.common.testing.EqualsTester; import com.google.common.testing.EqualsTester;
import com.mojang.brigadier.Command; import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandException; import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.exceptions.CommandExceptionType;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.Map;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument; import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
public class ArgumentCommandNodeTest extends AbstractCommandNodeTest { public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
ArgumentCommandNode node; ArgumentCommandNode<Integer> node;
CommandContextBuilder<Object> contextBuilder; CommandContextBuilder<Object> contextBuilder;
@Override @Override
@@ -44,9 +50,15 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
assertThat(contextBuilder.getArguments().get("foo").getResult(), is((Object) 123)); assertThat(contextBuilder.getArguments().get("foo").getResult(), is((Object) 123));
} }
@Test(expected = CommandException.class) @Test
public void testParseInvalid() throws Exception { public void testParseInvalid() throws Exception {
try {
node.parse("foo", contextBuilder); node.parse("foo", contextBuilder);
fail();
} catch (CommandException ex) {
assertThat(ex.getType(), is((CommandExceptionType) IntegerArgumentType.ERROR_NOT_A_NUMBER));
assertThat(ex.getData(), is((Map<String, Object>) ImmutableMap.<String, Object>of("found", "foo")));
}
} }
@Test @Test
@@ -1,15 +1,20 @@
package com.mojang.brigadier.tree; package com.mojang.brigadier.tree;
import com.google.common.collect.ImmutableMap;
import com.google.common.testing.EqualsTester; import com.google.common.testing.EqualsTester;
import com.mojang.brigadier.Command; import com.mojang.brigadier.Command;
import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandException; import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.exceptions.CommandExceptionType;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.Map;
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal; import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
public class LiteralCommandNodeTest extends AbstractCommandNodeTest { public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
@@ -37,14 +42,26 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
assertThat(node.parse("foo", contextBuilder), is("")); assertThat(node.parse("foo", contextBuilder), is(""));
} }
@Test(expected = CommandException.class) @Test
public void testParseSimilar() throws Exception { public void testParseSimilar() throws Exception {
try {
node.parse("foobar", contextBuilder); node.parse("foobar", contextBuilder);
fail();
} catch (CommandException ex) {
assertThat(ex.getType(), is((CommandExceptionType) LiteralCommandNode.ERROR_INCORRECT_LITERAL));
assertThat(ex.getData(), is((Map<String, Object>)ImmutableMap.<String, Object>of("expected", "foo")));
}
} }
@Test(expected = CommandException.class) @Test
public void testParseInvalid() throws Exception { public void testParseInvalid() throws Exception {
try {
node.parse("bar", contextBuilder); node.parse("bar", contextBuilder);
fail();
} catch (CommandException ex) {
assertThat(ex.getType(), is((CommandExceptionType) LiteralCommandNode.ERROR_INCORRECT_LITERAL));
assertThat(ex.getData(), is((Map<String, Object>)ImmutableMap.<String, Object>of("expected", "foo")));
}
} }
@Test @Test