-
Notifications
You must be signed in to change notification settings - Fork 627
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If a key is missing in json, try finding a valid subpath: ```kotlin "Failure message states if key is missing, shows valid subpath" { shouldFail { json.shouldContainJsonKeyValue("$.store.bicycle.engine", "V2") }.message shouldBe """ Expected given to contain json key <'$.store.bicycle.engine'> but key was not found. Found shorter valid subpath: <'$.store.bicycle'> """.trimIndent() } ``` I had to add a few public methods, as I couldn't invoke internal ones from a public inline function. Also made sure that we distinguish between these two cases: * key not found * key found, the corresponding value is null Ideally we short also find indexes out of bound such as ```kotlin json.shouldContainJsonKeyValue("$.store.book[2].author", "Evelyn Waugh") saying <'$.store.book'> has only 2 elements ``` But that might be done later in another PR. WDYT @Kantis ? --------- Co-authored-by: Sam <sam@sksamuel.com>
- Loading branch information
1 parent
7d07732
commit 6f371fb
Showing
4 changed files
with
166 additions
and
14 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
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
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
65 changes: 65 additions & 0 deletions
65
...st-assertions-json/src/jvmTest/kotlin/com/sksamuel/kotest/tests/json/ExtractByPathTest.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,65 @@ | ||
package com.sksamuel.kotest.tests.json | ||
|
||
import io.kotest.assertions.json.ExtractedValue | ||
import io.kotest.assertions.json.JsonPathNotFound | ||
import io.kotest.assertions.json.extractByPath | ||
import io.kotest.assertions.json.findValidSubPath | ||
import io.kotest.assertions.json.removeLastPartFromPath | ||
import io.kotest.core.spec.style.WordSpec | ||
import io.kotest.matchers.shouldBe | ||
import org.intellij.lang.annotations.Language | ||
|
||
class ExtractByPathTest: WordSpec() { | ||
@Language("JSON") | ||
private val json = """ | ||
{ | ||
"ingredients": ["Rice", "Water", "Salt"], | ||
"appliance": { | ||
"type": "Stove", | ||
"kind": "Electric" | ||
}, | ||
"regime": { | ||
"temperature": { | ||
"degrees": 320, | ||
"unit": "F" | ||
} | ||
}, | ||
"comments": null | ||
} | ||
""".trimIndent() | ||
|
||
init { | ||
"extractByPath" should { | ||
"find not null value by valid path" { | ||
extractByPath<String>(json, "$.regime.temperature.unit") shouldBe ExtractedValue("F") | ||
} | ||
"find null value by valid path" { | ||
extractByPath<String>(json, "$.comments") shouldBe ExtractedValue(null) | ||
} | ||
"path not found" { | ||
extractByPath<String>(json, "$.regime.temperature.unit.name") shouldBe JsonPathNotFound | ||
} | ||
} | ||
|
||
"removeLastPartFromPath" should { | ||
"remove last part" { | ||
removeLastPartFromPath("$.regime.temperature.unit.name") shouldBe "$.regime.temperature.unit" | ||
removeLastPartFromPath("$.regime.temperature.unit") shouldBe "$.regime.temperature" | ||
removeLastPartFromPath("$.regime.temperature") shouldBe "$.regime" | ||
removeLastPartFromPath("$.regime") shouldBe "$" | ||
} | ||
} | ||
|
||
"findValidSubPath" should { | ||
"find valid sub path" { | ||
findValidSubPath(json, "$.regime.temperature.unit.name.some.more.tokens") shouldBe "$.regime.temperature.unit" | ||
findValidSubPath(json, "$.regime.temperature.unit.name") shouldBe "$.regime.temperature.unit" | ||
findValidSubPath(json, "$.regime.temperature.name") shouldBe "$.regime.temperature" | ||
findValidSubPath(json, "$.regime.no_such_element") shouldBe "$.regime" | ||
} | ||
"return null when nothing found" { | ||
findValidSubPath(json, "$.no.such.path") shouldBe null | ||
} | ||
} | ||
} | ||
} |