Skip to content

Commit 7285c83

Browse files
committed
Merge pull request skyscreamer#20 from carterpage/add-compare-mode
Add JSONCompareMode to asserts (issue skyscreamer#19)
2 parents 99b9a78 + 8b2593c commit 7285c83

2 files changed

Lines changed: 172 additions & 74 deletions

File tree

src/main/java/org/skyscreamer/jsonassert/JSONAssert.java

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,23 @@ private JSONAssert() {}
4949
*/
5050
public static void assertEquals(String expectedStr, JSONObject actual, boolean strict)
5151
throws JSONException {
52+
assertEquals(expectedStr, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
53+
}
54+
55+
/**
56+
* Asserts that the JSONObject provided matches the expected string. If it isn't it throws an
57+
* {@link AssertionError}.
58+
*
59+
* @param expectedStr Expected JSON string
60+
* @param actual JSONObject to compare
61+
* @param compareMode Specifies which comparison mode to use
62+
* @throws JSONException
63+
*/
64+
public static void assertEquals(String expectedStr, JSONObject actual, JSONCompareMode compareMode)
65+
throws JSONException {
5266
Object expected = JSONParser.parseJSON(expectedStr);
5367
if (expected instanceof JSONObject) {
54-
assertEquals((JSONObject)expected, actual, strict);
68+
assertEquals((JSONObject)expected, actual, compareMode);
5569
}
5670
else {
5771
throw new AssertionError("Expecting a JSON array, but passing in a JSON object");
@@ -69,9 +83,23 @@ public static void assertEquals(String expectedStr, JSONObject actual, boolean s
6983
*/
7084
public static void assertEquals(String expectedStr, JSONArray actual, boolean strict)
7185
throws JSONException {
86+
assertEquals(expectedStr, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
87+
}
88+
89+
/**
90+
* Asserts that the JSONArray provided matches the expected string. If it isn't it throws an
91+
* {@link AssertionError}.
92+
*
93+
* @param expectedStr Expected JSON string
94+
* @param actual JSONArray to compare
95+
* @param compareMode Specifies which comparison mode to use
96+
* @throws JSONException
97+
*/
98+
public static void assertEquals(String expectedStr, JSONArray actual, JSONCompareMode compareMode)
99+
throws JSONException {
72100
Object expected = JSONParser.parseJSON(expectedStr);
73101
if (expected instanceof JSONArray) {
74-
assertEquals((JSONArray)expected, actual, strict);
102+
assertEquals((JSONArray)expected, actual, compareMode);
75103
}
76104
else {
77105
throw new AssertionError("Expecting a JSON object, but passing in a JSON array");
@@ -89,7 +117,21 @@ public static void assertEquals(String expectedStr, JSONArray actual, boolean st
89117
*/
90118
public static void assertEquals(String expectedStr, String actualStr, boolean strict)
91119
throws JSONException {
92-
JSONCompareResult result = JSONCompare.compareJSON(expectedStr, actualStr, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
120+
assertEquals(expectedStr, actualStr, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
121+
}
122+
123+
/**
124+
* Asserts that the JSONArray provided matches the expected string. If it isn't it throws an
125+
* {@link AssertionError}.
126+
*
127+
* @param expectedStr Expected JSON string
128+
* @param actualStr String to compare
129+
* @param compareMode Specifies which comparison mode to use
130+
* @throws JSONException
131+
*/
132+
public static void assertEquals(String expectedStr, String actualStr, JSONCompareMode compareMode)
133+
throws JSONException {
134+
JSONCompareResult result = JSONCompare.compareJSON(expectedStr, actualStr, compareMode);
93135
if (result.failed()) {
94136
throw new AssertionError(result.getMessage());
95137
}
@@ -123,7 +165,22 @@ public static void assertEquals(String expectedStr, String actualStr, JSONCompar
123165
*/
124166
public static void assertEquals(JSONObject expected, JSONObject actual, boolean strict)
125167
throws JSONException {
126-
JSONCompareResult result = JSONCompare.compareJSON(expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
168+
assertEquals(expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
169+
}
170+
171+
/**
172+
* Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws an
173+
* {@link AssertionError}.
174+
*
175+
* @param expected Expected JSONObject
176+
* @param actual JSONObject to compare
177+
* @param compareMode Specifies which comparison mode to use
178+
* @throws JSONException
179+
*/
180+
public static void assertEquals(JSONObject expected, JSONObject actual, JSONCompareMode compareMode)
181+
throws JSONException
182+
{
183+
JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode);
127184
if (result.failed()) {
128185
throw new AssertionError(result.getMessage());
129186
}
@@ -140,7 +197,21 @@ public static void assertEquals(JSONObject expected, JSONObject actual, boolean
140197
*/
141198
public static void assertEquals(JSONArray expected, JSONArray actual, boolean strict)
142199
throws JSONException {
143-
JSONCompareResult result = JSONCompare.compareJSON(expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
200+
assertEquals(expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT);
201+
}
202+
203+
/**
204+
* Asserts that the JSONArray provided matches the expected JSONArray. If it isn't it throws an
205+
* {@link AssertionError}.
206+
*
207+
* @param expected Expected JSONArray
208+
* @param actual JSONArray to compare
209+
* @param compareMode Specifies which comparison mode to use
210+
* @throws JSONException
211+
*/
212+
public static void assertEquals(JSONArray expected, JSONArray actual, JSONCompareMode compareMode)
213+
throws JSONException {
214+
JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode);
144215
if (result.failed()) {
145216
throw new AssertionError(result.getMessage());
146217
}

0 commit comments

Comments
 (0)