-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc624b7
commit 2a19d3e
Showing
1 changed file
with
122 additions
and
0 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
...c/commonTest/kotlin/io/github/optimumcode/json/schema/internal/wrapper/JsonWrapperTest.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,122 @@ | ||
package io.github.optimumcode.json.schema.internal.wrapper | ||
|
||
import io.github.optimumcode.json.schema.model.contentOrNull | ||
import io.kotest.assertions.assertSoftly | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.booleans.shouldBeFalse | ||
import io.kotest.matchers.booleans.shouldBeTrue | ||
import io.kotest.matchers.collections.shouldContainExactly | ||
import io.kotest.matchers.nulls.shouldBeNull | ||
import io.kotest.matchers.should | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.types.instanceOf | ||
import io.kotest.matchers.types.shouldBeInstanceOf | ||
import kotlinx.serialization.json.JsonNull | ||
import kotlinx.serialization.json.JsonPrimitive | ||
import kotlinx.serialization.json.buildJsonArray | ||
import kotlinx.serialization.json.buildJsonObject | ||
|
||
class JsonWrapperTest : FunSpec() { | ||
init { | ||
mapOf( | ||
buildJsonObject { } to JsonObjectWrapper::class, | ||
buildJsonArray { } to JsonArrayWrapper::class, | ||
JsonPrimitive("hello") to JsonPrimitiveWrapper::class, | ||
JsonNull to JsonPrimitiveWrapper::class, | ||
).forEach { (node, wrapperClass) -> | ||
test("node ${node::class.simpleName} wraps into ${wrapperClass.simpleName}") { | ||
node.wrap() shouldBe instanceOf(wrapperClass) | ||
} | ||
} | ||
|
||
test("object wrapper") { | ||
buildJsonObject { | ||
put("a", JsonPrimitive("hello")) | ||
put("b", buildJsonArray { }) | ||
}.wrap().shouldBeInstanceOf<JsonObjectWrapper> { | ||
assertSoftly { | ||
it.size shouldBe 2 | ||
it.keys shouldContainExactly setOf("a", "b") | ||
it["a"].shouldBeInstanceOf<JsonPrimitiveWrapper>() | ||
it["b"].shouldBeInstanceOf<JsonArrayWrapper>() | ||
} | ||
} | ||
} | ||
|
||
test("array wrapper") { | ||
buildJsonArray { | ||
add(JsonPrimitive("hello")) | ||
add(buildJsonObject { }) | ||
}.wrap().shouldBeInstanceOf<JsonArrayWrapper> { | ||
assertSoftly { | ||
it.size shouldBe 2 | ||
it[0].shouldBeInstanceOf<JsonPrimitiveWrapper>() | ||
it[1].shouldBeInstanceOf<JsonObjectWrapper>() | ||
} | ||
} | ||
} | ||
|
||
test("primitive wrapper for null") { | ||
JsonNull.wrap().shouldBeInstanceOf<JsonPrimitiveWrapper> { | ||
assertSoftly { | ||
it.isString.shouldBeFalse() | ||
it.isNumber.shouldBeFalse() | ||
it.isBoolean.shouldBeFalse() | ||
it.isNull.shouldBeTrue() | ||
it.content shouldBe "null" | ||
it.contentOrNull.shouldBeNull() | ||
} | ||
} | ||
} | ||
|
||
test("primitive wrapper for boolean") { | ||
JsonPrimitive(true).wrap().shouldBeInstanceOf<JsonPrimitiveWrapper> { | ||
assertSoftly { | ||
it.isString.shouldBeFalse() | ||
it.isNumber.shouldBeFalse() | ||
it.isBoolean.shouldBeTrue() | ||
it.isNull.shouldBeFalse() | ||
it.content shouldBe "true" | ||
it.contentOrNull shouldBe "true" | ||
} | ||
} | ||
} | ||
|
||
test("primitive wrapper for number") { | ||
JsonPrimitive(42).wrap().shouldBeInstanceOf<JsonPrimitiveWrapper> { | ||
assertSoftly { | ||
it.isString.shouldBeFalse() | ||
it.isNumber.shouldBeTrue() | ||
it.isBoolean.shouldBeFalse() | ||
it.isNull.shouldBeFalse() | ||
it.content shouldBe "42" | ||
it.contentOrNull shouldBe "42" | ||
} | ||
} | ||
} | ||
|
||
test("primitive wrapper for string") { | ||
JsonPrimitive("42").wrap().shouldBeInstanceOf<JsonPrimitiveWrapper> { | ||
assertSoftly { | ||
it.isString.shouldBeTrue() | ||
it.isNumber.shouldBeFalse() | ||
it.isBoolean.shouldBeFalse() | ||
it.isNull.shouldBeFalse() | ||
it.content shouldBe "42" | ||
it.contentOrNull shouldBe "42" | ||
} | ||
} | ||
} | ||
|
||
test("string wrapper for property") { | ||
StringWrapper("prop").should { | ||
it.isString.shouldBeTrue() | ||
it.isNumber.shouldBeFalse() | ||
it.isBoolean.shouldBeFalse() | ||
it.isNull.shouldBeFalse() | ||
it.content shouldBe "prop" | ||
it.contentOrNull shouldBe "prop" | ||
} | ||
} | ||
} | ||
} |