Skip to content

Commit

Permalink
#512 Special handling of numeric values in containsEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed May 5, 2022
1 parent 4bfae60 commit 04a64d1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
Expand Up @@ -31,6 +31,7 @@
import static java.util.Arrays.stream;
import static java.util.Objects.deepEquals;
import static java.util.stream.Collectors.toList;
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.json;
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.wrapDeserializedObject;
import static org.assertj.core.api.Assertions.entry;
import static org.assertj.core.error.ShouldContain.shouldContain;
Expand Down Expand Up @@ -184,11 +185,15 @@ private boolean doesContainEntry(Entry<? extends String, ?> entry) {
return false;
}
Object actualValue = actual.get(key);
if (entry.getValue() instanceof Node) {
Node value = (Node) entry.getValue();
Object expectedValue = entry.getValue();
if (expectedValue instanceof Number) {
expectedValue = json(expectedValue);
}
if (expectedValue instanceof Node) {
Node value = (Node) expectedValue;
return isSimilar(actualValue, value);
} else {
return deepEquals(actualValue, entry.getValue());
return deepEquals(actualValue, expectedValue);
}
}

Expand Down
Expand Up @@ -1153,12 +1153,28 @@ void testEqualsToArray() {
}

@Test
void assertTolerance() {
void assertContainsEntryNumber() {
assertThatJson("{\"a\":1, \"b\":2.0}")
.withTolerance(0)
.isObject()
.containsEntry("a", json(1))
.containsEntry("b", json(2));
.containsEntry("a", 1)
.containsEntry("b", 2);
}

@Test
void assertContainsEntryNumberFailure() {
assertThatThrownBy(() ->
assertThatJson("{\"a\":1, \"b\":2.0}")
.isObject()
.containsEntry("a", 1)
.containsEntry("b", 2)
).hasMessage("[Different value found in node \"\"] \n" +
"Expecting map:\n" +
" {\"a\":1,\"b\":2.0}\n" +
"to contain:\n" +
" [\"b\"=2]\n" +
"but could not find the following map entries:\n" +
" [\"b\"=2]\n");
}

@Test
Expand Down

0 comments on commit 04a64d1

Please sign in to comment.