feat(core): add unit tests for the standard parsers (#362)

* feat(core): add BooleanParser unit tests

* feat(core): add ByteParser unit tests

* feat(core): add IntegerParser, LongParser & ShortParser unit tests

* feat(core): add EnumParser unit tests

* feat(core): add CharacterParser unit tests

* feat(core): add DoubleParser & FloatParser unit tests

* feat(core): add UUIDParser & StringArrayParser unit tests
This commit is contained in:
Alexander Söderberg 2022-05-28 14:23:37 +02:00 committed by Jason
parent 46a3f7129f
commit 09044b9c1b
25 changed files with 1769 additions and 4 deletions

View file

@ -111,6 +111,7 @@ repositories {
dependencies { dependencies {
compileOnlyApi(libs.checkerQual) compileOnlyApi(libs.checkerQual)
testImplementation(libs.jupiterEngine) testImplementation(libs.jupiterEngine)
testImplementation(libs.jupiterParams)
testImplementation(libs.mockitoCore) testImplementation(libs.mockitoCore)
testImplementation(libs.mockitoKotlin) testImplementation(libs.mockitoKotlin)
testImplementation(libs.mockitoJupiter) testImplementation(libs.mockitoJupiter)

View file

@ -34,6 +34,7 @@ import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
import cloud.commandframework.exceptions.parsing.ParserException; import cloud.commandframework.exceptions.parsing.ParserException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Queue; import java.util.Queue;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
@ -163,7 +164,7 @@ public final class BooleanArgument<C> extends CommandArgument<C, Boolean> {
/** /**
* Construct a new boolean parser * Construct a new boolean parser
* *
* @param liberal Whether or not it'll accept boolean-esque strings, or just booleans * @param liberal Whether it'll accept boolean-esque strings, or just booleans
*/ */
public BooleanParser(final boolean liberal) { public BooleanParser(final boolean liberal) {
this.liberal = liberal; this.liberal = liberal;
@ -244,7 +245,7 @@ public final class BooleanArgument<C> extends CommandArgument<C, Boolean> {
* Construct a new boolean parse exception * Construct a new boolean parse exception
* *
* @param input Input * @param input Input
* @param liberal Whether or not the parser allows truthy and falsy values, or strictly true/false * @param liberal Whether the parser allows truthy and falsy values, or strictly true/false
* @param context Command context * @param context Command context
*/ */
public BooleanParseException( public BooleanParseException(
@ -268,7 +269,7 @@ public final class BooleanArgument<C> extends CommandArgument<C, Boolean> {
* *
* @return String value * @return String value
*/ */
public String getInput() { public @NonNull String getInput() {
return this.input; return this.input;
} }
@ -281,6 +282,23 @@ public final class BooleanArgument<C> extends CommandArgument<C, Boolean> {
return this.liberal; return this.liberal;
} }
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}
final BooleanParseException that = (BooleanParseException) o;
return this.liberal == that.liberal && this.input.equals(that.input);
}
@Override
public int hashCode() {
return Objects.hash(this.input, this.liberal);
}
} }
} }

View file

@ -31,6 +31,7 @@ import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.parsing.NoInputProvidedException; import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
import cloud.commandframework.exceptions.parsing.NumberParseException; import cloud.commandframework.exceptions.parsing.NumberParseException;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Queue; import java.util.Queue;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
@ -348,6 +349,23 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
return "byte"; return "byte";
} }
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}
final ByteParseException that = (ByteParseException) o;
return this.parser.equals(that.parser);
}
@Override
public int hashCode() {
return Objects.hash(this.parser);
}
} }
} }

View file

@ -33,6 +33,7 @@ import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.parsing.NoInputProvidedException; import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
import cloud.commandframework.exceptions.parsing.ParserException; import cloud.commandframework.exceptions.parsing.ParserException;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Queue; import java.util.Queue;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
@ -186,6 +187,23 @@ public final class CharArgument<C> extends CommandArgument<C, Character> {
return this.input; return this.input;
} }
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}
final CharParseException that = (CharParseException) o;
return this.input.equals(that.input);
}
@Override
public int hashCode() {
return Objects.hash(this.input);
}
} }
} }

View file

@ -31,6 +31,7 @@ import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.parsing.NoInputProvidedException; import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
import cloud.commandframework.exceptions.parsing.NumberParseException; import cloud.commandframework.exceptions.parsing.NumberParseException;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Queue; import java.util.Queue;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
@ -337,6 +338,23 @@ public final class DoubleArgument<C> extends CommandArgument<C, Double> {
return "double"; return "double";
} }
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}
final DoubleParseException that = (DoubleParseException) o;
return this.parser.equals(that.parser);
}
@Override
public int hashCode() {
return Objects.hash(this.parser);
}
} }
} }

View file

@ -34,6 +34,7 @@ import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
import cloud.commandframework.exceptions.parsing.ParserException; import cloud.commandframework.exceptions.parsing.ParserException;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Queue; import java.util.Queue;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -256,6 +257,23 @@ public class EnumArgument<C, E extends Enum<E>> extends CommandArgument<C, E> {
return this.enumClass; return this.enumClass;
} }
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}
final EnumParseException that = (EnumParseException) o;
return this.input.equals(that.input) && this.enumClass.equals(that.enumClass);
}
@Override
public int hashCode() {
return Objects.hash(this.input, this.enumClass);
}
} }
} }

