Skip to content

Commit

Permalink
Presence test
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Sep 14, 2023
1 parent 0375966 commit d93b1fd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ fun configuration(configurer: Configuration.() -> Configuration): Configuration
}

/**
* Takes given JSON and moves assertion to given path.
* Takes given JSON and moves assertion to given path. For example:
*
* ```kotlin
* """{"test":1}""" inPath ("test") should beJsonNumber()
* ```
*/
infix fun Any?.inPath(path: String): Any = JsonPathAdapter.inPath(this, path)

Expand Down Expand Up @@ -77,7 +81,6 @@ fun beJsonBoolean(): Matcher<Any?> = beType(NodeType.BOOLEAN)
*/
fun beJsonNull(): Matcher<Any?> = beType(NodeType.NULL)

// todo: test
fun bePresent(): Matcher<Any?> = Matcher { actual ->
val node = getNode(actual)
MatcherResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER
import net.javacrumbs.jsonunit.kotest.beJsonBoolean
import net.javacrumbs.jsonunit.kotest.beJsonNull
import net.javacrumbs.jsonunit.kotest.beJsonNumber
import net.javacrumbs.jsonunit.kotest.bePresent
import net.javacrumbs.jsonunit.kotest.configuration
import net.javacrumbs.jsonunit.kotest.equalJson
import net.javacrumbs.jsonunit.kotest.inPath
Expand Down Expand Up @@ -54,7 +55,7 @@ Different value found in node "test", expected: <2> but was: <1>.""")
@Test
fun `Should assert path`() {
assertThrows<AssertionError> {
"""{"test":1}""" inPath ("test") should equalJson("""2""")
"""{"test":1}""" inPath "test" should equalJson("""2""")
}.shouldHaveMessage("""JSON documents are different:
Different value found in node "test", expected: <2> but was: <1>.""")
}
Expand All @@ -80,12 +81,12 @@ Different value found in node "$.test", expected: <2> but was: <1>.""")

@Test
fun `Should assert number`() {
"""{"test":1}""" inPath ("test") should beJsonNumber()
"""{"test":1}""" inPath "test" should beJsonNumber()
}

@Test
fun `Should assert null`() {
"""{"test":null}""" inPath ("test") should beJsonNull()
"""{"test":null}""" inPath "test" should beJsonNull()
}

@Test
Expand All @@ -106,24 +107,29 @@ Different value found in node "$.test", expected: <2> but was: <1>.""")
@Test
fun `Should assert number fail`() {
assertThrows<AssertionError> {
"""{"test": true}""" inPath ("test") should beJsonNumber()
"""{"test": true}""" inPath "test" should beJsonNumber()
}.shouldHaveMessage("""Node "test" has invalid type, expected: <number> but was: <true>.""")
}

@Test
fun `Should assert not number fail`() {
assertThrows<AssertionError> {
"""{"test": 1}""" inPath ("test") shouldNot beJsonNumber()
"""{"test": 1}""" inPath "test" shouldNot beJsonNumber()
}.shouldHaveMessage("""Node "test" has invalid type, expected to not be number but was: <1>.""")
}

@Test
fun `Should assert number fail missing`() {
assertThrows<AssertionError> {
"""{"test": true}""" inPath ("missing") should beJsonNumber()
"""{"test": true}""" inPath "missing" should beJsonNumber()
}.shouldHaveMessage("""Node "missing" is missing.""")
}

@Test
fun `Should assert absent`() {
"""{"test":1}""" inPath "absent" shouldNot bePresent()
}

@Test
fun `Should assert array chained`() {
assertThrows<AssertionError> {
Expand Down

0 comments on commit d93b1fd

Please sign in to comment.