Skip to content

Commit

Permalink
Chore: Improve the test setup for cloud-core and clean up some test c…
Browse files Browse the repository at this point in the history
…ases.
  • Loading branch information
Citymonstret authored and jpenilla committed Dec 2, 2021
1 parent af58148 commit 943c42a
Show file tree
Hide file tree
Showing 19 changed files with 356 additions and 193 deletions.
3 changes: 3 additions & 0 deletions build-logic/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
4 changes: 4 additions & 0 deletions build-logic/src/main/kotlin/cloud.base-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,21 +37,29 @@

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})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -103,10 +105,8 @@ void testPredicateFilter() {
* 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 ")
&& !command.toString().endsWith(" thing");
};
final Predicate<Command<TestCommandSender>> predicate = (command) -> command.toString().startsWith("test ")
&& !command.toString().endsWith(" thing");

/*
* List all commands from root, which should show only:
Expand All @@ -132,7 +132,7 @@ void testPredicateFilter() {
*/
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
Expand Down Expand Up @@ -228,7 +228,7 @@ private void printMultiHelpTopic(final CommandHelpHandler.MultiHelpTopic<TestCom
printBuilder.append("└── ");
}
printBuilder.append(suggestion);
System.out.println(printBuilder.toString());
System.out.println(printBuilder);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,7 +44,7 @@ final class CommandPerformanceTest {

@BeforeAll
static void setup() {
manager = new TestCommandManager();
manager = createManager();

final StringBuilder literalBuilder = new StringBuilder("literals");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,7 +35,7 @@
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;

Expand All @@ -42,6 +44,7 @@
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 {
Expand All @@ -58,16 +61,18 @@ static void setup() {

@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()
);
Expand All @@ -77,10 +82,12 @@ void testComplexPermissions() {
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
Expand Down Expand Up @@ -126,7 +133,7 @@ void testPredicatePermissions() {
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()
);
Expand All @@ -141,7 +148,7 @@ private PermissionOutputtingCommandManager() {

@Override
public boolean hasPermission(
final TestCommandSender sender,
final @NonNull TestCommandSender sender,
final String permission
) {
if (permission.equalsIgnoreCase("first")) {
Expand All @@ -154,7 +161,7 @@ public boolean hasPermission(
}

@Override
public CommandMeta createDefaultCommandMeta() {
public @NonNull CommandMeta createDefaultCommandMeta() {
return SimpleCommandMeta.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand All @@ -48,13 +51,13 @@ static void newTree() {
@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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand Down Expand Up @@ -72,7 +75,7 @@ enum SampleEnum {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,43 @@
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;

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(
Expand All @@ -62,7 +73,7 @@ void testChangingRegistrationHandlerFails() {

@Test
void testRegistrationFailsInAfterRegistrationState() {
final TestCommandManager manager = new TestCommandManager();
final CommandManager<TestCommandSender> manager = createManager();
manager.command(manager.commandBuilder("test").handler(ctx -> {
}));

Expand All @@ -76,7 +87,7 @@ void testRegistrationFailsInAfterRegistrationState() {

@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 -> {
}));
Expand Down
Loading

0 comments on commit 943c42a

Please sign in to comment.