Skip to content

Commit

Permalink
Merge pull request skyscreamer#111 from suraj1291993/master
Browse files Browse the repository at this point in the history
Fixes for issue skyscreamer#105
  • Loading branch information
carterpage authored Nov 18, 2020
2 parents abd09d8 + d46714c commit d889f22
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ protected void compareJSONArrayOfSimpleValues(String key, JSONArray expected, JS

protected void compareJSONArrayWithStrictOrder(String key, JSONArray expected, JSONArray actual, JSONCompareResult result) throws JSONException {
for (int i = 0; i < expected.length(); ++i) {
Object expectedValue = expected.get(i);
Object actualValue = actual.get(i);
Object expectedValue = JSONCompareUtil.getObjectOrNull(expected, i);
Object actualValue = JSONCompareUtil.getObjectOrNull(actual, i);
compareValues(key + "[" + i + "]", expectedValue, actualValue, result);
}
}
Expand All @@ -138,10 +138,17 @@ protected void recursivelyCompareJSONArray(String key, JSONArray expected, JSONA
JSONCompareResult result) throws JSONException {
Set<Integer> matched = new HashSet<Integer>();
for (int i = 0; i < expected.length(); ++i) {
Object expectedElement = expected.get(i);
Object expectedElement = JSONCompareUtil.getObjectOrNull(expected, i);
boolean matchFound = false;
for (int j = 0; j < actual.length(); ++j) {
Object actualElement = actual.get(j);
Object actualElement = JSONCompareUtil.getObjectOrNull(actual, j);
if (expectedElement == actualElement) {
matchFound = true;
break;
}
if ((expectedElement == null && actualElement != null) || (expectedElement != null && actualElement == null)) {
continue;
}
if (matched.contains(j) || !actualElement.getClass().equals(expectedElement.getClass())) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public void compareJSON(String prefix, JSONObject expected, JSONObject actual, J
@Override
public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result)
throws JSONException {
if (expectedValue == actualValue) {
return;
}
if ((expectedValue == null && actualValue != null) || (expectedValue != null && actualValue == null)) {
result.fail(prefix, expectedValue, actualValue);
}
if (areNumbers(expectedValue, actualValue)) {
if (areNotSameDoubles(expectedValue, actualValue)) {
result.fail(prefix, expectedValue, actualValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,23 @@ public static boolean isUsableAsUniqueKey(String candidate, JSONArray array) thr
public static List<Object> jsonArrayToList(JSONArray expected) throws JSONException {
List<Object> jsonObjects = new ArrayList<Object>(expected.length());
for (int i = 0; i < expected.length(); ++i) {
jsonObjects.add(expected.get(i));
jsonObjects.add(getObjectOrNull(expected, i));
}
return jsonObjects;
}

/**
* Returns the value present in the given index position. If null value is present, it will return null
*
* @param jsonArray the JSON array to get value from
* @param index index of object to retrieve
* @return value at the given index position
* @throws JSONException JSON parsing error
*/
public static Object getObjectOrNull(JSONArray jsonArray, int index) throws JSONException {
return jsonArray.isNull(index) ? null : jsonArray.get(index);
}

/**
* Returns whether all of the elements in the given array are simple values.
*
Expand All @@ -136,7 +148,7 @@ public static List<Object> jsonArrayToList(JSONArray expected) throws JSONExcept
*/
public static boolean allSimpleValues(JSONArray array) throws JSONException {
for (int i = 0; i < array.length(); ++i) {
if (!isSimpleValue(array.get(i))) {
if (!array.isNull(i) && !isSimpleValue(array.get(i))) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.skyscreamer.jsonassert;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;

public class JSONArrayWithNullTest {
@Test
public void testJSONArrayWithNullValue() throws JSONException {
JSONArray jsonArray1 = getJSONArray1();
JSONArray jsonArray2 = getJSONArray2();

JSONAssert.assertEquals(jsonArray1, jsonArray2, true);
JSONAssert.assertEquals(jsonArray1, jsonArray2, false);
}

@Test
public void testJSONArrayWithNullValueAndJsonObject() throws JSONException {
JSONArray jsonArray1 = getJSONArray1();
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("hey", "value");

JSONArray jsonArray2 = getJSONArray2();
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("hey", "value");

JSONAssert.assertEquals(jsonArray1, jsonArray2, true);
JSONAssert.assertEquals(jsonArray1, jsonArray2, false);
}

private JSONArray getJSONArray1() {
JSONArray jsonArray1 = new JSONArray();
jsonArray1.put(1);
jsonArray1.put(null);
jsonArray1.put(3);
jsonArray1.put(2);
return jsonArray1;
}

private JSONArray getJSONArray2() {
JSONArray jsonArray1 = new JSONArray();
jsonArray1.put(1);
jsonArray1.put(null);
jsonArray1.put(3);
jsonArray1.put(2);
return jsonArray1;
}
}

0 comments on commit d889f22

Please sign in to comment.