All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonTest.kotlinx.serialization.UnknownElementIndexTest.kt Maven / Gradle / Ivy

There is a newer version: 1.7.3
Show newest version
package kotlinx.serialization

import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.CompositeDecoder.Companion.UNKNOWN_NAME
import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.*
import kotlin.test.Test
import kotlin.test.assertFailsWith

class UnknownElementIndexTest {
    enum class Choices { A, B, C }

    @Serializable
    data class Holder(val c: Choices)

    class MalformedReader : AbstractDecoder() {
        override val serializersModule: SerializersModule = EmptySerializersModule

        override fun decodeElementIndex(descriptor: SerialDescriptor): Int {
            return UNKNOWN_NAME
        }
    }

    @Test
    fun testCompilerComplainsAboutIncorrectIndex() {
        assertFailsWith(SerializationException::class) {
            MalformedReader().decodeSerializableValue(Holder.serializer())
        }
    }

    @Test
    fun testErrorMessage() {
        val message = "kotlinx.serialization.UnknownElementIndexTest.Choices does not contain element with name 'D'"
        assertFailsWith(SerializationException::class, message) {
            Json.decodeFromString(Holder.serializer(), """{"c":"D"}""")
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy