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

commonTest.kotlinx.serialization.test.TestingFramework.kt Maven / Gradle / Ivy

There is a newer version: 1.7.3
Show newest version
/*
 * Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.serialization.test

import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlin.test.*


inline fun  assertStringFormAndRestored(
    expected: String,
    original: T,
    serializer: KSerializer,
    format: StringFormat = Json,
    printResult: Boolean = false
) {
    val string = format.encodeToString(serializer, original)
    if (printResult) println("[Serialized form] $string")
    assertEquals(expected, string)
    val restored = format.decodeFromString(serializer, string)
    if (printResult) println("[Restored form] $restored")
    assertEquals(original, restored)
}

inline fun  StringFormat.assertStringFormAndRestored(
    expected: String,
    original: T,
    serializer: KSerializer,
    printResult: Boolean = false
) {
    val string = this.encodeToString(serializer, original)
    if (printResult) println("[Serialized form] $string")
    assertEquals(expected, string)
    val restored = this.decodeFromString(serializer, string)
    if (printResult) println("[Restored form] $restored")
    assertEquals(original, restored)
}

fun  assertSerializedAndRestored(
    original: T,
    serializer: KSerializer,
    format: StringFormat = Json,
    printResult: Boolean = false
) {
    if (printResult) println("[Input] $original")
    val string = format.encodeToString(serializer, original)
    if (printResult) println("[Serialized form] $string")
    val restored = format.decodeFromString(serializer, string)
    if (printResult) println("[Restored form] $restored")
    assertEquals(original, restored)
}

inline fun  assertFailsWithMessage(
    message: String,
    assertionMessage: String? = null,
    block: () -> Unit
) {
    val exception = assertFailsWith(T::class, assertionMessage, block)
    assertTrue(
        exception.message!!.contains(message),
        "Expected message '${exception.message}' to contain substring '$message'"
    )
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy