Skip to content

Commit 797c5c1

Browse files
committed
Merge pull request skyscreamer#50 from riccorazza/patch-1
Update JSONCompareResult.java Still needs some more work on unit tests, but I'll merge then add those in.
2 parents 9a66989 + e3a94eb commit 797c5c1

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class JSONCompareResult {
1717
private Object _expected;
1818
private Object _actual;
1919
private final List<FieldComparisonFailure> _fieldFailures = new ArrayList<FieldComparisonFailure>();
20+
private final List<FieldComparisonFailure> _fieldMissing = new ArrayList<FieldComparisonFailure>();
21+
private final List<FieldComparisonFailure> _fieldUnexpected = new ArrayList<FieldComparisonFailure>();
2022

2123
/**
2224
* Default constructor.
@@ -60,6 +62,20 @@ public String getMessage() {
6062
public List<FieldComparisonFailure> getFieldFailures() {
6163
return Collections.unmodifiableList(_fieldFailures);
6264
}
65+
66+
/**
67+
* Get the list of missed on field comparisons
68+
*/
69+
public List<FieldComparisonFailure> getFieldMissing() {
70+
return Collections.unmodifiableList(_fieldMissing);
71+
}
72+
73+
/**
74+
* Get the list of failures on field comparisons
75+
*/
76+
public List<FieldComparisonFailure> getFieldUnexpected() {
77+
return Collections.unmodifiableList(_fieldUnexpected);
78+
}
6379

6480
/**
6581
* Actual field value
@@ -69,6 +85,7 @@ public List<FieldComparisonFailure> getFieldFailures() {
6985
* particular field
7086
* @deprecated Superseded by {@link #getFieldFailures()}
7187
*/
88+
@Deprecated
7289
public Object getActual() {
7390
return _actual;
7491
}
@@ -81,6 +98,7 @@ public Object getActual() {
8198
* particular field
8299
* @deprecated Superseded by {@link #getFieldFailures()}
83100
*/
101+
@Deprecated
84102
public Object getExpected() {
85103
return _expected;
86104
}
@@ -91,6 +109,20 @@ public Object getExpected() {
91109
public boolean isFailureOnField() {
92110
return !_fieldFailures.isEmpty();
93111
}
112+
113+
/**
114+
* Check if comparison failed with missing on any particular fields
115+
*/
116+
public boolean isMissingOnField() {
117+
return !_fieldMissing.isEmpty();
118+
}
119+
120+
/**
121+
* Check if comparison failed with unexpected on any particular fields
122+
*/
123+
public boolean isUnexpectedOnField() {
124+
return !_fieldUnexpected.isEmpty();
125+
}
94126

95127
/**
96128
* Dot-separated path the the field that failed comparison
@@ -99,6 +131,7 @@ public boolean isFailureOnField() {
99131
* not fail on a particular field
100132
* @deprecated Superseded by {@link #getFieldFailures()}
101133
*/
134+
@Deprecated
102135
public String getField() {
103136
return _field;
104137
}
@@ -147,6 +180,7 @@ private String formatFailureMessage(String field, Object expected, Object actual
147180
}
148181

149182
public JSONCompareResult missing(String field, Object expected) {
183+
_fieldMissing.add(new FieldComparisonFailure(field, expected, null));
150184
fail(formatMissing(field, expected));
151185
return this;
152186
}
@@ -159,6 +193,7 @@ private String formatMissing(String field, Object expected) {
159193
}
160194

161195
public JSONCompareResult unexpected(String field, Object value) {
196+
_fieldUnexpected.add(new FieldComparisonFailure(field, null, value));
162197
fail(formatUnexpected(field, value));
163198
return this;
164199
}

src/test/java/org/skyscreamer/jsonassert/JSONCompareTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.skyscreamer.jsonassert;
22

33
import static org.hamcrest.core.IsEqual.equalTo;
4+
import static org.junit.Assert.assertEquals;
45
import static org.junit.Assert.assertThat;
56
import static org.junit.Assert.assertTrue;
67
import static org.skyscreamer.jsonassert.JSONCompare.compareJSON;
@@ -32,18 +33,22 @@ public void reportsArraysOfUnequalLength() throws JSONException {
3233
public void reportsArrayMissingExpectedElement() throws JSONException {
3334
JSONCompareResult result = compareJSON("[4]", "[7]", LENIENT);
3435
assertThat(result, failsWithMessage(equalTo("[]\nExpected: 4\n but none found\n ; []\nUnexpected: 7\n")));
36+
assertEquals(result.getFieldMissing().size(), 1);
37+
assertEquals(result.getFieldUnexpected().size(), 1);
3538
}
3639

3740
@Test
3841
public void reportsMismatchedFieldValues() throws JSONException {
3942
JSONCompareResult result = compareJSON("{\"id\": 3}", "{\"id\": 5}", LENIENT);
4043
assertThat(result, failsWithMessage(equalTo("id\nExpected: 3\n got: 5\n")));
44+
assertThat(result, failsWithMessage(equalTo("id\nExpected: 3\n got: 5\n")));
4145
}
4246

4347
@Test
4448
public void reportsMissingField() throws JSONException {
4549
JSONCompareResult result = compareJSON("{\"obj\": {\"id\": 3}}", "{\"obj\": {}}", LENIENT);
4650
assertThat(result, failsWithMessage(equalTo("obj\nExpected: id\n but none found\n")));
51+
assertEquals(result.getFieldMissing().size(), 1);
4752
}
4853

4954
@Test
@@ -74,6 +79,7 @@ public void reportsUnexpectedNonNull() throws JSONException {
7479
public void reportsUnexpectedFieldInNonExtensibleMode() throws JSONException {
7580
JSONCompareResult result = compareJSON("{\"obj\": {}}", "{\"obj\": {\"id\": 3}}", NON_EXTENSIBLE);
7681
assertThat(result, failsWithMessage(equalTo("obj\nUnexpected: id\n")));
82+
assertEquals(result.getFieldUnexpected().size(), 1);
7783
}
7884

7985
@Test
@@ -86,13 +92,16 @@ public void reportsMismatchedTypes() throws JSONException {
8692
public void reportsWrongSimpleValueCountInUnorderedArray() throws JSONException {
8793
JSONCompareResult result = compareJSON("[5, 5]", "[5, 7]", LENIENT);
8894
assertThat(result, failsWithMessage(equalTo("[]: Expected 2 occurrence(s) of 5 but got 1 occurrence(s) ; []\nUnexpected: 7\n")));
95+
assertEquals(result.getFieldUnexpected().size(), 1);
8996
}
9097

9198
@Test
9299
public void reportsMissingJSONObjectWithUniqueKeyInUnorderedArray() throws JSONException {
93100
JSONCompareResult result = compareJSON("[{\"id\" : 3}]", "[{\"id\" : 5}]", LENIENT);
94101
assertThat(result, failsWithMessage(equalTo("[id=3]\nExpected: a JSON object\n but none found\n ; " +
95102
"[id=5]\nUnexpected: a JSON object\n")));
103+
assertEquals(result.getFieldMissing().size(), 1);
104+
assertEquals(result.getFieldUnexpected().size(), 1);
96105
}
97106

98107
@Test

0 commit comments

Comments
 (0)