Skip to content

Commit 51eddbd

Browse files
committed
Update config test validation methods. Add config modules to code coverage.
1 parent f50a549 commit 51eddbd

File tree

2 files changed

+60
-55
lines changed
  • commandapi-codecov
  • commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test

2 files changed

+60
-55
lines changed

commandapi-codecov/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@
4040
<artifactId>commandapi-bukkit-core</artifactId>
4141
<version>${project.version}</version>
4242
</dependency>
43+
<dependency>
44+
<groupId>dev.jorel</groupId>
45+
<artifactId>commandapi-plugin</artifactId>
46+
<version>${project.version}</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>dev.jorel</groupId>
50+
<artifactId>commandapi-bukkit-plugin-common</artifactId>
51+
<version>${project.version}</version>
52+
</dependency>
4353

4454
<!-- Code coverage the tests -->
4555
<dependency>

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/ConfigGenerationTests.java

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
import java.util.Map;
1919
import java.util.Set;
2020

21-
public class ConfigGenerationTests {
21+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
22+
import static org.junit.jupiter.api.Assertions.assertFalse;
23+
import static org.junit.jupiter.api.Assertions.assertNotNull;
24+
import static org.junit.jupiter.api.Assertions.assertNull;
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
27+
class ConfigGenerationTests {
2228

2329
private CommentedConfigOption<Boolean> silentLogs;
2430
private CommentedConfigOption<Boolean> verboseOutputs;
@@ -61,7 +67,7 @@ public void setup() {
6167
Map<String, CommentedSection> sections = new LinkedHashMap<>();
6268
sections.put("messages", messages);
6369

64-
ConfigurationAdapter<YamlConfiguration> adapter = new BukkitConfigurationAdapter(new YamlConfiguration());
70+
ConfigurationAdapter<YamlConfiguration, DefaultBukkitConfig> adapter = new BukkitConfigurationAdapter(new YamlConfiguration());
6571
bukkitConfig = DefaultBukkitConfig.create(options, sections);
6672
generator = ConfigGenerator.createNew(bukkitConfig);
6773
this.adapter = (BukkitConfigurationAdapter) generator.generate(adapter);
@@ -78,15 +84,48 @@ public void reset() {
7884
this.adapter = null;
7985
}
8086

87+
// Test methods
88+
private void validateConfigOptions(Set<String> options, BukkitConfigurationAdapter adapter) {
89+
for (String option : options) {
90+
assertTrue(adapter.contains(option), "Config option '" + option + "' does not exist!");
91+
}
92+
}
93+
94+
private void validateConfigOptionComments(Map<String, String[]> comments, BukkitConfigurationAdapter adapter) {
95+
for (String option : comments.keySet()) {
96+
String[] expectedComment = comments.get(option);
97+
String[] generatedComment = adapter.getComment(option);
98+
assertArrayEquals(expectedComment, generatedComment, "Config option comment for option '" + option + "' does not exist or was incorrect!");
99+
}
100+
}
101+
102+
private void validateConfigOptionsAbsent(Set<String> options, BukkitConfigurationAdapter adapter) {
103+
for (String option : options) {
104+
assertFalse(adapter.contains(option), "Config option '" + option + "' does still exist!");
105+
}
106+
}
107+
108+
private void validateSections(List<String> sections, String expectedOption, YamlConfiguration config) {
109+
ConfigurationSection root = config.getConfigurationSection(sections.get(0));
110+
assertNotNull(root, "Section '" + sections.get(0) + "' does not exist!");
111+
112+
for (int i = 1; i < sections.size(); i++) {
113+
root = root.getConfigurationSection(sections.get(i));
114+
assertNotNull(root, "Section '" + sections.get(i) + "' does not exist!");
115+
}
116+
Object expectedValue = root.get(expectedOption);
117+
assertNotNull(expectedValue, "Expected option '" + expectedOption + "' was not found in section '" + root.getName() + "'!");
118+
}
119+
81120
@Test
82-
public void testDefaultConfigOptionGeneration() {
121+
void testDefaultConfigOptionGeneration() {
83122
validateConfigOptions(Set.of(
84123
"silent-logs", "verbose-outputs", "messages.missing-executor-implementation"
85124
), adapter);
86125
}
87126

88127
@Test
89-
public void testDefaultConfigOptionCommentGeneration() {
128+
void testDefaultConfigOptionCommentGeneration() {
90129
validateConfigOptionComments(Map.ofEntries(
91130
Map.entry("silent-logs", silentLogs.comment()),
92131
Map.entry("verbose-outputs", verboseOutputs.comment()),
@@ -96,7 +135,7 @@ public void testDefaultConfigOptionCommentGeneration() {
96135
}
97136

98137
@Test
99-
public void testConfigOptionAddition() {
138+
void testConfigOptionAddition() {
100139
CommentedConfigOption<Boolean> createDispatcherJson = new CommentedConfigOption<>(new String[] {
101140
"Create dispatcher JSON (default: false)",
102141
"If \"true\", the CommandAPI creates a command_registration.json file showing the",
@@ -122,7 +161,7 @@ public void testConfigOptionAddition() {
122161
}
123162

124163
@Test
125-
public void testConfigOptionDeletion() {
164+
void testConfigOptionDeletion() {
126165
bukkitConfig.getAllOptions().remove("silent-logs");
127166
generator = ConfigGenerator.createNew(bukkitConfig);
128167
BukkitConfigurationAdapter updatedAdapter = (BukkitConfigurationAdapter) generator.generate(adapter);
@@ -131,7 +170,7 @@ public void testConfigOptionDeletion() {
131170
}
132171

133172
@Test
134-
public void testConfigOptionCommentUpdate() {
173+
void testConfigOptionCommentUpdate() {
135174
silentLogs = new CommentedConfigOption<>(new String[] {
136175
"Defines if silent logs should happen"
137176
}, false);
@@ -146,7 +185,7 @@ public void testConfigOptionCommentUpdate() {
146185
}
147186

148187
@Test
149-
public void testNestedSections() {
188+
void testNestedSections() {
150189
CommentedConfigOption<Boolean> subSubOption = new CommentedConfigOption<>(new String[0], false);
151190

152191
bukkitConfig.getAllOptions().put("root.nested.option", subSubOption);
@@ -156,53 +195,9 @@ public void testNestedSections() {
156195
validateSections(List.of("root", "nested"), "option", updatedAdapter.config());
157196
}
158197

159-
// Test methods
160-
public void validateConfigOptions(Set<String> options, BukkitConfigurationAdapter adapter) {
161-
boolean containsAll;
162-
for (String option : options) {
163-
containsAll = adapter.contains(option);
164-
if (!containsAll) {
165-
throw new IllegalStateException("Config option '" + option + "' does not exist!");
166-
}
167-
}
168-
}
169-
170-
public void validateConfigOptionComments(Map<String, String[]> comments, BukkitConfigurationAdapter adapter) {
171-
boolean correctComment;
172-
for (String option : comments.keySet()) {
173-
String[] generatedComment = adapter.getComment(option);
174-
correctComment = Arrays.equals(comments.get(option), generatedComment);
175-
if (!correctComment) {
176-
throw new IllegalStateException("Config option comment for option '" + option + "' does not exist or was incorrect!");
177-
}
178-
}
179-
}
180-
181-
public void validateConfigOptionsAbsent(Set<String> options, BukkitConfigurationAdapter adapter) {
182-
boolean isAbsent;
183-
for (String option : options) {
184-
isAbsent = !adapter.contains(option);
185-
if (!isAbsent) {
186-
throw new IllegalStateException("Config option '" + option + "' does still exist!");
187-
}
188-
}
189-
}
190-
191-
public void validateSections(List<String> sections, String expectedOption, YamlConfiguration config) {
192-
ConfigurationSection root = config.getConfigurationSection(sections.get(0));
193-
if (root == null) {
194-
throw new IllegalStateException("Section '" + sections.get(0) + "' does not exist!");
195-
}
196-
for (int i = 1; i < sections.size(); i++) {
197-
root = root.getConfigurationSection(sections.get(i));
198-
if (root == null) {
199-
throw new IllegalStateException("Section '" + sections.get(i) + "' does not exist!");
200-
}
201-
}
202-
Object expectedValue = root.get(expectedOption);
203-
if (expectedValue == null) {
204-
throw new IllegalStateException("Expected option '" + expectedOption + "' was not found in section '" + root.getName() + "'!");
205-
}
198+
@Test
199+
void testConfigUpdateNotNeeded() {
200+
assertNull(generator.generate(adapter));
206201
}
207202

208203
}

0 commit comments

Comments
 (0)