Skip to content

Commit 742e387

Browse files
committed
Merge pull request skyscreamer#16 from skyscreamer/diagnostics-enhance
This is really great. The messages are much more informative and useful.
2 parents d30fa83 + cdb7de5 commit 742e387

File tree

2 files changed

+82
-21
lines changed

2 files changed

+82
-21
lines changed

src/main/java/org/skyscreamer/jsonassert/JSONCompare.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ private static void compareJSON(String prefix, JSONObject expected, JSONObject a
9191
}
9292
}
9393

94-
// If strict, check for vice-versa
94+
// If non-extensible, check for vice-versa
9595
if (!mode.isExtensible()) {
9696
Set<String> actualKeys = getKeys(actual);
9797
for(String key : actualKeys) {
9898
if (!expected.has(key)) {
99-
result.fail("Strict checking failed. Got but did not expect: " + prefix + key);
99+
result.fail("Got unexpected field: " + prefix + key);
100100
}
101101
}
102102
}
@@ -169,8 +169,8 @@ else if (allSimpleValues(expected)) {
169169
result.fail(key + "[]: Expected " + o + ", but not found");
170170
}
171171
else if (actualCount.get(o) != expectedCount.get(o)) {
172-
result.fail(key + "[]: Expected contains " + expectedCount.get(o) + " " + o
173-
+ " actual contains " + actualCount.get(o));
172+
result.fail(key + "[]: Expected " + expectedCount.get(o) + " occurrence(s) of " + o
173+
+ " but got " + actualCount.get(o) + " occurrence(s)");
174174
}
175175
}
176176
for(Object o : actualCount.keySet()) {
@@ -199,7 +199,7 @@ else if (allJSONObjects(expected)) {
199199
}
200200
for(Object id : actualValueMap.keySet()) {
201201
if (!expectedValueMap.containsKey(id)) {
202-
result.fail(key + "[]: Contains object where \" + uniqueKey + \"=\" + id + \", but not expected");
202+
result.fail(key + "[]: Contains object where " + uniqueKey + "=" + id + ", but not expected");
203203
}
204204
}
205205
}
@@ -220,35 +220,36 @@ else if (allJSONArrays(expected)) {
220220
private static void recursivelyCompareJSONArray(String key, JSONArray expected, JSONArray actual,
221221
JSONCompareMode mode, JSONCompareResult result) throws JSONException {
222222
Set<Integer> matched = new HashSet<Integer>();
223-
for(int i = 0 ; i < actual.length() ; ++i) {
224-
Object actualElement = actual.get(i);
223+
for(int i = 0 ; i < expected.length() ; ++i) {
224+
Object expectedElement = expected.get(i);
225225
boolean matchFound = false;
226-
for(int j = 0 ; j < expected.length() ; ++j) {
227-
if (matched.contains(j) || !actual.get(i).getClass().equals(expected.get(j).getClass())) {
226+
for(int j = 0 ; j < actual.length() ; ++j) {
227+
Object actualElement = actual.get(j);
228+
if (matched.contains(j) || !actualElement.getClass().equals(expectedElement.getClass())) {
228229
continue;
229230
}
230-
if (actualElement instanceof JSONObject) {
231-
if (compareJSON((JSONObject)expected.get(j), (JSONObject)actualElement, mode).passed()) {
231+
if (expectedElement instanceof JSONObject) {
232+
if (compareJSON((JSONObject)expectedElement, (JSONObject)actualElement, mode).passed()) {
232233
matched.add(j);
233234
matchFound = true;
234235
break;
235236
}
236237
}
237-
else if (actualElement instanceof JSONArray) {
238-
if (compareJSON((JSONArray)expected.get(j), (JSONArray)actualElement, mode).passed()) {
238+
else if (expectedElement instanceof JSONArray) {
239+
if (compareJSON((JSONArray)expectedElement, (JSONArray)actualElement, mode).passed()) {
239240
matched.add(j);
240241
matchFound = true;
241242
break;
242243
}
243244
}
244-
else if (actualElement.equals(expected.get(j))) {
245+
else if (expectedElement.equals(actualElement)) {
245246
matched.add(j);
246247
matchFound = true;
247248
break;
248249
}
249250
}
250251
if (!matchFound) {
251-
result.fail("Could not find match for element " + actualElement);
252+
result.fail(key + "[" + i + "] Could not find match for element " + expectedElement);
252253
return;
253254
}
254255
}

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

Lines changed: 66 additions & 6 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,23 +17,82 @@
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("{\"obj\": {}}", "{\"obj\": {\"id\": 3}}", NON_EXTENSIBLE);
70+
assertThat(result, failsWithMessage(equalTo("Got unexpected field: obj.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);
22-
assertThat(result, failsWithMessage(equalTo("[]: Expected contains 2 5 actual contains 1 ; []: Contains 7, but not expected")));
82+
assertThat(result, failsWithMessage(equalTo("[]: Expected 2 occurrence(s) of 5 but got 1 occurrence(s) ; []: Contains 7, but not expected")));
2383
}
2484

2585
@Test
2686
public void reportsMissingJSONObjectWithUniqueKeyInUnorderedArray() throws JSONException {
2787
JSONCompareResult result = compareJSON("[{\"id\" : 3}]", "[{\"id\" : 5}]", LENIENT);
2888
assertThat(result, failsWithMessage(equalTo("[]: Expected but did not find object where id=3 ; " +
29-
"[]: Contains object where \" + uniqueKey + \"=\" + id + \", but not expected")));
89+
"[]: Contains object where id=5, but not expected")));
3090
}
3191

3292
@Test
3393
public void reportsUnmatchedJSONObjectInUnorderedArray() throws JSONException {
3494
JSONCompareResult result = compareJSON("[{\"address\" : {\"street\" : \"Acacia Avenue\"}}]", "[{\"age\" : 23}]", LENIENT);
35-
assertThat(result, failsWithMessage(equalTo("Could not find match for element {\"age\":23}")));
95+
assertThat(result, failsWithMessage(equalTo("[0] Could not find match for element {\"address\":{\"street\":\"Acacia Avenue\"}}")));
3696
}
3797

3898
@Test
@@ -55,19 +115,19 @@ public void succeedsWithSomeNestedJSONObjectsInUnorderedArray() throws JSONExcep
55115
@Test
56116
public void reportsUnmatchesIntegerValueInUnorderedArrayContainingJSONObject() throws JSONException {
57117
JSONCompareResult result = compareJSON("[{\"address\" : {\"street\" : \"Acacia Avenue\"}}, 5]", "[{\"address\" : {\"street\" : \"Acacia Avenue\"}}, 2]", LENIENT);
58-
assertThat(result, failsWithMessage(equalTo("Could not find match for element 2")));
118+
assertThat(result, failsWithMessage(equalTo("[1] Could not find match for element 5")));
59119
}
60120

61121
@Test
62122
public void reportsUnmatchedJSONArrayWhereOnlyExpectedContainsJSONObjectWithUniqueKey() throws JSONException {
63123
JSONCompareResult result = compareJSON("[{\"id\": 3}]", "[{}]", LENIENT);
64-
assertThat(result, failsWithMessage(equalTo("Could not find match for element {}")));
124+
assertThat(result, failsWithMessage(equalTo("[0] Could not find match for element {\"id\":3}")));
65125
}
66126

67127
@Test
68128
public void reportsUnmatchedJSONArrayWhereExpectedContainsJSONObjectWithUniqueKeyButActualContainsElementOfOtherType() throws JSONException {
69129
JSONCompareResult result = compareJSON("[{\"id\": 3}]", "[5]", LENIENT);
70-
assertThat(result, failsWithMessage(equalTo("Could not find match for element 5")));
130+
assertThat(result, failsWithMessage(equalTo("[0] Could not find match for element {\"id\":3}")));
71131
}
72132

73133
private Matcher<JSONCompareResult> failsWithMessage(final Matcher<String> expectedMessage) {

0 commit comments

Comments
 (0)