Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More verbose diff view on assertion error #91 #150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/main/java/org/skyscreamer/jsonassert/JSONCompare.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,38 @@ else if (expected instanceof JSONObject) {
}
}

/**
* Compares JSON string provided to the expected JSON string using provided comparator, and returns the results of
* the comparison.
* @param expectedStr Expected JSON string
* @param actualStr JSON string to compare
* @param comparator Comparator to use
* @param verbose Showing the verbose diff if set to true
* @return result of the comparison
* @throws JSONException JSON parsing error
* @throws IllegalArgumentException when type of expectedStr doesn't match the type of actualStr
*/
public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONComparator comparator, Boolean verbose)
throws JSONException {
Object expected = JSONParser.parseJSON(expectedStr);
Object actual = JSONParser.parseJSON(actualStr);
if ((expected instanceof JSONObject) && (actual instanceof JSONObject)) {
return compareJSON((JSONObject) expected, (JSONObject) actual, comparator, verbose);
}
else if ((expected instanceof JSONArray) && (actual instanceof JSONArray)) {
return compareJSON((JSONArray)expected, (JSONArray)actual, comparator);
}
else if (expected instanceof JSONString && actual instanceof JSONString) {
return compareJson((JSONString) expected, (JSONString) actual);
}
else if (expected instanceof JSONObject) {
return new JSONCompareResult().fail("", expected, actual);
}
else {
return new JSONCompareResult().fail("", expected, actual);
}
}

/**
* Compares JSON object provided to the expected JSON object using provided comparator, and returns the results of
* the comparison.
Expand All @@ -79,6 +111,21 @@ public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actu
return comparator.compareJSON(expected, actual);
}

/**
* Compares JSON object provided to the expected JSON object using provided comparator, and returns the results of
* the comparison.
* @param expected expected json object
* @param actual actual json object
* @param comparator comparator to use
* @param verbose showing the verbose diff if set to true
* @return result of the comparison
* @throws JSONException JSON parsing error
*/
public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actual, JSONComparator comparator, boolean verbose)
throws JSONException {
return comparator.compareJSON(expected, actual, verbose);
}

/**
* Compares JSON object provided to the expected JSON object using provided comparator, and returns the results of
* the comparison.
Expand Down Expand Up @@ -125,6 +172,21 @@ public static JSONCompareResult compareJSON(String expectedStr, String actualStr
return compareJSON(expectedStr, actualStr, getComparatorForMode(mode));
}

/**
* Compares JSON string provided to the expected JSON string, and returns the results of the comparison.
*
* @param expectedStr Expected JSON string
* @param actualStr JSON string to compare
* @param mode Defines comparison behavior
* @param verbose Showing the verbose diff if set to true
* @return result of the comparison
* @throws JSONException JSON parsing error
*/
public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONCompareMode mode, Boolean verbose)
throws JSONException {
return compareJSON(expectedStr, actualStr, getComparatorForMode(mode), verbose);
}

/**
* Compares JSONObject provided to the expected JSONObject, and returns the results of the comparison.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ public final JSONCompareResult compareJSON(JSONObject expected, JSONObject actua
return result;
}

/**
* Compares JSONObject provided to the expected JSONObject, and returns the results of the comparison.
*
* @param expected Expected JSONObject
* @param actual JSONObject to compare
* @param verbose Showing verbose diff if set to true
* @throws JSONException JSON parsing error
*/
@Override
public final JSONCompareResult compareJSON(JSONObject expected, JSONObject actual, boolean verbose) throws JSONException {
JSONCompareResult result = new JSONCompareResult();
compareJSON("", expected, actual, result);
if (verbose) {
result.fail("Verbose Diff:", expected.toString(), actual.toString());
}
return result;
}

/**
* Compares JSONArray provided to the expected JSONArray, and returns the results of the comparison.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ public interface JSONComparator {
*/
JSONCompareResult compareJSON(JSONObject expected, JSONObject actual) throws JSONException;

/**
* Compares two {@link JSONObject}s and returns the result of the comparison in a {@link JSONCompareResult} object.
*
* @param expected the expected JSON object
* @param actual the actual JSON object
* @param verbose showing the verbose diff if set to true
* @return the result of the comparison
* @throws JSONException JSON parsing error
*/
JSONCompareResult compareJSON(JSONObject expected, JSONObject actual, boolean verbose) throws JSONException;


/**
* Compares two {@link JSONArray}s and returns the result of the comparison in a {@link JSONCompareResult} object.
*
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/org/skyscreamer/jsonassert/JSONCompareTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.skyscreamer.jsonassert.JSONCompare.compareJSON;
import static org.skyscreamer.jsonassert.JSONCompareMode.LENIENT;
import static org.skyscreamer.jsonassert.JSONCompareMode.NON_EXTENSIBLE;
import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
Expand All @@ -32,6 +33,42 @@
* Unit tests for {@code JSONCompare}.
*/
public class JSONCompareTest {
@Test
public void reportsUnmatchedJSONObjectWithVerboseMessage() throws JSONException {
String json1 = "{\n" +
" \"id\": \"101\",\n" +
" \"key\": {\n" +
" \"a\": \"value\",\n" +
" \"b\": \"xyz\",\n" +
" \"c\": 201\n" +
" }\n" +
"}";

String json2 = "{\n" +
" \"id\": \"102\",\n" +
" \"key\": {\n" +
" \"a\": \"value\",\n" +
" \"d\": [1, 2]\n" +
" }\n" +
"}";

JSONCompareResult result = compareJSON(json1, json2, STRICT, true);
assertThat(result, failsWithMessage(equalTo("id\n" +
"Expected: 101\n" +
" got: 102\n" +
" ; key\n" +
"Expected: b\n" +
" but none found\n" +
" ; key\n" +
"Expected: c\n" +
" but none found\n" +
" ; key\n" +
"Unexpected: d\n" +
" ; Verbose Diff:\n" +
"Expected: {\"id\":\"101\",\"key\":{\"a\":\"value\",\"b\":\"xyz\",\"c\":201}}\n" +
" got: {\"id\":\"102\",\"key\":{\"a\":\"value\",\"d\":[1,2]}}\n")));
}

@Test
public void succeedsWithEmptyArrays() throws JSONException {
assertTrue(compareJSON("[]", "[]", LENIENT).passed());
Expand Down