Skip to content

Commit 70aee76

Browse files
committed
skyscreamer#12 provide diagnostics when comparing JSONObject with JSONArray
1 parent 19806c1 commit 70aee76

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ else if ((expected instanceof JSONArray) && (actual instanceof JSONArray)) {
3333
return compareJSON((JSONArray)expected, (JSONArray)actual, mode);
3434
}
3535
else if (expected instanceof JSONObject) {
36-
throw new IllegalArgumentException("Expected a JSON object, but passed in a JSON array.");
36+
return new JSONCompareResult().fail("", "a JSON object", "a JSON array");
3737
}
3838
else {
39-
throw new IllegalArgumentException("Expected a JSON array, but passed in a JSON object.");
39+
return new JSONCompareResult().fail("", "a JSON array", "a JSON object");
4040
}
4141
}
4242

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,12 @@ protected void fail(String message) {
9595
}
9696
}
9797

98-
protected void fail(String field, Object expected, Object actual) {
98+
protected JSONCompareResult fail(String field, Object expected, Object actual) {
9999
this._field = field;
100100
this._expected = expected;
101101
this._actual = actual;
102102
fail(formatFailureMessage(field, expected, actual));
103+
return this;
103104
}
104105

105106
private String formatFailureMessage(String field, Object expected, Object actual) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ public void testNullEquality() throws JSONException {
166166
testPass("{id:1,name:null}", "{id:1,name:null}", true);
167167
}
168168

169+
@Test
170+
public void testExpectedArrayButActualObject() throws JSONException {
171+
testFail("[1]", "{id:1}", false);
172+
}
173+
174+
@Test
175+
public void testExpectedObjectButActualArray() throws JSONException {
176+
testFail("{id:1}", "[1]", false);
177+
}
178+
169179
private void testPass(String expected, String actual, boolean strict)
170180
throws JSONException
171181
{

0 commit comments

Comments
 (0)