Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fslev committed Oct 3, 2022
1 parent e7071d0 commit 0816ea1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
13 changes: 4 additions & 9 deletions src/main/java/io/json/compare/matcher/JsonArrayMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public List<String> match() {
private List<String> matchWithJsonArray(int expPosition, JsonNode expElement, JsonNode actualArray) {
List<String> diffs = new ArrayList<>();
UseCase useCase = getUseCase(expElement);
boolean found = false;

for (int j = 0; j < actualArray.size(); j++) {
if (matchedPositions.contains(j)) {
Expand Down Expand Up @@ -70,7 +69,9 @@ private List<String> matchWithJsonArray(int expPosition, JsonNode expElement, Js
if (areOfSameType(expElement, actElement)) {
elementDiffs = new JsonMatcher(expElement, actElement, comparator, compareModes).match();
if (!elementDiffs.isEmpty()) {
found = true;
diffs.add("Expected element from position " + (expPosition + 1)
+ " was FOUND:\n" + MessageUtil.cropL(JSONCompare.prettyPrint(expElement)));
return diffs;
}
}
break;
Expand All @@ -80,16 +81,10 @@ private List<String> matchWithJsonArray(int expPosition, JsonNode expElement, Js
expElement, expPosition + 1));
return diffs;
}
if (found) {
break;
}
}
if (!found && useCase == UseCase.MATCH) {
if (useCase == UseCase.MATCH) {
diffs.add("Expected element from position " + (expPosition + 1) + " was NOT FOUND:\n"
+ MessageUtil.cropL(JSONCompare.prettyPrint(expElement)));
} else if (found) {
diffs.add("Expected element from position " + (expPosition + 1)
+ " was FOUND:\n" + MessageUtil.cropL(JSONCompare.prettyPrint(expElement)));
} else if (useCase == UseCase.MATCH_ANY) {
diffs.add(String.format("Actual Json Array has no extra elements. Condition %s from position %s means there" +
" should be more actual elements", expElement, expPosition + 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ public void compareNumbers() {

@Test
public void compareAll() {
String expected = "{\"a\":null,\"b\":1,\"c\":false,\"d\":\"false\"}";
String actual = "{\"a\":\"null\",\"b\":\"1\",\"c\":\"false\",\"d\":false}";
String expected = "{\"a\":null,\"b\":1,\"c\":false,\"d\":\"false\",\"e\":\"text\"}";
String actual = "{\"a\":\"null\",\"b\":\"1\",\"c\":\"false\",\"d\":false,\"e\":false}";
AssertionError error = assertThrows(AssertionError.class, () -> JSONCompare.assertMatches(expected, actual));
assertTrue(error.getMessage().matches("(?s).*FOUND 3 DIFFERENCE.*" +
assertTrue(error.getMessage().matches("(?s).*FOUND 4 DIFFERENCE.*" +
"a ->.*Expected null: But got: \"null\".*" +
"b ->.*Expected number: 1 But got: \"1\".*" +
"c ->.*Expected boolean: false But got: \"false\".*"));
"c ->.*Expected boolean: false But got: \"false\".*" +
"e ->.*Expected value: \"text\" But got: false.*"));
JSONCompare.assertNotMatches(expected, actual);
}
}

0 comments on commit 0816ea1

Please sign in to comment.