-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented tests for YamlParser and YamlDumper, bumped bson to 5.0.0
- Loading branch information
Showing
7 changed files
with
131 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 19 additions & 10 deletions
29
src/main/java/org/javawebstack/abstractdata/yaml/LegacyYamlParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,34 @@ | ||
package org.javawebstack.abstractdata.yaml; | ||
|
||
import org.javawebstack.abstractdata.AbstractElement; | ||
import org.javawebstack.abstractdata.AbstractNull; | ||
import org.yaml.snakeyaml.Yaml; | ||
import org.yaml.snakeyaml.parser.ParserException; | ||
|
||
import java.util.HashMap; | ||
import java.text.ParseException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class LegacyYamlParser { | ||
|
||
protected static AbstractElement parse(String source, boolean singleRoot) { | ||
Yaml yaml = new Yaml(); | ||
Object object = yaml.load(source); | ||
if (singleRoot && object instanceof List) { | ||
List<Object> list = (List<Object>) object; | ||
if (list.size() == 0) { | ||
object = new HashMap<>(); | ||
} else { | ||
protected static AbstractElement parse(String source, boolean singleRoot) throws ParseException { | ||
if(source.trim().equalsIgnoreCase("null")) | ||
return AbstractNull.VALUE; | ||
if(source.trim().isEmpty()) | ||
throw new ParseException("Invalid yaml", 0); | ||
try { | ||
Yaml yaml = new Yaml(); | ||
List<Object> list = new ArrayList<>(); | ||
yaml.loadAll(source).forEach(list::add); | ||
Object object = list; | ||
if (singleRoot && list.size() == 1) { | ||
object = list.get(0); | ||
} | ||
return AbstractElement.fromObject(object); | ||
} catch (ParserException e) { | ||
throw new ParseException(e.getMessage(), e.getProblemMark().getIndex()); | ||
} | ||
return AbstractElement.fromObject(object); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/test/java/org/javawebstack/abstractdata/yaml/YamlDumperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.javawebstack.abstractdata.yaml; | ||
|
||
import org.javawebstack.abstractdata.AbstractArray; | ||
import org.javawebstack.abstractdata.AbstractObject; | ||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class YamlDumperTest { | ||
|
||
@Test | ||
public void testDump() { | ||
AbstractObject object = new AbstractObject() | ||
.set("a", 1) | ||
.set("b", new AbstractArray()); | ||
String expected = "a: 1\nb: []\n"; // Not actually minified but that's what snakeyaml outputs | ||
String dumped = new YamlDumper().dump(object); | ||
assertEquals(expected, dumped); | ||
} | ||
|
||
@Test | ||
public void testDumpPretty() { | ||
AbstractObject object = new AbstractObject() | ||
.set("a", 1) | ||
.set("b", new AbstractArray()); | ||
String expected = "a: 1\nb: [\n ]\n"; // Not actually expected, as it's not pretty but that's what snakeyaml calls pretty | ||
String dumped = new YamlDumper().setPretty(true).dump(object); | ||
assertEquals(expected, dumped); | ||
} | ||
|
||
@Test | ||
public void testSetPretty() { | ||
YamlDumper dumper = new YamlDumper(); | ||
assertFalse(dumper.isPretty()); | ||
dumper.setPretty(true); | ||
assertTrue(dumper.isPretty()); | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
src/test/java/org/javawebstack/abstractdata/yaml/YamlParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.javawebstack.abstractdata.yaml; | ||
|
||
import org.javawebstack.abstractdata.AbstractElement; | ||
import org.junit.jupiter.api.Test; | ||
import org.yaml.snakeyaml.Yaml; | ||
|
||
import java.text.ParseException; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class YamlParserTest { | ||
|
||
@Test | ||
public void testParse() { | ||
AbstractElement e = assertDoesNotThrow(() -> new YamlParser().setSingleRoot(true).parse("test:\n a: 123\n b: false\n c: 'abc'")); | ||
assertNotNull(e); | ||
assertTrue(e.isObject()); | ||
assertEquals(1, e.object().size()); | ||
AbstractElement testElement = e.object().get("test"); | ||
assertNotNull(testElement); | ||
assertTrue(testElement.isObject()); | ||
assertEquals(3, testElement.object().size()); | ||
AbstractElement aElement = testElement.object().get("a"); | ||
assertNotNull(aElement); | ||
assertTrue(aElement.isNumber()); | ||
assertEquals(123, aElement.number()); | ||
AbstractElement bElement = testElement.object().get("b"); | ||
assertNotNull(bElement); | ||
assertTrue(bElement.isBoolean()); | ||
assertEquals(false, bElement.bool()); | ||
AbstractElement cElement = testElement.object().get("c"); | ||
assertNotNull(cElement); | ||
assertTrue(cElement.isString()); | ||
assertEquals("abc", cElement.string()); | ||
} | ||
|
||
@Test | ||
public void testParseEmptyDocument() { | ||
assertThrows(ParseException.class, () -> new YamlParser().parse("")); | ||
} | ||
|
||
@Test | ||
public void testParseNull() { | ||
AbstractElement e = assertDoesNotThrow(() -> new YamlParser().parse("null")); | ||
assertNotNull(e); | ||
assertTrue(e.isNull()); | ||
} | ||
|
||
@Test | ||
public void testParseInvalidDocument() { | ||
assertThrows(ParseException.class, () -> new YamlParser().parse("\"a\"1")); | ||
} | ||
|
||
@Test | ||
public void testSetSingleRoot() { | ||
YamlParser parser = new YamlParser(); | ||
assertFalse(parser.isSingleRoot()); | ||
parser.setSingleRoot(true); | ||
assertTrue(parser.isSingleRoot()); | ||
} | ||
|
||
} |