Skip to content

Commit

Permalink
feat(common): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Citymonstret committed Jan 15, 2024
1 parent 56a510e commit 0caf14b
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// 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 org.incendo.cloud.discord.slash;

import cloud.commandframework.CommandManager;
import cloud.commandframework.Description;
import org.incendo.cloud.discord.util.TestCommandManager;
import org.incendo.cloud.discord.util.TestCommandSender;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static cloud.commandframework.arguments.standard.BooleanParser.booleanParser;
import static cloud.commandframework.arguments.standard.IntegerParser.integerParser;
import static cloud.commandframework.arguments.standard.StringParser.stringParser;
import static com.google.common.truth.Truth.assertThat;

class StandardDiscordCommandFactoryTest {

private DiscordCommandFactory<TestCommandSender> commandFactory;
private CommandManager<TestCommandSender> commandManager;

@BeforeEach
void setup() {
final OptionRegistry<TestCommandSender> optionRegistry = new StandardOptionRegistry<>();
this.commandFactory = new StandardDiscordCommandFactory<>(optionRegistry);
this.commandManager = new TestCommandManager();
}

@Test
void testCommandCreation() {
// Arrange
this.commandManager.command(
this.commandManager.commandBuilder("command", Description.of("Command Description"))
.literal("foo")
.required("integer", integerParser(1, 10), Description.of("Integer Argument"))
.optional("boolean", booleanParser())
);
this.commandManager.command(
this.commandManager.commandBuilder("command")
.literal("bar")
.required("string", stringParser(), DiscordChoices.strings("cat", "dog"))
);

// Act
final DiscordCommand<TestCommandSender> command =
this.commandFactory.create(this.commandManager.commandTree().getNamedNode("command"));

// Assert
assertThat(command.name()).isEqualTo("command");
assertThat(command.description()).isEqualTo("Command Description");
assertThat(command.options()).containsExactly(
ImmutableSubCommand.<TestCommandSender>builder()
.name("bar")
.description("bar")
.addOption(
ImmutableVariable.<TestCommandSender>builder()
.name("string")
.description("string")
.type(DiscordOptionType.STRING)
.required(true)
.autocomplete(false)
.addChoices(
DiscordOptionChoice.of("cat", "cat"),
DiscordOptionChoice.of("dog", "dog")
).build()
).build(),
ImmutableSubCommand.<TestCommandSender>builder()
.name("foo")
.description("foo")
.addOption(
ImmutableVariable.<TestCommandSender>builder()
.name("integer")
.description("Integer Argument")
.type(DiscordOptionType.INTEGER)
.required(true)
.autocomplete(true)
.range(Range.of(1, 10))
.build()
).addOption(
ImmutableVariable.<TestCommandSender>builder()
.name("boolean")
.description("boolean")
.type(DiscordOptionType.BOOLEAN)
.required(false)
.autocomplete(false)
.build()
).build()
);
}

@Test
void testSubCommandGroup() {
// Arrange
this.commandManager.command(
this.commandManager.commandBuilder("command")
.literal("group")
.literal("foo")
);
this.commandManager.command(
this.commandManager.commandBuilder("command")
.literal("group")
.literal("bar")
);

// Act
final DiscordCommand<TestCommandSender> command =
this.commandFactory.create(this.commandManager.commandTree().getNamedNode("command"));

// Assert
assertThat(command.name()).isEqualTo("command");
assertThat(command.options()).hasSize(1);
assertThat(command.options().get(0).type()).isEqualTo(DiscordOptionType.SUB_COMMAND_GROUP);
assertThat(command.options().get(0).name()).isEqualTo("group");
assertThat(((DiscordOption.SubCommand<?>) command.options().get(0)).options()).containsExactly(
ImmutableSubCommand.builder()
.name("foo")
.description("foo")
.build(),
ImmutableSubCommand.builder()
.name("bar")
.description("bar")
.build()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// 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 org.incendo.cloud.discord.slash;

import io.leangen.geantyref.TypeToken;
import java.util.stream.Stream;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static com.google.common.truth.Truth.assertThat;

class StandardOptionRegistryTest {

private StandardOptionRegistry<Object> optionRegistry;

@BeforeEach
void setup() {
this.optionRegistry = new StandardOptionRegistry<>();
}

@ParameterizedTest
@MethodSource("testDefaultMappingsSource")
void testDefaultMappings(final @NonNull Class<?> clazz, final @NonNull DiscordOptionType<?> expected) {
// Act
final DiscordOptionType<?> actual = this.optionRegistry.getOption(TypeToken.get(clazz));

// Assert
assertThat(actual).isEqualTo(expected);
}

static Stream<Arguments> testDefaultMappingsSource() {
return Stream.of(
Arguments.arguments(String.class, DiscordOptionType.STRING),
Arguments.arguments(Integer.class, DiscordOptionType.INTEGER),
Arguments.arguments(Boolean.class, DiscordOptionType.BOOLEAN),
Arguments.arguments(Double.class, DiscordOptionType.NUMBER)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// 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 org.incendo.cloud.discord.util;

import cloud.commandframework.CommandManager;
import cloud.commandframework.execution.ExecutionCoordinator;
import cloud.commandframework.internal.CommandRegistrationHandler;
import org.checkerframework.checker.nullness.qual.NonNull;

public final class TestCommandManager extends CommandManager<TestCommandSender> {

public TestCommandManager() {
super(ExecutionCoordinator.simpleCoordinator(), CommandRegistrationHandler.nullCommandRegistrationHandler());
}

@Override
public boolean hasPermission(@NonNull final TestCommandSender sender, @NonNull final String permission) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// 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 org.incendo.cloud.discord.util;

public interface TestCommandSender {

}
26 changes: 26 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:base"
],
"ignoreDeps": [],
"labels": [
"dependencies"
],
"packageRules": [
{
"matchManagers": ["github-actions", "gradle-wrapper"],
"groupName": "gradle and github actions"
},
{
"matchDepTypes": ["plugin"],
"groupName": "gradle and github actions"
},
{
"matchPaths": ["gradle/build-logic/*", "buildSrc/*"],
"groupName": "gradle and github actions"
}
],
"semanticCommitType": "build",
"commitMessagePrefix": "chore(deps): "
}

0 comments on commit 0caf14b

Please sign in to comment.