Skip to content

Commit e75339b

Browse files
committed
Unify more diagnostics and get JSONCompareResult to do more of the pretty printing
1 parent 742e387 commit e75339b

File tree

3 files changed

+21
-31
lines changed

3 files changed

+21
-31
lines changed

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

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ else if ((expected instanceof JSONArray) && (actual instanceof JSONArray)) {
3535
return compareJSON((JSONArray)expected, (JSONArray)actual, mode);
3636
}
3737
else if (expected instanceof JSONObject) {
38-
return new JSONCompareResult().fail("", "a JSON object", "a JSON array");
38+
return new JSONCompareResult().fail("", expected, actual);
3939
}
4040
else {
41-
return new JSONCompareResult().fail("", "a JSON array", "a JSON object");
41+
return new JSONCompareResult().fail("", expected, actual);
4242
}
4343
}
4444

@@ -115,30 +115,7 @@ else if (!expectedValue.equals(actualValue)) {
115115
result.fail(fullKey, expectedValue, actualValue);
116116
}
117117
} else {
118-
if (isNull(expectedValue)) {
119-
result.fail(fullKey + ": expected null, but got " + classToType(actualValue));
120-
} else if (isNull(actualValue)) {
121-
result.fail(fullKey + ": expected " + classToType(expectedValue) + ", but got null");
122-
} else {
123-
result.fail("Values of " + fullKey + " have different types: expected " + classToType(expectedValue)
124-
+ ", but got " + classToType(actualValue));
125-
}
126-
}
127-
}
128-
129-
private static boolean isNull(Object value) {
130-
return value.getClass().getSimpleName().equals("Null");
131-
}
132-
133-
private static String classToType(Object value) {
134-
if (value instanceof JSONArray) {
135-
return "an array";
136-
} else if (value instanceof JSONObject) {
137-
return "an object";
138-
} else if (value instanceof String) {
139-
return "a string";
140-
} else {
141-
return value.getClass().getName();
118+
result.fail(fullKey, expectedValue, actualValue);
142119
}
143120
}
144121

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import java.util.Collections;
55
import java.util.List;
66

7+
import org.json.JSONArray;
8+
import org.json.JSONObject;
9+
710
/**
811
* Bean for holding results from JSONCompare.
912
*/
@@ -129,13 +132,23 @@ private String formatFailureMessage(String field, Object expected, Object actual
129132
StringBuffer message= new StringBuffer();
130133
message.append(field);
131134
message.append("\nExpected: ");
132-
message.append(expected + "");
135+
message.append(describe(expected));
133136
message.append("\n got: ");
134-
message.append(actual + "");
137+
message.append(describe(actual));
135138
message.append("\n");
136139
return message.toString();
137140
}
138141

142+
private static String describe(Object value) {
143+
if (value instanceof JSONArray) {
144+
return "a JSON array";
145+
} else if (value instanceof JSONObject) {
146+
return "a JSON object";
147+
} else {
148+
return value.toString();
149+
}
150+
}
151+
139152
@Override
140153
public String toString() {
141154
return _message;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ public void reportsUnexpectedObjectWhenExpectingArray() throws JSONException {
5555
@Test
5656
public void reportsUnexpectedNull() throws JSONException {
5757
JSONCompareResult result = compareJSON("{\"id\": 3}", "{\"id\": null}", LENIENT);
58-
assertThat(result, failsWithMessage(equalTo("id: expected java.lang.Integer, but got null")));
58+
assertThat(result, failsWithMessage(equalTo("id\nExpected: 3\n got: null\n")));
5959
}
6060

6161
@Test
6262
public void reportsUnexpectedNonNull() throws JSONException {
6363
JSONCompareResult result = compareJSON("{\"id\": null}", "{\"id\": \"abc\"}", LENIENT);
64-
assertThat(result, failsWithMessage(equalTo("id: expected null, but got a string")));
64+
assertThat(result, failsWithMessage(equalTo("id\nExpected: null\n got: abc\n")));
6565
}
6666

6767
@Test
@@ -73,7 +73,7 @@ public void reportsUnexpectedFieldInNonExtensibleMode() throws JSONException {
7373
@Test
7474
public void reportsMismatchedTypes() throws JSONException {
7575
JSONCompareResult result = compareJSON("{\"arr\":[]}", "{\"arr\":{}}", LENIENT);
76-
assertThat(result, failsWithMessage(equalTo("Values of arr have different types: expected an array, but got an object")));
76+
assertThat(result, failsWithMessage(equalTo("arr\nExpected: a JSON array\n got: a JSON object\n")));
7777
}
7878

7979
@Test

0 commit comments

Comments
 (0)