Chore: Improve the test setup for cloud-core and clean up some test cases.

This commit is contained in:
alexander.soderberg 2021-11-27 16:30:37 +01:00 committed by Jason
parent dd4f67ad2e
commit 29abe82215
19 changed files with 362 additions and 199 deletions

View file

@ -33,4 +33,7 @@ object Versions {
// TEST DEPENDENCIES
const val jupiterEngine = "5.8.1"
const val jmh = "1.27"
const val mockitoCore = "4.1.0"
const val mockitoKotlin = "4.0.0"
const val truth = "1.1.3"
}

View file

@ -89,6 +89,10 @@ repositories {
dependencies {
compileOnlyApi("org.checkerframework", "checker-qual", Versions.checkerQual)
testImplementation("org.junit.jupiter", "junit-jupiter-engine", Versions.jupiterEngine)
testImplementation("org.mockito", "mockito-core", Versions.mockitoCore)
testImplementation("org.mockito.kotlin", "mockito-kotlin", Versions.mockitoKotlin)
testImplementation("com.google.truth", "truth", Versions.truth)
testImplementation("com.google.truth.extensions", "truth-java8-extension", Versions.truth)
errorprone("com.google.errorprone", "error_prone_core", Versions.errorprone)
// Silences compiler warnings from guava using errorprone
compileOnly("com.google.errorprone", "error_prone_annotations", Versions.errorprone)

View file

@ -23,8 +23,9 @@
//
package cloud.commandframework;
import static com.google.common.truth.Truth.assertThat;
import cloud.commandframework.annotations.AnnotationAccessor;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.lang.annotation.ElementType;
@ -36,21 +37,29 @@ import java.lang.reflect.Parameter;
public class AnnotationAccessorTest {
@Qualifier("method")
public static void annotatedMethod(@Qualifier("parameter") final String parameter) {
private static final String QUALIFIER_TEST_STRING_PARAMETER = "parameter";
private static final String QUALIFIER_TEST_STRING_METHOD = "method";
@Qualifier(QUALIFIER_TEST_STRING_METHOD)
public static void annotatedMethod(@Qualifier(QUALIFIER_TEST_STRING_PARAMETER) final String parameter) {
}
@Test
void testQualifierResolutionOrder() throws Exception {
// Given
final Method method = AnnotationAccessorTest.class.getMethod("annotatedMethod", String.class);
final Parameter parameter = method.getParameters()[0];
final AnnotationAccessor accessor = AnnotationAccessor.of(
AnnotationAccessor.of(parameter),
AnnotationAccessor.of(method)
);
// When
final Qualifier qualifier = accessor.annotation(Qualifier.class);
Assertions.assertNotNull(qualifier);
Assertions.assertEquals("parameter", qualifier.value());
// Then
assertThat(qualifier).isNotNull();
assertThat(qualifier.value()).isEqualTo(QUALIFIER_TEST_STRING_PARAMETER);
}
@Target({ElementType.PARAMETER, ElementType.METHOD})

View file

@ -23,6 +23,8 @@
//
package cloud.commandframework;
import static cloud.commandframework.util.TestUtils.createManager;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.StaticArgument;
import cloud.commandframework.arguments.standard.IntegerArgument;
@ -47,7 +49,7 @@ class CommandHelpHandlerTest {
@BeforeAll
static void setup() {
manager = new TestCommandManager();
manager = createManager();
final SimpleCommandMeta meta1 = SimpleCommandMeta.builder().with(CommandMeta.DESCRIPTION, "Command with only literals").build();
manager.command(manager.commandBuilder("test", meta1).literal("this").literal("thing").build());
final SimpleCommandMeta meta2 = SimpleCommandMeta.builder().with(CommandMeta.DESCRIPTION, "Command with variables").build();
@ -103,10 +105,8 @@ class CommandHelpHandlerTest {
* This predicate only displays the commands starting with /test
* The one command ending in 'thing' is excluded as well, for complexity
*/
final Predicate<Command<TestCommandSender>> predicate = (command) -> {
return command.toString().startsWith("test ")
final Predicate<Command<TestCommandSender>> predicate = (command) -> command.toString().startsWith("test ")
&& !command.toString().endsWith(" thing");
};
/*
* List all commands from root, which should show only:
@ -132,7 +132,7 @@ class CommandHelpHandlerTest {
*/
final CommandHelpHandler.HelpTopic<TestCommandSender> query3 = manager.getCommandHelpHandler(predicate).queryHelp("test int");
Assertions.assertTrue(query3 instanceof CommandHelpHandler.VerboseHelpTopic);
Assertions.assertEquals(Arrays.asList("test int <int>"), getSortedSyntaxStrings(query3));
Assertions.assertEquals(Collections.singletonList("test int <int>"), getSortedSyntaxStrings(query3));
/*
* List all commands from /vec, which should show none
@ -228,7 +228,7 @@ class CommandHelpHandlerTest {
printBuilder.append("└── ");
}
printBuilder.append(suggestion);
System.out.println(printBuilder.toString());
System.out.println(printBuilder);
}
}

View file

@ -23,6 +23,8 @@
//
package cloud.commandframework;
import static cloud.commandframework.util.TestUtils.createManager;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.execution.CommandResult;
import org.junit.jupiter.api.Assertions;
@ -42,7 +44,7 @@ final class CommandPerformanceTest {
@BeforeAll
static void setup() {
manager = new TestCommandManager();
manager = createManager();
final StringBuilder literalBuilder = new StringBuilder("literals");

View file

@ -23,6 +23,8 @@
//
package cloud.commandframework;
import static com.google.common.truth.Truth.assertThat;
import cloud.commandframework.arguments.standard.IntegerArgument;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.keys.SimpleCloudKey;
@ -33,7 +35,7 @@ import cloud.commandframework.permission.CommandPermission;
import cloud.commandframework.permission.OrPermission;
import cloud.commandframework.permission.Permission;
import cloud.commandframework.permission.PredicatePermission;
import org.junit.jupiter.api.Assertions;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@ -42,6 +44,7 @@ import java.util.concurrent.CompletionException;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class CommandPermissionTest {
@ -58,16 +61,18 @@ class CommandPermissionTest {
@Test
void testCompoundPermission() {
Assertions.assertTrue(manager.suggest(new TestCommandSender(), "t").isEmpty());
assertFalse(manager.suggest(new TestCommandSender("test.permission.four"), "t").isEmpty());
assertThat(manager.suggest(new TestCommandSender(), "t")).isEmpty();
assertThat(manager.suggest(new TestCommandSender("test.permission.four"), "t")).isNotEmpty();
}
@Test
void testComplexPermissions() {
manager.command(manager.commandBuilder("first").permission("first"));
manager.command(manager.commandBuilder("first").argument(IntegerArgument.of("second")).permission("second"));
manager.executeCommand(new TestCommandSender(), "first").join();
Assertions.assertThrows(
assertThrows(
CompletionException.class,
() -> manager.executeCommand(new TestCommandSender(), "first 10").join()
);
@ -77,10 +82,12 @@ class CommandPermissionTest {
void testAndPermissions() {
final CommandPermission test = Permission.of("one").and(Permission.of("two"));
final TestCommandSender sender = new TestCommandSender("one");
assertFalse(manager.hasPermission(sender, test));
assertFalse(manager.hasPermission(new TestCommandSender("two"), test));
assertThat(manager.hasPermission(sender, test)).isFalse();
assertThat(manager.hasPermission(new TestCommandSender("two"), test)).isFalse();
sender.addPermission("two");
assertTrue(manager.hasPermission(sender, test));
assertThat(manager.hasPermission(sender, test)).isTrue();
}
@Test
@ -126,7 +133,7 @@ class CommandPermissionTest {
manager.executeCommand(new TestCommandSender(), "predicate").join();
// Now we force it to fail
condition.set(false);
Assertions.assertThrows(
assertThrows(
CompletionException.class,
() -> manager.executeCommand(new TestCommandSender(), "predicate").join()
);
@ -141,7 +148,7 @@ class CommandPermissionTest {
@Override
public boolean hasPermission(
final TestCommandSender sender,
final @NonNull TestCommandSender sender,
final String permission
) {
if (permission.equalsIgnoreCase("first")) {
@ -154,7 +161,7 @@ class CommandPermissionTest {
}
@Override
public CommandMeta createDefaultCommandMeta() {
public @NonNull CommandMeta createDefaultCommandMeta() {
return SimpleCommandMeta.empty();
}

View file

@ -23,10 +23,13 @@
//
package cloud.commandframework;
import static cloud.commandframework.util.TestUtils.createManager;
import cloud.commandframework.execution.postprocessor.CommandPostprocessingContext;
import cloud.commandframework.execution.postprocessor.CommandPostprocessor;
import cloud.commandframework.meta.SimpleCommandMeta;
import cloud.commandframework.services.types.ConsumerService;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@ -38,7 +41,7 @@ public class CommandPostProcessorTest {
@BeforeAll
static void newTree() {
manager = new TestCommandManager();
manager = createManager();
manager.command(manager.commandBuilder("test", SimpleCommandMeta.empty())
.handler(c -> state[0] = true)
.build());
@ -48,13 +51,13 @@ public class CommandPostProcessorTest {
@Test
void testPreprocessing() {
manager.executeCommand(new TestCommandSender(), "test").join();
Assertions.assertEquals(false, state[0]);
Assertions.assertFalse(state[0]);
}
static final class SamplePostprocessor implements CommandPostprocessor<TestCommandSender> {
@Override
public void accept(final CommandPostprocessingContext<TestCommandSender> context) {
public void accept(final @NonNull CommandPostprocessingContext<TestCommandSender> context) {
ConsumerService.interrupt();
}

View file

@ -23,11 +23,14 @@
//
package cloud.commandframework;
import static cloud.commandframework.util.TestUtils.createManager;
import cloud.commandframework.arguments.standard.EnumArgument;
import cloud.commandframework.execution.preprocessor.CommandPreprocessingContext;
import cloud.commandframework.execution.preprocessor.CommandPreprocessor;
import cloud.commandframework.meta.SimpleCommandMeta;
import cloud.commandframework.services.types.ConsumerService;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@ -38,7 +41,7 @@ public class CommandPreProcessorTest {
@BeforeAll
static void newTree() {
manager = new TestCommandManager();
manager = createManager();
manager.command(manager.commandBuilder("test", SimpleCommandMeta.empty())
.argument(EnumArgument.of(SampleEnum.class, "enum"))
.handler(
@ -72,7 +75,7 @@ public class CommandPreProcessorTest {
static final class SamplePreprocessor implements CommandPreprocessor<TestCommandSender> {
@Override
public void accept(final CommandPreprocessingContext<TestCommandSender> context) {
public void accept(final @NonNull CommandPreprocessingContext<TestCommandSender> context) {
try {
final int num = Integer.parseInt(context.getInputQueue().removeFirst());
context.getCommandContext().store("int", num);

View file

@ -26,6 +26,7 @@ package cloud.commandframework;
import cloud.commandframework.internal.CommandRegistrationHandler;
import org.junit.jupiter.api.Test;
import static cloud.commandframework.util.TestUtils.createManager;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@ -33,25 +34,35 @@ public class CommandRegistrationStateTest {
@Test
void testInitialState() {
final TestCommandManager manager = new TestCommandManager();
final CommandManager<TestCommandSender> manager = createManager();
assertEquals(CommandManager.RegistrationState.BEFORE_REGISTRATION, manager.getRegistrationState());
}
@Test
void testRegistrationChangesState() {
final TestCommandManager manager = new TestCommandManager();
final CommandManager<TestCommandSender> manager = createManager();
manager.command(manager.commandBuilder("test").handler(ctx -> {
}));
assertEquals(CommandManager.RegistrationState.REGISTERING, manager.getRegistrationState());
// And a second registration maintains it
}
@Test
void testDoubleRegistrationPersistsState() {
final CommandManager<TestCommandSender> manager = createManager();
manager.command(manager.commandBuilder("test").handler(ctx -> {
}));
manager.command(manager.commandBuilder("test2").handler(ctx -> {
}));
assertEquals(CommandManager.RegistrationState.REGISTERING, manager.getRegistrationState());
}
@Test
void testChangingRegistrationHandlerFails() {
final TestCommandManager manager = new TestCommandManager();
final CommandManager<TestCommandSender> manager = createManager();
manager.command(manager.commandBuilder("test").handler(ctx -> {
}));
assertThrows(
@ -62,7 +73,7 @@ public class CommandRegistrationStateTest {
@Test
void testRegistrationFailsInAfterRegistrationState() {
final TestCommandManager manager = new TestCommandManager();
final CommandManager<TestCommandSender> manager = createManager();
manager.command(manager.commandBuilder("test").handler(ctx -> {
}));
@ -76,7 +87,7 @@ public class CommandRegistrationStateTest {
@Test
void testAllowUnsafeRegistration() {
final TestCommandManager manager = new TestCommandManager();
final CommandManager<TestCommandSender> manager = createManager();
manager.setSetting(CommandManager.ManagerSettings.ALLOW_UNSAFE_REGISTRATION, true);
manager.command(manager.commandBuilder("test").handler(ctx -> {
}));

View file

@ -23,6 +23,8 @@
//
package cloud.commandframework;
import static cloud.commandframework.util.TestUtils.createManager;
import cloud.commandframework.arguments.compound.ArgumentTriplet;
import cloud.commandframework.arguments.standard.BooleanArgument;
import cloud.commandframework.arguments.standard.EnumArgument;
@ -45,7 +47,7 @@ public class CommandSuggestionsTest {
@BeforeAll
static void setupManager() {
manager = new TestCommandManager();
manager = createManager();
manager.command(manager.commandBuilder("test", "testalias").literal("one").build());
manager.command(manager.commandBuilder("test").literal("two").build());
manager.command(manager.commandBuilder("test")
@ -107,16 +109,18 @@ public class CommandSuggestionsTest {
.argument(IntegerArgument.<TestCommandSender>newBuilder("num").withMin(5).withMax(100)));
manager.command(manager.commandBuilder("partial")
.argument(StringArgument.<TestCommandSender>newBuilder("arg").withSuggestionsProvider((contect, input) -> {
return Arrays.asList("hi", "hey", "heya", "hai", "hello");
}))
.argument(
StringArgument.<TestCommandSender>newBuilder("arg")
.withSuggestionsProvider((contect, input) -> Arrays.asList("hi", "hey", "heya", "hai", "hello"))
)
.literal("literal")
.build());
manager.command(manager.commandBuilder("literal_with_variable")
.argument(StringArgument.<TestCommandSender>newBuilder("arg").withSuggestionsProvider((context, input) -> {
return Arrays.asList("veni", "vidi");
}).build())
.argument(
StringArgument.<TestCommandSender>newBuilder("arg")
.withSuggestionsProvider((context, input) -> Arrays.asList("veni", "vidi")).build()
)
.literal("now"));
manager.command(manager.commandBuilder("literal_with_variable")
.literal("vici")
@ -239,7 +243,7 @@ public class CommandSuggestionsTest {
final String input2 = "flags3 --c";
final List<String> suggestions2 = manager.suggest(new TestCommandSender(), input2);
Assertions.assertEquals(Arrays.asList("--compound"), suggestions2);
Assertions.assertEquals(Collections.singletonList("--compound"), suggestions2);
final String input3 = "flags3 --compound ";
final List<String> suggestions3 = manager.suggest(new TestCommandSender(), input3);
@ -264,11 +268,11 @@ public class CommandSuggestionsTest {
final String input8 = "flags3 --compound 22 33 44 --pres";
final List<String> suggestions8 = manager.suggest(new TestCommandSender(), input8);
Assertions.assertEquals(Arrays.asList("--presence"), suggestions8);
Assertions.assertEquals(Collections.singletonList("--presence"), suggestions8);
final String input9 = "flags3 --compound 22 33 44 --presence ";
final List<String> suggestions9 = manager.suggest(new TestCommandSender(), input9);
Assertions.assertEquals(Arrays.asList("--single"), suggestions9);
Assertions.assertEquals(Collections.singletonList("--single"), suggestions9);
final String input10 = "flags3 --compound 22 33 44 --single ";
final List<String> suggestions10 = manager.suggest(new TestCommandSender(), input10);

View file

@ -23,6 +23,8 @@
//
package cloud.commandframework;
import static com.google.common.truth.Truth.assertThat;
import cloud.commandframework.arguments.StaticArgument;
import cloud.commandframework.arguments.standard.StringArgument;
import cloud.commandframework.meta.SimpleCommandMeta;
@ -33,14 +35,23 @@ class CommandTest {
@Test()
void noArguments() {
Assertions.assertEquals(1, Command.newBuilder("test", SimpleCommandMeta.empty()).build().getArguments().size());
assertThat(
Command
.newBuilder("test", SimpleCommandMeta.empty())
.build()
.getArguments()
.size()
).isEqualTo(1);
}
@Test
void ensureOrdering() {
Assertions.assertThrows(IllegalArgumentException.class, () ->
Command.newBuilder("test", SimpleCommandMeta.empty()).argument(StringArgument.optional("something"))
.argument(StaticArgument.of("somethingelse")).build());
Command.newBuilder("test", SimpleCommandMeta.empty())
.argument(StringArgument.optional("something"))
.argument(StaticArgument.of("somethingelse"))
.build()
);
}
}

View file

@ -23,6 +23,8 @@
//
package cloud.commandframework;
import static cloud.commandframework.util.TestUtils.createManager;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.compound.ArgumentPair;
import cloud.commandframework.arguments.flags.CommandFlag;
@ -55,7 +57,7 @@ class CommandTreeTest {
@BeforeAll
static void newTree() {
manager = new TestCommandManager();
manager = createManager();
/* Build general test commands */
manager.command(manager.commandBuilder("test", SimpleCommandMeta.empty())
@ -146,9 +148,7 @@ class CommandTreeTest {
/* Build command for testing float */
manager.command(manager.commandBuilder("float")
.argument(FloatArgument.of("num"))
.handler(c -> {
System.out.printf("%f\n", c.<Float>get("num"));
}));
.handler(c -> System.out.printf("%f\n", c.<Float>get("num"))));
/* Build command for testing preprocessing */
manager.command(manager.commandBuilder("preprocess")

View file

@ -23,6 +23,8 @@
//
package cloud.commandframework;
import static cloud.commandframework.util.TestUtils.createManager;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
@ -46,7 +48,7 @@ public class ExecutionBenchmark {
@Setup(Level.Trial)
public void setup() {
manager = new TestCommandManager();
manager = createManager();
final StringBuilder literalBuilder = new StringBuilder("literals");

View file

@ -23,6 +23,10 @@
//
package cloud.commandframework;
import static com.google.common.truth.Truth8.assertThat;
import static cloud.commandframework.util.TestUtils.createManager;
import cloud.commandframework.annotations.AnnotationAccessor;
import cloud.commandframework.annotations.injection.GuiceInjectionService;
import cloud.commandframework.annotations.injection.ParameterInjectorRegistry;
@ -50,7 +54,7 @@ public class ParameterInjectorRegistryTest {
@BeforeEach
void setup() {
this.commandSender = new TestCommandSender();
this.commandManager = new TestCommandManager();
this.commandManager = createManager();
this.commandContextFactory = new StandardCommandContextFactory<>();
this.parameterInjectorRegistry = new ParameterInjectorRegistry<>();
this.parameterInjectorRegistry.registerInjector(Integer.class, (context, annotationAccessor) -> INJECTED_INTEGER);
@ -64,30 +68,37 @@ public class ParameterInjectorRegistryTest {
@Test
void testSimpleInjection() {
Assertions.assertEquals(INJECTED_INTEGER, parameterInjectorRegistry.getInjectable(
assertThat(
parameterInjectorRegistry.getInjectable(
Integer.class,
this.createContext(),
AnnotationAccessor.empty()
).orElse(-1));
)
).hasValue(INJECTED_INTEGER);
}
@Test
void testGuiceInjection() {
this.parameterInjectorRegistry.registerInjectionService(GuiceInjectionService.create(this.injector));
Assertions.assertEquals(TestModule.INJECTED_INTEGER, parameterInjectorRegistry.getInjectable(
assertThat(
parameterInjectorRegistry.getInjectable(
Integer.class,
this.createContext(),
AnnotationAccessor.empty()
).orElse(-1));
)
).hasValue(TestModule.INJECTED_INTEGER);
}
@Test
void testNonExistentInjection() {
Assertions.assertNull(parameterInjectorRegistry.getInjectable(
assertThat(
parameterInjectorRegistry.getInjectable(
String.class,
this.createContext(),
AnnotationAccessor.empty()
).orElse(null));
)
).isEmpty();
}
private static final class TestModule extends AbstractModule {

View file

@ -23,6 +23,9 @@
//
package cloud.commandframework;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
import cloud.commandframework.annotations.specifier.Range;
import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.arguments.parser.ParserParameters;
@ -31,7 +34,10 @@ import cloud.commandframework.arguments.parser.StandardParameters;
import cloud.commandframework.arguments.parser.StandardParserRegistry;
import cloud.commandframework.arguments.standard.IntegerArgument;
import io.leangen.geantyref.TypeToken;
import org.junit.jupiter.api.Assertions;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.junit.jupiter.api.Test;
import java.lang.annotation.Annotation;
@ -40,14 +46,9 @@ import java.util.Objects;
public class ParserRegistryTest {
public static final int RANGE_MIN = 10;
public static final int RANGE_MAX = 100;
@SuppressWarnings("ReturnValueIgnored")
@Test
void testParserRegistry() {
final ParserRegistry<TestCommandSender> parserRegistry = new StandardParserRegistry<>();
final Range range = new Range() {
private static final int RANGE_MIN = 10;
private static final int RANGE_MAX = 100;
private static final Range RANGE = new Range() {
@Override
public Class<? extends Annotation> annotationType() {
@ -55,12 +56,12 @@ public class ParserRegistryTest {
}
@Override
public String min() {
public @NonNull String min() {
return Integer.toString(RANGE_MIN);
}
@Override
public String max() {
public @NonNull String max() {
return Integer.toString(RANGE_MAX);
}
@ -79,31 +80,71 @@ public class ParserRegistryTest {
}
};
@Test
void parsing_range_annotation_results_in_correct_parser_parameters() {
// Given
final ParserRegistry<TestCommandSender> parserRegistry = new StandardParserRegistry<>();
final TypeToken<?> parsedType = TypeToken.get(int.class);
final ParserParameters parserParameters = parserRegistry.parseAnnotations(parsedType, Collections.singleton(range));
Assertions.assertTrue(parserParameters.has(StandardParameters.RANGE_MIN));
Assertions.assertTrue(parserParameters.has(StandardParameters.RANGE_MAX));
// When
final ParserParameters parserParameters = parserRegistry.parseAnnotations(parsedType, Collections.singleton(RANGE));
// Then
assertThat(parserParameters.has(StandardParameters.RANGE_MIN)).isTrue();
assertThat(parserParameters.has(StandardParameters.RANGE_MAX)).isTrue();
}
@Test
void creating_integer_parser_from_parameters_results_in_correct_parser() {
final ParserRegistry<TestCommandSender> parserRegistry = new StandardParserRegistry<>();
final TypeToken<?> parsedType = TypeToken.get(int.class);
final ParserParameters parserParameters = ParserParameters.empty();
parserParameters.store(StandardParameters.RANGE_MIN, RANGE_MIN);
parserParameters.store(StandardParameters.RANGE_MAX, RANGE_MAX);
final ArgumentParser<TestCommandSender, ?> parser = parserRegistry.createParser(
parsedType,
parserParameters
)
.orElseThrow(
() -> new NullPointerException(
"No parser found"));
Assertions.assertTrue(parser instanceof IntegerArgument.IntegerParser);
).orElseThrow(() -> new NullPointerException("No parser found"));
assertThat(parser).isInstanceOf(IntegerArgument.IntegerParser.class);
@SuppressWarnings("unchecked") final IntegerArgument.IntegerParser<TestCommandSender> integerParser =
(IntegerArgument.IntegerParser<TestCommandSender>) parser;
Assertions.assertEquals(RANGE_MIN, integerParser.getMin());
Assertions.assertEquals(RANGE_MAX, integerParser.getMax());
/* Test integer */
parserRegistry.createParser(TypeToken.get(int.class), ParserParameters.empty())
.orElseThrow(() -> new IllegalArgumentException("No parser found for int.class"));
assertThat(integerParser.getMin()).isEqualTo(RANGE_MIN);
assertThat(integerParser.getMax()).isEqualTo(RANGE_MAX);
}
/* Test Enum */
parserRegistry.createParser(TypeToken.get(CommandManager.ManagerSettings.class), ParserParameters.empty())
.orElseThrow(() -> new IllegalArgumentException("No parser found for enum"));
@Test
void retrieving_integer_parser_from_parser_registry() {
// Given
final ParserRegistry<TestCommandSender> parserRegistry = new StandardParserRegistry<>();
// When
final Optional<?> parserOptional = parserRegistry.createParser(
TypeToken.get(int.class),
ParserParameters.empty()
);
// Then
assertThat(parserOptional).isPresent();
}
@Test
void retrieving_enum_parser_from_registry() {
// Given
final ParserRegistry<TestCommandSender> parserRegistry = new StandardParserRegistry<>();
// When
final Optional<?> parserOptional = parserRegistry.createParser(
TypeToken.get(CommandManager.ManagerSettings.class),
ParserParameters.empty()
);
// Then
assertThat(parserOptional).isPresent();
}
}

View file

@ -1,52 +0,0 @@
//
// 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;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.internal.CommandRegistrationHandler;
import cloud.commandframework.meta.SimpleCommandMeta;
public class TestCommandManager extends CommandManager<TestCommandSender> {
/**
* Construct a new test command manager
*/
public TestCommandManager() {
super(CommandExecutionCoordinator.simpleCoordinator(), CommandRegistrationHandler.nullCommandRegistrationHandler());
}
@Override
public final SimpleCommandMeta createDefaultCommandMeta() {
return SimpleCommandMeta.empty();
}
@Override
public final boolean hasPermission(final TestCommandSender sender, final String permission) {
System.out.printf("Testing permission: %s\n", permission);
return !permission.equalsIgnoreCase("no");
}
}

View file

@ -41,7 +41,6 @@ public class TestCommandSender {
return this.permissions.contains(permission);
}
public void addPermission(final String permission) {
this.permissions.add(permission);
}

View file

@ -23,9 +23,12 @@
//
package cloud.commandframework.arguments.standard;
import static com.google.common.truth.Truth.assertThat;
import static cloud.commandframework.util.TestUtils.createManager;
import cloud.commandframework.CommandManager;
import cloud.commandframework.TestCommandManager;
import cloud.commandframework.TestCommandSender;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@ -39,7 +42,7 @@ class StringArgumentTest {
@BeforeAll
static void setup() {
manager = new TestCommandManager();
manager = createManager();
manager.command(manager.commandBuilder("quoted")
.argument(StringArgument.of("message1", StringArgument.StringMode.QUOTED))
.argument(StringArgument.of("message2"))
@ -66,32 +69,44 @@ class StringArgumentTest {
.build());
}
private static void clear() {
@AfterEach
void reset() {
storage[0] = storage[1] = null;
}
@Test
void testSingle() {
clear();
manager.executeCommand(new TestCommandSender(), "single string");
Assertions.assertEquals("string", storage[0]);
void single_single() {
manager.executeCommand(new TestCommandSender(), "single string").join();
assertThat(storage[0]).isEqualTo("string");
}
@Test
void testQuotes() {
clear();
void quoted_single_quoted_string_containing_double_quote_followed_by_unquoted() {
manager.executeCommand(new TestCommandSender(), "quoted 'quoted \" string' unquoted").join();
Assertions.assertEquals("quoted \" string", storage[0]);
Assertions.assertEquals("unquoted", storage[1]);
clear();
manager.executeCommand(new TestCommandSender(), "quoted quoted unquoted");
Assertions.assertEquals("quoted", storage[0]);
Assertions.assertEquals("unquoted", storage[1]);
clear();
assertThat(storage[0]).isEqualTo("quoted \" string");
assertThat(storage[1]).isEqualTo("unquoted");
}
@Test
void quoted_unquoted_strings() {
manager.executeCommand(new TestCommandSender(), "quoted quoted unquoted").join();
assertThat(storage[0]).isEqualTo("quoted");
assertThat(storage[1]).isEqualTo("unquoted");
}
@Test
void quoted_quoted_string_containing_escaped_quote_followed_by_unquoted() {
manager.executeCommand(new TestCommandSender(), "quoted \"quoted \\\" string\" unquoted").join();
Assertions.assertEquals("quoted \" string", storage[0]);
Assertions.assertEquals("unquoted", storage[1]);
clear();
assertThat(storage[0]).isEqualTo("quoted \" string");
assertThat(storage[1]).isEqualTo("unquoted");
}
@Test
void quoted_unmatched_quotes_failing() {
Assertions.assertThrows(CompletionException.class, () -> manager.executeCommand(
new TestCommandSender(),
"'quoted quoted unquoted"
@ -99,10 +114,10 @@ class StringArgumentTest {
}
@Test
void testGreedy() {
clear();
void greedy_consumes_all() {
manager.executeCommand(new TestCommandSender(), "greedy greedy string content").join();
Assertions.assertEquals("greedy string content", storage[0]);
assertThat(storage[0]).isEqualTo("greedy string content");
}
}

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.util;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;
import cloud.commandframework.CommandManager;
import cloud.commandframework.CommandTree;
import cloud.commandframework.TestCommandSender;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.internal.CommandRegistrationHandler;
import cloud.commandframework.meta.SimpleCommandMeta;
import java.util.function.Function;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.mockito.Mockito;
public final class TestUtils {
/**
* A permission value which always returns {@code false}.
*/
public static final String FAILING_PERMISSION = "no";
private TestUtils() {
}
private abstract static class TestCommandSenderCommandManager extends CommandManager<TestCommandSender> {
protected TestCommandSenderCommandManager(
final @NonNull Function<@NonNull CommandTree<TestCommandSender>, @NonNull CommandExecutionCoordinator<TestCommandSender>> commandExecutionCoordinator,
final @NonNull CommandRegistrationHandler commandRegistrationHandler
) {
super(commandExecutionCoordinator, commandRegistrationHandler);
}
}
/**
* Creates a {@link CommandManager} that can be used for testing.
*
* @return Mocked command manager.
*/
public static @NonNull CommandManager<TestCommandSender> createManager() {
final CommandManager<TestCommandSender> manager = mock(
TestCommandSenderCommandManager.class,
withSettings().useConstructor(
CommandExecutionCoordinator.simpleCoordinator(),
CommandRegistrationHandler.nullCommandRegistrationHandler()
).defaultAnswer(Mockito.CALLS_REAL_METHODS)
);
// We don't care about the actual command meta.
when(manager.createDefaultCommandMeta()).thenReturn(SimpleCommandMeta.empty());
// The permission check should always return true, unless "no" is the parameter.
when(manager.hasPermission(any(), anyString())).thenReturn(true);
when(manager.hasPermission(any(), eq(FAILING_PERMISSION))).thenReturn(false);
return manager;
}
}