From 456c5a4d14459db697af7db2527e3a936561c98a Mon Sep 17 00:00:00 2001
From: DerEchtePilz <81232921+DerEchtePilz@users.noreply.github.com>
Date: Tue, 8 Oct 2024 16:45:15 +0200
Subject: [PATCH] Add some tests
---
.../commandapi-bukkit-test-tests/pom.xml | 5 +
.../test/ConfigGenerationTests.java | 178 ++++++++++++++++++
.../config/VelocityConfigurationAdapter.java | 9 +-
3 files changed, 184 insertions(+), 8 deletions(-)
create mode 100644 commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/ConfigGenerationTests.java
diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/pom.xml b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/pom.xml
index 1cd51fb33c..0f6083983c 100644
--- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/pom.xml
+++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/pom.xml
@@ -86,6 +86,11 @@
commandapi-bukkit-test-impl
${project.version}
+
+ dev.jorel
+ commandapi-bukkit-plugin-common
+ ${project.version}
+
diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/ConfigGenerationTests.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/ConfigGenerationTests.java
new file mode 100644
index 0000000000..0f0357a70e
--- /dev/null
+++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/ConfigGenerationTests.java
@@ -0,0 +1,178 @@
+package dev.jorel.commandapi.test;
+
+import dev.jorel.commandapi.config.BukkitConfigurationAdapter;
+import dev.jorel.commandapi.config.CommentedConfigOption;
+import dev.jorel.commandapi.config.CommentedSection;
+import dev.jorel.commandapi.config.ConfigGenerator;
+import dev.jorel.commandapi.config.ConfigurationAdapter;
+import dev.jorel.commandapi.config.DefaultBukkitConfig;
+import org.bukkit.configuration.file.YamlConfiguration;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+public class ConfigGenerationTests {
+
+ private CommentedConfigOption silentLogs;
+ private CommentedConfigOption verboseOutputs;
+ private CommentedConfigOption missingExecutorImplementation;
+
+ private CommentedSection messages;
+
+ private ConfigGenerator generator;
+ private DefaultBukkitConfig bukkitConfig;
+ private BukkitConfigurationAdapter adapter;
+
+ @BeforeEach
+ public void setup() {
+ silentLogs = new CommentedConfigOption<>(new String[]{
+ "Silent logs (default: false)",
+ "If \"true\", turns off all logging from the CommandAPI, except for errors."
+ }, false);
+ verboseOutputs = new CommentedConfigOption<>(new String[]{
+ "Verbose outputs (default: false)",
+ "If \"true\", outputs command registration and unregistration logs in the console"
+ }, false);
+ missingExecutorImplementation = new CommentedConfigOption<>(new String[]{
+ "Missing executor implementation (default: \"This command has no implementations for %s\")",
+ "The message to display to senders when a command has no executor. Available",
+ "parameters are:",
+ " %s - the executor class (lowercase)",
+ " %S - the executor class (normal case)"
+ }, "This command has no implementations for %s");
+
+ messages = new CommentedSection(new String[]{
+ "Messages",
+ "Controls messages that the CommandAPI displays to players"
+ });
+
+ Map> options = new LinkedHashMap<>();
+ options.put("silent-logs", silentLogs);
+ options.put("verbose-outputs", verboseOutputs);
+ options.put("messages.missing-executor-implementation", missingExecutorImplementation);
+
+ Map sections = new LinkedHashMap<>();
+ sections.put("messages", messages);
+
+ ConfigurationAdapter adapter = new BukkitConfigurationAdapter(new YamlConfiguration());
+ bukkitConfig = DefaultBukkitConfig.create(options, sections);
+ generator = ConfigGenerator.createNew(bukkitConfig);
+ this.adapter = (BukkitConfigurationAdapter) generator.generate(adapter);
+ }
+
+ @AfterEach
+ public void reset() {
+ this.silentLogs = null;
+ this.verboseOutputs = null;
+ this.missingExecutorImplementation = null;
+ this.messages = null;
+ this.generator = null;
+ this.bukkitConfig = null;
+ this.adapter = null;
+ }
+
+ @Test
+ public void testDefaultConfigOptionGeneration() {
+ validateConfigOptions(Set.of(
+ "silent-logs", "verbose-outputs", "messages.missing-executor-implementation"
+ ), adapter);
+ }
+
+ @Test
+ public void testDefaultConfigOptionCommentGeneration() {
+ validateConfigOptionComments(Map.ofEntries(
+ Map.entry("silent-logs", silentLogs.comment()),
+ Map.entry("verbose-outputs", verboseOutputs.comment()),
+ Map.entry("messages.missing-executor-implementation", missingExecutorImplementation.comment()),
+ Map.entry("messages", messages.comment())
+ ), adapter);
+ }
+
+ @Test
+ public void testConfigOptionAddition() {
+ CommentedConfigOption createDispatcherJson = new CommentedConfigOption<>(new String[] {
+ "Create dispatcher JSON (default: false)",
+ "If \"true\", the CommandAPI creates a command_registration.json file showing the",
+ "mapping of registered commands. This is designed to be used by developers -",
+ "setting this to \"false\" will improve command registration performance."
+ }, false);
+
+ bukkitConfig.getAllOptions().put("create-dispatcher-json", createDispatcherJson);
+ generator = ConfigGenerator.createNew(bukkitConfig);
+ BukkitConfigurationAdapter updatedAdapter = (BukkitConfigurationAdapter) generator.generate(adapter);
+
+ validateConfigOptions(Set.of(
+ "silent-logs", "verbose-outputs", "messages.missing-executor-implementation", "create-dispatcher-json"
+ ), updatedAdapter);
+
+ validateConfigOptionComments(Map.ofEntries(
+ Map.entry("silent-logs", silentLogs.comment()),
+ Map.entry("verbose-outputs", verboseOutputs.comment()),
+ Map.entry("messages.missing-executor-implementation", missingExecutorImplementation.comment()),
+ Map.entry("create-dispatcher-json", createDispatcherJson.comment()),
+ Map.entry("messages", messages.comment())
+ ), updatedAdapter);
+ }
+
+ @Test
+ public void testConfigOptionDeletion() {
+ bukkitConfig.getAllOptions().remove("silent-logs");
+ generator = ConfigGenerator.createNew(bukkitConfig);
+ BukkitConfigurationAdapter updatedAdapter = (BukkitConfigurationAdapter) generator.generate(adapter);
+
+ validateConfigOptionsAbsent(Set.of("silent-logs"), updatedAdapter);
+ }
+
+ @Test
+ public void testConfigOptionCommentUpdate() {
+ silentLogs = new CommentedConfigOption<>(new String[] {
+ "Defines if silent logs should happen"
+ }, false);
+
+ bukkitConfig.getAllOptions().put("silent-logs", silentLogs);
+ generator = ConfigGenerator.createNew(bukkitConfig);
+ BukkitConfigurationAdapter updatedAdapter = (BukkitConfigurationAdapter) generator.generate(adapter);
+
+ validateConfigOptionComments(Map.ofEntries(
+ Map.entry("silent-logs", silentLogs.comment())
+ ), updatedAdapter);
+ }
+
+ // Test methods
+ public void validateConfigOptions(Set options, BukkitConfigurationAdapter adapter) {
+ boolean containsAll;
+ for (String option : options) {
+ containsAll = adapter.contains(option);
+ if (!containsAll) {
+ throw new IllegalStateException("Config option '" + option + "' does not exist!");
+ }
+ }
+ }
+
+ public void validateConfigOptionComments(Map comments, BukkitConfigurationAdapter adapter) {
+ boolean correctComment;
+ for (String option : comments.keySet()) {
+ String[] generatedComment = adapter.getComment(option);
+ correctComment = Arrays.equals(comments.get(option), generatedComment);
+ if (!correctComment) {
+ throw new IllegalStateException("Config option comment for option '" + option + "' does not exist or was incorrect!");
+ }
+ }
+ }
+
+ public void validateConfigOptionsAbsent(Set options, BukkitConfigurationAdapter adapter) {
+ boolean isAbsent;
+ for (String option : options) {
+ isAbsent = !adapter.contains(option);
+ if (!isAbsent) {
+ throw new IllegalStateException("Config option '" + option + "' does still exist!");
+ }
+ }
+ }
+
+}
diff --git a/commandapi-platforms/commandapi-velocity/commandapi-velocity-plugin/src/main/java/dev/jorel/commandapi/config/VelocityConfigurationAdapter.java b/commandapi-platforms/commandapi-velocity/commandapi-velocity-plugin/src/main/java/dev/jorel/commandapi/config/VelocityConfigurationAdapter.java
index ea3f8deced..22bfbfe80f 100644
--- a/commandapi-platforms/commandapi-velocity/commandapi-velocity-plugin/src/main/java/dev/jorel/commandapi/config/VelocityConfigurationAdapter.java
+++ b/commandapi-platforms/commandapi-velocity/commandapi-velocity-plugin/src/main/java/dev/jorel/commandapi/config/VelocityConfigurationAdapter.java
@@ -29,14 +29,7 @@ public void setValue(String key, Object value) {
@Override
public void setComment(String key, String[] comment) {
- StringBuilder commentBuilder = new StringBuilder();
- for (int i = 0; i < comment.length; i++) {
- if (i > 0) {
- commentBuilder.append("\n");
- }
- commentBuilder.append(comment[i]);
- }
- node(key).comment(commentBuilder.toString());
+ node(key).comment(String.join("\n", comment));
}
@Override