Skip to content

Commit

Permalink
Add tests for halfway skipArray/skipObject
Browse files Browse the repository at this point in the history
  • Loading branch information
leadpony committed Sep 1, 2019
1 parent 923fa03 commit 31c3e85
Show file tree
Hide file tree
Showing 13 changed files with 5,748 additions and 3,336 deletions.
6 changes: 3 additions & 3 deletions docs/project-reports.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- Generated by Apache Maven Doxia Site Renderer 1.8.1 from org.apache.maven.plugins:maven-site-plugin:3.7.1:CategorySummaryDocumentRenderer at 2019-08-31 -->
<!-- Generated by Apache Maven Doxia Site Renderer 1.8.1 from org.apache.maven.plugins:maven-site-plugin:3.7.1:CategorySummaryDocumentRenderer at 2019-09-01 -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Expand All @@ -24,8 +24,8 @@
</div>
<div id="breadcrumbs">
<div class="xleft">
<span id="publishDate">Last Published: 2019-08-31</span>
&nbsp;| <span id="projectVersion">Version: 1.3.0-SNAPSHOT</span>
<span id="publishDate">Last Published: 2019-09-01</span>
&nbsp;| <span id="projectVersion">Version: 1.3.0</span>
</div>
<div class="xright"><a href="./" title="JSON-P Test Suite">JSON-P Test Suite</a> </div>
<div class="clear">
Expand Down
3,627 changes: 2,223 additions & 1,404 deletions docs/surefire-report-jakarta.html

Large diffs are not rendered by default.

2,578 changes: 1,656 additions & 922 deletions docs/surefire-report-johnzon.html

Large diffs are not rendered by default.

2,488 changes: 1,614 additions & 874 deletions docs/surefire-report-joy.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.leadpony</groupId>
<artifactId>jsonp-test-suite</artifactId>
<version>1.3.0-SNAPSHOT</version>
<version>1.3.0</version>
<packaging>jar</packaging>

<name>JSON-P Test Suite</name>
Expand Down Expand Up @@ -39,7 +39,7 @@
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.13.1</version>
<version>3.13.2</version>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.leadpony.jsonp.testsuite.tests;
package org.leadpony.jsonp.testsuite.helper;

import javax.json.JsonValue;

