@@ -6,6 +6,7 @@ import io.kotest.core.spec.style.FunSpec
66import io.kotest.data.forAll
77import io.kotest.data.row
88import io.kotest.matchers.booleans.shouldBeTrue
9+ import io.kotest.matchers.should
910import io.kotest.matchers.shouldBe
1011
1112class JsonPathProxyTest : FunSpec ({
@@ -62,29 +63,84 @@ class JsonPathProxyTest : FunSpec({
6263 }
6364 }
6465
65- context("complicated json-path evaluation ") {
66+ context("indefinite json path ") {
6667 val json = """
6768 {"people": [
68- {"name": "abc", "age": 21},
69- {"name": "def", "age": 28}
69+ {"name": "abc", "age": 21, "pocketMoney": 1.2, "worker?": true },
70+ {"name": "def", "age": 28, "pocketMoney": 2.3, "worker?": false }
7071 ]}
7172 """ .trimIndent()
7273
73- test("simple definite path ") {
74- JsonPathProxy .of(json, "$.people[0 ].name")
74+ test("should be true if single string value ") {
75+ JsonPathProxy .of(json, "$.people[? (@.name == 'abc') ].name")
7576 .shouldBe("abc").shouldBeTrue()
7677 }
7778
78- test("indefinite path") {
79+ test("should be failed if multiple string values") {
80+ shouldThrow<PlaytestAssertionError > {
81+ JsonPathProxy .of(json, "$.people[? (@.age > 1)].name")
82+ .shouldBe("abc")
83+ }.message.shouldBe("The path is indefinite and the result is not a single value")
84+ }
85+
86+ test("should be true if single long value") {
7987 JsonPathProxy .of(json, "$.people[? (@.name == 'abc')].age")
8088 .shouldBe(21).shouldBeTrue()
8189 }
8290
83- test("indefinite path with multiple results ") {
91+ test("should be failed if multiple long values ") {
8492 shouldThrow<PlaytestAssertionError > {
8593 JsonPathProxy .of(json, "$.people[? (@.age > 1)].age")
86- .shouldBe(21)
87- }
94+ .shouldBe(999)
95+ }.message.shouldBe("The path is indefinite and the result is not a single value")
96+ }
97+
98+ test("should be true if single double value") {
99+ JsonPathProxy .of(json, "$.people[? (@.name == 'abc')].pocketMoney")
100+ .shouldBe(1.2.toBigDecimal()).shouldBeTrue()
101+ }
102+
103+ test("should be failed if multiple double values") {
104+ shouldThrow<PlaytestAssertionError > {
105+ JsonPathProxy .of(json, "$.people[? (@.age > 1)].pocketMoney")
106+ .shouldBe(9.9.toBigDecimal())
107+ }.message.shouldBe("The path is indefinite and the result is not a single value")
108+ }
109+
110+ test("should be true if single boolean value") {
111+ JsonPathProxy .of(json, "$.people[? (@.name == 'abc')].worker? ")
112+ .shouldBe(true).shouldBeTrue()
113+ }
114+
115+ test("should be failed if multiple boolean values") {
116+ shouldThrow<PlaytestAssertionError > {
117+ JsonPathProxy .of(json, "$.people[? (@.age > 1)].worker? ")
118+ .shouldBe(false)
119+ }.message.shouldBe("The path is indefinite and the result is not a single value")
120+ }
121+
122+ test("should be true if single string value - contains") {
123+ JsonPathProxy .of(json, "$.people[? (@.name == 'abc')].name")
124+ .shouldContain("b").shouldBeTrue()
125+ }
126+
127+ test("should be failed if multiple string values - contains") {
128+ shouldThrow<PlaytestAssertionError > {
129+ JsonPathProxy .of(json, "$.people[? (@.age > 1)].name")
130+ .shouldContain("b")
131+ }.message.shouldBe("The path is indefinite and the result is not a single value")
132+ }
133+
134+ test("should be true if single string value - entire match") {
135+ JsonPathProxy .of(json, "$.people[? (@.name == 'abc')].name")
136+ .shouldMatch("[abc]{3}").shouldBeTrue()
137+ }
138+
139+ test("should be failed if multiple string values - entire match") {
140+ shouldThrow<PlaytestAssertionError > {
141+ JsonPathProxy .of(json, "$.people[? (@.age > 1)].name")
142+ .shouldMatch(".*")
143+ }.message.shouldBe("The path is indefinite and the result is not a single value")
88144 }
89145 }
90146})
0 commit comments