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

View file

@ -30,7 +30,7 @@ public class LiteralCommandNode extends CommandNode {
String expected = literal + (command.length() > literal.length() ? CommandDispatcher.ARGUMENT_SEPARATOR : "");
if (!command.startsWith(expected)) {
throw ERROR_INCORRECT_LITERAL.create(expected);
throw ERROR_INCORRECT_LITERAL.create(literal);
}
int start = expected.length();

View file

@ -1,20 +1,27 @@
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.exceptions.CommandException;
import com.mojang.brigadier.exceptions.CommandExceptionType;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
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.builder.LiteralArgumentBuilder.literal;
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.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class CommandDispatcherTest {
@ -77,15 +84,28 @@ public class CommandDispatcherTest {
verify(three).run(any(CommandContext.class));
}
@Test(expected = CommandException.class)
@Test
public void testExecuteUnknownCommand() throws Exception {
subject.execute("foo", source);
try {
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 {
subject.register(literal("foo").executes(command));
subject.execute("foo bar", source);
try {
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
@ -104,12 +124,18 @@ public class CommandDispatcherTest {
verify(subCommand).run(any(CommandContext.class));
}
@Test(expected = CommandException.class)
@Test
public void testExecuteInvalidSubcommand() throws Exception {
subject.register(literal("foo").then(
argument("bar", integer())
).executes(command));
subject.execute("foo bar", source);
try {
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")));
}
}
}

View file

@ -1,20 +1,26 @@
package com.mojang.brigadier.tree;
import com.google.common.collect.ImmutableMap;
import com.google.common.testing.EqualsTester;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.exceptions.CommandExceptionType;
import org.junit.Before;
import org.junit.Test;
import java.util.Map;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
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.Mockito.mock;
public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
ArgumentCommandNode node;
ArgumentCommandNode<Integer> node;
CommandContextBuilder<Object> contextBuilder;
@Override
@ -44,9 +50,15 @@ public class ArgumentCommandNodeTest extends AbstractCommandNodeTest {
assertThat(contextBuilder.getArguments().get("foo").getResult(), is((Object) 123));
}
@Test(expected = CommandException.class)
@Test
public void testParseInvalid() throws Exception {
node.parse("foo", contextBuilder);
try {
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

View file

@ -1,15 +1,20 @@
package com.mojang.brigadier.tree;
import com.google.common.collect.ImmutableMap;
import com.google.common.testing.EqualsTester;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.exceptions.CommandExceptionType;
import org.junit.Before;
import org.junit.Test;
import java.util.Map;
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
@ -37,14 +42,26 @@ public class LiteralCommandNodeTest extends AbstractCommandNodeTest {
assertThat(node.parse("foo", contextBuilder), is(""));
}
@Test(expected = CommandException.class)
@Test
public void testParseSimilar() throws Exception {
node.parse("foobar", contextBuilder);
try {
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 {
node.parse("bar", contextBuilder);
try {
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