Skip to content

Commit

Permalink
core: Fix #25 and add a unit test for CommentedConfig.clearComments()
Browse files Browse the repository at this point in the history
  • Loading branch information
TheElectronWill committed Aug 1, 2017
1 parent 59da0f2 commit 32f1ff0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,12 @@ public void clear() {
@Override
public void clearComments() {
commentMap.clear();
// Recursively clears the comments of the subconfigs:
for (Object o : map.values()) {
if (o instanceof CommentedConfig) {
((CommentedConfig)o).clearComments();
}
}
//NB: The UnmodifiableCommentedConfigs cannot be cleared from their comments.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.electronwill.nightconfig.core;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

/**
* @author TheElectronWill
*/
class AbstractCommentedConfigTest {

@Test
public void testClearComments() {
CommentedConfig config = CommentedConfig.inMemory();
config.set("a", "a");
config.setComment("a", "commentA");

CommentedConfig sub = CommentedConfig.inMemory();
sub.set("b", "b");
sub.setComment("b", "commentB");
config.set("sub", sub);
config.setComment("sub", "commentSub");

assertEquals(config.getComment("a"), "commentA");
assertEquals(config.getComment("sub.b"), "commentB");
assertEquals(config.getComment("sub"), "commentSub");
assertEquals(sub.getComment("b"), "commentB");

for (CommentedConfig.Entry entry : config.entrySet()) {
assertNotNull(entry.getComment());
}

config.clearComments();

assertNull(config.getComment("a"));
assertNull(config.getComment("sub.b"));
assertNull(config.getComment("sub"));
assertNull(sub.getComment("b"));

for (CommentedConfig.Entry entry : config.entrySet()) {
assertNull(entry.getComment());
assertNotNull(entry.getValue());
}
}

}

0 comments on commit 32f1ff0

Please sign in to comment.