Skip to content

Commit 456c5a4

Browse files
committed
Add some tests
1 parent af9c53d commit 456c5a4

File tree

3 files changed

+184
-8
lines changed

3 files changed

+184
-8
lines changed

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
<artifactId>commandapi-bukkit-test-impl</artifactId>
8787
<version>${project.version}</version>
8888
</dependency>
89+
<dependency>
90+
<groupId>dev.jorel</groupId>
91+
<artifactId>commandapi-bukkit-plugin-common</artifactId>
92+
<version>${project.version}</version>
93+
</dependency>
8994

9095
<!-- NBT API implementations (in no particular order) -->
9196
<dependency>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package dev.jorel.commandapi.test;
2+
3+
import dev.jorel.commandapi.config.BukkitConfigurationAdapter;
4+
import dev.jorel.commandapi.config.CommentedConfigOption;
5+
import dev.jorel.commandapi.config.CommentedSection;
6+
import dev.jorel.commandapi.config.ConfigGenerator;
7+
import dev.jorel.commandapi.config.ConfigurationAdapter;
8+
import dev.jorel.commandapi.config.DefaultBukkitConfig;
9+
import org.bukkit.configuration.file.YamlConfiguration;
10+
import org.junit.jupiter.api.AfterEach;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
14+
import java.util.Arrays;
15+
import java.util.LinkedHashMap;
16+
import java.util.Map;
17+
import java.util.Set;
18+
19+
public class ConfigGenerationTests {
20+
21+
private CommentedConfigOption<Boolean> silentLogs;
22+
private CommentedConfigOption<Boolean> verboseOutputs;
23+
private CommentedConfigOption<String> missingExecutorImplementation;
24+
25+
private CommentedSection messages;
26+
27+
private ConfigGenerator generator;
28+
private DefaultBukkitConfig bukkitConfig;
29+
private BukkitConfigurationAdapter adapter;
30+
31+
@BeforeEach
32+
public void setup() {
33+
silentLogs = new CommentedConfigOption<>(new String[]{
34+
"Silent logs (default: false)",
35+
"If \"true\", turns off all logging from the CommandAPI, except for errors."
36+
}, false);
37+
verboseOutputs = new CommentedConfigOption<>(new String[]{
38+
"Verbose outputs (default: false)",
39+
"If \"true\", outputs command registration and unregistration logs in the console"
40+
}, false);
41+
missingExecutorImplementation = new CommentedConfigOption<>(new String[]{
42+
"Missing executor implementation (default: \"This command has no implementations for %s\")",
43+
"The message to display to senders when a command has no executor. Available",
44+
"parameters are:",
45+
" %s - the executor class (lowercase)",
46+
" %S - the executor class (normal case)"
47+
}, "This command has no implementations for %s");
48+
49+
messages = new CommentedSection(new String[]{
50+
"Messages",
51+
"Controls messages that the CommandAPI displays to players"
52+
});
53+
54+
Map<String, CommentedConfigOption<?>> options = new LinkedHashMap<>();
55+
options.put("silent-logs", silentLogs);
56+
options.put("verbose-outputs", verboseOutputs);
57+
options.put("messages.missing-executor-implementation", missingExecutorImplementation);
58+
59+
Map<String, CommentedSection> sections = new LinkedHashMap<>();
60+
sections.put("messages", messages);
61+
62+
ConfigurationAdapter<YamlConfiguration> adapter = new BukkitConfigurationAdapter(new YamlConfiguration());
63+
bukkitConfig = DefaultBukkitConfig.create(options, sections);
64+
generator = ConfigGenerator.createNew(bukkitConfig);
65+
this.adapter = (BukkitConfigurationAdapter) generator.generate(adapter);
66+
}
67+
68+
@AfterEach
69+
public void reset() {
70+
this.silentLogs = null;
71+
this.verboseOutputs = null;
72+
this.missingExecutorImplementation = null;
73+
this.messages = null;
74+
this.generator = null;
75+
this.bukkitConfig = null;
76+
this.adapter = null;
77+
}
78+
79+
@Test
80+
public void testDefaultConfigOptionGeneration() {
81+
validateConfigOptions(Set.of(
82+
"silent-logs", "verbose-outputs", "messages.missing-executor-implementation"
83+
), adapter);
84+
}
85+
86+
@Test
87+
public void testDefaultConfigOptionCommentGeneration() {
88+
validateConfigOptionComments(Map.ofEntries(
89+
Map.entry("silent-logs", silentLogs.comment()),
90+
Map.entry("verbose-outputs", verboseOutputs.comment()),
91+
Map.entry("messages.missing-executor-implementation", missingExecutorImplementation.comment()),
92+
Map.entry("messages", messages.comment())
93+
), adapter);
94+
}
95+
96+
@Test
97+
public void testConfigOptionAddition() {
98+
CommentedConfigOption<Boolean> createDispatcherJson = new CommentedConfigOption<>(new String[] {
99+
"Create dispatcher JSON (default: false)",
100+
"If \"true\", the CommandAPI creates a command_registration.json file showing the",
101+
"mapping of registered commands. This is designed to be used by developers -",
102+
"setting this to \"false\" will improve command registration performance."
103+
}, false);
104+
105+
bukkitConfig.getAllOptions().put("create-dispatcher-json", createDispatcherJson);
106+
generator = ConfigGenerator.createNew(bukkitConfig);
107+
BukkitConfigurationAdapter updatedAdapter = (BukkitConfigurationAdapter) generator.generate(adapter);
108+
109+
validateConfigOptions(Set.of(
110+
"silent-logs", "verbose-outputs", "messages.missing-executor-implementation", "create-dispatcher-json"
111+
), updatedAdapter);
112+
113+
validateConfigOptionComments(Map.ofEntries(
114+
Map.entry("silent-logs", silentLogs.comment()),
115+
Map.entry("verbose-outputs", verboseOutputs.comment()),
116+
Map.entry("messages.missing-executor-implementation", missingExecutorImplementation.comment()),
117+
Map.entry("create-dispatcher-json", createDispatcherJson.comment()),
118+
Map.entry("messages", messages.comment())
119+
), updatedAdapter);
120+
}
121+
122+
@Test
123+
public void testConfigOptionDeletion() {
124+
bukkitConfig.getAllOptions().remove("silent-logs");
125+
generator = ConfigGenerator.createNew(bukkitConfig);
126+
BukkitConfigurationAdapter updatedAdapter = (BukkitConfigurationAdapter) generator.generate(adapter);
127+
128+
validateConfigOptionsAbsent(Set.of("silent-logs"), updatedAdapter);
129+
}
130+
131+
@Test
132+
public void testConfigOptionCommentUpdate() {
133+
silentLogs = new CommentedConfigOption<>(new String[] {
134+
"Defines if silent logs should happen"
135+
}, false);
136+
137+
bukkitConfig.getAllOptions().put("silent-logs", silentLogs);
138+
generator = ConfigGenerator.createNew(bukkitConfig);
139+
BukkitConfigurationAdapter updatedAdapter = (BukkitConfigurationAdapter) generator.generate(adapter);
140+
141+
validateConfigOptionComments(Map.ofEntries(
142+
Map.entry("silent-logs", silentLogs.comment())
143+
), updatedAdapter);
144+
}
145+
146+
// Test methods
147+
public void validateConfigOptions(Set<String> options, BukkitConfigurationAdapter adapter) {
148+
boolean containsAll;
149+
for (String option : options) {
150+
containsAll = adapter.contains(option);
151+
if (!containsAll) {
152+
throw new IllegalStateException("Config option '" + option + "' does not exist!");
153+
}
154+
}
155+
}
156+
157+
public void validateConfigOptionComments(Map<String, String[]> comments, BukkitConfigurationAdapter adapter) {
158+
boolean correctComment;
159+
for (String option : comments.keySet()) {
160+
String[] generatedComment = adapter.getComment(option);
161+
correctComment = Arrays.equals(comments.get(option), generatedComment);
162+
if (!correctComment) {
163+
throw new IllegalStateException("Config option comment for option '" + option + "' does not exist or was incorrect!");
164+
}
165+
}
166+
}
167+
168+
public void validateConfigOptionsAbsent(Set<String> options, BukkitConfigurationAdapter adapter) {
169+
boolean isAbsent;
170+
for (String option : options) {
171+
isAbsent = !adapter.contains(option);
172+
if (!isAbsent) {
173+
throw new IllegalStateException("Config option '" + option + "' does still exist!");
174+
}
175+
}
176+
}
177+
178+
}

commandapi-platforms/commandapi-velocity/commandapi-velocity-plugin/src/main/java/dev/jorel/commandapi/config/VelocityConfigurationAdapter.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,7 @@ public void setValue(String key, Object value) {
2929

3030
@Override
3131
public void setComment(String key, String[] comment) {
32-
StringBuilder commentBuilder = new StringBuilder();
33-
for (int i = 0; i < comment.length; i++) {
34-
if (i > 0) {
35-
commentBuilder.append("\n");
36-
}
37-
commentBuilder.append(comment[i]);
38-
}
39-
node(key).comment(commentBuilder.toString());
32+
node(key).comment(String.join("\n", comment));
4033
}
4134

4235
@Override

0 commit comments

Comments
 (0)