Fixed suffix with empty input
This commit is contained in:
parent
e82d657d26
commit
daea2d76d7
2 changed files with 39 additions and 1 deletions
|
@ -55,6 +55,11 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
|
|||
if (end > -1) {
|
||||
raw = command.substring(0, end);
|
||||
}
|
||||
|
||||
if (raw.length() < suffix.length()) {
|
||||
throw ERROR_WRONG_SUFFIX.create(this.suffix);
|
||||
}
|
||||
|
||||
String number = raw.substring(0, raw.length() - suffix.length());
|
||||
String suffix = raw.substring(number.length());
|
||||
|
||||
|
@ -74,7 +79,7 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
|
|||
|
||||
return new FixedParsedArgument<>(raw, value);
|
||||
} catch (NumberFormatException ignored) {
|
||||
throw ERROR_NOT_A_NUMBER.create(raw);
|
||||
throw ERROR_NOT_A_NUMBER.create(number);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -97,6 +97,39 @@ public class IntegerArgumentTypeTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseEmpty() throws Exception {
|
||||
try {
|
||||
type.parse("", new CommandContextBuilder<>(dispatcher, source));
|
||||
fail();
|
||||
} catch (CommandException ex) {
|
||||
assertThat(ex.getType(), is(IntegerArgumentType.ERROR_NOT_A_NUMBER));
|
||||
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("found", "")));
|
||||
}
|
||||
}
|
||||
|
||||
@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(IntegerArgumentType.ERROR_WRONG_SUFFIX));
|
||||
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("suffix", "L")));
|
||||
}
|
||||
}
|
||||
|
||||
@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(IntegerArgumentType.ERROR_NOT_A_NUMBER));
|
||||
assertThat(ex.getData(), is(ImmutableMap.<String, Object>of("found", "")));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseTooLow() throws Exception {
|
||||
try {
|
||||
|
|
Loading…
Reference in a new issue