Skip to content

Implements partial ignore #15 #77

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/main/java/org/skyscreamer/jsonassert/JSONCompare.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ private static JSONComparator getComparatorForMode(JSONCompareMode mode) {
return new DefaultComparator(mode);
}

private static JSONComparator getComparatorForModeWithWildcard(JSONCompareMode mode, String wildcard) {
return new DefaultComparator(mode, wildcard);
}

/**
* Compares JSON string provided to the expected JSON string using provided comparator, and returns the results of
* the comparison.
Expand Down Expand Up @@ -96,6 +100,25 @@ public static JSONCompareResult compareJson(final JSONString expected, final JSO
return result;
}

/**
* Compares {@link JSONString} provided to the expected {@code JSONString}, checking that the
* {@link org.json.JSONString#toJSONString()} are equal.
*
* @param expected Expected {@code JSONstring}
* @param actual {@code JSONstring} to compare
* @param wildcard wildcard used in the expceted json string
*/
public static JSONCompareResult compareJson(final JSONString expected, final JSONString actual, String wildcard) {
final JSONCompareResult result = new JSONCompareResult();
final String expectedJson = expected.toJSONString();
final String actualJson = actual.toJSONString();
if (wildcard == null || !wildcard.equals(expectedJson)
|| !expectedJson.equals(actualJson)) {
result.fail("");
}
return result;
}

/**
* Compares JSON string provided to the expected JSON string, and returns the results of the comparison.
*
Expand All @@ -109,6 +132,20 @@ 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 wildcard wildcard used in expected string
* @throws JSONException
*/
public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONCompareMode mode, String wildcard)
throws JSONException {
return compareJSON(expectedStr, actualStr, getComparatorForModeWithWildcard(mode, wildcard));
}

/**
* Compares JSONObject provided to the expected JSONObject, and returns the results of the comparison.
*
Expand All @@ -122,6 +159,19 @@ public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actu
return compareJSON(expected, actual, getComparatorForMode(mode));
}

/**
* Compares JSONObject provided to the expected JSONObject, and returns the results of the comparison.
*
* @param expected Expected JSONObject
* @param actual JSONObject to compare
* @param mode Defines comparison behavior
* @param wildcard wildcard used in the expected json object
* @throws JSONException
*/
public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actual, JSONCompareMode mode, String wildcard)
throws JSONException {
return compareJSON(expected, actual, getComparatorForModeWithWildcard(mode, wildcard));
}

