commonMain.AbstractSerializationTest.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ktor-serialization-kotlinx-tests-jvm Show documentation
Show all versions of ktor-serialization-kotlinx-tests-jvm Show documentation
Ktor is a framework for quickly creating web applications in Kotlin with minimal effort.
The newest version!
/*
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package io.ktor.serialization.kotlinx.test
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.serialization.*
import io.ktor.serialization.kotlinx.*
import io.ktor.test.dispatcher.*
import io.ktor.util.reflect.*
import io.ktor.utils.io.*
import io.ktor.utils.io.charsets.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.io.*
import kotlinx.serialization.*
import kotlin.test.*
@Serializable
internal data class User(val id: Long, val login: String)
@Serializable
internal data class Photo(val id: Long, val path: String)
@Serializable
public data class GithubProfile(
val login: String,
val id: Int,
val name: String
)
public abstract class AbstractSerializationTest {
protected abstract val defaultContentType: ContentType
protected abstract val defaultSerializationFormat: T
protected abstract fun assertEquals(
expectedAsJson: String,
actual: ByteArray,
format: T,
): Boolean
@Test
public fun testMapsElements() {
testSuspend {
val testSerializer = KotlinxSerializationConverter(defaultSerializationFormat)
testSerializer.testSerialize(
mapOf(
"a" to "1",
"b" to "2"
)
).let { result ->
assertEquals("""{"a":"1","b":"2"}""", result, defaultSerializationFormat)
}
testSerializer.testSerialize(
mapOf(
"a" to "1",
"b" to null
)
).let { result ->
assertEquals("""{"a":"1","b":null}""", result, defaultSerializationFormat)
}
testSerializer.testSerialize(
mapOf(
"a" to "1",
null to "2"
)
).let { result ->
assertEquals("""{"a":"1",null:"2"}""", result, defaultSerializationFormat)
}
// this is not yet supported
assertFails {
testSerializer.testSerialize
© 2015 - 2025 Weber Informatics LLC | Privacy Policy