commonMain.io.ks3.test.SerializerTestFactory.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ks3-test-jvm Show documentation
Show all versions of ks3-test-jvm Show documentation
KotlinX Serialization standard serializers
The newest version!
package io.ks3.test
import io.kotest.core.spec.style.funSpec
import io.kotest.matchers.shouldBe
import io.kotest.property.Exhaustive
import io.kotest.property.Gen
import io.kotest.property.checkAll
import io.kotest.property.exhaustive.cartesianPairs
import io.kotest.property.exhaustive.exhaustive
import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.Json
/**
* Creates a test factory for [serializer].
*/
inline fun generateSerializerTests(
serializer: KSerializer,
generator: Gen,
crossinline nameFn: () -> String = { "Encodes and decodes values back to original form" },
crossinline assertion: T.(T) -> Unit = { original -> this shouldBe original },
) = funSpec {
val (encoders, decoders) = Json.generateEncoders(serializer)
test(nameFn()) {
checkAll(generator, Exhaustive.cartesianPairs(encoders.exhaustive(), decoders.exhaustive())) { value, (encode, decode) ->
value.encode().decode().assertion(value)
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy