Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f5eb3d4

Browse filesBrowse files
committed
Unify diagnostics for unexpected values
1 parent 22b2c7c commit f5eb3d4
Copy full SHA for f5eb3d4

File tree

Expand file treeCollapse file tree

3 files changed

+23
-9
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+23
-9
lines changed
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: src/main/java/org/skyscreamer/jsonassert/JSONCompare.java
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private static void compareJSON(String prefix, JSONObject expected, JSONObject a
9595
Set<String> actualKeys = getKeys(actual);
9696
for(String key : actualKeys) {
9797
if (!expected.has(key)) {
98-
result.fail("Got unexpected field: " + qualify(prefix, key));
98+
result.unexpected(prefix, key);
9999
}
100100
}
101101
}
@@ -127,7 +127,7 @@ private static void compareJSONArray(String key, JSONArray expected, JSONArray a
127127
JSONCompareResult result) throws JSONException
128128
{
129129
if (expected.length() != actual.length()) {
130-
result.fail(key + "[]: Expected " + expected.length() + " values and got " + actual.length());
130+
result.fail(key + "[]: Expected " + expected.length() + " values but got " + actual.length());
131131
return;
132132
}
133133
else if (expected.length() == 0) {
@@ -155,7 +155,7 @@ else if (actualCount.get(o) != expectedCount.get(o)) {
155155
}
156156
for(Object o : actualCount.keySet()) {
157157
if (!expectedCount.containsKey(o)) {
158-
result.fail(key + "[]: Contains " + o + ", but not expected");
158+
result.unexpected(key + "[]", o);
159159
}
160160
}
161161
}
@@ -179,7 +179,7 @@ else if (allJSONObjects(expected)) {
179179
}
180180
for(Object id : actualValueMap.keySet()) {
181181
if (!expectedValueMap.containsKey(id)) {
182-
result.fail(key + "[]: Contains object where " + uniqueKey + "=" + id + ", but not expected");
182+
result.unexpected(formatUniqueKey(key, uniqueKey, id), actualValueMap.get(id));
183183
}
184184
}
185185
}
Collapse file

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

Copy file name to clipboardExpand all lines: src/main/java/org/skyscreamer/jsonassert/JSONCompareResult.java
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,20 @@ private String formatMissing(String field, Object expected) {
153153
return message.toString();
154154
}
155155

156+
public JSONCompareResult unexpected(String field, Object value) {
157+
fail(formatUnexpected(field, value));
158+
return this;
159+
}
160+
161+
private String formatUnexpected(String field, Object value) {
162+
StringBuffer message= new StringBuffer();
163+
message.append(field);
164+
message.append("\nUnexpected: ");
165+
message.append(describe(value));
166+
message.append("\n");
167+
return message.toString();
168+
}
169+
156170
private static String describe(Object value) {
157171
if (value instanceof JSONArray) {
158172
return "a JSON array";
Collapse file

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

Copy file name to clipboardExpand all lines: src/test/java/org/skyscreamer/jsonassert/JSONCompareTest.java
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ public void succeedsWithEmptyArrays() throws JSONException {
2525
@Test
2626
public void reportsArraysOfUnequalLength() throws JSONException {
2727
JSONCompareResult result = compareJSON("[4]", "[]", LENIENT);
28-
assertThat(result, failsWithMessage(equalTo("[]: Expected 1 values and got 0")));
28+
assertThat(result, failsWithMessage(equalTo("[]: Expected 1 values but got 0")));
2929
}
3030

3131
@Test
3232
public void reportsArrayMissingExpectedElement() throws JSONException {
3333
JSONCompareResult result = compareJSON("[4]", "[7]", LENIENT);
34-
assertThat(result, failsWithMessage(equalTo("[]\nExpected: 4\n but none found\n ; []: Contains 7, but not expected")));
34+
assertThat(result, failsWithMessage(equalTo("[]\nExpected: 4\n but none found\n ; []\nUnexpected: 7\n")));
3535
}
3636

3737
@Test
@@ -73,7 +73,7 @@ public void reportsUnexpectedNonNull() throws JSONException {
7373
@Test
7474
public void reportsUnexpectedFieldInNonExtensibleMode() throws JSONException {
7575
JSONCompareResult result = compareJSON("{\"obj\": {}}", "{\"obj\": {\"id\": 3}}", NON_EXTENSIBLE);
76-
assertThat(result, failsWithMessage(equalTo("Got unexpected field: obj.id")));
76+
assertThat(result, failsWithMessage(equalTo("obj\nUnexpected: id\n")));
7777
}
7878

7979
@Test
@@ -85,14 +85,14 @@ public void reportsMismatchedTypes() throws JSONException {
8585
@Test
8686
public void reportsWrongSimpleValueCountInUnorderedArray() throws JSONException {
8787
JSONCompareResult result = compareJSON("[5, 5]", "[5, 7]", LENIENT);
88-
assertThat(result, failsWithMessage(equalTo("[]: Expected 2 occurrence(s) of 5 but got 1 occurrence(s) ; []: Contains 7, but not expected")));
88+
assertThat(result, failsWithMessage(equalTo("[]: Expected 2 occurrence(s) of 5 but got 1 occurrence(s) ; []\nUnexpected: 7\n")));
8989
}
9090

9191
@Test
9292
public void reportsMissingJSONObjectWithUniqueKeyInUnorderedArray() throws JSONException {
9393
JSONCompareResult result = compareJSON("[{\"id\" : 3}]", "[{\"id\" : 5}]", LENIENT);
9494
assertThat(result, failsWithMessage(equalTo("[id=3]\nExpected: a JSON object\n but none found\n ; " +
95-
"[]: Contains object where id=5, but not expected")));
95+
"[id=5]\nUnexpected: a JSON object\n")));
9696
}
9797

9898
@Test

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.