Fixed ConfigReader parsing values after a list to a wrong level

This commit is contained in:
Rsl1122 2019-01-17 12:31:15 +02:00
parent 296a27dc63
commit b1bcb962cb
3 changed files with 107 additions and 1 deletions

View File

@ -46,6 +46,8 @@ public class ConfigReader implements Closeable {
private int indentMode = 4;
private List<String> unboundComment = new ArrayList<>();
private int lastDepth = -1;
/**
* Create a new ConfigReader for a Path.
*
@ -115,13 +117,16 @@ public class ConfigReader implements Closeable {
handleCommentLine(trimmed);
} else {
// Determine where the node belongs
parent = findParent(previousNode.getNodeDepth(), findCurrentDepth(line));
int currentDepth = findCurrentDepth(line);
parent = findParent(lastDepth, currentDepth);
Verify.nullCheck(parent, () -> new IllegalStateException("Could not determine parent on line: \"" + line + "\""));
// Get the node the line belongs to
previousNode = parseNode(trimmed);
Verify.nullCheck(previousNode, () -> new IllegalStateException("Could not parse node on line: \"" + line + "\""));
lastDepth = currentDepth;
handleUnboundComments();
}
}

View File

@ -0,0 +1,77 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.system.settings.config;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import java.util.Arrays;
import java.util.Scanner;
import static org.junit.jupiter.api.Assertions.*;
/**
* Test for {@link ConfigReader}
*
* @author Rsl1122
*/
@RunWith(JUnitPlatform.class)
class ConfigReaderTest {
@Test
void valueAfterAList() {
String read = "Test:\n" +
" List:\n" +
" - First\n" +
" - Second\n" +
" - Third\n" +
" Value: Example";
try (ConfigReader reader = new ConfigReader(new Scanner(read))) {
Config readConfig = reader.read();
assertTrue(readConfig.getNode("Test.List").isPresent());
assertTrue(readConfig.getNode("Test.Value").isPresent());
assertFalse(readConfig.getNode("Test.List.Value").isPresent());
assertEquals("Example", readConfig.getString("Test.Value"));
assertEquals(Arrays.asList("First", "Second", "Third"), readConfig.getStringList("Test.List"));
}
}
@Test
void valueAfterAList2() {
String read = "Plugins:\n" +
" Factions:\n" +
" HideFactions:\n" +
" - ExampleFaction\n" +
" Towny:\n" +
" HideTowns:\n" +
" - ExampleTown\n" +
" Enabled: true";
try (ConfigReader reader = new ConfigReader(new Scanner(read))) {
Config readConfig = reader.read();
assertTrue(readConfig.getNode("Plugins.Towny.HideTowns").isPresent());
assertTrue(readConfig.getNode("Plugins.Towny.Enabled").isPresent());
assertFalse(readConfig.getNode("Plugins.Towny.HideTowns.Enabled").isPresent());
}
}
}

View File

@ -150,4 +150,28 @@ class ConfigWriterTest {
assertEquals(expected, writtenLines);
}
@Test
void valueAfterAList() throws IOException {
ConfigNode root = new ConfigNode(null, null, null);
ConfigNode test = root.addNode("Test");
test.addNode("List").set(Arrays.asList("First", "Second", "Third"));
test.addNode("Value").set("Example");
root.addNode("Second").set(2);
Path out = tempFolder.resolve("listIndent.yml");
new ConfigWriter(out).write(root);
List<String> writtenLines = FileUtil.lines(out.toFile());
List<String> expected = Arrays.asList(
"Test:",
" List:",
" - First",
" - Second",
" - Third",
" Value: Example",
"Second: 2"
);
assertEquals(expected, writtenLines);
}
}