Skip to content

Commit e68ca9b

Browse files
committed
Impl indefinite json path proxy
1 parent 3ebf637 commit e68ca9b

File tree

2 files changed

+89
-18
lines changed

2 files changed

+89
-18
lines changed

playtest-http/src/main/kotlin/com/uzabase/playtest2/http/zoom/JsonSerializable.kt

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,32 @@ internal class IndefiniteJsonPathProxy(
8181
}
8282
}
8383

84-
override fun shouldBe(expected: Boolean): Boolean {
85-
TODO("Not yet implemented")
86-
}
84+
override fun shouldBe(expected: Boolean): Boolean =
85+
JsonPath.parse(json).read<List<Boolean>>(path).let { list ->
86+
if (list.size == 1) {
87+
list[0] == expected
88+
} else {
89+
throw PlaytestAssertionError("The path is indefinite and the result is not a single value")
90+
}
91+
}
8792

88-
override fun shouldContain(expected: String): Boolean {
89-
TODO("Not yet implemented")
90-
}
93+
override fun shouldContain(expected: String): Boolean =
94+
JsonPath.parse(json).read<List<String>>(path).let { list ->
95+
if (list.size == 1) {
96+
list[0].contains(expected)
97+
} else {
98+
throw PlaytestAssertionError("The path is indefinite and the result is not a single value")
99+
}
100+
}
91101

92-
override fun shouldMatch(expected: String): Boolean {
93-
TODO("Not yet implemented")
94-
}
102+
override fun shouldMatch(expected: String): Boolean =
103+
JsonPath.parse(json).read<List<String>>(path).let { list ->
104+
if (list.size == 1) {
105+
expected.toRegex().matches(list[0])
106+
} else {
107+
throw PlaytestAssertionError("The path is indefinite and the result is not a single value")
108+
}
109+
}
95110

96111
override fun shouldBeExist(): Boolean {
97112
TODO("Not yet implemented")

playtest-http/src/test/kotlin/com/uzabase/playtest2/http/zoom/JsonPathProxyTest.kt

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import io.kotest.core.spec.style.FunSpec
66
import io.kotest.data.forAll
77
import io.kotest.data.row
88
import io.kotest.matchers.booleans.shouldBeTrue
9+
import io.kotest.matchers.should
910
import io.kotest.matchers.shouldBe
1011

1112
class 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

Comments
 (0)