/**
* Compares JSONArray provided to the expected JSONArray, and returns the results of the comparison.
Expand All @@ -136,4 +186,18 @@ public static JSONCompareResult compareJSON(JSONArray expected, JSONArray actual
return compareJSON(expected, actual, getComparatorForMode(mode));
}

/**
* Compares JSONArray provided to the expected JSONArray, and returns the results of the comparison.
*
* @param expected Expected JSONArray
* @param actual JSONArray to compare
* @param mode Defines comparison behavior
* @param wildcard wildcard used in expected json array
* @throws JSONException
*/
public static JSONCompareResult compareJSON(JSONArray expected, JSONArray actual, JSONCompareMode mode, String wildcard)
throws JSONException {
return compareJSON(expected, actual, getComparatorForModeWithWildcard(mode, wildcard));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@
public class DefaultComparator extends AbstractComparator {

JSONCompareMode mode;
String wildcard;

public DefaultComparator(JSONCompareMode mode) {
this.mode = mode;
}

public DefaultComparator(JSONCompareMode mode, String wildcard) {
this.mode = mode;
this.wildcard = wildcard;
}

@Override
public void compareJSON(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result)
throws JSONException {
Expand All @@ -36,20 +42,22 @@ 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 instanceof Number && actualValue instanceof Number) {
if (((Number)expectedValue).doubleValue() != ((Number)actualValue).doubleValue()) {
if (wildcard == null || !wildcard.equals(expectedValue)) {
if (expectedValue instanceof Number && actualValue instanceof Number) {
if (((Number) expectedValue).doubleValue() != ((Number) actualValue).doubleValue()) {
result.fail(prefix, expectedValue, actualValue);
}
} else if (expectedValue.getClass().isAssignableFrom(actualValue.getClass())) {
if (expectedValue instanceof JSONArray) {
compareJSONArray(prefix, (JSONArray) expectedValue, (JSONArray) actualValue, result);
} else if (expectedValue instanceof JSONObject) {
compareJSON(prefix, (JSONObject) expectedValue, (JSONObject) actualValue, result);
} else if (!expectedValue.equals(actualValue)) {
result.fail(prefix, expectedValue, actualValue);
}
} else {
result.fail(prefix, expectedValue, actualValue);
}
} else if (expectedValue.getClass().isAssignableFrom(actualValue.getClass())) {
if (expectedValue instanceof JSONArray) {
compareJSONArray(prefix, (JSONArray) expectedValue, (JSONArray) actualValue, result);
} else if (expectedValue instanceof JSONObject) {
compareJSON(prefix, (JSONObject) expectedValue, (JSONObject) actualValue, result);
} else if (!expectedValue.equals(actualValue)) {
result.fail(prefix, expectedValue, actualValue);
}
} else {
result.fail(prefix, expectedValue, actualValue);
}
}

Expand Down
59 changes: 56 additions & 3 deletions src/test/java/org/skyscreamer/jsonassert/JSONAssertTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package org.skyscreamer.jsonassert;

import java.util.Arrays;

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

import static org.skyscreamer.jsonassert.JSONCompareMode.*;
import java.util.Arrays;

import static org.skyscreamer.jsonassert.JSONCompareMode.LENIENT;
import static org.skyscreamer.jsonassert.JSONCompareMode.NON_EXTENSIBLE;
import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT;
import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT_ORDER;

/**
* Unit tests for {@link JSONAssert}
Expand Down Expand Up @@ -359,6 +362,40 @@ public void testAssertNotEqualsJSONArray() throws JSONException {
JSONAssert.assertNotEquals(new JSONArray(Arrays.asList(1, 3, 2)), actual, true);
}

@Test()
public void testWildCard() throws JSONException {
testPass("{id:**,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",
"{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",
STRICT, "**"); // Exact to exact (strict)
testPass("{id:\"**\",name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",
"{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",
STRICT, "**"); // Exact to exact (strict)
testFail("{id:1,name:**,friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",
"{id:1,name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[]}",
STRICT, "**"); // Out-of-order fails (strict)
testFail("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:**},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",
"{id:1,name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[]}",
STRICT_ORDER, "**"); // Out-of-order fails (strict order)
testPass("{id:1,name:\"Joe\",friends:**,pets:[]}",
"{id:1,name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[]}",
LENIENT, "**"); // Out-of-order ok
testPass("{id:1,name:\"Joe\",friends:\"**\",pets:[]}",
"{id:1,name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[]}",
LENIENT, "**"); // Out-of-order ok
testPass("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:**}],pets:[]}",
"{id:1,name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[]}",
NON_EXTENSIBLE, "**"); // Out-of-order ok
testFail("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:++}",
"{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"cat\",\"fish\"]}],pets:[]}",
STRICT, "++"); // Mismatch (strict)
testPass("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:++}],pets:[]}",
"{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"cat\",\"fish\"]}],pets:[]}",
STRICT, "++"); // Mismatch (strict)
testPass("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:\"++\"}],pets:[]}",
"{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"cat\",\"fish\"]}],pets:[]}",
STRICT, "++"); // Mismatch (strict)
}

private void testPass(String expected, String actual, JSONCompareMode compareMode)
throws JSONException
{
Expand All @@ -374,4 +411,20 @@ private void testFail(String expected, String actual, JSONCompareMode compareMod
JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode);
Assert.assertTrue(message, result.failed());
}

private void testPass(String expected, String actual, JSONCompareMode compareMode, String wildcard)
throws JSONException
{
String message = expected + " == " + actual + " (" + compareMode + ")";
JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode, wildcard);
Assert.assertTrue(message + "\n " + result.getMessage(), result.passed());
}

private void testFail(String expected, String actual, JSONCompareMode compareMode, String wildcard)
throws JSONException
{
String message = expected + " != " + actual + " (" + compareMode + ")";
JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode, wildcard);
Assert.assertTrue(message, result.failed());
}
}