-
Notifications
You must be signed in to change notification settings - Fork 623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better assertion failures for containJsonKeyValue #3949
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to print partial matches, such as:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose it would, but I don't believe we get that information out of the JsonPath library. How do you intend that we should calculate that subkey?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think perhaps we can merge this small improvement as-is, and we could make further improvements in a separate PR :) We can continue the discussion here even after PR is merged
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure - #4013