Skip to content

Commit

Permalink
Merge pull request #9 from orangain/fix-object-match
Browse files Browse the repository at this point in the history
Fix match of empty object literal
  • Loading branch information
orangain authored Feb 6, 2023
2 parents fb148dc + 2dcee10 commit 2e22865
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public ObjectLiteralPatternNode(@NotNull String expected, @NotNull Map<String, J
@NotNull
@Override
public Optional<JsonMatchErrorDetail> matches(@NotNull JsonPath path, @NotNull JsonNode actualNode) {
if (!actualNode.isObject()) {
String reason = "not a json object";
return Optional.of(error(path, actualNode, reason));
}
Set<String> actualFieldNames = new HashSet<>();
actualNode.fieldNames().forEachRemaining(actualFieldNames::add);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.github.orangain.jsonmatch

import org.assertj.core.api.Assertions
import org.junit.Test

class EmptyObjectLiteralTest {
val patternJson = "{}"

@Test
fun matchEmptyObject() {
// language=JSON
JsonStringAssert.assertThat("{}").jsonMatches(patternJson)
}

@Test
fun doesNotMatchArray() {
Assertions.assertThatThrownBy {
// language=JSON
JsonStringAssert.assertThat("[]").jsonMatches(patternJson)
}.isInstanceOf(AssertionError::class.java)
.hasMessageContaining("""path: $, actual: [], expected: {}, reason: not a json object""")
}

@Test
fun doesNotMatchNumber() {
Assertions.assertThatThrownBy {
// language=JSON
JsonStringAssert.assertThat("0").jsonMatches(patternJson)
}.isInstanceOf(AssertionError::class.java)
.hasMessageContaining("""path: $, actual: 0, expected: {}, reason: not a json object""")
}

@Test
fun doesNotMatchString() {
Assertions.assertThatThrownBy {
// language=JSON
JsonStringAssert.assertThat("\"foo\"").jsonMatches(patternJson)
}.isInstanceOf(AssertionError::class.java)
.hasMessageContaining("""path: $, actual: "foo", expected: {}, reason: not a json object""")
}

@Test
fun doesNotMatchNull() {
Assertions.assertThatThrownBy {
// language=JSON
JsonStringAssert.assertThat("null").jsonMatches(patternJson)
}.isInstanceOf(AssertionError::class.java)
.hasMessageContaining("""path: $, actual: null, expected: {}, reason: not a json object""")
}
}

0 comments on commit 2e22865

Please sign in to comment.