View file

@ -31,6 +31,7 @@ import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.parsing.NoInputProvidedException; import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
import cloud.commandframework.exceptions.parsing.NumberParseException; import cloud.commandframework.exceptions.parsing.NumberParseException;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Queue; import java.util.Queue;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
@ -332,6 +333,23 @@ public final class FloatArgument<C> extends CommandArgument<C, Float> {
return "float"; return "float";
} }
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}
final FloatParseException that = (FloatParseException) o;
return this.parser.equals(that.parser);
}
@Override
public int hashCode() {
return Objects.hash(this.parser);
}
} }
} }

View file

@ -33,6 +33,7 @@ import cloud.commandframework.exceptions.parsing.NumberParseException;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Queue; import java.util.Queue;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
@ -395,6 +396,23 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
return "integer"; return "integer";
} }
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}
final IntegerParseException that = (IntegerParseException) o;
return this.parser.equals(that.parser);
}
@Override
public int hashCode() {
return Objects.hash(this.parser);
}
} }
} }

View file

@ -31,6 +31,7 @@ import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.parsing.NoInputProvidedException; import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
import cloud.commandframework.exceptions.parsing.NumberParseException; import cloud.commandframework.exceptions.parsing.NumberParseException;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Queue; import java.util.Queue;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
@ -340,6 +341,23 @@ public final class LongArgument<C> extends CommandArgument<C, Long> {
return "long"; return "long";
} }
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}
final LongParseException that = (LongParseException) o;
return this.parser.equals(that.parser);
}
@Override
public int hashCode() {
return Objects.hash(this.parser);
}
} }
} }

View file

@ -31,6 +31,7 @@ import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.parsing.NoInputProvidedException; import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
import cloud.commandframework.exceptions.parsing.NumberParseException; import cloud.commandframework.exceptions.parsing.NumberParseException;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Queue; import java.util.Queue;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
@ -337,6 +338,23 @@ public final class ShortArgument<C> extends CommandArgument<C, Short> {
return "short"; return "short";
} }
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}
final ShortParseException that = (ShortParseException) o;
return this.parser.equals(that.parser);
}
@Override
public int hashCode() {
return Objects.hash(this.parser);
}
} }
} }

View file

@ -33,6 +33,7 @@ import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.parsing.NoInputProvidedException; import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
import cloud.commandframework.exceptions.parsing.ParserException; import cloud.commandframework.exceptions.parsing.ParserException;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Queue; import java.util.Queue;
import java.util.UUID; import java.util.UUID;
import java.util.function.BiFunction; import java.util.function.BiFunction;
@ -192,6 +193,23 @@ public final class UUIDArgument<C> extends CommandArgument<C, UUID> {
return this.input; return this.input;
} }
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null ||this.getClass() != o.getClass()) {
return false;
}
final UUIDParseException that = (UUIDParseException) o;
return this.input.equals(that.input);
}
@Override
public int hashCode() {
return Objects.hash(this.input);
}
} }
} }

View file

