Skip to content

Commit f69f820

Browse files
Merge pull request #177 from fslev/feature_request_diff_better_style
Feature request diff better style. Fixes #175
2 parents ff9810a + 2cb3797 commit f69f820

18 files changed

+297
-306
lines changed

Changelog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changelog
22

3-
## 6.19-SNAPSHOT
3+
## 7.0 (2025-03-28)
4+
- #### Changed
5+
- Changed style for JSON diffs
46

57
## 6.18 (2024-09-26)
68
- #### Changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<groupId>com.github.fslev</groupId>
2424
<artifactId>json-compare</artifactId>
25-
<version>6.19-SNAPSHOT</version>
25+
<version>7.0-SNAPSHOT</version>
2626

2727
<dependencyManagement>
2828
<dependencies>

src/main/java/io/json/compare/JSONCompare.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.io.IOException;
99
import java.util.List;
1010
import java.util.Set;
11+
import java.util.stream.Collectors;
1112

1213

1314
/**
@@ -72,7 +73,7 @@ public static void assertMatches(Object expected, Object actual, JsonComparator
7273
String defaultMessage = String.format("FOUND %s DIFFERENCE(S):%s%s%s",
7374
diffs.size(), System.lineSeparator(), diffs.stream().map(diff ->
7475
System.lineSeparator() + System.lineSeparator() + "_________________________DIFF__________________________" +
75-
System.lineSeparator() + diff).reduce(String::concat).get(), System.lineSeparator());
76+
System.lineSeparator() + "$" + diff).reduce(String::concat).get(), System.lineSeparator());
7677
if (comparator == null || comparator.getClass().equals(DefaultJsonComparator.class)) {
7778
defaultMessage += System.lineSeparator() + System.lineSeparator() + ASSERTION_ERROR_HINT_MESSAGE + System.lineSeparator();
7879
}
@@ -110,8 +111,9 @@ public static List<String> diffs(Object expected, Object actual, JsonComparator
110111
public static List<String> diffs(Object expected, Object actual, JsonComparator comparator, Set<CompareMode> compareModes) {
111112
JsonNode expectedJson = toJson(expected);
112113
JsonNode actualJson = toJson(actual);
113-
return new JsonMatcher(expectedJson, actualJson,
114+
List<String> diffs = new JsonMatcher(expectedJson, actualJson,
114115
comparator == null ? new DefaultJsonComparator(compareModes) : comparator, compareModes).match();
116+
return diffs.stream().map(diff -> "$" + diff).collect(Collectors.toList());
115117
}
116118

117119
public static String prettyPrint(JsonNode jsonNode) {

src/main/java/io/json/compare/matcher/JsonArrayMatcher.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public List<String> match() {
3434
}
3535
}
3636
if (compareModes.contains(CompareMode.JSON_ARRAY_NON_EXTENSIBLE) && expected.size() - getDoNotMatchUseCases(expected) < actual.size()) {
37-
diffs.add("Actual JSON ARRAY has extra elements");
37+
diffs.add(" -> Actual JSON ARRAY has extra elements");
3838
}
3939
return diffs;
4040
}
@@ -63,11 +63,9 @@ private List<String> matchWithJsonArray(int expPosition, JsonNode expElement, Us
6363
return Collections.emptyList();
6464
} else {
6565
if (compareModes.contains(CompareMode.JSON_ARRAY_STRICT_ORDER)) {
66-
diffs.add(String.format("JSON ARRAY elements differ at position %s:" +
67-
System.lineSeparator() + "%s" + System.lineSeparator() +
68-
"________diffs________" + System.lineSeparator() + "%s", expPosition + 1,
69-
MessageUtil.cropL(JSONCompare.prettyPrint(expElement)), String.join(
70-
System.lineSeparator() + "_____________________" + System.lineSeparator(), elementDiffs)));
66+
elementDiffs.forEach(elementDiff ->
67+
diffs.add(String.format("[%s]%s", expPosition, elementDiff))
68+
);
7169
return diffs;
7270
}
7371
}
@@ -80,27 +78,26 @@ private List<String> matchWithJsonArray(int expPosition, JsonNode expElement, Us
8078
if (areOfSameType(expElement, actElement)) {
8179
elementDiffs = new JsonMatcher(expElement, actElement, comparator, compareModes).match();
8280
if (!elementDiffs.isEmpty()) {
83-
diffs.add("Expected element from position " + (expPosition + 1)
84-
+ " was FOUND:" + System.lineSeparator() + MessageUtil.cropL(JSONCompare.prettyPrint(expElement)));
81+
diffs.add("[" + expPosition + "]"
82+
+ " was found:" + System.lineSeparator() + MessageUtil.cropL(JSONCompare.prettyPrint(expElement)));
8583
return diffs;
8684
}
8785
}
8886
break;
8987
case DO_NOT_MATCH_ANY:
9088
if (expected.size() - getDoNotMatchUseCases(expected) < actual.size()) {
91-
diffs.add(String.format("Expected condition %s from position %s was not met." +
92-
" Actual JSON ARRAY has extra elements",
93-
expElement, expPosition + 1));
89+
diffs.add(String.format("[%s] -> Expected condition %s was not met." +
90+
" Actual JSON ARRAY has extra elements", expPosition, expElement));
9491
}
9592
return diffs;
9693
}
9794
}
9895
if (useCase == UseCase.MATCH) {
99-
diffs.add(System.lineSeparator() + "Expected element from position " + (expPosition + 1) + " was NOT FOUND:" + System.lineSeparator()
96+
diffs.add("[" + expPosition + "] was not found:" + System.lineSeparator()
10097
+ MessageUtil.cropL(JSONCompare.prettyPrint(expElement)));
10198
} else if (useCase == UseCase.MATCH_ANY) {
102-
diffs.add(String.format("Expected condition %s from position %s was not met." +
103-
" Actual JSON ARRAY has no extra elements", expElement, expPosition + 1));
99+
diffs.add(String.format("[%s] -> Expected condition %s was not met." +
100+
" Actual JSON ARRAY has no extra elements", expPosition, expElement));
104101
}
105102
return diffs;
106103
}

src/main/java/io/json/compare/matcher/JsonMatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public List<String> match() {
2929
return Collections.emptyList();
3030
} else {
3131
List<String> diffs = new ArrayList<>();
32-
diffs.add("Different JSON types: expected " + expected.getClass().getSimpleName() + " but got " + actual.getClass().getSimpleName());
32+
diffs.add(" -> Different JSON types: expected " + expected.getClass().getSimpleName() + " but got " + actual.getClass().getSimpleName());
3333
return diffs;
3434
}
3535
}

src/main/java/io/json/compare/matcher/JsonObjectMatcher.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,41 +43,41 @@ public List<String> match() {
4343
case MATCH:
4444
if (!jsonPathExpression.isPresent()) {
4545
if (candidateEntries.isEmpty()) {
46-
diffs.add(String.format("Field '%s' was NOT FOUND", expectedField));
46+
diffs.add(String.format(".%s was not found", expectedField));
4747
} else {
4848
diffs.addAll(matchWithCandidates(expectedSanitizedField, expectedValue, candidateEntries));
4949
}
5050
} else {
5151
try {
5252
diffs.addAll(new JsonPathMatcher(jsonPathExpression.get(), expectedValue, actual, comparator, compareModes).match());
5353
} catch (PathNotFoundException e) {
54-
diffs.add(String.format("Json path '%s' -> %s", jsonPathExpression.get(), e.getMessage()));
54+
diffs.add(String.format("." + JSON_PATH_EXP_PREFIX + "%s" + JSON_PATH_EXP_SUFFIX + " -> Json path -> %s", jsonPathExpression.get(), e.getMessage()));
5555
}
5656
}
5757
break;
5858
case DO_NOT_MATCH_ANY:
5959
if (expected.size() - getDoNotMatchUseCases(expected) < actual.size()) {
60-
diffs.add(String.format("Expected condition '%s' was not met. Actual JSON OBJECT has extra fields", expectedField));
60+
diffs.add(String.format(".\"%s\" condition was not met. Actual JSON OBJECT has extra fields", expectedField));
6161
}
6262
break;
6363
case DO_NOT_MATCH:
6464
if (!jsonPathExpression.isPresent()) {
6565
if (!candidateEntries.isEmpty()) {
66-
diffs.add(String.format("Field '%s' was FOUND", expectedField));
66+
diffs.add(String.format(".\"%s\" was found", expectedField));
6767
}
6868
} else {
6969
try {
7070
new JsonPathMatcher(jsonPathExpression.get(), expectedValue, actual, comparator, compareModes).match();
7171
} catch (PathNotFoundException e) {
7272
break;
7373
}
74-
diffs.add(String.format("Json path '%s' was FOUND", expectedField));
74+
diffs.add(String.format("." + JSON_PATH_EXP_PREFIX + "%s" + JSON_PATH_EXP_SUFFIX + " -> Json path was found", expectedField));
7575
}
7676
break;
7777
}
7878
}
7979
if (compareModes.contains(CompareMode.JSON_OBJECT_NON_EXTENSIBLE) && expected.size() - getDoNotMatchUseCases(expected) < actual.size()) {
80-
diffs.add("Actual JSON OBJECT has extra fields");
80+
diffs.add(" -> Actual JSON OBJECT has extra fields");
8181
}
8282
return diffs;
8383
}
@@ -101,7 +101,7 @@ private List<String> matchWithCandidates(String expectedField, JsonNode expected
101101
matchedFieldNames.add(candidateField);
102102
return Collections.emptyList();
103103
} else {
104-
candidateDiffs.forEach(diff -> diffs.add(String.format("%s -> %s", expectedField, diff)));
104+
candidateDiffs.forEach(diff -> diffs.add(String.format(".%s%s", expectedField, diff)));
105105
}
106106
}
107107
return diffs;

src/main/java/io/json/compare/matcher/JsonPathMatcher.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ public List<String> match() {
3939
List<String> diffs = new ArrayList<>();
4040
JsonNode result = MAPPER.convertValue(PARSE_CONTEXT.parse(actual).read(jsonPath), JsonNode.class);
4141
List<String> jsonPathDiffs = new JsonMatcher(expected, result, comparator, compareModes).match();
42-
jsonPathDiffs.forEach(diff -> diffs.add(String.format("Json path '%s' -> Expected json path result:" +
43-
System.lineSeparator() + "%s" + System.lineSeparator() + "But got:" +
44-
System.lineSeparator() + "%s" + System.lineSeparator() + "________diffs________" + System.lineSeparator() + "%s",
45-
jsonPath, expected, result, diff)));
42+
jsonPathDiffs.forEach(diff -> diffs.add(String.format("." + JSON_PATH_EXP_PREFIX + "%s" + JSON_PATH_EXP_SUFFIX + "%s" + System.lineSeparator() + "Expected json path result:" +
43+
System.lineSeparator() + "%s" + System.lineSeparator() + "But got:" + System.lineSeparator() + "%s" + System.lineSeparator(),
44+
jsonPath, diff, expected, result)));
4645
return diffs;
4746
}
4847
}

src/test/java/io/json/compare/matcher/JSONCompareMessageTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void checkNullMessageFromJSONObjectFailedCompare() {
7575
try {
7676
JSONCompare.assertMatches(expected, actual, new HashSet<>(Arrays.asList(CompareMode.JSON_OBJECT_NON_EXTENSIBLE)));
7777
} catch (AssertionError e) {
78-
assertTrue(e.getMessage().contains("Field 'a' was NOT FOUND"));
78+
assertTrue(e.getMessage().contains("$.a was not found"));
7979
}
8080
}
8181

@@ -104,8 +104,8 @@ public void checkMessageFromFailedMatchingBetweenHighDepthJsons() {
104104
AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual,
105105
new HashSet<>(Collections.singletonList(CompareMode.JSON_OBJECT_NON_EXTENSIBLE))));
106106
assertTrue(error.getMessage().matches("(?s).*FOUND 2 DIFFERENCE.*" +
107-
"@ -> instanceId2 -> Actual JSON OBJECT has extra fields.*" +
108-
"@ -> Field 'version' was NOT FOUND.*"));
107+
"\\Q$.@.instanceId2\\E -> Actual JSON OBJECT has extra fields.*" +
108+
"\\Q$.@.version\\E was not found.*"));
109109
JSONCompare.assertNotMatches(expected, actual,
110110
new HashSet<>(Collections.singletonList(CompareMode.JSON_OBJECT_NON_EXTENSIBLE)));
111111
}

0 commit comments

Comments
 (0)