Skip to content

Commit

Permalink
Update config test validation methods. Add config modules to code cov…
Browse files Browse the repository at this point in the history
…erage.
  • Loading branch information
DerEchtePilz committed Oct 9, 2024
1 parent f50a549 commit 51eddbd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 55 deletions.
10 changes: 10 additions & 0 deletions commandapi-codecov/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
<artifactId>commandapi-bukkit-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dev.jorel</groupId>
<artifactId>commandapi-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dev.jorel</groupId>
<artifactId>commandapi-bukkit-plugin-common</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Code coverage the tests -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
import java.util.Map;
import java.util.Set;

public class ConfigGenerationTests {
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ConfigGenerationTests {

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

ConfigurationAdapter<YamlConfiguration> adapter = new BukkitConfigurationAdapter(new YamlConfiguration());
ConfigurationAdapter<YamlConfiguration, DefaultBukkitConfig> adapter = new BukkitConfigurationAdapter(new YamlConfiguration());
bukkitConfig = DefaultBukkitConfig.create(options, sections);
generator = ConfigGenerator.createNew(bukkitConfig);
this.adapter = (BukkitConfigurationAdapter) generator.generate(adapter);
Expand All @@ -78,15 +84,48 @@ public void reset() {
this.adapter = null;
}

// Test methods
private void validateConfigOptions(Set<String> options, BukkitConfigurationAdapter adapter) {
for (String option : options) {
assertTrue(adapter.contains(option), "Config option '" + option + "' does not exist!");
}
}

private void validateConfigOptionComments(Map<String, String[]> comments, BukkitConfigurationAdapter adapter) {
for (String option : comments.keySet()) {
String[] expectedComment = comments.get(option);
String[] generatedComment = adapter.getComment(option);
assertArrayEquals(expectedComment, generatedComment, "Config option comment for option '" + option + "' does not exist or was incorrect!");
}
}

private void validateConfigOptionsAbsent(Set<String> options, BukkitConfigurationAdapter adapter) {
for (String option : options) {
assertFalse(adapter.contains(option), "Config option '" + option + "' does still exist!");
}
}

private void validateSections(List<String> sections, String expectedOption, YamlConfiguration config) {
ConfigurationSection root = config.getConfigurationSection(sections.get(0));
assertNotNull(root, "Section '" + sections.get(0) + "' does not exist!");

for (int i = 1; i < sections.size(); i++) {
root = root.getConfigurationSection(sections.get(i));
assertNotNull(root, "Section '" + sections.get(i) + "' does not exist!");
}
Object expectedValue = root.get(expectedOption);
assertNotNull(expectedValue, "Expected option '" + expectedOption + "' was not found in section '" + root.getName() + "'!");
}

@Test
public void testDefaultConfigOptionGeneration() {
void testDefaultConfigOptionGeneration() {
validateConfigOptions(Set.of(
"silent-logs", "verbose-outputs", "messages.missing-executor-implementation"
), adapter);
}

@Test
public void testDefaultConfigOptionCommentGeneration() {
void testDefaultConfigOptionCommentGeneration() {
validateConfigOptionComments(Map.ofEntries(
Map.entry("silent-logs", silentLogs.comment()),
Map.entry("verbose-outputs", verboseOutputs.comment()),
Expand All @@ -96,7 +135,7 @@ public void testDefaultConfigOptionCommentGeneration() {
}

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

@Test
public void testConfigOptionDeletion() {
void testConfigOptionDeletion() {
bukkitConfig.getAllOptions().remove("silent-logs");
generator = ConfigGenerator.createNew(bukkitConfig);
BukkitConfigurationAdapter updatedAdapter = (BukkitConfigurationAdapter) generator.generate(adapter);
Expand All @@ -131,7 +170,7 @@ public void testConfigOptionDeletion() {
}

@Test
public void testConfigOptionCommentUpdate() {
void testConfigOptionCommentUpdate() {
silentLogs = new CommentedConfigOption<>(new String[] {
"Defines if silent logs should happen"
}, false);
Expand All @@ -146,7 +185,7 @@ public void testConfigOptionCommentUpdate() {
}

@Test
public void testNestedSections() {
void testNestedSections() {
CommentedConfigOption<Boolean> subSubOption = new CommentedConfigOption<>(new String[0], false);

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

// Test methods
public void validateConfigOptions(Set<String> 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<String, String[]> 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<String> options, BukkitConfigurationAdapter adapter) {
boolean isAbsent;
for (String option : options) {
isAbsent = !adapter.contains(option);
if (!isAbsent) {
throw new IllegalStateException("Config option '" + option + "' does still exist!");
}
}
}

public void validateSections(List<String> sections, String expectedOption, YamlConfiguration config) {
ConfigurationSection root = config.getConfigurationSection(sections.get(0));
if (root == null) {
throw new IllegalStateException("Section '" + sections.get(0) + "' does not exist!");
}
for (int i = 1; i < sections.size(); i++) {
root = root.getConfigurationSection(sections.get(i));
if (root == null) {
throw new IllegalStateException("Section '" + sections.get(i) + "' does not exist!");
}
}
Object expectedValue = root.get(expectedOption);
if (expectedValue == null) {
throw new IllegalStateException("Expected option '" + expectedOption + "' was not found in section '" + root.getName() + "'!");
}
@Test
void testConfigUpdateNotNeeded() {
assertNull(generator.generate(adapter));
}

}

0 comments on commit 51eddbd

Please sign in to comment.