/**
* @author leadpony
*/
interface JsonSource {
public interface JsonSupplier {

/**
* Supplies JSON as a string.
*
* @return JSON as a string.
*/
String getJson();

default JsonValue getValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.junit.jupiter.api.Assertions.fail;

import java.math.BigDecimal;
import java.util.AbstractMap;
Expand Down Expand Up @@ -46,6 +47,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.leadpony.jsonp.testsuite.helper.Ambiguous;

/**
* Tests for {@link JsonParser} which parses in-memory JSON structures.
Expand Down Expand Up @@ -998,7 +1000,44 @@ public enum SkipArrayTestCase {
SECOND_ARRAY_IN_OBJECT(
ARRAY_IN_OBJECT.value,
9,
Event.END_OBJECT);
Event.END_OBJECT),

SKIP_NESTED_ARRAY(
array(b -> {
b.add(array(b2 -> {
b2.add(1);
b2.add(2);
b2.add(array(b3 -> b3.add(3).add(4)));
}));
b.add(array(b2 -> {
b2.add(5);
b2.add(6);
}));
}),
2,
Event.START_ARRAY
),

SKIP_NESTED_OBJECT(
array(b -> {
b.add(array(b2 -> {
b2.add(1);
b2.add(2);
b2.add(object(b3 -> b3.add("a", 3)));
}));
b.add(array(b2 -> {
b2.add(4);
b2.add(5);
}));
}),
2,
Event.START_ARRAY
),

EMPTY_ARRAY(
JsonValue.EMPTY_JSON_ARRAY,
1,
null);

final JsonStructure value;
final int iterations;
Expand Down Expand Up @@ -1030,6 +1069,65 @@ public void skipArrayShouldSkipArrayAsExpected(SkipArrayTestCase test) {
assertThat(actual).isEqualTo(test.expected);
}

/**
* @author leadpony
*/
public enum SkipHalfwayArrayTestCase {
ARRAY(
array(b -> b.add(1).add(2).add(3)),
3,
null),

ARRAY_END(
array(b -> b.add(1).add(2).add(3)),
5,
null),

IN_OBJECT(
array(b -> {
b.add(1);
b.add(object(b2 -> b2.add("a", 2)));
b.add(3);
}),
4,
null
);

final JsonStructure value;
final int iterations;
final Event expected;

SkipHalfwayArrayTestCase(JsonStructure value, int iterations, Event expected) {
this.value = value;
this.iterations = iterations;
this.expected = expected;
}
}

@Ambiguous
@ParameterizedTest
@EnumSource(SkipHalfwayArrayTestCase.class)
public void skipArrayShouldSkipHalfwayArrayAsExpected(SkipHalfwayArrayTestCase test) {
JsonParser parser = createParser(test.value);

int iterations = test.iterations;
while (iterations-- > 0) {
parser.next();
}

try {
parser.skipArray();
} catch (Exception e) {
fail(e);
}

Event actual = parser.hasNext() ? parser.next() : null;

parser.close();

assertThat(actual).isEqualTo(test.expected);
}

/**
* Test cases for {@link JsonParser#skipArray()}.
*
Expand Down Expand Up @@ -1059,7 +1157,38 @@ public enum SkipObjectTestCase {
SECOND_OBJECT_IN_OBJECT(
OBJECT_IN_OBJECT.value,
10,
Event.END_OBJECT);
Event.END_OBJECT),

SKIP_NESTED_ARRAY(
object(b -> {
b.add("a", object(b2 -> {
b2.add("b", array(b3 -> {
b3.add(1).add(2);
}));
}));
b.add("c", 3);
}),
3,
Event.KEY_NAME
),

SKIP_NESTED_OBJECT(
object(b -> {
b.add("a", object(b2 -> {
b2.add("b", object(b3 -> {
b3.add("c", 1);
}));
}));
b.add("d", 2);
}),
3,
Event.KEY_NAME
),

EMPTY_OBJECT(
JsonValue.EMPTY_JSON_OBJECT,
1,
null);

final JsonStructure value;
final int iterations;
Expand Down Expand Up @@ -1091,6 +1220,63 @@ public void skipObjectShouldSkipObjectAsExpected(SkipObjectTestCase test) {
assertThat(actual).isEqualTo(test.expected);
}

/**
* @author leadpony
*/
public enum SkipHalfwayObjectTestCase {
OBJECT(
object(b -> b.add("a", 365).add("b", "hello")),
2,
null),

OBJECT_END(
object(b -> b.add("a", 365).add("b", "hello")),
6,
null),

IN_ARRAY(
object(b -> {
b.add("a", array(b2 -> b2.add(1).add(2)));
}),
4,
null
);

final JsonStructure value;
final int iterations;
final Event expected;

SkipHalfwayObjectTestCase(JsonStructure value, int iterations, Event expected) {
this.value = value;
this.iterations = iterations;
this.expected = expected;
}
}

@Ambiguous
@ParameterizedTest
@EnumSource(SkipHalfwayObjectTestCase.class)
public void skipObjectShouldSkipHalfwayObjectAsExpected(SkipHalfwayObjectTestCase test) {
JsonParser parser = createParser(test.value);

int iterations = test.iterations;
while (iterations-- > 0) {
parser.next();
}

try {
parser.skipObject();
} catch (Exception e) {
fail(e);
}

Event actual = parser.hasNext() ? parser.next() : null;

parser.close();

assertThat(actual).isEqualTo(test.expected);
}

private <T> T extractValue(JsonStructure value, int iterations, Function<JsonParser, T> mapper) {
AtomicReference<T> result = new AtomicReference<>();
try (JsonParser parser = createParser(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.leadpony.jsonp.testsuite.helper.JsonSupplier;
import org.leadpony.jsonp.testsuite.helper.LogHelper;

/**
Expand All @@ -50,7 +51,7 @@ public static void setUpOnce() {
*
* @author leadpony
*/
enum IllegalStringRetrievalTestCase implements JsonSource {
enum IllegalStringRetrievalTestCase implements JsonSupplier {
EMPTY("", 0),

TRUE("true", 1),
Expand Down Expand Up @@ -93,7 +94,7 @@ public void getStringShouldThrowIllegalStateException(IllegalStringRetrievalTest
*
* @author leadpony
*/
enum IllegalNumberRetrievalTestCase implements JsonSource {
enum IllegalNumberRetrievalTestCase implements JsonSupplier {
EMPTY("", 0),

TRUE("true", 1),
Expand Down Expand Up @@ -174,7 +175,7 @@ public void getLongNumberShouldThrowIllegalStateException(IllegalNumberRetrieval
*
* @author leadpony
*/
enum IllegalValueRetrievalTestCase implements JsonSource {
enum IllegalValueRetrievalTestCase implements JsonSupplier {
EMPTY("", 0),
ARRAY_CLOSING("[]", 2),
OBJECT_CLOSING("{}", 2);
Expand Down
Loading

0 comments on commit 31c3e85

Please sign in to comment.