@ -50,7 +50,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
* *
* @param <C> Command sender type * @param <C> Command sender type
*/ */
public final class CommandContext<C> { public class CommandContext<C> {
private final CaptionVariableReplacementHandler captionVariableReplacementHandler; private final CaptionVariableReplacementHandler captionVariableReplacementHandler;
private final Map<CommandArgument<C, ?>, ArgumentTiming> argumentTimings = new HashMap<>(); private final Map<CommandArgument<C, ?>, ArgumentTiming> argumentTimings = new HashMap<>();

View file

@ -0,0 +1,37 @@
//
// MIT License
//
// Copyright (c) 2021 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.arguments.standard;
import java.util.Arrays;
import java.util.LinkedList;
import org.checkerframework.checker.nullness.qual.NonNull;
final class ArgumentTestHelper {
static @NonNull LinkedList<@NonNull String> linkedListOf(
final @NonNull String... strings
) {
return new LinkedList<>(Arrays.asList(strings));
}
}

View file

@ -0,0 +1,155 @@
//
// MIT License
//
// Copyright (c) 2021 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.arguments.standard;
import cloud.commandframework.TestCommandSender;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.context.CommandContext;
import java.util.List;
import java.util.Locale;
import java.util.Queue;
import java.util.stream.Stream;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
@ExtendWith(MockitoExtension.class)
class BooleanParserTest {
@Mock
private CommandContext<TestCommandSender> context;
@ParameterizedTest
@MethodSource("Parse_NonLiberal_ValidInputs_SuccessfulParse_Source")
void Parse_NonLiberal_ValidInputs_SuccessfulParse(
final Queue<String> input,
final boolean expectedResult
) {
// Arrange
final BooleanArgument.BooleanParser<TestCommandSender> parser = new BooleanArgument.BooleanParser<>(false /* liberal */);
// Act
final ArgumentParseResult<Boolean> result = parser.parse(
this.context,
input
);
// Assert
assertThat(result.getFailure()).isEmpty();
assertThat(result.getParsedValue()).hasValue(expectedResult);
assertThat(input).isEmpty();
}
static Stream<Arguments> Parse_NonLiberal_ValidInputs_SuccessfulParse_Source() {
return Stream.of(
Arguments.arguments(ArgumentTestHelper.linkedListOf("true"), true),
Arguments.arguments(ArgumentTestHelper.linkedListOf("false"), false)
);
}
@ParameterizedTest
@MethodSource("Parse_Liberal_ValidInputs_SuccessfulParse_Source")
void Parse_Liberal_ValidInputs_SuccessfulParse(
final Queue<String> input,
final boolean expectedResult
) {
// Arrange
final BooleanArgument.BooleanParser<TestCommandSender> parser = new BooleanArgument.BooleanParser<>(true /* liberal */);
// Act
final ArgumentParseResult<Boolean> result = parser.parse(
this.context,
input
);
// Assert
assertThat(result.getFailure()).isEmpty();
assertThat(result.getParsedValue()).hasValue(expectedResult);
assertThat(input).isEmpty();
}
static Stream<Arguments> Parse_Liberal_ValidInputs_SuccessfulParse_Source() {
return Stream.concat(
Stream.of("true", "yes", "on")
.flatMap(input -> Stream.of(input, input.toUpperCase(Locale.ROOT)))
.map(input -> Arguments.arguments(ArgumentTestHelper.linkedListOf(input), true)),
Stream.of("false", "no", "off")
.flatMap(input -> Stream.of(input, input.toUpperCase(Locale.ROOT)))
.map(input -> Arguments.arguments(ArgumentTestHelper.linkedListOf(input), false))
);
}
@ParameterizedTest
@ValueSource(booleans = { true, false })
void Parse_NonBooleanInput_FailedParse(final boolean liberal) {
// Arrange
final BooleanArgument.BooleanParser<TestCommandSender> parser = new BooleanArgument.BooleanParser<>(liberal);
// Act
final ArgumentParseResult<Boolean> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("not-a-boolean")
);
// Assert
assertThat(result.getFailure()).hasValue(new BooleanArgument.BooleanParseException(
"not-a-boolean",
liberal,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
@ParameterizedTest
@MethodSource("Suggestions_ExpectedSuggestions_Source")
void Suggestions_ExpectedSuggestions(final boolean liberal, final List<String> expectedSuggestions) {
// Arrange
final BooleanArgument.BooleanParser<TestCommandSender> parser = new BooleanArgument.BooleanParser<>(liberal);
// Act
final List<String> suggestions = parser.suggestions(
this.context,
""
);
// Assert
assertThat(suggestions).containsExactlyElementsIn(expectedSuggestions);
}
static Stream<Arguments> Suggestions_ExpectedSuggestions_Source() {
return Stream.of(
Arguments.arguments(false, ArgumentTestHelper.linkedListOf("TRUE", "FALSE")),
Arguments.arguments(true, ArgumentTestHelper.linkedListOf("TRUE", "YES", "ON", "FALSE", "NO", "OFF"))
);
}
}

View file

@ -0,0 +1,185 @@
//
// MIT License
//
// Copyright (c) 2021 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.arguments.standard;
import cloud.commandframework.TestCommandSender;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.context.CommandContext;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
@ExtendWith(MockitoExtension.class)
class ByteParserTest {
@Mock
private CommandContext<TestCommandSender> context;
@Test
void Parse_NoMinMax_SuccessfulParse() {
// Arrange
final ByteArgument.ByteParser<TestCommandSender> parser = new ByteArgument.ByteParser<>(
ByteArgument.ByteParser.DEFAULT_MINIMUM,
ByteArgument.ByteParser.DEFAULT_MAXIMUM
);
final byte byteInput = (byte) ThreadLocalRandom.current().nextInt(Byte.MAX_VALUE);
final LinkedList<String> input = ArgumentTestHelper.linkedListOf(Byte.toString(byteInput));
// Act
final ArgumentParseResult<Byte> result = parser.parse(
this.context,
input
);
// Assert
assertThat(result.getFailure()).isEmpty();
assertThat(result.getParsedValue()).hasValue(byteInput);
assertThat(input).isEmpty();
}
@Test
void Parse_ValueBelowMin_FailedParse() {
// Arrange
final ByteArgument.ByteParser<TestCommandSender> parser = new ByteArgument.ByteParser<>(
(byte) 5 /* min */,
ByteArgument.ByteParser.DEFAULT_MAXIMUM
);
// Act
final ArgumentParseResult<Byte> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("4")
);
// Assert
assertThat(result.getFailure()).hasValue(new ByteArgument.ByteParseException(
"4",
parser,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
@Test
void Parse_ValueAboveMax_FailedParse() {
// Arrange
final ByteArgument.ByteParser<TestCommandSender> parser = new ByteArgument.ByteParser<>(
ByteArgument.ByteParser.DEFAULT_MINIMUM,
(byte) 5 /* max */
);
// Act
final ArgumentParseResult<Byte> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("6")
);
// Assert
assertThat(result.getFailure()).hasValue(new ByteArgument.ByteParseException(
"6",
parser,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
@Test
void Parse_NonByteInput_FailedParse() {
// Arrange
final ByteArgument.ByteParser<TestCommandSender> parser = new ByteArgument.ByteParser<>(
ByteArgument.ByteParser.DEFAULT_MINIMUM,
ByteArgument.ByteParser.DEFAULT_MAXIMUM
);
// Act
final ArgumentParseResult<Byte> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("cow")
);
// Assert
assertThat(result.getFailure()).hasValue(new ByteArgument.ByteParseException(
"cow",
parser,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
@Test
void Suggestions_EmptyInput_ExpectedSuggestions() {
// Arrange
final ByteArgument.ByteParser<TestCommandSender> parser = new ByteArgument.ByteParser<>(
ByteArgument.ByteParser.DEFAULT_MINIMUM,
ByteArgument.ByteParser.DEFAULT_MAXIMUM
);
final List<String> expectedSuggestions = new ArrayList<>();
for (int i = 0; i <= 9; i++) {
expectedSuggestions.add(Byte.toString((byte) i));
}
// Act
final List<String> suggestions = parser.suggestions(
this.context,
""
);
// Assert
assertThat(suggestions).containsExactlyElementsIn(expectedSuggestions);
}
@Test
void Suggestions_NegativeSignInput_ExpectedSuggestions() {
// Arrange
final ByteArgument.ByteParser<TestCommandSender> parser = new ByteArgument.ByteParser<>(
ByteArgument.ByteParser.DEFAULT_MINIMUM,
ByteArgument.ByteParser.DEFAULT_MAXIMUM
);
final List<String> expectedSuggestions = new ArrayList<>();
for (int i = 0; i <= 9; i++) {
expectedSuggestions.add(Byte.toString((byte) -i));
}
// Act
final List<String> suggestions = parser.suggestions(
this.context,
"-"
);
// Assert
assertThat(suggestions).containsExactlyElementsIn(expectedSuggestions);
}
}

View file

@ -0,0 +1,88 @@
//
// MIT License
//
// Copyright (c) 2021 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.arguments.standard;
import cloud.commandframework.TestCommandSender;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.context.CommandContext;
import java.util.LinkedList;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
@ExtendWith(MockitoExtension.class)
class CharacterParserTest {
private CharArgument.CharacterParser<TestCommandSender> parser;
@Mock
private CommandContext<TestCommandSender> context;
@BeforeEach
void setup() {
this.parser = new CharArgument.CharacterParser<>();
}
@Test
void Parse_ValidChar_SuccessfulParse() {
// Arrange
final LinkedList<String> input = ArgumentTestHelper.linkedListOf("a");
// Act
final ArgumentParseResult<Character> result = this.parser.parse(
this.context,
input
);
// Assert
assertThat(result.getFailure()).isEmpty();
assertThat(result.getParsedValue()).hasValue('a');
assertThat(input).isEmpty();
}
@Test
void Parse_TooLongString_FailedParse() {
// Arrange
final LinkedList<String> input = ArgumentTestHelper.linkedListOf("aa");
// Act
final ArgumentParseResult<Character> result = this.parser.parse(
this.context,
input
);
// Assert
assertThat(result.getFailure()).hasValue(new CharArgument.CharParseException(
"aa",
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
}

View file

@ -0,0 +1,137 @@
//
// MIT License
//
// Copyright (c) 2021 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.arguments.standard;
import cloud.commandframework.TestCommandSender;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.context.CommandContext;
import java.util.LinkedList;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
@ExtendWith(MockitoExtension.class)
class DoubleParserTest {
@Mock
private CommandContext<TestCommandSender> context;
@Test
void Parse_NoMinMax_SuccessfulParse() {
// Arrange
final DoubleArgument.DoubleParser<TestCommandSender> parser = new DoubleArgument.DoubleParser<>(
DoubleArgument.DoubleParser.DEFAULT_MINIMUM,
DoubleArgument.DoubleParser.DEFAULT_MAXIMUM
);
final double doubleInput = ThreadLocalRandom.current().nextDouble();
final LinkedList<String> input = ArgumentTestHelper.linkedListOf(Double.toString(doubleInput));
// Act
final ArgumentParseResult<Double> result = parser.parse(
this.context,
input
);
// Assert
assertThat(result.getFailure()).isEmpty();
assertThat(result.getParsedValue()).hasValue(doubleInput);
assertThat(input).isEmpty();
}
@Test
void Parse_ValueBelowMin_FailedParse() {
// Arrange
final DoubleArgument.DoubleParser<TestCommandSender> parser = new DoubleArgument.DoubleParser<>(
5 /* min */,
DoubleArgument.DoubleParser.DEFAULT_MAXIMUM
);
// Act
final ArgumentParseResult<Double> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("4.0")
);
// Assert
assertThat(result.getFailure()).hasValue(new DoubleArgument.DoubleParseException(
"4.0",
parser,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
@Test
void Parse_ValueAboveMax_FailedParse() {
// Arrange
final DoubleArgument.DoubleParser<TestCommandSender> parser = new DoubleArgument.DoubleParser<>(
DoubleArgument.DoubleParser.DEFAULT_MINIMUM,
5.0D /* max */
);
// Act
final ArgumentParseResult<Double> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("6.0")
);
// Assert
assertThat(result.getFailure()).hasValue(new DoubleArgument.DoubleParseException(
"6.0",
parser,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
@Test
void Parse_NonDoubleInput_FailedParse() {
// Arrange
final DoubleArgument.DoubleParser<TestCommandSender> parser = new DoubleArgument.DoubleParser<>(
DoubleArgument.DoubleParser.DEFAULT_MINIMUM,
DoubleArgument.DoubleParser.DEFAULT_MAXIMUM
);
// Act
final ArgumentParseResult<Double> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("cow")
);
// Assert
assertThat(result.getFailure()).hasValue(new DoubleArgument.DoubleParseException(
"cow",
parser,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
}

View file

@ -0,0 +1,113 @@
//
// MIT License
//
// Copyright (c) 2021 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.arguments.standard;
import cloud.commandframework.TestCommandSender;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.context.CommandContext;
import java.util.LinkedList;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
@ExtendWith(MockitoExtension.class)
class EnumParserTest {
@Mock
private CommandContext<TestCommandSender> context;
@ParameterizedTest
@EnumSource(TestEnum.class)
void Parse_EnumValues_SuccessfulParse(final TestEnum value) {
// Arrange
final EnumArgument.EnumParser<TestCommandSender, TestEnum> parser = new EnumArgument.EnumParser<>(
TestEnum.class
);
final LinkedList<String> input = ArgumentTestHelper.linkedListOf(value.name());
// Act
final ArgumentParseResult<TestEnum> result = parser.parse(
this.context,
input
);
// Assert
assertThat(result.getFailure()).isEmpty();
assertThat(result.getParsedValue()).hasValue(value);
assertThat(input).isEmpty();
}
@Test
void Parse_NonEnumValue_FailedParse() {
// Arrange
final EnumArgument.EnumParser<TestCommandSender, TestEnum> parser = new EnumArgument.EnumParser<>(
TestEnum.class
);
// Act
final ArgumentParseResult<TestEnum> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("not-an-enum-value")
);
// Assert
assertThat(result.getFailure()).hasValue(new EnumArgument.EnumParseException(
"not-an-enum-value",
TestEnum.class,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
@Test
void Suggestions_ExpectedSuggestions() {
// Arrange
final EnumArgument.EnumParser<TestCommandSender, TestEnum> parser = new EnumArgument.EnumParser<>(
TestEnum.class
);
// Act
final List<String> suggestions = parser.suggestions(
this.context,
""
);
// Assert
assertThat(suggestions).containsExactly("aaa", "bbb", "ccc");
}
enum TestEnum {
AAA,
BBB,
CCC
}
}

View file

@ -0,0 +1,139 @@
//
// MIT License
//
// Copyright (c) 2021 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.arguments.standard;
import cloud.commandframework.TestCommandSender;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.context.CommandContext;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
@ExtendWith(MockitoExtension.class)
class FloatParserTest {
@Mock
private CommandContext<TestCommandSender> context;
@Test
void Parse_NoMinMax_SuccessfulParse() {
// Arrange
final FloatArgument.FloatParser<TestCommandSender> parser = new FloatArgument.FloatParser<>(
FloatArgument.FloatParser.DEFAULT_MINIMUM,
FloatArgument.FloatParser.DEFAULT_MAXIMUM
);
final float floatInput = ThreadLocalRandom.current().nextFloat();
final LinkedList<String> input = ArgumentTestHelper.linkedListOf(Float.toString(floatInput));
// Act
final ArgumentParseResult<Float> result = parser.parse(
this.context,
input
);
// Assert
assertThat(result.getFailure()).isEmpty();
assertThat(result.getParsedValue()).hasValue(floatInput);
assertThat(input).isEmpty();
}
@Test
void Parse_ValueBelowMin_FailedParse() {
// Arrange
final FloatArgument.FloatParser<TestCommandSender> parser = new FloatArgument.FloatParser<>(
5 /* min */,
FloatArgument.FloatParser.DEFAULT_MAXIMUM
);
// Act
final ArgumentParseResult<Float> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("4.0")
);
// Assert
assertThat(result.getFailure()).hasValue(new FloatArgument.FloatParseException(
"4.0",
parser,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
@Test
void Parse_ValueAboveMax_FailedParse() {
// Arrange
final FloatArgument.FloatParser<TestCommandSender> parser = new FloatArgument.FloatParser<>(
FloatArgument.FloatParser.DEFAULT_MINIMUM,
5.0f /* max */
);
// Act
final ArgumentParseResult<Float> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("6.0")
);
// Assert
assertThat(result.getFailure()).hasValue(new FloatArgument.FloatParseException(
"6.0",
parser,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
@Test
void Parse_NonFloatInput_FailedParse() {
// Arrange
final FloatArgument.FloatParser<TestCommandSender> parser = new FloatArgument.FloatParser<>(
FloatArgument.FloatParser.DEFAULT_MINIMUM,
FloatArgument.FloatParser.DEFAULT_MAXIMUM
);
// Act
final ArgumentParseResult<Float> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("cow")
);
// Assert
assertThat(result.getFailure()).hasValue(new FloatArgument.FloatParseException(
"cow",
parser,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
}

View file

@ -0,0 +1,185 @@
//
// MIT License
//
// Copyright (c) 2021 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.arguments.standard;
import cloud.commandframework.TestCommandSender;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.context.CommandContext;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
@ExtendWith(MockitoExtension.class)
class IntegerParserTest {
@Mock
private CommandContext<TestCommandSender> context;
@Test
void Parse_NoMinMax_SuccessfulParse() {
// Arrange
final IntegerArgument.IntegerParser<TestCommandSender> parser = new IntegerArgument.IntegerParser<>(
IntegerArgument.IntegerParser.DEFAULT_MINIMUM,
IntegerArgument.IntegerParser.DEFAULT_MAXIMUM
);
final int intInput = ThreadLocalRandom.current().nextInt();
final LinkedList<String> input = ArgumentTestHelper.linkedListOf(Integer.toString(intInput));
// Act
final ArgumentParseResult<Integer> result = parser.parse(
this.context,
input
);
// Assert
assertThat(result.getFailure()).isEmpty();
assertThat(result.getParsedValue()).hasValue(intInput);
assertThat(input).isEmpty();
}
@Test
void Parse_ValueBelowMin_FailedParse() {
// Arrange
final IntegerArgument.IntegerParser<TestCommandSender> parser = new IntegerArgument.IntegerParser<>(
5 /* min */,
IntegerArgument.IntegerParser.DEFAULT_MAXIMUM
);
// Act
final ArgumentParseResult<Integer> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("4")
);
// Assert
assertThat(result.getFailure()).hasValue(new IntegerArgument.IntegerParseException(
"4",
parser,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
@Test
void Parse_ValueAboveMax_FailedParse() {
// Arrange
final IntegerArgument.IntegerParser<TestCommandSender> parser = new IntegerArgument.IntegerParser<>(
IntegerArgument.IntegerParser.DEFAULT_MINIMUM,
5 /* max */
);
// Act
final ArgumentParseResult<Integer> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("6")
);
// Assert
assertThat(result.getFailure()).hasValue(new IntegerArgument.IntegerParseException(
"6",
parser,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
@Test
void Parse_NonIntegerInput_FailedParse() {
// Arrange
final IntegerArgument.IntegerParser<TestCommandSender> parser = new IntegerArgument.IntegerParser<>(
IntegerArgument.IntegerParser.DEFAULT_MINIMUM,
IntegerArgument.IntegerParser.DEFAULT_MAXIMUM
);
// Act
final ArgumentParseResult<Integer> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("cow")
);
// Assert
assertThat(result.getFailure()).hasValue(new IntegerArgument.IntegerParseException(
"cow",
parser,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
@Test
void Suggestions_EmptyInput_ExpectedSuggestions() {
// Arrange
final IntegerArgument.IntegerParser<TestCommandSender> parser = new IntegerArgument.IntegerParser<>(
IntegerArgument.IntegerParser.DEFAULT_MINIMUM,
IntegerArgument.IntegerParser.DEFAULT_MAXIMUM
);
final List<String> expectedSuggestions = new ArrayList<>();
for (int i = 0; i <= 9; i++) {
expectedSuggestions.add(Integer.toString(i));
}
// Act
final List<String> suggestions = parser.suggestions(
this.context,
""
);
// Assert
assertThat(suggestions).containsExactlyElementsIn(expectedSuggestions);
}
@Test
void Suggestions_NegativeSignInput_ExpectedSuggestions() {
// Arrange
final IntegerArgument.IntegerParser<TestCommandSender> parser = new IntegerArgument.IntegerParser<>(
IntegerArgument.IntegerParser.DEFAULT_MINIMUM,
IntegerArgument.IntegerParser.DEFAULT_MAXIMUM
);
final List<String> expectedSuggestions = new ArrayList<>();
for (int i = 0; i <= 9; i++) {
expectedSuggestions.add(Integer.toString(-i));
}
// Act
final List<String> suggestions = parser.suggestions(
this.context,
"-"
);
// Assert
assertThat(suggestions).containsExactlyElementsIn(expectedSuggestions);
}
}

View file

@ -0,0 +1,185 @@
//
// MIT License
//
// Copyright (c) 2021 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.arguments.standard;
import cloud.commandframework.TestCommandSender;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.context.CommandContext;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
@ExtendWith(MockitoExtension.class)
class LongParserTest {
@Mock
private CommandContext<TestCommandSender> context;
@Test
void Parse_NoMinMax_SuccessfulParse() {
// Arrange
final LongArgument.LongParser<TestCommandSender> parser = new LongArgument.LongParser<>(
LongArgument.LongParser.DEFAULT_MINIMUM,
LongArgument.LongParser.DEFAULT_MAXIMUM
);
final long longInput = ThreadLocalRandom.current().nextLong();
final LinkedList<String> input = ArgumentTestHelper.linkedListOf(Long.toString(longInput));
// Act
final ArgumentParseResult<Long> result = parser.parse(
this.context,
input
);
// Assert
assertThat(result.getFailure()).isEmpty();
assertThat(result.getParsedValue()).hasValue(longInput);
assertThat(input).isEmpty();
}
@Test
void Parse_ValueBelowMin_FailedParse() {
// Arrange
final LongArgument.LongParser<TestCommandSender> parser = new LongArgument.LongParser<>(
5L /* min */,
LongArgument.LongParser.DEFAULT_MAXIMUM
);
// Act
final ArgumentParseResult<Long> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("4")
);
// Assert
assertThat(result.getFailure()).hasValue(new LongArgument.LongParseException(
"4",
parser,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
@Test
void Parse_ValueAboveMax_FailedParse() {
// Arrange
final LongArgument.LongParser<TestCommandSender> parser = new LongArgument.LongParser<>(
LongArgument.LongParser.DEFAULT_MINIMUM,
5L /* max */
);
// Act
final ArgumentParseResult<Long> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("6")
);
// Assert
assertThat(result.getFailure()).hasValue(new LongArgument.LongParseException(
"6",
parser,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
@Test
void Parse_NonLongInput_FailedParse() {
// Arrange
final LongArgument.LongParser<TestCommandSender> parser = new LongArgument.LongParser<>(
LongArgument.LongParser.DEFAULT_MINIMUM,
LongArgument.LongParser.DEFAULT_MAXIMUM
);
// Act
final ArgumentParseResult<Long> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("cow")
);
// Assert
assertThat(result.getFailure()).hasValue(new LongArgument.LongParseException(
"cow",
parser,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
@Test
void Suggestions_EmptyInput_ExpectedSuggestions() {
// Arrange
final LongArgument.LongParser<TestCommandSender> parser = new LongArgument.LongParser<>(
LongArgument.LongParser.DEFAULT_MINIMUM,
LongArgument.LongParser.DEFAULT_MAXIMUM
);
final List<String> expectedSuggestions = new ArrayList<>();
for (int i = 0; i <= 9; i++) {
expectedSuggestions.add(Long.toString((long) i));
}
// Act
final List<String> suggestions = parser.suggestions(
this.context,
""
);
// Assert
assertThat(suggestions).containsExactlyElementsIn(expectedSuggestions);
}
@Test
void Suggestions_NegativeSignInput_ExpectedSuggestions() {
// Arrange
final LongArgument.LongParser<TestCommandSender> parser = new LongArgument.LongParser<>(
LongArgument.LongParser.DEFAULT_MINIMUM,
LongArgument.LongParser.DEFAULT_MAXIMUM
);
final List<String> expectedSuggestions = new ArrayList<>();
for (int i = 0; i <= 9; i++) {
expectedSuggestions.add(Long.toString((long) -i));
}
// Act
final List<String> suggestions = parser.suggestions(
this.context,
"-"
);
// Assert
assertThat(suggestions).containsExactlyElementsIn(expectedSuggestions);
}
}

View file

@ -0,0 +1,185 @@
//
// MIT License
//
// Copyright (c) 2021 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.arguments.standard;
import cloud.commandframework.TestCommandSender;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.context.CommandContext;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
@ExtendWith(MockitoExtension.class)
class ShortParserTest {
@Mock
private CommandContext<TestCommandSender> context;
@Test
void Parse_NoMinMax_SuccessfulParse() {
// Arrange
final ShortArgument.ShortParser<TestCommandSender> parser = new ShortArgument.ShortParser<>(
ShortArgument.ShortParser.DEFAULT_MINIMUM,
ShortArgument.ShortParser.DEFAULT_MAXIMUM
);
final short shortInput = (short) ThreadLocalRandom.current().nextInt(Short.MAX_VALUE);
final LinkedList<String> input = ArgumentTestHelper.linkedListOf(Short.toString(shortInput));
// Act
final ArgumentParseResult<Short> result = parser.parse(
this.context,
input
);
// Assert
assertThat(result.getFailure()).isEmpty();
assertThat(result.getParsedValue()).hasValue(shortInput);
assertThat(input).isEmpty();
}
@Test
void Parse_ValueBelowMin_FailedParse() {
// Arrange
final ShortArgument.ShortParser<TestCommandSender> parser = new ShortArgument.ShortParser<>(
(short) 5 /* min */,
ShortArgument.ShortParser.DEFAULT_MAXIMUM
);
// Act
final ArgumentParseResult<Short> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("4")
);
// Assert
assertThat(result.getFailure()).hasValue(new ShortArgument.ShortParseException(
"4",
parser,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
@Test
void Parse_ValueAboveMax_FailedParse() {
// Arrange
final ShortArgument.ShortParser<TestCommandSender> parser = new ShortArgument.ShortParser<>(
ShortArgument.ShortParser.DEFAULT_MINIMUM,
(short) 5 /* max */
);
// Act
final ArgumentParseResult<Short> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("6")
);
// Assert
assertThat(result.getFailure()).hasValue(new ShortArgument.ShortParseException(
"6",
parser,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
@Test
void Parse_NonShortInput_FailedParse() {
// Arrange
final ShortArgument.ShortParser<TestCommandSender> parser = new ShortArgument.ShortParser<>(
ShortArgument.ShortParser.DEFAULT_MINIMUM,
ShortArgument.ShortParser.DEFAULT_MAXIMUM
);
// Act
final ArgumentParseResult<Short> result = parser.parse(
this.context,
ArgumentTestHelper.linkedListOf("cow")
);
// Assert
assertThat(result.getFailure()).hasValue(new ShortArgument.ShortParseException(
"cow",
parser,
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
@Test
void Suggestions_EmptyInput_ExpectedSuggestions() {
// Arrange
final ShortArgument.ShortParser<TestCommandSender> parser = new ShortArgument.ShortParser<>(
ShortArgument.ShortParser.DEFAULT_MINIMUM,
ShortArgument.ShortParser.DEFAULT_MAXIMUM
);
final List<String> expectedSuggestions = new ArrayList<>();
for (int i = 0; i <= 9; i++) {
expectedSuggestions.add(Short.toString((short) i));
}
// Act
final List<String> suggestions = parser.suggestions(
this.context,
""
);
// Assert
assertThat(suggestions).containsExactlyElementsIn(expectedSuggestions);
}
@Test
void Suggestions_NegativeSignInput_ExpectedSuggestions() {
// Arrange
final ShortArgument.ShortParser<TestCommandSender> parser = new ShortArgument.ShortParser<>(
ShortArgument.ShortParser.DEFAULT_MINIMUM,
ShortArgument.ShortParser.DEFAULT_MAXIMUM
);
final List<String> expectedSuggestions = new ArrayList<>();
for (int i = 0; i <= 9; i++) {
expectedSuggestions.add(Short.toString((short) -i));
}
// Act
final List<String> suggestions = parser.suggestions(
this.context,
"-"
);
// Assert
assertThat(suggestions).containsExactlyElementsIn(expectedSuggestions);
}
}

View file

@ -0,0 +1,81 @@
//
// MIT License
//
// Copyright (c) 2021 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.arguments.standard;
import cloud.commandframework.TestCommandSender;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.context.CommandContext;
import java.util.LinkedList;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
@ExtendWith(MockitoExtension.class)
class StringArrayParserTest {
private StringArrayArgument.StringArrayParser<TestCommandSender> parser;
@Mock
private CommandContext<TestCommandSender> context;
@BeforeEach
void setup() {
this.parser = new StringArrayArgument.StringArrayParser<>();
}
@Test
void Parse_RandomInput_CapturesAll() {
// Arrange
final LinkedList<String> input = new LinkedList<>();
for (int i = 0; i < 10; i++) {
input.add(
ThreadLocalRandom.current()
.ints()
.mapToObj(Integer::toString)
.limit(32)
.collect(Collectors.joining())
);
}
final LinkedList<String> inputCopy = new LinkedList<>(input);
// Act
final ArgumentParseResult<String[]> result = this.parser.parse(
this.context,
input
);
// Assert
assertThat(result.getFailure()).isEmpty();
assertThat(result.getParsedValue()).hasValue(inputCopy.toArray(new String[0]));
assertThat(input).isEmpty();
}
}

View file

@ -0,0 +1,90 @@
//
// MIT License
//
// Copyright (c) 2021 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.arguments.standard;
import cloud.commandframework.TestCommandSender;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.context.CommandContext;
import java.util.LinkedList;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
@ExtendWith(MockitoExtension.class)
class UUIDParserTest {
private UUIDArgument.UUIDParser<TestCommandSender> parser;
@Mock
private CommandContext<TestCommandSender> context;
@BeforeEach
void setup() {
this.parser = new UUIDArgument.UUIDParser<>();
}
@Test
void Parse_ValidUUID_SuccessfulParse() {
// Arrange
final UUID inputUUID = UUID.randomUUID();
final LinkedList<String> input = ArgumentTestHelper.linkedListOf(inputUUID.toString());
// Act
final ArgumentParseResult<UUID> result = this.parser.parse(
this.context,
input
);
// Assert
assertThat(result.getFailure()).isEmpty();
assertThat(result.getParsedValue()).hasValue(inputUUID);
assertThat(input).isEmpty();
}
@Test
void Parse_NonUUID_FailedParse() {
// Arrange
final LinkedList<String> input = ArgumentTestHelper.linkedListOf("non-uuid");
// Act
final ArgumentParseResult<UUID> result = this.parser.parse(
this.context,
input
);
// Assert
assertThat(result.getFailure()).hasValue(new UUIDArgument.UUIDParseException(
"non-uuid",
this.context
));
assertThat(result.getParsedValue()).isEmpty();
}
}

View file

@ -199,6 +199,10 @@ dependencies:
group: org.junit.jupiter group: org.junit.jupiter
name: junit-jupiter-engine name: junit-jupiter-engine
version: { ref: jupiterEngine } version: { ref: jupiterEngine }
jupiterParams:
group: org.junit.jupiter
name: junit-jupiter-params
version: { ref: jupiterEngine }
mockitoCore: mockitoCore:
group: org.mockito group: org.mockito
name: mockito-core name: mockito-core