-
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 test for collectors and JsonDumper
- Loading branch information
Showing
6 changed files
with
164 additions
and
2 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
20 changes: 20 additions & 0 deletions
20
src/test/java/org/javawebstack/abstractdata/collector/AbstractArrayCollectorTest.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,20 @@ | ||
package org.javawebstack.abstractdata.collector; | ||
|
||
import org.javawebstack.abstractdata.AbstractArray; | ||
import org.javawebstack.abstractdata.AbstractPrimitive; | ||
import org.junit.jupiter.api.Test; | ||
import java.util.stream.Stream; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class AbstractArrayCollectorTest { | ||
|
||
@Test | ||
public void testCollection() { | ||
AbstractArray array = Stream.of("a", "b", "c").parallel().collect(new AbstractArrayCollector<>(AbstractPrimitive::new)); | ||
assertEquals(3, array.size()); | ||
assertEquals("a", array.string(0)); | ||
assertEquals("b", array.string(1)); | ||
assertEquals("c", array.string(2)); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/test/java/org/javawebstack/abstractdata/collector/AbstractObjectCollectorTest.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,21 @@ | ||
package org.javawebstack.abstractdata.collector; | ||
|
||
import org.javawebstack.abstractdata.AbstractObject; | ||
import org.javawebstack.abstractdata.AbstractPrimitive; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.stream.Stream; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class AbstractObjectCollectorTest { | ||
|
||
@Test | ||
public void testCollection() { | ||
AbstractObject object = Stream.of("a", "bc", "def").parallel().collect(new AbstractObjectCollector<>(s -> s, s -> new AbstractPrimitive(s.length()))); | ||
assertEquals(3, object.size()); | ||
assertEquals(1, object.number("a")); | ||
assertEquals(2, object.number("bc")); | ||
assertEquals(3, object.number("def")); | ||
} | ||
|
||
} |
116 changes: 116 additions & 0 deletions
116
src/test/java/org/javawebstack/abstractdata/json/JsonDumperTest.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,116 @@ | ||
package org.javawebstack.abstractdata.json; | ||
|
||
import org.javawebstack.abstractdata.*; | ||
import org.junit.jupiter.api.Test; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class JsonDumperTest { | ||
|
||
@Test | ||
public void testDumpJavaNull() { | ||
assertEquals("null", new JsonDumper().dump(null)); | ||
} | ||
|
||
@Test | ||
public void testDumpNull() { | ||
assertEquals("null", new JsonDumper().dump(AbstractNull.VALUE)); | ||
} | ||
|
||
@Test | ||
public void testDumpString() { | ||
assertEquals("\"abc\"", new JsonDumper().dump(new AbstractPrimitive("abc"))); | ||
} | ||
|
||
@Test | ||
public void testDumpNumber() { | ||
assertEquals("123.456", new JsonDumper().dump(new AbstractPrimitive(123.456))); | ||
} | ||
|
||
@Test | ||
public void testDumpBoolean() { | ||
assertEquals("true", new JsonDumper().dump(new AbstractPrimitive(true))); | ||
assertEquals("false", new JsonDumper().dump(new AbstractPrimitive(false))); | ||
} | ||
|
||
@Test | ||
public void testDumpObject() { | ||
assertEquals("{\"a\":1,\"b\":2}", new JsonDumper().dump(new AbstractObject().set("a", 1).set("b", 2))); | ||
} | ||
|
||
@Test | ||
public void testDumpArray() { | ||
assertEquals("[1,2,3]", new JsonDumper().dump(new AbstractArray().add(1).add(2).add(3))); | ||
} | ||
|
||
@Test | ||
public void testEscapeStringEscapeSeq() { | ||
Map<String, String> escapes = new HashMap<>(); | ||
escapes.put("\"", "\\\""); | ||
escapes.put("\\", "\\\\"); | ||
escapes.put("\b", "\\b"); | ||
escapes.put("\f", "\\f"); | ||
escapes.put("\n", "\\n"); | ||
escapes.put("\r", "\\r"); | ||
escapes.put("\t", "\\t"); | ||
escapes.put("\0", "\\0"); | ||
escapes.put("/", "\\/"); | ||
for(String c : escapes.keySet()) { | ||
assertEquals("\"" + escapes.get(c) + "\"", new JsonDumper().dump(new AbstractPrimitive(c))); | ||
} | ||
} | ||
|
||
@Test | ||
public void testEscapeStringUnicode() { | ||
assertEquals("\"\\u001F\"", new JsonDumper().dump(new AbstractPrimitive("\u001F"))); | ||
} | ||
|
||
@Test | ||
public void testPrettyFlag() { | ||
AbstractObject o = new AbstractObject() | ||
.set("a", 1) | ||
.set("b", new AbstractArray().add("a").add("b")); | ||
String expectedNonPretty = "{\"a\":1,\"b\":[\"a\",\"b\"]}"; | ||
String expectedPretty = "{\n \"a\": 1,\n \"b\": [\n \"a\",\n \"b\"\n ]\n}"; | ||
assertEquals(expectedNonPretty, new JsonDumper().dump(o)); | ||
assertEquals(expectedPretty, new JsonDumper().setPretty(true).dump(o)); | ||
} | ||
|
||
@Test | ||
public void testIndent() { | ||
AbstractObject o = new AbstractObject() | ||
.set("a", 1) | ||
.set("b", new AbstractArray().add("a").add("b")); | ||
String defaultIndent = " "; | ||
String shortIndent = " "; | ||
Function<String, String> expectedFn = indent -> "{\n" + indent + "\"a\": 1,\n" + indent + "\"b\": [\n" + indent + indent + "\"a\",\n" + indent + indent + "\"b\"\n" + indent + "]\n}"; | ||
assertEquals(expectedFn.apply(defaultIndent), new JsonDumper().setPretty(true).dump(o)); | ||
assertEquals(expectedFn.apply(shortIndent), new JsonDumper().setIndent(shortIndent).setPretty(true).dump(o)); | ||
} | ||
|
||
@Test | ||
public void testNoNewlineOnEmptyObject() { | ||
AbstractObject object = new AbstractObject().set("a", new AbstractObject()); | ||
String expected = "{\n \"a\": {}\n}"; | ||
assertEquals(expected, new JsonDumper().setPretty(true).dump(object)); | ||
} | ||
|
||
@Test | ||
public void testNoNewlineOnEmptyArray() { | ||
AbstractObject object = new AbstractObject().set("a", new AbstractArray()); | ||
String expected = "{\n \"a\": []\n}"; | ||
assertEquals(expected, new JsonDumper().setPretty(true).dump(object)); | ||
} | ||
|
||
@Test | ||
public void testDumpUnknownType() { | ||
AbstractElement e = mock(AbstractElement.class); | ||
assertThrows(IllegalArgumentException.class, () -> new JsonDumper().dump(e)); | ||
} | ||
|
||
} |