Fixed up tests to use the new reader in type parsing
This commit is contained in:
parent
9e481e3f24
commit
d2a5eeedc5
3 changed files with 43 additions and 218 deletions
|
@ -8,6 +8,7 @@ import com.mojang.brigadier.exceptions.CommandException;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public interface ArgumentType<T> {
|
public interface ArgumentType<T> {
|
||||||
|
@Deprecated
|
||||||
default <S> ParsedArgument<S, T> parse(String command, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
default <S> ParsedArgument<S, T> parse(String command, CommandContextBuilder<S> contextBuilder) throws CommandException {
|
||||||
StringReader reader = new StringReader(command);
|
StringReader reader = new StringReader(command);
|
||||||
T result = parse(reader, contextBuilder);
|
T result = parse(reader, contextBuilder);
|
||||||
|
|
|
@ -3,11 +3,8 @@ package com.mojang.brigadier.arguments;
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
import com.google.common.testing.EqualsTester;
|
import com.google.common.testing.EqualsTester;
|
||||||
import com.mojang.brigadier.CommandDispatcher;
|
|
||||||
import com.mojang.brigadier.StringReader;
|
import com.mojang.brigadier.StringReader;
|
||||||
import com.mojang.brigadier.context.CommandContext;
|
|
||||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||||
import com.mojang.brigadier.context.ParsedArgument;
|
|
||||||
import com.mojang.brigadier.exceptions.CommandException;
|
import com.mojang.brigadier.exceptions.CommandException;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -16,7 +13,6 @@ import org.mockito.Mock;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.mockito.runners.MockitoJUnitRunner;
|
import org.mockito.runners.MockitoJUnitRunner;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
|
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
|
||||||
|
@ -31,9 +27,7 @@ import static org.junit.Assert.fail;
|
||||||
public class IntegerArgumentTypeTest {
|
public class IntegerArgumentTypeTest {
|
||||||
private IntegerArgumentType type;
|
private IntegerArgumentType type;
|
||||||
@Mock
|
@Mock
|
||||||
private Object source;
|
private CommandContextBuilder<Object> context;
|
||||||
@Mock
|
|
||||||
private CommandDispatcher<Object> dispatcher;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
|
@ -41,139 +35,67 @@ public class IntegerArgumentTypeTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParse() throws Exception {
|
public void parse_noSuffix() throws Exception {
|
||||||
|
StringReader reader = new StringReader("15");
|
||||||
ParsedArgument<Object, Integer> result = type.parse("50", new CommandContextBuilder<>(dispatcher, source));
|
assertThat(integer().parse(reader, context), is(15));
|
||||||
|
assertThat(reader.canRead(), is(false));
|
||||||
assertThat(result.getRaw(), is("50"));
|
|
||||||
assertThat(result.getResult(), is(50));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParse_suffix() throws Exception {
|
public void parse_suffix() throws Exception {
|
||||||
ParsedArgument<Object, Integer> result = integer(0, 100, "L").parse("50L", new CommandContextBuilder<>(dispatcher, source));
|
StringReader reader = new StringReader("15L");
|
||||||
|
assertThat(integer(0, 100, "L").parse(reader, context), is(15));
|
||||||
assertThat(result.getRaw(), is("50L"));
|
assertThat(reader.canRead(), is(false));
|
||||||
assertThat(result.getResult(), is(50));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParse_noSuffix() throws Exception {
|
public void parse_suffix_incorrect() throws Exception {
|
||||||
|
StringReader reader = new StringReader("15W");
|
||||||
try {
|
try {
|
||||||
integer(0, 0, "L").parse("50", new CommandContextBuilder<>(dispatcher, source));
|
integer(0, 100, "L").parse(reader, context);
|
||||||
fail();
|
fail();
|
||||||
} catch (CommandException ex) {
|
} catch (CommandException ex) {
|
||||||
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_WRONG_SUFFIX));
|
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_WRONG_SUFFIX));
|
||||||
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("suffix", "L")));
|
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("suffix", "L")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParse_wrongSuffix() throws Exception {
|
public void parse_suffix_missing() throws Exception {
|
||||||
|
StringReader reader = new StringReader("15");
|
||||||
try {
|
try {
|
||||||
integer(0, 0, "L").parse("50B", new CommandContextBuilder<>(dispatcher, source));
|
integer(0, 100, "L").parse(reader, context);
|
||||||
fail();
|
fail();
|
||||||
} catch (CommandException ex) {
|
} catch (CommandException ex) {
|
||||||
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_WRONG_SUFFIX));
|
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_WRONG_SUFFIX));
|
||||||
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("suffix", "L")));
|
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("suffix", "L")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParse_unexpectedSuffix() throws Exception {
|
public void parse_tooSmall() throws Exception {
|
||||||
type.parse("50L", new CommandContextBuilder<>(dispatcher, source));
|
StringReader reader = new StringReader("-5");
|
||||||
// This has to pass, it's the responsibility of a node to decide "this isn't right, it's followed by text!"
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseInvalid() throws Exception {
|
|
||||||
try {
|
try {
|
||||||
type.parse("fifty", new CommandContextBuilder<>(dispatcher, source));
|
integer(0, 100).parse(reader, context);
|
||||||
fail();
|
|
||||||
} catch (CommandException ex) {
|
|
||||||
assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_INT));
|
|
||||||
assertThat(ex.getData(), is(Collections.emptyMap()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseEmpty() throws Exception {
|
|
||||||
try {
|
|
||||||
type.parse("", new CommandContextBuilder<>(dispatcher, source));
|
|
||||||
fail();
|
|
||||||
} catch (CommandException ex) {
|
|
||||||
assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_INT));
|
|
||||||
assertThat(ex.getData(), is(Collections.emptyMap()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseEmpty_suffix() throws Exception {
|
|
||||||
try {
|
|
||||||
integer(0, 100, "L").parse("", new CommandContextBuilder<>(dispatcher, source));
|
|
||||||
fail();
|
|
||||||
} catch (CommandException ex) {
|
|
||||||
assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_INT));
|
|
||||||
assertThat(ex.getData(), is(Collections.emptyMap()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testPars_suffix_onlySuffix() throws Exception {
|
|
||||||
try {
|
|
||||||
integer(0, 100, "L").parse("L", new CommandContextBuilder<>(dispatcher, source));
|
|
||||||
fail();
|
|
||||||
} catch (CommandException ex) {
|
|
||||||
assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_INT));
|
|
||||||
assertThat(ex.getData(), is(Collections.emptyMap()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseTooLow() throws Exception {
|
|
||||||
try {
|
|
||||||
type.parse("-101", new CommandContextBuilder<>(dispatcher, source));
|
|
||||||
fail();
|
fail();
|
||||||
} catch (CommandException ex) {
|
} catch (CommandException ex) {
|
||||||
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_TOO_SMALL));
|
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_TOO_SMALL));
|
||||||
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("found", -101, "minimum", -100)));
|
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("found", -5, "minimum", 0)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParseLowerLimit() throws Exception {
|
public void parse_tooBig() throws Exception {
|
||||||
ParsedArgument<Object, Integer> result = type.parse("-100", new CommandContextBuilder<>(dispatcher, source));
|
StringReader reader = new StringReader("5");
|
||||||
|
|
||||||
assertThat(result.getRaw(), is("-100"));
|
|
||||||
assertThat(result.getResult(), is(-100));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseTooHigh() throws Exception {
|
|
||||||
try {
|
try {
|
||||||
type.parse("101", new CommandContextBuilder<>(dispatcher, source));
|
integer(-100, 0).parse(reader, context);
|
||||||
fail();
|
fail();
|
||||||
} catch (CommandException ex) {
|
} catch (CommandException ex) {
|
||||||
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_TOO_BIG));
|
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_TOO_BIG));
|
||||||
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("found", 101, "maximum", 100)));
|
assertThat(ex.getData(), equalTo(ImmutableMap.<String, Object>of("found", 5, "maximum", 0)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseHigherLimit() throws Exception {
|
|
||||||
ParsedArgument<Object, Integer> result = type.parse("100", new CommandContextBuilder<>(dispatcher, source));
|
|
||||||
|
|
||||||
assertThat(result.getRaw(), is("100"));
|
|
||||||
assertThat(result.getResult(), is(100));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetInteger() throws Exception {
|
|
||||||
CommandContext context = new CommandContextBuilder<>(dispatcher, new Object()).withArgument("foo", type.parse("100", new CommandContextBuilder<>(dispatcher, source))).build();
|
|
||||||
|
|
||||||
assertThat(IntegerArgumentType.getInteger(context, "foo"), is(100));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSuggestions() throws Exception {
|
public void testSuggestions() throws Exception {
|
||||||
Set<String> set = Sets.newHashSet();
|
Set<String> set = Sets.newHashSet();
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
package com.mojang.brigadier.arguments;
|
package com.mojang.brigadier.arguments;
|
||||||
|
|
||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
import com.mojang.brigadier.CommandDispatcher;
|
import com.mojang.brigadier.StringReader;
|
||||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||||
import com.mojang.brigadier.context.ParsedArgument;
|
|
||||||
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.Mockito;
|
|
||||||
import org.mockito.runners.MockitoJUnitRunner;
|
import org.mockito.runners.MockitoJUnitRunner;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -21,138 +19,42 @@ import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.hasToString;
|
import static org.hamcrest.Matchers.hasToString;
|
||||||
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.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class StringArgumentTypeTest {
|
public class StringArgumentTypeTest {
|
||||||
private StringArgumentType type;
|
|
||||||
@Mock
|
@Mock
|
||||||
private Object source;
|
private CommandContextBuilder<Object> context;
|
||||||
@Mock
|
|
||||||
private CommandDispatcher<Object> dispatcher;
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParseWord() throws Exception {
|
public void testParseWord() throws Exception {
|
||||||
type = word();
|
StringReader reader = mock(StringReader.class);
|
||||||
ParsedArgument<Object, String> result = type.parse("hello world", new CommandContextBuilder<>(dispatcher, source));
|
when(reader.readUnquotedString()).thenReturn("hello");
|
||||||
|
assertThat(word().parse(reader, context), equalTo("hello"));
|
||||||
assertThat(result.getRaw(), is("hello"));
|
verify(reader).readUnquotedString();
|
||||||
assertThat(result.getResult(), is("hello"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseWord_empty() throws Exception {
|
|
||||||
type = word();
|
|
||||||
ParsedArgument<Object, String> result = type.parse("", new CommandContextBuilder<>(dispatcher, source));
|
|
||||||
|
|
||||||
assertThat(result.getRaw(), is(""));
|
|
||||||
assertThat(result.getResult(), is(""));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseWord_simple() throws Exception {
|
|
||||||
type = word();
|
|
||||||
ParsedArgument<Object, String> result = type.parse("hello", new CommandContextBuilder<>(dispatcher, source));
|
|
||||||
|
|
||||||
assertThat(result.getRaw(), is("hello"));
|
|
||||||
assertThat(result.getResult(), is("hello"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParseString() throws Exception {
|
public void testParseString() throws Exception {
|
||||||
type = string();
|
StringReader reader = mock(StringReader.class);
|
||||||
ParsedArgument<Object, String> result = type.parse("hello world", new CommandContextBuilder<>(dispatcher, source));
|
when(reader.readString()).thenReturn("hello world");
|
||||||
|
assertThat(string().parse(reader, context), equalTo("hello world"));
|
||||||
assertThat(result.getRaw(), is("hello"));
|
verify(reader).readString();
|
||||||
assertThat(result.getResult(), is("hello"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParseGreedyString() throws Exception {
|
public void testParseGreedyString() throws Exception {
|
||||||
type = greedyString();
|
StringReader reader = new StringReader("Hello world! This is a test.");
|
||||||
ParsedArgument<Object, String> result = type.parse("hello world", new CommandContextBuilder<>(dispatcher, source));
|
assertThat(greedyString().parse(reader, context), equalTo("Hello world! This is a test."));
|
||||||
|
assertThat(reader.canRead(), is(false));
|
||||||
assertThat(result.getRaw(), is("hello world"));
|
|
||||||
assertThat(result.getResult(), is("hello world"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParse() throws Exception {
|
|
||||||
type = string();
|
|
||||||
ParsedArgument<Object, String> result = type.parse("hello", new CommandContextBuilder<>(dispatcher, source));
|
|
||||||
|
|
||||||
assertThat(result.getRaw(), is("hello"));
|
|
||||||
assertThat(result.getResult(), is("hello"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseWordQuoted() throws Exception {
|
|
||||||
type = word();
|
|
||||||
ParsedArgument<Object, String> result = type.parse("\"hello \\\" world\"", new CommandContextBuilder<>(dispatcher, source));
|
|
||||||
|
|
||||||
assertThat(result.getRaw(), is(""));
|
|
||||||
assertThat(result.getResult(), is(""));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseQuoted() throws Exception {
|
|
||||||
type = string();
|
|
||||||
ParsedArgument<Object, String> result = type.parse("\"hello \\\" world\"", new CommandContextBuilder<>(dispatcher, source));
|
|
||||||
|
|
||||||
assertThat(result.getRaw(), is("\"hello \\\" world\""));
|
|
||||||
assertThat(result.getResult(), is("hello \" world"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseQuotedWithRemaining() throws Exception {
|
|
||||||
type = string();
|
|
||||||
ParsedArgument<Object, String> result = type.parse("\"hello \\\" world\" with remaining", new CommandContextBuilder<>(dispatcher, source));
|
|
||||||
|
|
||||||
assertThat(result.getRaw(), is("\"hello \\\" world\""));
|
|
||||||
assertThat(result.getResult(), is("hello \" world"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseNotQuoted() throws Exception {
|
|
||||||
type = string();
|
|
||||||
ParsedArgument<Object, String> result = type.parse("hello world", new CommandContextBuilder<>(dispatcher, source));
|
|
||||||
|
|
||||||
assertThat(result.getRaw(), is("hello"));
|
|
||||||
assertThat(result.getResult(), is("hello"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseQuote_earlyUnquoteWithRemaining() throws Exception {
|
|
||||||
type = string();
|
|
||||||
ParsedArgument<Object, String> result = type.parse("\"hello\" world", new CommandContextBuilder<>(dispatcher, source));
|
|
||||||
|
|
||||||
assertThat(result.getRaw(), is("\"hello\""));
|
|
||||||
assertThat(result.getResult(), is("hello"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseQuote_lateQuoteWithRemaining() throws Exception {
|
|
||||||
type = string();
|
|
||||||
ParsedArgument<Object, String> result = type.parse("hello \"world\"", new CommandContextBuilder<>(dispatcher, source));
|
|
||||||
|
|
||||||
assertThat(result.getRaw(), is("hello"));
|
|
||||||
assertThat(result.getResult(), is("hello"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseEmpty() throws Exception {
|
|
||||||
type = string();
|
|
||||||
ParsedArgument<Object, String> result = type.parse("", new CommandContextBuilder<>(dispatcher, source));
|
|
||||||
|
|
||||||
assertThat(result.getRaw(), is(""));
|
|
||||||
assertThat(result.getResult(), is(""));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSuggestions() throws Exception {
|
public void testSuggestions() throws Exception {
|
||||||
type = string();
|
|
||||||
Set<String> set = Sets.newHashSet();
|
Set<String> set = Sets.newHashSet();
|
||||||
@SuppressWarnings("unchecked") final CommandContextBuilder<Object> context = Mockito.mock(CommandContextBuilder.class);
|
string().listSuggestions("", set, context);
|
||||||
type.listSuggestions("", set, context);
|
|
||||||
assertThat(set, is(empty()));
|
assertThat(set, is(empty()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue