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

Fixed issue where CustomComparator is ignored when matching certain r… #160

Open
wants to merge 4 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public final JSONCompareResult compareJSON(JSONObject expected, JSONObject actua
return result;
}

public final JSONCompareResult compareJSONPreserveContext(String prefix, JSONObject expected, JSONObject actual) throws JSONException{
JSONCompareResult result = new JSONCompareResult();
compareJSON(prefix, expected, actual, result);
return result;
}

/**
* Compares JSONArray provided to the expected JSONArray, and returns the results of the comparison.
*
Expand All @@ -57,6 +63,12 @@ public final JSONCompareResult compareJSON(JSONArray expected, JSONArray actual)
return result;
}

public final JSONCompareResult compareJSONPreserveContext(String prefix, JSONArray expected, JSONArray actual) throws JSONException{
JSONCompareResult result = new JSONCompareResult();
compareJSONArray(prefix, expected, actual, result);
return result;
}

protected void checkJsonObjectKeysActualInExpected(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) {
Set<String> actualKeys = getKeys(actual);
for (String key : actualKeys) {
Expand Down Expand Up @@ -153,13 +165,13 @@ protected void recursivelyCompareJSONArray(String key, JSONArray expected, JSONA
continue;
}
if (expectedElement instanceof JSONObject) {
if (compareJSON((JSONObject) expectedElement, (JSONObject) actualElement).passed()) {
if (compareJSONPreserveContext(key + "[" + i + "]",(JSONObject) expectedElement, (JSONObject) actualElement).passed()) {
matched.add(j);
matchFound = true;
break;
}
} else if (expectedElement instanceof JSONArray) {
if (compareJSON((JSONArray) expectedElement, (JSONArray) actualElement).passed()) {
if (compareJSONPreserveContext(key + "[" + i + "]", (JSONArray) expectedElement, (JSONArray) actualElement).passed()) {
matched.add(j);
matchFound = true;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.skyscreamer.jsonassert.JSONCompare.compareJSON;

public class JSONCustomComparatorTest {
Expand Down Expand Up @@ -118,6 +119,15 @@ public class JSONCustomComparatorTest {
" }\n" +
"}";

String rootDeepWildcardWithArray = "{\n" +
" \"foo\": [{\n" +
" \"bar\": {\n" +
" \"num\": \"not_a_number\"" +
" }\n" +
" }]\n" +
"}";


int comparatorCallCount = 0;
ValueMatcher<Object> comparator = new ValueMatcher<Object>() {
@Override
Expand Down Expand Up @@ -166,4 +176,20 @@ public void whenRootDeepWildcardPathMatchesCallCustomMatcher() throws JSONExcept
assertTrue(result.getMessage(), result.passed());
assertEquals(4, comparatorCallCount);
}

@Test
public void whenDeepWildcardPathWithArrayMatchesCallCustomMatcher() throws JSONException {
JSONComparator jsonCmpStrict = new CustomComparator(JSONCompareMode.STRICT, new Customization("foo[*].bar.num", new RegularExpressionValueMatcher<Object>("\\d")));
JSONComparator jsonCmpNonExtensible = new CustomComparator(JSONCompareMode.NON_EXTENSIBLE, new Customization("foo[*].bar.num", new RegularExpressionValueMatcher<Object>("\\d")));
JSONComparator jsonCmpNonLenient = new CustomComparator(JSONCompareMode.LENIENT, new Customization("foo[*].bar.num", new RegularExpressionValueMatcher<Object>("\\d")));

JSONCompareResult resultStrict = compareJSON(rootDeepWildcardWithArray, rootDeepWildcardWithArray, jsonCmpStrict);
assertFalse(resultStrict.getMessage(), resultStrict.passed());

JSONCompareResult resultNonExtensible = compareJSON(rootDeepWildcardWithArray, rootDeepWildcardWithArray, jsonCmpNonExtensible);
assertFalse(resultNonExtensible.getMessage(), resultNonExtensible.passed());

JSONCompareResult resultLenient = compareJSON(rootDeepWildcardWithArray, rootDeepWildcardWithArray, jsonCmpNonLenient);
assertFalse(resultLenient.getMessage(), resultLenient.passed());
}
}