commonTest.kotlinx.serialization.json.JsonParserFailureModesTest.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlinx-serialization-json
Show all versions of kotlinx-serialization-json
Kotlin multiplatform serialization runtime library
/*
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.serialization.json
import kotlinx.serialization.*
import kotlinx.serialization.json.internal.*
import kotlinx.serialization.test.*
import kotlin.test.*
class JsonParserFailureModesTest : JsonTestBase() {
@Serializable
data class Holder(
val id: Long
)
@Test
fun testFailureModes() = parametrizedTest {
assertFailsWith {
default.decodeFromString(
Holder.serializer(),
"""{"id": "}""",
it
)
}
assertFailsWith {
default.decodeFromString(
Holder.serializer(),
"""{"id": ""}""",
it
)
}
assertFailsWith {
default.decodeFromString(
Holder.serializer(),
"""{"id":a}""",
it
)
}
assertFailsWith {
default.decodeFromString(
Holder.serializer(),
"""{"id":2.0}""",
it
)
}
assertFailsWith {
default.decodeFromString(
Holder.serializer(),
"""{"id2":2}""",
it
)
}
// 9223372036854775807 is Long.MAX_VALUE
assertFailsWith {
default.decodeFromString(
Holder.serializer(),
"""{"id":${Long.MAX_VALUE}""" + "00" + "}",
it
)
}
// -9223372036854775808 is Long.MIN_VALUE
assertFailsWith {
default.decodeFromString(
Holder.serializer(),
"""{"id":9223372036854775808}""",
it
)
}
assertFailsWith { default.decodeFromString(Holder.serializer(), """{"id"}""", it) }
assertFailsWith { default.decodeFromString(Holder.serializer(), """{"id}""", it) }
assertFailsWith { default.decodeFromString(Holder.serializer(), """{"i}""", it) }
assertFailsWith { default.decodeFromString(Holder.serializer(), """{"}""", it) }
assertFailsWithMissingField { default.decodeFromString(Holder.serializer(), """{}""", it) }
assertFailsWith { default.decodeFromString(Holder.serializer(), """{""", it) }
assertFailsWith { default.decodeFromString(Holder.serializer(), """}""", it) }
assertFailsWith { default.decodeFromString(Holder.serializer(), """{""", it) }
}
@Serializable
class BooleanHolder(val b: Boolean)
@Test
fun testBoolean() = parametrizedTest {
assertFailsWith {
default.decodeFromString(
BooleanHolder.serializer(),
"""{"b": fals}""",
it
)
}
assertFailsWith {
default.decodeFromString(
BooleanHolder.serializer(),
"""{"b": 123}""",
it
)
}
}
@Serializable
class PrimitiveHolder(
val b: Byte = 0, val s: Short = 0, val i: Int = 0
)
@Test
fun testOverflow() = parametrizedTest {
// Byte overflow
assertFailsWith { default.decodeFromString("""{"b": 128}""", it) }
// Short overflow
assertFailsWith { default.decodeFromString("""{"s": 32768}""", it) }
// Int overflow
assertFailsWith {
default.decodeFromString(
"""{"i": 2147483648}""",
it
)
}
}
@Test
fun testNoOverflow() = parametrizedTest {
default.decodeFromString("""{"b": ${Byte.MAX_VALUE}}""", it)
default.decodeFromString("""{"b": ${Byte.MIN_VALUE}}""", it)
default.decodeFromString("""{"s": ${Short.MAX_VALUE}}""", it)
default.decodeFromString("""{"s": ${Short.MIN_VALUE}}""", it)
default.decodeFromString("""{"i": ${Int.MAX_VALUE}}""", it)
default.decodeFromString("""{"i": ${Int.MIN_VALUE}}""", it)
default.decodeFromString("""{"id": ${Long.MIN_VALUE.toString()}}""", it)
default.decodeFromString("""{"id": ${Long.MAX_VALUE.toString()}}""", it)
}
@Test
fun testInvalidNumber() = parametrizedTest {
assertFailsWith { default.decodeFromString("""{"id":-}""", it) }
assertFailsWith { default.decodeFromString("""{"id":+}""", it) }
assertFailsWith { default.decodeFromString("""{"id":--}""", it) }
assertFailsWith { default.decodeFromString("""{"id":1-1}""", it) }
assertFailsWith { default.decodeFromString("""{"id":0-1}""", it) }
assertFailsWith { default.decodeFromString("""{"id":0-}""", it) }
assertFailsWith { default.decodeFromString("""{"id":a}""", it) }
assertFailsWith { default.decodeFromString("""{"id":-a}""", it) }
}
@Serializable
data class BooleanWrapper(val b: Boolean)
@Serializable
data class StringWrapper(val s: String)
@Test
fun testUnexpectedNull() = parametrizedTest {
assertFailsWith { default.decodeFromString("""{"b":{"b":"b"}}""", it) }
assertFailsWith { default.decodeFromString("""{"b":null}""", it) }
assertFailsWith { default.decodeFromString("""{"s":{"s":"s"}}""", it) }
assertFailsWith { default.decodeFromString("""{"s":null}""", it) }
}
}