1 /*
2 * Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3 */
4
5 package kotlinx.serialization
6
7 import kotlin.test.*
8
assertSerializedToBinaryAndRestorednull9 internal inline fun <reified T : Any> assertSerializedToBinaryAndRestored(
10 original: T,
11 serializer: KSerializer<T>,
12 format: BinaryFormat,
13 printResult: Boolean = false,
14 hexResultToCheck: String? = null
15 ) {
16 val bytes = format.encodeToByteArray(serializer, original)
17 val hexString = HexConverter.printHexBinary(bytes, lowerCase = true)
18 if (printResult) {
19 println("[Serialized form] $hexString")
20 }
21 if (hexResultToCheck != null) {
22 assertEquals(
23 hexResultToCheck.lowercase(),
24 hexString,
25 "Expected serialized binary to be equal in hex representation"
26 )
27 }
28 val restored = format.decodeFromByteArray(serializer, bytes)
29 if (printResult) println("[Restored form] $restored")
30 assertEquals(original, restored)
31 }
32
assertFailsWithMessagenull33 inline fun <reified T : Throwable> assertFailsWithMessage(message: String, block: () -> Unit) {
34 val exception = assertFailsWith(T::class, null, block)
35 assertTrue(exception.message!!.contains(message), "Expected message '${exception.message}' to contain substring '$message'")
36 }
37