Best Kotest code snippet using io.kotest.matchers.doubles.Multiple.beMultipleOf
parse.kt
Source:parse.kt
...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)...
SchemaWithMatcherTest.kt
Source:SchemaWithMatcherTest.kt
...3import io.kotest.core.spec.style.FunSpec4import io.kotest.matchers.and5import io.kotest.matchers.doubles.beGreaterThanOrEqualTo6import io.kotest.matchers.doubles.beLessThanOrEqualTo7import io.kotest.matchers.doubles.beMultipleOf8import io.kotest.matchers.ints.beEven9import io.kotest.matchers.ints.beInRange10import io.kotest.matchers.longs.beInRange11import io.kotest.matchers.shouldBe12import io.kotest.matchers.string.haveMaxLength13import io.kotest.matchers.string.haveMinLength14import io.kotest.matchers.types.beInstanceOf15class SchemaWithMatcherTest : FunSpec(16 {17 test("Even numbers") {18 val evenNumbers = jsonSchema { number { beMultipleOf(2.0) } }19 "2" shouldMatchSchema evenNumbers20 shouldFail { "3" shouldMatchSchema evenNumbers }21 .message shouldBe """22 $ => 3.0 should be multiple of 2.023 """.trimIndent()24 }25 context("smoke") {26 val schema = jsonSchema {27 array {28 obj {29 withProperty("name") { string { haveMinLength(3) and haveMaxLength(20) } }30 withProperty("age") { number { beGreaterThanOrEqualTo(0.0) and beLessThanOrEqualTo(120.0) } }31 }32 }...
Multiple.kt
Source:Multiple.kt
...7/**8 * Beware, has no tolerance handling so will fail for numbers where the orders of magnitude vary greatly.9 */10@ExperimentalKotest11infix fun Double?.shouldBeMultipleOf(other: Double) = this should beMultipleOf(other)12/**13 * Beware, has no tolerance handling so will fail for numbers where the orders of magnitude vary greatly.14 */15@ExperimentalKotest16fun beMultipleOf(other: Double) = Matcher<Double?> { value ->17 MatcherResult(18 value != null && value % other == 0.0,19 { "${value} should be multiple of ${other}" },20 { "${value} should not be multiple of ${other}" }21 )22}...
beMultipleOf
Using AI Code Generation
1result should beMultipleOf(1.0)2result should beMultipleOf(1.0)3result should beMultipleOf(1.0)4result should beMultipleOf(1.0)5result should beMultipleOf(1.0)6result should beMultipleOf(1.0)7result should beMultipleOf(1.0)8result should beMultipleOf(1.0)9result should beMultipleOf(1.0)10result should beMultipleOf(1.0)11result should beMultipleOf(1.0)12result should beMultipleOf(1.0)13result should beMultipleOf(1.0)
beMultipleOf
Using AI Code Generation
1import io.kotest.matchers.doubles.beMultipleOf2import io.kotest.matchers.doubles.beLessThan3import io.kotest.matchers.doubles.beLessThanOrEqual4import io.kotest.matchers.doubles.beGreaterThan5import io.kotest.matchers.doubles.beGreaterThanOrEqual6import io.kotest.matchers.doubles.beBetween7import io.kotest.matchers.doubles.beCloseTo8import io.kotest.matchers.doubles.bePositive91.0 should bePositive()
beMultipleOf
Using AI Code Generation
1fun `should be multiple of 2`(){2result.shouldBeMultipleOf(2.0)3}4fun `should be multiple of 2`(){5result.shouldBeMultipleOf(2.0)6}7fun `should be multiple of 2`(){8result.shouldBeMultipleOf(2.0)9}10fun `should be multiple of 2`(){11result.shouldBeMultipleOf(2.0)12}13fun `should be multiple of 2`(){14result.shouldBeMultipleOf(2.0)15}16fun `should be multiple of 2`(){17result.shouldBeMultipleOf(2.0)18}19fun `should be multiple of 2`(){20result.shouldBeMultipleOf(2.0)21}22fun `should be multiple of 2`(){23result.shouldBeMultipleOf(2.0)24}25fun `should be multiple of 2`(){26result.shouldBeMultipleOf(2.0)27}28fun `should be multiple of 2`(){
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!