commonTest.kotlinx.serialization.TestUtils.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlinx-serialization-protobuf-metadata Show documentation
Show all versions of kotlinx-serialization-protobuf-metadata Show documentation
Kotlin multiplatform serialization runtime library
The newest version!
/*
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.serialization
import kotlin.test.*
internal inline fun assertSerializedToBinaryAndRestored(
original: T,
serializer: KSerializer,
format: BinaryFormat,
printResult: Boolean = false,
hexResultToCheck: String? = null
) {
val bytes = format.encodeToByteArray(serializer, original)
val hexString = HexConverter.printHexBinary(bytes, lowerCase = true)
if (printResult) {
println("[Serialized form] $hexString")
}
if (hexResultToCheck != null) {
assertEquals(
hexResultToCheck.toLowerCase(),
hexString,
"Expected serialized binary to be equal in hex representation"
)
}
val restored = format.decodeFromByteArray(serializer, bytes)
if (printResult) println("[Restored form] $restored")
assertEquals(original, restored)
}