Skip to content

Commit 2a19d3e

Browse files
committed
Add test for json wrappers
1 parent fc624b7 commit 2a19d3e

File tree

1 file changed

+122
-0
lines changed
  • json-schema-validator/src/commonTest/kotlin/io/github/optimumcode/json/schema/internal/wrapper

1 file changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package io.github.optimumcode.json.schema.internal.wrapper
2+
3+
import io.github.optimumcode.json.schema.model.contentOrNull
4+
import io.kotest.assertions.assertSoftly
5+
import io.kotest.core.spec.style.FunSpec
6+
import io.kotest.matchers.booleans.shouldBeFalse
7+
import io.kotest.matchers.booleans.shouldBeTrue
8+
import io.kotest.matchers.collections.shouldContainExactly
9+
import io.kotest.matchers.nulls.shouldBeNull
10+
import io.kotest.matchers.should
11+
import io.kotest.matchers.shouldBe
12+
import io.kotest.matchers.types.instanceOf
13+
import io.kotest.matchers.types.shouldBeInstanceOf
14+
import kotlinx.serialization.json.JsonNull
15+
import kotlinx.serialization.json.JsonPrimitive
16+
import kotlinx.serialization.json.buildJsonArray
17+
import kotlinx.serialization.json.buildJsonObject
18+
19+
class JsonWrapperTest : FunSpec() {
20+
init {
21+
mapOf(
22+
buildJsonObject { } to JsonObjectWrapper::class,
23+
buildJsonArray { } to JsonArrayWrapper::class,
24+
JsonPrimitive("hello") to JsonPrimitiveWrapper::class,
25+
JsonNull to JsonPrimitiveWrapper::class,
26+
).forEach { (node, wrapperClass) ->
27+
test("node ${node::class.simpleName} wraps into ${wrapperClass.simpleName}") {
28+
node.wrap() shouldBe instanceOf(wrapperClass)
29+
}
30+
}
31+
32+
test("object wrapper") {
33+
buildJsonObject {
34+
put("a", JsonPrimitive("hello"))
35+
put("b", buildJsonArray { })
36+
}.wrap().shouldBeInstanceOf<JsonObjectWrapper> {
37+
assertSoftly {
38+
it.size shouldBe 2
39+
it.keys shouldContainExactly setOf("a", "b")
40+
it["a"].shouldBeInstanceOf<JsonPrimitiveWrapper>()
41+
it["b"].shouldBeInstanceOf<JsonArrayWrapper>()
42+
}
43+
}
44+
}
45+
46+
test("array wrapper") {
47+
buildJsonArray {
48+
add(JsonPrimitive("hello"))
49+
add(buildJsonObject { })
50+
}.wrap().shouldBeInstanceOf<JsonArrayWrapper> {
51+
assertSoftly {
52+
it.size shouldBe 2
53+
it[0].shouldBeInstanceOf<JsonPrimitiveWrapper>()
54+
it[1].shouldBeInstanceOf<JsonObjectWrapper>()
55+
}
56+
}
57+
}
58+
59+
test("primitive wrapper for null") {
60+
JsonNull.wrap().shouldBeInstanceOf<JsonPrimitiveWrapper> {
61+
assertSoftly {
62+
it.isString.shouldBeFalse()
63+
it.isNumber.shouldBeFalse()
64+
it.isBoolean.shouldBeFalse()
65+
it.isNull.shouldBeTrue()
66+
it.content shouldBe "null"
67+
it.contentOrNull.shouldBeNull()
68+
}
69+
}
70+
}
71+
72+
test("primitive wrapper for boolean") {
73+
JsonPrimitive(true).wrap().shouldBeInstanceOf<JsonPrimitiveWrapper> {
74+
assertSoftly {
75+
it.isString.shouldBeFalse()
76+
it.isNumber.shouldBeFalse()
77+
it.isBoolean.shouldBeTrue()
78+
it.isNull.shouldBeFalse()
79+
it.content shouldBe "true"
80+
it.contentOrNull shouldBe "true"
81+
}
82+
}
83+
}
84+
85+
test("primitive wrapper for number") {
86+
JsonPrimitive(42).wrap().shouldBeInstanceOf<JsonPrimitiveWrapper> {
87+
assertSoftly {
88+
it.isString.shouldBeFalse()
89+
it.isNumber.shouldBeTrue()
90+
it.isBoolean.shouldBeFalse()
91+
it.isNull.shouldBeFalse()
92+
it.content shouldBe "42"
93+
it.contentOrNull shouldBe "42"
94+
}
95+
}
96+
}
97+
98+
test("primitive wrapper for string") {
99+
JsonPrimitive("42").wrap().shouldBeInstanceOf<JsonPrimitiveWrapper> {
100+
assertSoftly {
101+
it.isString.shouldBeTrue()
102+
it.isNumber.shouldBeFalse()
103+
it.isBoolean.shouldBeFalse()
104+
it.isNull.shouldBeFalse()
105+
it.content shouldBe "42"
106+
it.contentOrNull shouldBe "42"
107+
}
108+
}
109+
}
110+
111+
test("string wrapper for property") {
112+
StringWrapper("prop").should {
113+
it.isString.shouldBeTrue()
114+
it.isNumber.shouldBeFalse()
115+
it.isBoolean.shouldBeFalse()
116+
it.isNull.shouldBeFalse()
117+
it.content shouldBe "prop"
118+
it.contentOrNull shouldBe "prop"
119+
}
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)