-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better assertion failures for containJsonKeyValue (#3949)
Now prints whether the key was missing, or if it had wrong value <!-- If this PR updates documentation, please update all relevant versions of the docs, see: https://github.com/kotest/kotest/tree/master/documentation/versioned_docs The documentation at https://github.com/kotest/kotest/tree/master/documentation/docs is the documentation for the next minor or major version _TO BE RELEASED_ -->
- Loading branch information
Showing
3 changed files
with
121 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...ertions-json/src/jvmTest/kotlin/com/sksamuel/kotest/tests/json/ContainJsonKeyValueTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.sksamuel.kotest.tests.json | ||
|
||
import io.kotest.assertions.asClue | ||
import io.kotest.assertions.json.shouldContainJsonKeyValue | ||
import io.kotest.assertions.json.shouldNotContainJsonKeyValue | ||
import io.kotest.assertions.shouldFail | ||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.shouldBe | ||
import org.intellij.lang.annotations.Language | ||
|
||
class ContainJsonKeyValueTest : StringSpec({ | ||
@Language("JSON") | ||
val json = """ | ||
{ | ||
"store": { | ||
"book": [ | ||
{ | ||
"category": "reference", | ||
"author": "Nigel Rees", | ||
"title": "Sayings of the Century", | ||
"price": 8.95 | ||
}, | ||
{ | ||
"category": "fiction", | ||
"author": "Evelyn Waugh", | ||
"title": "Sword of Honour", | ||
"price": 12.99 | ||
} | ||
], | ||
"bicycle": { | ||
"color": "red", | ||
"price": 19.95, | ||
"code": 1 | ||
} | ||
} | ||
} | ||
""".trimIndent() | ||
|
||
"Negated assertions" { | ||
"".shouldNotContainJsonKeyValue("$.store.bicycle.color", "red") | ||
"{}".shouldNotContainJsonKeyValue("$.store.bicycle.color", "red") | ||
"""{ "foo": "bar" }""".shouldNotContainJsonKeyValue("foo", "baz") | ||
shouldFail { | ||
"""{ "foo": "bar" }""".shouldNotContainJsonKeyValue("foo", "bar") | ||
}.message shouldBe """{ "foo": "bar" } should not contain the element foo = bar""" | ||
} | ||
|
||
"Failure message states if key is missing, when it's missing" { | ||
shouldFail { | ||
json.shouldContainJsonKeyValue("$.bicycle.engine", "V2") | ||
}.message shouldBe """ | ||
Expected given to contain json key <'$.bicycle.engine'> but key was not found. | ||
""".trimIndent() | ||
} | ||
|
||
"Failure message states states value mismatch if key is present with different value" { | ||
shouldFail { | ||
json.shouldContainJsonKeyValue("$.store.book[0].price", 9.95) | ||
}.message shouldBe """ | ||
Value mismatch at '$.store.book[0].price': expected:<9.95> but was:<8.95> | ||
""".trimIndent() | ||
} | ||
|
||
"test json key value" { | ||
json.shouldContainJsonKeyValue("$.store.bicycle.color", "red") | ||
json.shouldContainJsonKeyValue("$.store.book[0].category", "reference") | ||
json.shouldContainJsonKeyValue("$.store.book[0].price", 8.95) | ||
json.shouldContainJsonKeyValue("$.store.book[1].author", "Evelyn Waugh") | ||
json.shouldContainJsonKeyValue("$.store.book[1].author", "Evelyn Waugh") | ||
json.shouldContainJsonKeyValue("$.store.bicycle.code", 1L) | ||
|
||
json.shouldNotContainJsonKeyValue("$.store.book[1].author", "JK Rowling") | ||
|
||
shouldThrow<AssertionError> { null.shouldContainJsonKeyValue("ab", "cd") }.message shouldBe | ||
"Expected a valid JSON, but was null" | ||
|
||
shouldThrow<AssertionError> { "".shouldContainJsonKeyValue("ab", "cd") }.message shouldBe | ||
"Expected a valid JSON, but was empty" | ||
|
||
"contract should work".asClue { | ||
fun use(@Suppress("UNUSED_PARAMETER") json: String) {} | ||
|
||
val nullableJson = """{"data": "value"}""" | ||
nullableJson.shouldContainJsonKeyValue("data", "value") | ||
use(nullableJson) | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters