Skip to content

Commit 4e79823

Browse files
committed
Add more tests of diagnostics
1 parent 4708f8a commit 4e79823

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

src/test/java/org/skyscreamer/jsonassert/JSONCompareTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.junit.Assert.assertTrue;
66
import static org.skyscreamer.jsonassert.JSONCompare.compareJSON;
77
import static org.skyscreamer.jsonassert.JSONCompareMode.LENIENT;
8+
import static org.skyscreamer.jsonassert.JSONCompareMode.NON_EXTENSIBLE;
89

910
import org.hamcrest.Description;
1011
import org.hamcrest.Matcher;
@@ -16,6 +17,65 @@
1617
* Unit tests for {@code JSONCompare}.
1718
*/
1819
public class JSONCompareTest {
20+
@Test
21+
public void succeedsWithEmptyArrays() throws JSONException {
22+
assertTrue(compareJSON("[]", "[]", LENIENT).passed());
23+
}
24+
25+
@Test
26+
public void reportsArraysOfUnequalLength() throws JSONException {
27+
JSONCompareResult result = compareJSON("[4]", "[]", LENIENT);
28+
assertThat(result, failsWithMessage(equalTo("[]: Expected 1 values and got 0")));
29+
}
30+
31+
@Test
32+
public void reportsArrayMissingExpectedElement() throws JSONException {
33+
JSONCompareResult result = compareJSON("[4]", "[7]", LENIENT);
34+
assertThat(result, failsWithMessage(equalTo("[]: Expected 4, but not found ; []: Contains 7, but not expected")));
35+
}
36+
37+
@Test
38+
public void reportsMismatchedFieldValues() throws JSONException {
39+
JSONCompareResult result = compareJSON("{\"id\": 3}", "{\"id\": 5}", LENIENT);
40+
assertThat(result, failsWithMessage(equalTo("id\nExpected: 3\n got: 5\n")));
41+
}
42+
43+
@Test
44+
public void reportsUnexpectedArrayWhenExpectingObject() throws JSONException {
45+
JSONCompareResult result = compareJSON("{}", "[]", LENIENT);
46+
assertThat(result, failsWithMessage(equalTo("\nExpected: a JSON object\n got: a JSON array\n")));
47+
}
48+
49+
@Test
50+
public void reportsUnexpectedObjectWhenExpectingArray() throws JSONException {
51+
JSONCompareResult result = compareJSON("[]", "{}", LENIENT);
52+
assertThat(result, failsWithMessage(equalTo("\nExpected: a JSON array\n got: a JSON object\n")));
53+
}
54+
55+
@Test
56+
public void reportsUnexpectedNull() throws JSONException {
57+
JSONCompareResult result = compareJSON("{\"id\": 3}", "{\"id\": null}", LENIENT);
58+
assertThat(result, failsWithMessage(equalTo("id: expected java.lang.Integer, but got null")));
59+
}
60+
61+
@Test
62+
public void reportsUnexpectedNonNull() throws JSONException {
63+
JSONCompareResult result = compareJSON("{\"id\": null}", "{\"id\": \"abc\"}", LENIENT);
64+
assertThat(result, failsWithMessage(equalTo("id: expected null, but got a string")));
65+
}
66+
67+
@Test
68+
public void reportsUnexpectedFieldInNonExtensibleMode() throws JSONException {
69+
JSONCompareResult result = compareJSON("{}", "{\"id\": 3}", NON_EXTENSIBLE);
70+
assertThat(result, failsWithMessage(equalTo("Strict checking failed. Got but did not expect: id")));
71+
}
72+
73+
@Test
74+
public void reportsMismatchedTypes() throws JSONException {
75+
JSONCompareResult result = compareJSON("{\"arr\":[]}", "{\"arr\":{}}", LENIENT);
76+
assertThat(result, failsWithMessage(equalTo("Values of arr have different types: expected an array, but got an object")));
77+
}
78+
1979
@Test
2080
public void reportsWrongSimpleValueCountInUnorderedArray() throws JSONException {
2181
JSONCompareResult result = compareJSON("[5, 5]", "[5, 7]", LENIENT);

0 commit comments

Comments
 (0)