commonTest.com.copperleaf.json.values.TestJsonValues.kt Maven / Gradle / Ivy
package com.copperleaf.json.values
import com.copperleaf.json.pointer.JsonPointer
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.shouldBe
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
class TestJsonValues : FreeSpec({
"objects" - {
"primitive 3 value" {
val notObjectMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonObject"
val notArrayMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonArray"
val value = JsonPrimitive(3)
val map = JsonObject(mapOf("name" to value))
shouldThrow { value.string("name") }.message shouldBe notObjectMessage
shouldThrow { value.int("name") }.message shouldBe notObjectMessage
shouldThrow { value.long("name") }.message shouldBe notObjectMessage
shouldThrow { value.float("name") }.message shouldBe notObjectMessage
shouldThrow { value.double("name") }.message shouldBe notObjectMessage
shouldThrow { value.boolean("name") }.message shouldBe notObjectMessage
map.string("name") shouldBe "3"
map.int("name") shouldBe 3
map.long("name") shouldBe 3L
map.float("name") shouldBe 3f
map.double("name") shouldBe 3.0
shouldThrow { map.boolean("name") }.message shouldBe "3 does not represent a Boolean"
shouldThrow { map.objectAt("name") }.message shouldBe notObjectMessage
shouldThrow { map.arrayAt("name") }.message shouldBe notArrayMessage
}
"primitive 3.14 value" {
val notObjectMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonObject"
val notArrayMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonArray"
val value = JsonPrimitive(3.14)
val map = JsonObject(mapOf("name" to value))
shouldThrow { value.string("name") }.message shouldBe notObjectMessage
shouldThrow { value.int("name") }.message shouldBe notObjectMessage
shouldThrow { value.long("name") }.message shouldBe notObjectMessage
shouldThrow { value.float("name") }.message shouldBe notObjectMessage
shouldThrow { value.double("name") }.message shouldBe notObjectMessage
shouldThrow { value.boolean("name") }.message shouldBe notObjectMessage
map.string("name") shouldBe "3.14"
shouldThrow { map.int("name") }.message shouldBe "For input string: \"3.14\""
shouldThrow { map.long("name") }.message shouldBe "For input string: \"3.14\""
map.float("name") shouldBe 3.14f
map.double("name") shouldBe 3.14
shouldThrow { map.boolean("name") }.message shouldBe "3.14 does not represent a Boolean"
shouldThrow { map.objectAt("name") }.message shouldBe notObjectMessage
shouldThrow { map.arrayAt("name") }.message shouldBe notArrayMessage
}
"primitive string value" {
val notObjectMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonObject"
val notArrayMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonArray"
val value = JsonPrimitive("something else")
val map = JsonObject(mapOf("name" to value))
shouldThrow { value.string("name") }.message shouldBe notObjectMessage
shouldThrow { value.int("name") }.message shouldBe notObjectMessage
shouldThrow { value.long("name") }.message shouldBe notObjectMessage
shouldThrow { value.float("name") }.message shouldBe notObjectMessage
shouldThrow { value.double("name") }.message shouldBe notObjectMessage
shouldThrow { value.boolean("name") }.message shouldBe notObjectMessage
map.string("name") shouldBe "something else"
shouldThrow { map.int("name") }.message shouldBe "For input string: \"something else\""
shouldThrow { map.long("name") }.message shouldBe "For input string: \"something else\""
shouldThrow { map.float("name") }.message shouldBe "For input string: \"something else\""
shouldThrow { map.double("name") }.message shouldBe "For input string: \"something else\""
shouldThrow { map.boolean("name") }.message shouldBe "\"something else\" does not represent a Boolean"
shouldThrow { map.objectAt("name") }.message shouldBe notObjectMessage
shouldThrow { map.arrayAt("name") }.message shouldBe notArrayMessage
}
"primitive boolean value" {
val notObjectMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonObject"
val notArrayMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonArray"
val value = JsonPrimitive(true)
val map = JsonObject(mapOf("name" to value))
shouldThrow { value.string("name") }.message shouldBe notObjectMessage
shouldThrow { value.int("name") }.message shouldBe notObjectMessage
shouldThrow { value.long("name") }.message shouldBe notObjectMessage
shouldThrow { value.float("name") }.message shouldBe notObjectMessage
shouldThrow { value.double("name") }.message shouldBe notObjectMessage
shouldThrow { value.boolean("name") }.message shouldBe notObjectMessage
map.string("name") shouldBe "true"
shouldThrow { map.int("name") }.message shouldBe "For input string: \"true\""
shouldThrow { map.long("name") }.message shouldBe "For input string: \"true\""
shouldThrow { map.float("name") }.message shouldBe "For input string: \"true\""
shouldThrow { map.double("name") }.message shouldBe "For input string: \"true\""
map.boolean("name") shouldBe true
shouldThrow { map.objectAt("name") }.message shouldBe notObjectMessage
shouldThrow { map.arrayAt("name") }.message shouldBe notArrayMessage
}
"object value" {
val notPrimitiveMessage = "Element class kotlinx.serialization.json.JsonObject is not a JsonPrimitive"
val notArrayMessage = "Element class kotlinx.serialization.json.JsonObject is not a JsonArray"
val value = JsonObject(mapOf("inner" to JsonPrimitive("value")))
val map = JsonObject(mapOf("name" to value))
shouldThrow { map.string("name") }.message shouldBe notPrimitiveMessage
shouldThrow { map.int("name") }.message shouldBe notPrimitiveMessage
shouldThrow { map.long("name") }.message shouldBe notPrimitiveMessage
shouldThrow { map.float("name") }.message shouldBe notPrimitiveMessage
shouldThrow { map.double("name") }.message shouldBe notPrimitiveMessage
shouldThrow { map.boolean("name") }.message shouldBe notPrimitiveMessage
map.objectAt("name") shouldBe value
shouldThrow { map.arrayAt("name") }.message shouldBe notArrayMessage
}
"array value" {
val notPrimitiveMessage = "Element class kotlinx.serialization.json.JsonArray is not a JsonPrimitive"
val notObjectMessage = "Element class kotlinx.serialization.json.JsonArray is not a JsonObject"
val value = JsonArray(listOf(JsonPrimitive("value")))
val map = JsonObject(mapOf("name" to value))
shouldThrow { map.string("name") }.message shouldBe notPrimitiveMessage
shouldThrow { map.int("name") }.message shouldBe notPrimitiveMessage
shouldThrow { map.long("name") }.message shouldBe notPrimitiveMessage
shouldThrow { map.float("name") }.message shouldBe notPrimitiveMessage
shouldThrow { map.double("name") }.message shouldBe notPrimitiveMessage
shouldThrow { map.boolean("name") }.message shouldBe notPrimitiveMessage
shouldThrow { map.objectAt("name") }.message shouldBe notObjectMessage
map.arrayAt("name") shouldBe value
}
}
"arrays" - {
"primitive 3 value" {
val notObjectMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonObject"
val notArrayMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonArray"
val value = JsonPrimitive(3)
val array = JsonArray(listOf(JsonNull, value))
shouldThrow { value.string(1) }.message shouldBe notArrayMessage
shouldThrow { value.int(1) }.message shouldBe notArrayMessage
shouldThrow { value.long(1) }.message shouldBe notArrayMessage
shouldThrow { value.float(1) }.message shouldBe notArrayMessage
shouldThrow { value.double(1) }.message shouldBe notArrayMessage
shouldThrow { value.boolean(1) }.message shouldBe notArrayMessage
array.string(1) shouldBe "3"
array.int(1) shouldBe 3
array.long(1) shouldBe 3L
array.float(1) shouldBe 3f
array.double(1) shouldBe 3.0
shouldThrow { array.boolean(1) }.message shouldBe "3 does not represent a Boolean"
shouldThrow { array.objectAt(1) }.message shouldBe notObjectMessage
shouldThrow { array.arrayAt(1) }.message shouldBe notArrayMessage
}
"primitive 3.14 value" {
val notObjectMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonObject"
val notArrayMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonArray"
val value = JsonPrimitive(3.14)
val array = JsonArray(listOf(JsonNull, value))
shouldThrow { value.string(1) }.message shouldBe notArrayMessage
shouldThrow { value.int(1) }.message shouldBe notArrayMessage
shouldThrow { value.long(1) }.message shouldBe notArrayMessage
shouldThrow { value.float(1) }.message shouldBe notArrayMessage
shouldThrow { value.double(1) }.message shouldBe notArrayMessage
shouldThrow { value.boolean(1) }.message shouldBe notArrayMessage
array.string(1) shouldBe "3.14"
shouldThrow { array.int(1) }.message shouldBe "For input string: \"3.14\""
shouldThrow { array.long(1) }.message shouldBe "For input string: \"3.14\""
array.float(1) shouldBe 3.14f
array.double(1) shouldBe 3.14
shouldThrow { array.boolean(1) }.message shouldBe "3.14 does not represent a Boolean"
shouldThrow { array.objectAt(1) }.message shouldBe notObjectMessage
shouldThrow { array.arrayAt(1) }.message shouldBe notArrayMessage
}
"primitive string value" {
val notObjectMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonObject"
val notArrayMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonArray"
val value = JsonPrimitive("something else")
val array = JsonArray(listOf(JsonNull, value))
shouldThrow { value.string(1) }.message shouldBe notArrayMessage
shouldThrow { value.int(1) }.message shouldBe notArrayMessage
shouldThrow { value.long(1) }.message shouldBe notArrayMessage
shouldThrow { value.float(1) }.message shouldBe notArrayMessage
shouldThrow { value.double(1) }.message shouldBe notArrayMessage
shouldThrow { value.boolean(1) }.message shouldBe notArrayMessage
array.string(1) shouldBe "something else"
shouldThrow { array.int(1) }.message shouldBe "For input string: \"something else\""
shouldThrow { array.long(1) }.message shouldBe "For input string: \"something else\""
shouldThrow { array.float(1) }.message shouldBe "For input string: \"something else\""
shouldThrow { array.double(1) }.message shouldBe "For input string: \"something else\""
shouldThrow { array.boolean(1) }.message shouldBe "\"something else\" does not represent a Boolean"
shouldThrow { array.objectAt(1) }.message shouldBe notObjectMessage
shouldThrow { array.arrayAt(1) }.message shouldBe notArrayMessage
}
"primitive boolean value" {
val notObjectMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonObject"
val notArrayMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonArray"
val value = JsonPrimitive(true)
val array = JsonArray(listOf(JsonNull, value))
shouldThrow { value.string(1) }.message shouldBe notArrayMessage
shouldThrow { value.int(1) }.message shouldBe notArrayMessage
shouldThrow { value.long(1) }.message shouldBe notArrayMessage
shouldThrow { value.float(1) }.message shouldBe notArrayMessage
shouldThrow { value.double(1) }.message shouldBe notArrayMessage
shouldThrow { value.boolean(1) }.message shouldBe notArrayMessage
array.string(1) shouldBe "true"
shouldThrow { array.int(1) }.message shouldBe "For input string: \"true\""
shouldThrow { array.long(1) }.message shouldBe "For input string: \"true\""
shouldThrow { array.float(1) }.message shouldBe "For input string: \"true\""
shouldThrow { array.double(1) }.message shouldBe "For input string: \"true\""
array.boolean(1) shouldBe true
shouldThrow { array.objectAt(1) }.message shouldBe notObjectMessage
shouldThrow { array.arrayAt(1) }.message shouldBe notArrayMessage
}
"object value" {
val notPrimitiveMessage = "Element class kotlinx.serialization.json.JsonObject is not a JsonPrimitive"
val notArrayMessage = "Element class kotlinx.serialization.json.JsonObject is not a JsonArray"
val value = JsonObject(mapOf("inner" to JsonPrimitive("value")))
val array = JsonArray(listOf(JsonNull, value))
shouldThrow { array.string(1) }.message shouldBe notPrimitiveMessage
shouldThrow { array.int(1) }.message shouldBe notPrimitiveMessage
shouldThrow { array.long(1) }.message shouldBe notPrimitiveMessage
shouldThrow { array.float(1) }.message shouldBe notPrimitiveMessage
shouldThrow { array.double(1) }.message shouldBe notPrimitiveMessage
shouldThrow { array.boolean(1) }.message shouldBe notPrimitiveMessage
array.objectAt(1) shouldBe value
shouldThrow { array.arrayAt(1) }.message shouldBe notArrayMessage
}
"array value" {
val notPrimitiveMessage = "Element class kotlinx.serialization.json.JsonArray is not a JsonPrimitive"
val notObjectMessage = "Element class kotlinx.serialization.json.JsonArray is not a JsonObject"
val value = JsonArray(listOf(JsonPrimitive("value")))
val array = JsonArray(listOf(JsonNull, value))
shouldThrow { array.string(1) }.message shouldBe notPrimitiveMessage
shouldThrow { array.int(1) }.message shouldBe notPrimitiveMessage
shouldThrow { array.long(1) }.message shouldBe notPrimitiveMessage
shouldThrow { array.float(1) }.message shouldBe notPrimitiveMessage
shouldThrow { array.double(1) }.message shouldBe notPrimitiveMessage
shouldThrow { array.boolean(1) }.message shouldBe notPrimitiveMessage
shouldThrow { array.objectAt(1) }.message shouldBe notObjectMessage
array.arrayAt(1) shouldBe value
}
}
"pointer" - {
"primitive 3 value" {
val pointerNotFound = "value at pointer '#/name' could not be found"
val notObjectMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonObject"
val notArrayMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonArray"
val pointer = JsonPointer.parse("#/name")
val value = JsonPrimitive(3)
val map = JsonObject(mapOf("name" to value))
shouldThrow { value.string(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.int(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.long(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.float(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.double(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.boolean(pointer) }.message shouldBe pointerNotFound
map.string(pointer) shouldBe "3"
map.int(pointer) shouldBe 3
map.long(pointer) shouldBe 3L
map.float(pointer) shouldBe 3f
map.double(pointer) shouldBe 3.0
shouldThrow { map.boolean(pointer) }.message shouldBe "3 does not represent a Boolean"
shouldThrow { map.objectAt(pointer) }.message shouldBe notObjectMessage
shouldThrow { map.arrayAt(pointer) }.message shouldBe notArrayMessage
}
"primitive 3.14 value" {
val pointerNotFound = "value at pointer '#/name' could not be found"
val notObjectMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonObject"
val notArrayMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonArray"
val pointer = JsonPointer.parse("#/name")
val value = JsonPrimitive(3.14)
val map = JsonObject(mapOf("name" to value))
shouldThrow { value.string(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.int(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.long(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.float(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.double(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.boolean(pointer) }.message shouldBe pointerNotFound
map.string(pointer) shouldBe "3.14"
shouldThrow { map.int(pointer) }.message shouldBe "For input string: \"3.14\""
shouldThrow { map.long(pointer) }.message shouldBe "For input string: \"3.14\""
map.float(pointer) shouldBe 3.14f
map.double(pointer) shouldBe 3.14
shouldThrow { map.boolean(pointer) }.message shouldBe "3.14 does not represent a Boolean"
shouldThrow { map.objectAt(pointer) }.message shouldBe notObjectMessage
shouldThrow { map.arrayAt(pointer) }.message shouldBe notArrayMessage
}
"primitive string value" {
val pointerNotFound = "value at pointer '#/name' could not be found"
val notObjectMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonObject"
val notArrayMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonArray"
val pointer = JsonPointer.parse("#/name")
val value = JsonPrimitive("something else")
val map = JsonObject(mapOf("name" to value))
shouldThrow { value.string(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.int(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.long(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.float(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.double(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.boolean(pointer) }.message shouldBe pointerNotFound
map.string(pointer) shouldBe "something else"
shouldThrow { map.int(pointer) }.message shouldBe "For input string: \"something else\""
shouldThrow { map.long(pointer) }.message shouldBe "For input string: \"something else\""
shouldThrow { map.float(pointer) }.message shouldBe "For input string: \"something else\""
shouldThrow { map.double(pointer) }.message shouldBe "For input string: \"something else\""
shouldThrow { map.boolean(pointer) }.message shouldBe "\"something else\" does not represent a Boolean"
shouldThrow { map.objectAt(pointer) }.message shouldBe notObjectMessage
shouldThrow { map.arrayAt(pointer) }.message shouldBe notArrayMessage
}
"primitive boolean value" {
val pointerNotFound = "value at pointer '#/name' could not be found"
val notObjectMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonObject"
val notArrayMessage = "Element class kotlinx.serialization.json.JsonLiteral is not a JsonArray"
val pointer = JsonPointer.parse("#/name")
val value = JsonPrimitive(true)
val map = JsonObject(mapOf("name" to value))
shouldThrow { value.string(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.int(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.long(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.float(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.double(pointer) }.message shouldBe pointerNotFound
shouldThrow { value.boolean(pointer) }.message shouldBe pointerNotFound
map.string(pointer) shouldBe "true"
shouldThrow { map.int(pointer) }.message shouldBe "For input string: \"true\""
shouldThrow { map.long(pointer) }.message shouldBe "For input string: \"true\""
shouldThrow { map.float(pointer) }.message shouldBe "For input string: \"true\""
shouldThrow { map.double(pointer) }.message shouldBe "For input string: \"true\""
map.boolean(pointer) shouldBe true
shouldThrow { map.objectAt(pointer) }.message shouldBe notObjectMessage
shouldThrow { map.arrayAt(pointer) }.message shouldBe notArrayMessage
}
"object value" {
val notPrimitiveMessage = "Element class kotlinx.serialization.json.JsonObject is not a JsonPrimitive"
val notArrayMessage = "Element class kotlinx.serialization.json.JsonObject is not a JsonArray"
val pointer = JsonPointer.parse("#/name")
val value = JsonObject(mapOf("inner" to JsonPrimitive("value")))
val map = JsonObject(mapOf("name" to value))
shouldThrow { map.string(pointer) }.message shouldBe notPrimitiveMessage
shouldThrow { map.int(pointer) }.message shouldBe notPrimitiveMessage
shouldThrow { map.long(pointer) }.message shouldBe notPrimitiveMessage
shouldThrow { map.float(pointer) }.message shouldBe notPrimitiveMessage
shouldThrow { map.double(pointer) }.message shouldBe notPrimitiveMessage
shouldThrow { map.boolean(pointer) }.message shouldBe notPrimitiveMessage
map.objectAt(pointer) shouldBe value
shouldThrow { map.arrayAt(pointer) }.message shouldBe notArrayMessage
}
"array value" {
val notPrimitiveMessage = "Element class kotlinx.serialization.json.JsonArray is not a JsonPrimitive"
val notObjectMessage = "Element class kotlinx.serialization.json.JsonArray is not a JsonObject"
val pointer = JsonPointer.parse("#/name")
val value = JsonArray(listOf(JsonPrimitive("value")))
val map = JsonObject(mapOf("name" to value))
shouldThrow { map.string(pointer) }.message shouldBe notPrimitiveMessage
shouldThrow { map.int(pointer) }.message shouldBe notPrimitiveMessage
shouldThrow { map.long(pointer) }.message shouldBe notPrimitiveMessage
shouldThrow { map.float(pointer) }.message shouldBe notPrimitiveMessage
shouldThrow { map.double(pointer) }.message shouldBe notPrimitiveMessage
shouldThrow { map.boolean(pointer) }.message shouldBe notPrimitiveMessage
shouldThrow { map.objectAt(pointer) }.message shouldBe notObjectMessage
map.arrayAt(pointer) shouldBe value
}
}
})
© 2015 - 2024 Weber Informatics LLC | Privacy Policy