diff --git a/src/main/java/com/mojang/brigadier/arguments/ArgumentType.java b/src/main/java/com/mojang/brigadier/arguments/ArgumentType.java index 0b05767..a733d88 100644 --- a/src/main/java/com/mojang/brigadier/arguments/ArgumentType.java +++ b/src/main/java/com/mojang/brigadier/arguments/ArgumentType.java @@ -8,6 +8,7 @@ import com.mojang.brigadier.exceptions.CommandException; import java.util.Set; public interface ArgumentType { + @Deprecated default ParsedArgument parse(String command, CommandContextBuilder contextBuilder) throws CommandException { StringReader reader = new StringReader(command); T result = parse(reader, contextBuilder); diff --git a/src/test/java/com/mojang/brigadier/arguments/IntegerArgumentTypeTest.java b/src/test/java/com/mojang/brigadier/arguments/IntegerArgumentTypeTest.java index 9f506a6..35f7e46 100644 --- a/src/test/java/com/mojang/brigadier/arguments/IntegerArgumentTypeTest.java +++ b/src/test/java/com/mojang/brigadier/arguments/IntegerArgumentTypeTest.java @@ -3,11 +3,8 @@ package com.mojang.brigadier.arguments; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Sets; import com.google.common.testing.EqualsTester; -import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.StringReader; -import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContextBuilder; -import com.mojang.brigadier.context.ParsedArgument; import com.mojang.brigadier.exceptions.CommandException; import org.junit.Before; import org.junit.Test; @@ -16,7 +13,6 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; -import java.util.Collections; import java.util.Set; import static com.mojang.brigadier.arguments.IntegerArgumentType.integer; @@ -31,9 +27,7 @@ import static org.junit.Assert.fail; public class IntegerArgumentTypeTest { private IntegerArgumentType type; @Mock - private Object source; - @Mock - private CommandDispatcher dispatcher; + private CommandContextBuilder context; @Before public void setUp() throws Exception { @@ -41,139 +35,67 @@ public class IntegerArgumentTypeTest { } @Test - public void testParse() throws Exception { - - ParsedArgument result = type.parse("50", new CommandContextBuilder<>(dispatcher, source)); - - assertThat(result.getRaw(), is("50")); - assertThat(result.getResult(), is(50)); + public void parse_noSuffix() throws Exception { + StringReader reader = new StringReader("15"); + assertThat(integer().parse(reader, context), is(15)); + assertThat(reader.canRead(), is(false)); } @Test - public void testParse_suffix() throws Exception { - ParsedArgument result = integer(0, 100, "L").parse("50L", new CommandContextBuilder<>(dispatcher, source)); - - assertThat(result.getRaw(), is("50L")); - assertThat(result.getResult(), is(50)); + public void parse_suffix() throws Exception { + StringReader reader = new StringReader("15L"); + assertThat(integer(0, 100, "L").parse(reader, context), is(15)); + assertThat(reader.canRead(), is(false)); } @Test - public void testParse_noSuffix() throws Exception { + public void parse_suffix_incorrect() throws Exception { + StringReader reader = new StringReader("15W"); try { - integer(0, 0, "L").parse("50", new CommandContextBuilder<>(dispatcher, source)); + integer(0, 100, "L").parse(reader, context); fail(); } catch (CommandException ex) { assertThat(ex.getType(), is(IntegerArgumentType.ERROR_WRONG_SUFFIX)); - assertThat(ex.getData(), is(ImmutableMap.of("suffix", "L"))); + assertThat(ex.getData(), equalTo(ImmutableMap.of("suffix", "L"))); } } @Test - public void testParse_wrongSuffix() throws Exception { + public void parse_suffix_missing() throws Exception { + StringReader reader = new StringReader("15"); try { - integer(0, 0, "L").parse("50B", new CommandContextBuilder<>(dispatcher, source)); + integer(0, 100, "L").parse(reader, context); fail(); } catch (CommandException ex) { assertThat(ex.getType(), is(IntegerArgumentType.ERROR_WRONG_SUFFIX)); - assertThat(ex.getData(), is(ImmutableMap.of("suffix", "L"))); + assertThat(ex.getData(), equalTo(ImmutableMap.of("suffix", "L"))); } } @Test - public void testParse_unexpectedSuffix() throws Exception { - type.parse("50L", new CommandContextBuilder<>(dispatcher, source)); - // 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 { + public void parse_tooSmall() throws Exception { + StringReader reader = new StringReader("-5"); try { - type.parse("fifty", 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() 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)); + integer(0, 100).parse(reader, context); fail(); } catch (CommandException ex) { assertThat(ex.getType(), is(IntegerArgumentType.ERROR_TOO_SMALL)); - assertThat(ex.getData(), is(ImmutableMap.of("found", -101, "minimum", -100))); + assertThat(ex.getData(), equalTo(ImmutableMap.of("found", -5, "minimum", 0))); } } @Test - public void testParseLowerLimit() throws Exception { - ParsedArgument result = type.parse("-100", new CommandContextBuilder<>(dispatcher, source)); - - assertThat(result.getRaw(), is("-100")); - assertThat(result.getResult(), is(-100)); - } - - @Test - public void testParseTooHigh() throws Exception { + public void parse_tooBig() throws Exception { + StringReader reader = new StringReader("5"); try { - type.parse("101", new CommandContextBuilder<>(dispatcher, source)); + integer(-100, 0).parse(reader, context); fail(); } catch (CommandException ex) { assertThat(ex.getType(), is(IntegerArgumentType.ERROR_TOO_BIG)); - assertThat(ex.getData(), is(ImmutableMap.of("found", 101, "maximum", 100))); + assertThat(ex.getData(), equalTo(ImmutableMap.of("found", 5, "maximum", 0))); } } - @Test - public void testParseHigherLimit() throws Exception { - ParsedArgument 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 public void testSuggestions() throws Exception { Set set = Sets.newHashSet(); diff --git a/src/test/java/com/mojang/brigadier/arguments/StringArgumentTypeTest.java b/src/test/java/com/mojang/brigadier/arguments/StringArgumentTypeTest.java index 7796e28..35c4547 100644 --- a/src/test/java/com/mojang/brigadier/arguments/StringArgumentTypeTest.java +++ b/src/test/java/com/mojang/brigadier/arguments/StringArgumentTypeTest.java @@ -1,13 +1,11 @@ package com.mojang.brigadier.arguments; 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.ParsedArgument; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; 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.is; 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) public class StringArgumentTypeTest { - private StringArgumentType type; @Mock - private Object source; - @Mock - private CommandDispatcher dispatcher; + private CommandContextBuilder context; @Test public void testParseWord() throws Exception { - type = word(); - ParsedArgument result = type.parse("hello world", new CommandContextBuilder<>(dispatcher, source)); - - assertThat(result.getRaw(), is("hello")); - assertThat(result.getResult(), is("hello")); - } - - @Test - public void testParseWord_empty() throws Exception { - type = word(); - ParsedArgument 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 result = type.parse("hello", new CommandContextBuilder<>(dispatcher, source)); - - assertThat(result.getRaw(), is("hello")); - assertThat(result.getResult(), is("hello")); + StringReader reader = mock(StringReader.class); + when(reader.readUnquotedString()).thenReturn("hello"); + assertThat(word().parse(reader, context), equalTo("hello")); + verify(reader).readUnquotedString(); } @Test public void testParseString() throws Exception { - type = string(); - ParsedArgument result = type.parse("hello world", new CommandContextBuilder<>(dispatcher, source)); - - assertThat(result.getRaw(), is("hello")); - assertThat(result.getResult(), is("hello")); + StringReader reader = mock(StringReader.class); + when(reader.readString()).thenReturn("hello world"); + assertThat(string().parse(reader, context), equalTo("hello world")); + verify(reader).readString(); } @Test public void testParseGreedyString() throws Exception { - type = greedyString(); - ParsedArgument result = type.parse("hello world", new CommandContextBuilder<>(dispatcher, source)); - - assertThat(result.getRaw(), is("hello world")); - assertThat(result.getResult(), is("hello world")); - } - - @Test - public void testParse() throws Exception { - type = string(); - ParsedArgument 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 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 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 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 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 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 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 result = type.parse("", new CommandContextBuilder<>(dispatcher, source)); - - assertThat(result.getRaw(), is("")); - assertThat(result.getResult(), is("")); + StringReader reader = new StringReader("Hello world! This is a test."); + assertThat(greedyString().parse(reader, context), equalTo("Hello world! This is a test.")); + assertThat(reader.canRead(), is(false)); } @Test public void testSuggestions() throws Exception { - type = string(); Set set = Sets.newHashSet(); - @SuppressWarnings("unchecked") final CommandContextBuilder context = Mockito.mock(CommandContextBuilder.class); - type.listSuggestions("", set, context); + string().listSuggestions("", set, context); assertThat(set, is(empty())); }