How to use beMultipleOf method of io.kotest.matchers.longs.multipleOf class

Best Kotest code snippet using io.kotest.matchers.longs.multipleOf.beMultipleOf

parse.kt

Source:parse.kt Github

copy

Full Screen

...5import io.kotest.matchers.doubles.beGreaterThan6import io.kotest.matchers.doubles.beGreaterThanOrEqualTo7import io.kotest.matchers.doubles.beLessThan8import io.kotest.matchers.doubles.beLessThanOrEqualTo9import io.kotest.matchers.doubles.beMultipleOf10import io.kotest.matchers.longs.beGreaterThan11import io.kotest.matchers.longs.beGreaterThanOrEqualTo12import io.kotest.matchers.longs.beLessThan13import io.kotest.matchers.longs.beLessThanOrEqualTo14import io.kotest.matchers.longs.beMultipleOf15import io.kotest.matchers.string.haveMaxLength16import io.kotest.matchers.string.haveMinLength17import io.kotest.matchers.string.match18import kotlinx.serialization.DeserializationStrategy19import kotlinx.serialization.KSerializer20import kotlinx.serialization.decodeFromString21import kotlinx.serialization.descriptors.SerialDescriptor22import kotlinx.serialization.descriptors.buildClassSerialDescriptor23import kotlinx.serialization.descriptors.element24import kotlinx.serialization.encoding.CompositeDecoder25import kotlinx.serialization.encoding.Decoder26import kotlinx.serialization.encoding.Encoder27import kotlinx.serialization.encoding.decodeStructure28import kotlinx.serialization.json.Json29import kotlinx.serialization.json.JsonContentPolymorphicSerializer30import kotlinx.serialization.json.JsonElement31import kotlinx.serialization.json.JsonNull32import kotlinx.serialization.json.JsonObject33import kotlinx.serialization.json.jsonObject34import kotlinx.serialization.json.jsonPrimitive35private val schemaJsonConfig = Json {36 ignoreUnknownKeys = true37 classDiscriminator = "type"38}39/**40 * Parses a subset of JSON Schema into [JsonSchemaElement] which can be used to verify a json document with41 * [shouldMatchSchema]42 */43@ExperimentalKotest44fun parseSchema(jsonSchema: String): JsonSchema =45 JsonSchema(root = schemaJsonConfig.decodeFromString(SchemaDeserializer, jsonSchema))46@ExperimentalKotest47internal object SchemaDeserializer : JsonContentPolymorphicSerializer<JsonSchemaElement>(JsonSchemaElement::class) {48 override fun selectDeserializer(element: JsonElement): DeserializationStrategy<out JsonSchemaElement> {49 return when (val type = element.jsonObject.get("type")?.jsonPrimitive?.content) {50 "array" -> JsonSchema.JsonArray.serializer()51 "object" -> JsonSchema.JsonObject.serializer()52 "string" -> JsonSchemaStringSerializer53 "integer" -> JsonSchemaIntegerSerializer54 "number" -> JsonSchemaNumberSerializer55 "boolean" -> JsonSchema.JsonBoolean.serializer()56 "null" -> JsonSchema.Null.serializer()57 else -> error("Unknown type: $type")58 }59 }60}61private infix fun <T> Matcher<T>?.and(other: Matcher<T>) =62 if (this != null) this and other else other63@ExperimentalKotest64internal object JsonSchemaStringSerializer : KSerializer<JsonSchema.JsonString> {65 override fun deserialize(decoder: Decoder): JsonSchema.JsonString =66 decoder.decodeStructure(descriptor) {67 var matcher: Matcher<String>? = null68 while (true) {69 when (val index = decodeElementIndex(descriptor)) {70 1 -> matcher = matcher and haveMinLength(decodeIntElement(descriptor, index))71 2 -> matcher = matcher and haveMaxLength(decodeIntElement(descriptor, index))72 3 -> matcher = matcher and match(decodeStringElement(descriptor, index).toRegex())73 // Formats: https://json-schema.org/understanding-json-schema/reference/string.html#built-in-formats74 // TODO: Map formats to matchers75 4 -> println("Formats are currently not supported")76 CompositeDecoder.DECODE_DONE -> break77 }78 }79 JsonSchema.JsonString(matcher)80 }81 override val descriptor = buildClassSerialDescriptor("JsonSchema.JsonString") {82 element<String>("type")83 element<Int>("minLength", isOptional = true)84 element<Int>("maxLength", isOptional = true)85 element<String>("pattern", isOptional = true)86 element<String>("format", isOptional = true)87 }88 override fun serialize(encoder: Encoder, value: JsonSchema.JsonString) {89 TODO("Serialization of JsonSchema not supported atm")90 }91}92@ExperimentalKotest93internal object JsonSchemaIntegerSerializer : KSerializer<JsonSchema.JsonInteger> {94 override fun deserialize(decoder: Decoder): JsonSchema.JsonInteger =95 decoder.decodeStructure(descriptor) {96 var matcher: Matcher<Long>? = null97 while (true) {98 when (val index = decodeElementIndex(descriptor)) {99 1 -> matcher = matcher and beMultipleOf(decodeLongElement(descriptor, index))100 2 -> matcher = matcher and beGreaterThanOrEqualTo(decodeLongElement(descriptor, index))101 3 -> matcher = matcher and beGreaterThan(decodeLongElement(descriptor, index))102 4 -> matcher = matcher and beLessThanOrEqualTo(decodeLongElement(descriptor, index))103 5 -> matcher = matcher and beLessThan(decodeLongElement(descriptor, index))104 CompositeDecoder.DECODE_DONE -> break105 }106 }107 JsonSchema.JsonInteger(matcher)108 }109 override val descriptor = buildClassSerialDescriptor("JsonSchema.JsonInteger") {110 element<String>("type")111 element<Long>("multipleOf", isOptional = true)112 element<Long>("minimum", isOptional = true)113 element<Long>("exclusiveMinimum", isOptional = true)114 element<Long>("maximum", isOptional = true)115 element<Long>("exclusiveMaximum", isOptional = true)116 }117 override fun serialize(encoder: Encoder, value: JsonSchema.JsonInteger) {118 TODO("Not yet implemented")119 }120}121@ExperimentalKotest122internal object JsonSchemaNumberSerializer : KSerializer<JsonSchema.JsonDecimal> {123 override fun deserialize(decoder: Decoder): JsonSchema.JsonDecimal =124 decoder.decodeStructure(descriptor) {125 var matcher: Matcher<Double>? = null126 while (true) {127 when (val index = decodeElementIndex(descriptor)) {128 1 -> matcher = matcher and beMultipleOf(decodeDoubleElement(descriptor, index))129 2 -> matcher = matcher and beGreaterThanOrEqualTo(decodeDoubleElement(descriptor, index))130 3 -> matcher = matcher and beGreaterThan(decodeDoubleElement(descriptor, index))131 4 -> matcher = matcher and beLessThanOrEqualTo(decodeDoubleElement(descriptor, index))132 5 -> matcher = matcher and beLessThan(decodeDoubleElement(descriptor, index))133 CompositeDecoder.DECODE_DONE -> break134 }135 }136 JsonSchema.JsonDecimal(matcher)137 }138 override val descriptor = buildClassSerialDescriptor("JsonSchema.JsonDecimal") {139 element<String>("type")140 element<Double>("multipleOf", isOptional = true)141 element<Double>("minimum", isOptional = true)142 element<Double>("exclusiveMinimum", isOptional = true)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Kotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in multipleOf

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful