Skip to content

Commit

Permalink
Merge pull request #184 from lukas-krecan/ignore-missing2
Browse files Browse the repository at this point in the history
#182 Ignore paths even if present in expected value
  • Loading branch information
lukas-krecan authored May 25, 2019
2 parents 8a67530 + e1eaa97 commit 0eb1924
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,13 @@ public ConfigurableJsonAssert withTolerance(double tolerance) {
return withTolerance(BigDecimal.valueOf(tolerance));
}

/**
* Makes JsonUnit ignore the specified paths in the actual value. If the path matches,
* it's completely ignored. It may be missing, null or have any value
*
* @param pathsToBeIgnored
* @return
*/
public ConfigurableJsonAssert whenIgnoringPaths(String... pathsToBeIgnored) {
return withConfiguration(c -> c.whenIgnoringPaths(pathsToBeIgnored));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ public Configuration withOptions(Options options) {
return new Configuration(tolerance, options, ignorePlaceholder, matchers, pathsToBeIgnored, differenceListener);
}

/**
* Makes JsonUnit ignore the specified paths in the actual value. If the path matches,
* it's completely ignored. It may be missing, null or have any value
*
* @param pathsToBeIgnored
* @return
*/
public Configuration whenIgnoringPaths(String... pathsToBeIgnored) {
return new Configuration(tolerance, options, ignorePlaceholder, matchers, asList(pathsToBeIgnored), differenceListener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ private void compareObjectNodes(Context context) {
}

removePathsToBeIgnored(path, extraKeys);
removePathsToBeIgnored(path, missingKeys);

removeMissingIgnoredElements(expected, missingKeys);

Expand All @@ -172,8 +173,8 @@ private void compareObjectNodes(Context context) {
}
}

private boolean removeMissingIgnoredElements(Node expected, Set<String> missingKeys) {
return missingKeys.removeIf(missingKey -> shouldIgnoreElement(expected.get(missingKey)));
private void removeMissingIgnoredElements(Node expected, Set<String> missingKeys) {
missingKeys.removeIf(missingKey -> shouldIgnoreElement(expected.get(missingKey)));
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ public ConfigurableJsonFluentAssert node(String newPath) {
}

/**
* Adds paths to be ignored
* Makes JsonUnit ignore the specified paths in the actual value. If the path matches,
* it's completely ignored. It may be missing, null or have any value
*/
public ConfigurableJsonFluentAssert whenIgnoringPaths(String... pathsToBeIgnored) {
return new ConfigurableJsonFluentAssert(actual, path, description, configuration.whenIgnoringPaths(pathsToBeIgnored));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ public ConfigurableJsonMatcher<T> withMatcher(String matcherName, Matcher<?> mat
return this;
}

/**
* Makes JsonUnit ignore the specified paths in the actual value. If the path matches,
* it's completely ignored. It may be missing, null or have any value
*/
public ConfigurableJsonMatcher<T> whenIgnoringPaths(String... paths) {
configuration = configuration.whenIgnoringPaths(paths);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,20 @@ void multipleFailuresErrorShouldbeCorrectlyFormatted() {
});
}

@Test
void shouldIgnoreMissingPathEvenIfItIsInExpectedValue() {
assertThatJson("{\"root\":{\"foo\":1}}")
.whenIgnoringPaths("root.bar", "missing")
.isEqualTo("{\"root\":{\"foo\":1, \"bar\":2}, \"missing\":{\"quux\":\"test\"}}");
}

@Test
void shouldIgnoreArrayElement() {
assertThatJson("{\"root\":[0, 1, 2]}")
.whenIgnoringPaths("root[1]")
.isEqualTo("{\"root\":[0, 8, 2]}");
}

@Test
void arraySimpleIgnoringOrderNotEqualComparison() {
assertThatJson("{\"a\":[{\"b\": 1}, {\"c\": 1}, {\"d\": 1}]}").when(Option.IGNORING_ARRAY_ORDER).node("a").isArray()
Expand Down

0 comments on commit 0eb1924

Please sign in to comment.