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

com.github.mvysny.kaributesting.v10.Utils.kt Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
package com.github.mvysny.kaributesting.v10

import elemental.json.Json
import elemental.json.JsonArray
import elemental.json.JsonObject
import elemental.json.JsonValue
import java.io.ByteArrayOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.io.Serializable
import java.lang.IllegalArgumentException
import java.net.URL
import kotlin.test.expect

fun Serializable.serializeToBytes(): ByteArray = ByteArrayOutputStream().use { it -> ObjectOutputStream(it).writeObject(this); it }.toByteArray()
inline fun  ByteArray.deserialize(): T = ObjectInputStream(inputStream()).readObject() as T
inline fun  T.serializeDeserialize() = serializeToBytes().deserialize()

val IntRange.size: Int get() = (endInclusive + 1 - start).coerceAtLeast(0)

/**
 * Expects that [actual] list of objects matches [expected] list of objects. Fails otherwise.
 */
fun  expectList(vararg expected: T, actual: ()->List) = expect(expected.toList(), actual)

internal fun URL.readJson(): JsonObject = Json.parse(readText())

/**
 * Adds a [value] at the end of the array.
 */
fun JsonArray.add(value: JsonValue) {
    set(length(), value)
}

private fun JsonObject.put(key: String, value: Any) {
    when (value) {
        is JsonValue -> put(key, value)
        is String -> put(key, value)
        is Double -> put(key, value)
        is Boolean -> put(key, value)
        else -> throw IllegalArgumentException("Unsupported value type ${value.javaClass} for $value")
    }
}

internal fun jsonCreateObject(vararg contents: Pair): JsonObject = Json.createObject().apply {
    contents.forEach { put(it.first, it.second) }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy