Skip to content

Commit

Permalink
Increased test cases coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
pchitolina-fauna committed Jan 31, 2024
1 parent 209c335 commit 10ab034
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 94 deletions.
23 changes: 1 addition & 22 deletions faunaJava/src/main/java/com/fauna/serialization/FaunaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,7 @@ public FaunaTokenType getCurrentTokenType() {
END_REF,
END_ARRAY
));

public void skip() throws IOException {
switch (getCurrentTokenType()) {
case START_OBJECT:
case START_ARRAY:
case START_PAGE:
case START_REF:
case START_DOCUMENT:
skipInternal();
break;
}
}

private void skipInternal() throws IOException {
int startCount = tokenStack.size();
while (read()) {
if (tokenStack.size() < startCount) {
break;
}
}
}


public boolean read() throws IOException {
taggedTokenValue = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ public void testReadObjectTokens() throws IOException {
new AbstractMap.SimpleEntry<>(FaunaTokenType.END_OBJECT, null)
);

assertReader(reader, expectedTokens, false);
assertReader(reader, expectedTokens);
}

@Test
Expand Down Expand Up @@ -499,7 +499,7 @@ public void testReadArray() throws IOException {
new AbstractMap.SimpleEntry<>(FaunaTokenType.END_ARRAY, null)
);

assertReader(reader, expectedTokens, false);
assertReader(reader, expectedTokens);
}

@Test
Expand All @@ -512,76 +512,6 @@ public void throwsOnMalformedJson() {
}, "Failed to advance underlying JSON reader.");
}

@Test
public void skipValues() throws IOException {
List<String> tests = List.of(
"{\"k1\": {}, \"k2\": {}}",
"[\"k1\",[],{}]",
"{\"@ref\": {}}",
"{\"@doc\": {}}",
"{\"@set\": {}}",
"{\"@object\":{}}"
);

for (String test : tests) {
FaunaParser reader = new FaunaParser(new ByteArrayInputStream(test.getBytes()));
reader.read();
reader.skip();
assertFalse(reader.read());
}
}

@Test
public void skipNestedEscapedObject() throws IOException {
String test = "{\"@object\": {\"inner\": {\"@object\": {\"foo\": \"bar\"}}, \"k2\": {}}}";
FaunaParser reader = new FaunaParser(new ByteArrayInputStream(test.getBytes()));
reader.read(); // {"@object":{
assertEquals(FaunaTokenType.START_OBJECT, reader.getCurrentTokenType());
reader.read(); // inner
assertEquals(FaunaTokenType.FIELD_NAME, reader.getCurrentTokenType());
reader.read(); // {"@object":{
assertEquals(FaunaTokenType.START_OBJECT, reader.getCurrentTokenType());
reader.skip(); // "foo": "bar"}}
assertEquals(FaunaTokenType.END_OBJECT, reader.getCurrentTokenType());
reader.read();
assertEquals(FaunaTokenType.FIELD_NAME, reader.getCurrentTokenType());
assertEquals("k2", reader.getValueAsString());
}

@Test
public void skipNestedObject() throws IOException {
String test = "{\"k\":{\"inner\":{}},\"k2\":{}}";
FaunaParser reader = new FaunaParser(new ByteArrayInputStream(test.getBytes()));
reader.read(); // {
assertEquals(FaunaTokenType.START_OBJECT, reader.getCurrentTokenType());
reader.read(); // k
assertEquals(FaunaTokenType.FIELD_NAME, reader.getCurrentTokenType());
reader.read(); // {
assertEquals(FaunaTokenType.START_OBJECT, reader.getCurrentTokenType());
reader.skip(); // "inner":{}}
assertEquals(FaunaTokenType.END_OBJECT, reader.getCurrentTokenType());
reader.read();
assertEquals(FaunaTokenType.FIELD_NAME, reader.getCurrentTokenType());
assertEquals("k2", reader.getValueAsString());
}

@Test
public void skipNestedArrays() throws IOException {
String test = "{\"k\":[\"1\",\"2\"],\"k2\":{}}";
FaunaParser reader = new FaunaParser(new ByteArrayInputStream(test.getBytes()));
reader.read(); // {
assertEquals(FaunaTokenType.START_OBJECT, reader.getCurrentTokenType());
reader.read(); // k
assertEquals(FaunaTokenType.FIELD_NAME, reader.getCurrentTokenType());
reader.read(); // [
assertEquals(FaunaTokenType.START_ARRAY, reader.getCurrentTokenType());
reader.skip(); // "1","2"]
assertEquals(FaunaTokenType.END_ARRAY, reader.getCurrentTokenType());
reader.read();
assertEquals(FaunaTokenType.FIELD_NAME, reader.getCurrentTokenType());
assertEquals("k2", reader.getValueAsString());
}

private static void assertReader(FaunaParser reader,
List<Map.Entry<FaunaTokenType, Object>> tokens) throws IOException {
for (Map.Entry<FaunaTokenType, Object> entry : tokens) {
Expand Down

0 comments on commit 10ab034

Please sign in to comment.