Best Kotest code snippet using io.kotest.matchers.doubles.Multiple
DoubleMatchersTest.kt
Source:DoubleMatchersTest.kt
...20import io.kotest.matchers.doubles.shouldBeGreaterThan21import io.kotest.matchers.doubles.shouldBeGreaterThanOrEqual22import io.kotest.matchers.doubles.shouldBeLessThan23import io.kotest.matchers.doubles.shouldBeLessThanOrEqual24import io.kotest.matchers.doubles.shouldBeMultipleOf25import io.kotest.matchers.doubles.shouldBeNaN26import io.kotest.matchers.doubles.shouldBeNegative27import io.kotest.matchers.doubles.shouldBeNegativeInfinity28import io.kotest.matchers.doubles.shouldBePositive29import io.kotest.matchers.doubles.shouldBePositiveInfinity30import io.kotest.matchers.doubles.shouldBeZero31import io.kotest.matchers.doubles.shouldNotBeBetween32import io.kotest.matchers.doubles.shouldNotBeGreaterThan33import io.kotest.matchers.doubles.shouldNotBeGreaterThanOrEqual34import io.kotest.matchers.doubles.shouldNotBeLessThan35import io.kotest.matchers.doubles.shouldNotBeLessThanOrEqual36import io.kotest.matchers.doubles.shouldNotBeNaN37import io.kotest.matchers.doubles.shouldNotBeNegative38import io.kotest.matchers.doubles.shouldNotBeNegativeInfinity39import io.kotest.matchers.doubles.shouldNotBePositive40import io.kotest.matchers.doubles.shouldNotBePositiveInfinity41import io.kotest.matchers.doubles.shouldNotBeZero42import io.kotest.matchers.should43import io.kotest.matchers.shouldBe44import io.kotest.matchers.shouldNot45import io.kotest.matchers.shouldNotBe46import io.kotest.property.arbitrary.filterNot47import io.kotest.property.checkAll48import kotlin.Double.Companion.MAX_VALUE49import kotlin.Double.Companion.MIN_VALUE50import kotlin.Double.Companion.NEGATIVE_INFINITY51import kotlin.Double.Companion.NaN52import kotlin.Double.Companion.POSITIVE_INFINITY53import kotlin.math.absoluteValue54class DoubleMatchersTest : FreeSpec() {55 init {56 "Between matcher" - {57 "Every numeric double that is not Double.MAX_VALUE" - {58 "Should match between" - {59 "When it's equal to the first number of the range" - {60 "With tolerance" {61 checkAll(100, nonMinNorMaxValueDoubles) {62 it.shouldMatchBetween(it, it.slightlyGreater(), it.toleranceValue())63 }64 }65 "Without tolerance" {66 checkAll(100, nonMinNorMaxValueDoubles) {67 it.shouldMatchBetween(it, it.slightlyGreater(), 0.0)68 }69 }70 }71 "When it's between the first number of the range and the last one" - {72 "With tolerance" {73 checkAll(100, nonMinNorMaxValueDoubles) {74 it.shouldMatchBetween(it.slightlySmaller(), it.slightlyGreater(), it.toleranceValue())75 }76 }77 "Without tolerance" {78 checkAll(100, nonMinNorMaxValueDoubles) {79 it.shouldMatchBetween(it.slightlySmaller(), it.slightlyGreater(), 0.0)80 }81 }82 }83 "When it's equal to the last number of the range" - {84 "With tolerance" {85 checkAll(100, nonMinNorMaxValueDoubles) {86 it.shouldMatchBetween(it.slightlySmaller(), it, it.toleranceValue())87 }88 }89 "Without tolerance" {90 checkAll(100, nonMinNorMaxValueDoubles) {91 it.shouldMatchBetween(it.slightlySmaller(), it, 0.0)92 }93 }94 }95 }96 "Should not match between" - {97 "When it's smaller than the first number of the range" - {98 "With tolerance" {99 checkAll(100, nonMinNorMaxValueDoubles) {100 it.shouldNotMatchBetween(it.slightlyGreater(), it.muchGreater(), it.toleranceValue())101 }102 }103 "Without tolerance" {104 checkAll(100, nonMinNorMaxValueDoubles) {105 it.shouldNotMatchBetween(it.slightlyGreater(), it.muchGreater(), 0.0)106 }107 }108 }109 "When it's bigger than the last number of the range" - {110 "With tolerance" {111 checkAll(100, nonMinNorMaxValueDoubles) {112 it.shouldNotMatchBetween(it.muchSmaller(), it.slightlySmaller(), it.toleranceValue())113 }114 }115 "Without tolerance" {116 checkAll(100, nonMinNorMaxValueDoubles) {117 it.shouldNotMatchBetween(it.muchSmaller(), it.slightlySmaller(), 0.0)118 }119 }120 }121 }122 }123 }124 "Less than matcher" - {125 "Every numeric double" - {126 "Should be less than" - {127 "Numbers bigger than itself" {128 checkAll(100, nonMinNorMaxValueDoubles) {129 it shouldMatchLessThan it.slightlyGreater()130 it shouldMatchLessThan it.muchGreater()131 }132 }133 "Infinity" {134 checkAll(100, nonMinNorMaxValueDoubles) {135 it shouldMatchLessThan POSITIVE_INFINITY136 }137 }138 }139 "Should not be less than" - {140 "Itself" {141 checkAll(100, nonMinNorMaxValueDoubles) {142 it shouldNotMatchLessThan it143 }144 }145 "Numbers smaller than itself" {146 checkAll(100, nonMinNorMaxValueDoubles) {147 it shouldNotMatchLessThan it.slightlySmaller()148 it shouldNotMatchLessThan it.muchSmaller()149 }150 }151 "Negative Infinity" {152 checkAll(100, nonMinNorMaxValueDoubles) {153 it shouldNotMatchLessThan it154 }155 }156 "NaN" {157 checkAll(100, nonMinNorMaxValueDoubles) {158 it shouldNotMatchLessThan NaN159 }160 }161 }162 }163 "The non-numeric double" - {164 "NaN" - {165 "Should not be less than" - {166 "Any numeric double" {167 checkAll(100, nonMinNorMaxValueDoubles) {168 NaN shouldNotMatchLessThan it169 }170 }171 "Any non-numeric double" {172 nonNumericDoubles.forEach {173 NaN shouldNotMatchLessThan it174 }175 }176 }177 }178 "Positive Infinity" - {179 "Should not be less than" - {180 "Any numeric double" {181 checkAll(100, nonMinNorMaxValueDoubles) {182 POSITIVE_INFINITY shouldNotMatchLessThan it183 }184 }185 "Any non-numeric double" {186 nonNumericDoubles.forEach {187 POSITIVE_INFINITY shouldNotMatchLessThan it188 }189 }190 }191 }192 "Negative Infinity" - {193 "Should be less than" - {194 "Any numeric double" {195 checkAll(100, nonMinNorMaxValueDoubles) {196 NEGATIVE_INFINITY shouldMatchLessThan it197 }198 }199 "Positive Infinity" {200 NEGATIVE_INFINITY shouldMatchLessThan POSITIVE_INFINITY201 }202 }203 "Should not be less than" - {204 "Itself" {205 NEGATIVE_INFINITY shouldNotMatchLessThan NEGATIVE_INFINITY206 }207 "NaN" {208 NEGATIVE_INFINITY shouldNotMatchLessThan NaN209 }210 }211 }212 }213 }214 "Positive matcher" - {215 "Zero" - {216 "Should not be positive" {217 0.0.shouldNotMatchPositive()218 }219 }220 "Every positive number" - {221 "Should be positive" {222 checkAll(100, numericDoubles.filterNot { it == 0.0 }) {223 it.absoluteValue.shouldMatchPositive()224 }225 }226 }227 "Every non-positive number" - {228 "Should not be positive" {229 checkAll(100, numericDoubles) {230 (-it.absoluteValue).shouldNotMatchPositive()231 }232 }233 }234 "The non-numeric double" - {235 "Positive Infinity" - {236 "Should be positive" {237 POSITIVE_INFINITY.shouldMatchPositive()238 }239 }240 "Negative Infinity" - {241 "Should not be positive" {242 NEGATIVE_INFINITY.shouldNotMatchPositive()243 }244 }245 "NaN" - {246 "Should not be positive" {247 NaN.shouldNotMatchPositive()248 }249 }250 }251 }252 "Negative matcher" - {253 "Zero" - {254 "Should not be negative" {255 0.0.shouldNotMatchNegative()256 }257 }258 "Every negative number" - {259 "Should be negative" {260 checkAll(100, numericDoubles.filterNot { it == 0.0 }) {261 (-it.absoluteValue).shouldMatchNegative()262 }263 }264 }265 "Every non-negative number" - {266 "Should not be negative" {267 checkAll(100, numericDoubles) {268 it.absoluteValue.shouldNotMatchNegative()269 }270 }271 }272 "The non-numeric double" - {273 "Positive Infinity" - {274 "Should not be negative" {275 POSITIVE_INFINITY.shouldNotMatchNegative()276 }277 }278 "Negative Infinity" - {279 "Should be negative" {280 NEGATIVE_INFINITY.shouldMatchNegative()281 }282 }283 "NaN" - {284 "Should not be negative" {285 NaN.shouldNotMatchNegative()286 }287 }288 }289 }290 "MultipleOf matcher" - {291 "Matches a simple multiple" {292 300.0 shouldBeMultipleOf 1.0293 }294 "Fails due to precision problems" {295 shouldFail {296 3.6e300 shouldBeMultipleOf 1.2297 }298 }299 }300 "Less than or equal matcher" - {301 "Every numeric double" - {302 "Should be less than or equal" - {303 "Itself" {304 checkAll(100, nonMinNorMaxValueDoubles) {305 it shouldMatchLessThanOrEqual it306 }307 }308 "Numbers bigger than itself" {309 checkAll(100, nonMinNorMaxValueDoubles) {310 it shouldMatchLessThanOrEqual it.muchGreater()...
AssertSoftlyTest.kt
Source:AssertSoftlyTest.kt
1package com.sksamuel.kotest.matchers2import io.kotest.assertions.assertSoftly3import io.kotest.assertions.throwables.shouldThrow4import io.kotest.assertions.withClue5import io.kotest.core.spec.style.FreeSpec6import io.kotest.matchers.collections.containExactly7import io.kotest.matchers.comparables.beLessThan8import io.kotest.matchers.doubles.negative9import io.kotest.matchers.doubles.positive10import io.kotest.matchers.doubles.shouldBeNegative11import io.kotest.matchers.ints.shouldBeLessThan12import io.kotest.matchers.ints.shouldBePositive13import io.kotest.matchers.maps.haveKey14import io.kotest.matchers.should15import io.kotest.matchers.shouldBe16import io.kotest.matchers.shouldNot17import io.kotest.matchers.shouldNotBe18import io.kotest.matchers.string.contain19import io.kotest.matchers.string.endWith20import io.kotest.matchers.string.shouldContain21import io.kotest.matchers.string.shouldNotContain22import io.kotest.matchers.string.shouldNotEndWith23import io.kotest.matchers.string.shouldStartWith24class AssertSoftlyTest : FreeSpec({25 "assertSoftly" - {26 "passes when all assertions pass" {27 assertSoftly {28 1 shouldBe 129 "foo" shouldBe "foo"30 }31 }32 "rethrows single failures" {33 val exception = shouldThrow<AssertionError> {34 assertSoftly {35 1 shouldBe 236 }37 }38 exception.message shouldBe "expected:<2> but was:<1>"39 exception.stackTrace.first().className shouldStartWith "com.sksamuel.kotest.matchers.AssertSoftlyTest"40 }41 "groups multiple failures" {42 shouldThrow<AssertionError> {43 assertSoftly {44 1 shouldBe 245 1 shouldBe 1 // should pass46 "foo" shouldNotBe "foo"47 }48 }.let {49 it.message should contain("1) expected:<2> but was:<1>")50 it.message should contain("2) \"foo\" should not equal \"foo\"")51 it.stackTrace.first().className shouldStartWith "com.sksamuel.kotest.matchers.AssertSoftlyTest"52 }53 }54 "works with all array types" {55 shouldThrow<AssertionError> {56 assertSoftly {57 booleanArrayOf(true) shouldBe booleanArrayOf(false)58 intArrayOf(1) shouldBe intArrayOf(2)59 shortArrayOf(1) shouldBe shortArrayOf(2)60 floatArrayOf(1f) shouldBe floatArrayOf(2f)61 doubleArrayOf(1.0) shouldBe doubleArrayOf(2.0)62 longArrayOf(1) shouldBe longArrayOf(2)63 byteArrayOf(1) shouldBe byteArrayOf(2)64 charArrayOf('a') shouldBe charArrayOf('b')65 arrayOf("foo") shouldBe arrayOf("bar")66 }67 }.let {68 it.message should contain("1) expected:<[false]> but was:<[true]>")69 it.message should contain("2) expected:<[2]> but was:<[1]>")70 it.message should contain("3) expected:<[2]> but was:<[1]>")71 it.message should contain("4) expected:<[2.0f]> but was:<[1.0f]>")72 it.message should contain("5) expected:<[2.0]> but was:<[1.0]>")73 it.message should contain("6) expected:<[2L]> but was:<[1L]>")74 it.message should contain("7) expected:<[2]> but was:<[1]>")75 it.message should contain("8) expected:<['b']> but was:<['a']>")76 it.message should contain("""9) Element differ at index: [0]77 |expected:<["bar"]> but was:<["foo"]>""".trimMargin())78 it.message shouldNot contain("10) ")79 }80 }81 "works with any matcher" {82 shouldThrow<AssertionError> {83 assertSoftly {84 1 should beLessThan(0)85 "foobar" shouldNot endWith("bar")86 1 shouldBe positive() // should pass87 1.0 shouldBe negative()88 listOf(1) shouldNot containExactly(1)89 mapOf(1 to 2) should haveKey(3)90 }91 }.let {92 it.message should contain("5) Map should contain key 3")93 it.message shouldNot contain("6) ")94 }95 }96 "works with extension functions" {97 shouldThrow<AssertionError> {98 assertSoftly {99 1.shouldBeLessThan(0)100 "foobar".shouldNotEndWith("bar")101 1.shouldBePositive() // should pass102 1.0.shouldBeNegative()103 }104 }.let {105 it.message should contain("""1) 1 should be < 0""")106 it.message should contain("""2) "foobar" should not end with "bar"""")107 it.message should contain("""3) 1.0 should be < 0.0""")108 it.message shouldNot contain("4) ")109 }110 }111 "can be nested" {112 shouldThrow<AssertionError> {113 assertSoftly {114 1 shouldBe 2115 assertSoftly {116 2 shouldBe 3117 }118 }119 }.let {120 it.message should contain("1) expected:<2> but was:<1>")121 it.message should contain("2) expected:<3> but was:<2>")122 }123 }124 "should not have any receiver context" {125 data class Person(val name: String, val age: Int)126 fun verifier(person: Person, assertion: (Person) -> Unit) {127 assertion(person)128 }129 val person = Person("foo", 0)130 verifier(person) {131 it shouldBe person132 assertSoftly {133 it shouldBe person // it being person verifies assertSoftly does not have any receiver134 }135 }136 }137 "Receiver version" - {138 "works on a receiver object" {139 shouldThrow<AssertionError> {140 assertSoftly("foo") {141 length shouldBe 2142 this[1] shouldBe 'o' // should pass143 this shouldNotBe "foo"144 }145 }.let {146 it.message should contain("1) expected:<2> but was:<3>")147 it.message should contain("2) \"foo\" should not equal \"foo\"")148 }149 }150 "Returns the receiver" {151 val a = assertSoftly("foo") {152 this shouldNotBe "bar"153 shouldNotEndWith("abc")154 }155 a shouldBe "foo"156 }157 "works with 'it' receiver" {158 val a = assertSoftly("foo") {159 it shouldNotBe "bar"160 }161 a shouldBe "foo"162 }163 "works with my parameter name" {164 val a =165 assertSoftly("foo") { foo -> // No idea why anybody would use this, but it's better to keep the verification that this works166 foo shouldNotBe "bar"167 }168 a shouldBe "foo"169 }170 }171 "Assert softly with data classes" - {172 // Added as a verification of https://github.com/kotest/kotest/issues/1831173 "work with enum in data class" {174 val source = WithSimpleEnum(enumValue = SimpleEnum.First)175 val result = WithSimpleEnum(enumValue = SimpleEnum.Second)176 val error = shouldThrow<AssertionError> {177 assertSoftly {178 withClue("simple strings") {179 "a" shouldBe "b"180 "a" shouldNotBe "b"181 }182 withClue("more complex with data class and enums") {183 source shouldBe result184 source shouldNotBe result185 }186 }187 }188 error.message shouldContain "1) simple strings\n" +189 "expected:<\"b\"> but was:<\"a\">"190 error.message shouldContain "2) data class diff for com.sksamuel.kotest.matchers.WithSimpleEnum\n" +191 "â enumValue: more complex with data class and enums\n" +192 "expected:<Second> but was:<First>"193 error.message shouldNotContain "3) "194 }195 }196 }197})198enum class SimpleEnum {199 First,200 Second201}202data class WithSimpleEnum(val enumValue: SimpleEnum = SimpleEnum.First)...
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)...
RandomSpec.kt
Source:RandomSpec.kt
...20 "Single weight should return 0" {21 Random(1).nextBin(0.1) shouldBe 022 Random(1).nextBin(1) shouldBe 023 }24 "Multiple weights should return correctly" {25 val random = Random(1)26 val bins = mutableMapOf(27 0 to 0,28 1 to 0,29 2 to 0,30 3 to 0,31 4 to 0,32 )33 val weights = doubleArrayOf(1.0, 3.0, 2.0, 0.0, 5.0)34 val weightsSum = weights.sum()35 val rounds = 10000036 repeat(rounds) {37 val bin = random.nextBin(weights)38 bins[bin] = bins[bin]!! + 139 }40 println(bins)41 val roundsDivSum = rounds / weightsSum42 withClue("Bin 0") {43 bins[0]!!.toDouble() shouldBe (roundsDivSum * weights[0]).plusOrMinus(1.percent)44 }45 withClue("Bin 1") {46 bins[1]!!.toDouble() shouldBe (roundsDivSum * weights[1]).plusOrMinus(1.percent)47 }48 withClue("Bin 2") {49 bins[2]!!.toDouble() shouldBe (roundsDivSum * weights[2]).plusOrMinus(1.percent)50 }51 withClue("Bin 3") {52 bins[3]!!.toDouble() shouldBe (roundsDivSum * weights[3]).plusOrMinus(1.percent)53 }54 withClue("Bin 4") {55 bins[4]!!.toDouble() shouldBe (roundsDivSum * weights[4]).plusOrMinus(1.percent)56 }57 }58 }59 "nextBin() - pairs of weight to value" - {60 "Multiple weights should return correctly" {61 val random = Random(1)62 data class V(val v: Int)63 val v1 = V(1)64 val v2 = V(2)65 val v3 = V(3)66 val v4 = V(4)67 val v5 = V(5)68 val bins = mutableMapOf(69 v1 to 0,70 v2 to 0,71 v3 to 0,72 v4 to 0,73 v5 to 0,74 )...
cab-invoiceChecker.kt
Source:cab-invoiceChecker.kt
1import io.kotest.core.spec.style.StringSpec2//import io.kotest.matchers.booleans.shouldBeFalse3//import io.kotest.matchers.booleans.shouldBeTrue4//import io.kotest.matchers.doubles.shouldBeExactly5import io.kotest.matchers.shouldBe6class cabInvoiceTest : StringSpec() {7 init {8 "If car travels for '1.0' km in '10' minutes"{9 // var invoice = carInvoiceTest()10 //var actualFare : Double = invoice.fairCalculate(1.0,10)11 //actualFare shouldBeExactly 1.012 cabInvoiceCalculate().fairCalculate(1.0,10) shouldBe 20.013 }14 "If car travels for '0.5' km in '5' minutes"{15 cabInvoiceCalculate().fairCalculate(0.5,5) shouldBe 10.016 }17 "If car travels for '0.0' km in '10' minutes"{18 cabInvoiceCalculate().fairCalculate(0.0,10) shouldBe 10.019 }20 "'x' person travels for total '10.0' km in '10' minutes "{21// cabInvoiceCalculate().fair(Ride(0.5,5)) shouldBe 10.022 cabInvoiceCalculate().fair(listOf(Ride(0.5,5)) ) shouldBe 10.023 }24 "'3' times 'x' person travels for total '1.5' km in '25' minutes"{ // fare of multiple rides is now being calculated25 val rideOne = Ride(0.5,5)26 val rideTwo = Ride(1.0,10)27 val rideThree = Ride(0.0,10)28 cabInvoiceCalculate().fair(listOf(rideOne,rideTwo,rideThree)) shouldBe 40.029 }30 "After multiple stops total fare of multiple rides"{31 val rideOne = Ride(0.5,5)32 val rideTwo = Ride(1.0,10)33 val rideThree = Ride(0.0,10)34 cabInvoiceCalculate().fair(listOf(rideOne,rideTwo,rideThree)) shouldBe 40.035 }36 "No rides per month"{37 cabInvoiceCalculate().fair(listOf()) shouldBe 0.038 }39// "when Getting Nullable Rides"{40// //var totalRides = listOf(Ride(null,null))41// cabInvoiceCalculate().fair(listOf(Ride(null,null))) shouldBe null42// }43 }44}45class cabInvoiceDetailedTest : StringSpec() {46 init{47 "calculating total fair,Number of rides taken & Average cost per ride (per person) "{48 val rideOne = Ride(0.5,5)49 val rideTwo = Ride(1.0,10)50 val rideThree = Ride(0.0,10)51 var totalFare = cabInvoiceCalculate().fair(listOf(rideOne,rideTwo,rideThree))52 totalFare shouldBe 40.053 var totalRides = cabInvoiceCalculate().totalRides(listOf(rideOne,rideTwo,rideThree))54 totalRides shouldBe 355 var averageCostPerRide = totalFare / totalRides56 averageCostPerRide shouldBe 13.33333333333333457 }58 }59}60class cabInvoiceDetailedTest1 : StringSpec() {61 init{62 "calculating total fair,Number of rides taken & Average cost per ride (per person) "{63 val rideOne = Ride(0.5,5)64 val rideTwo = Ride(1.0,10)65 val rideThree = Ride(0.0,10)66 var data = cabInvoiceCalculate().totalInvoiceData(listOf(rideOne,rideTwo,rideThree))67 data.totalFair shouldBe 40.068 data.totalRides shouldBe 369 data.averageCost shouldBe 13.33333333333333470// var totalFare = cabInvoiceCalculate().fair(listOf(rideOne,rideTwo,rideThree))71// totalFare shouldBe 40.072// var totalRides = cabInvoiceCalculate().totalRides(listOf(rideOne,rideTwo,rideThree))73// totalRides shouldBe 374// var averageCostPerRide = totalFare / totalRides75// averageCostPerRide shouldBe 13.33333333333333476 }77 }78}...
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 }...
KalkulatorTest.kt
Source:KalkulatorTest.kt
...13 "Simple binary expression" to Case("45 / 3", 15.0),14 "Simple binary expression with precedence" to Case("7 + 6 * 52 + 3", 322.0),15 "Parenthesis with binary expressions" to Case("52 / (7 + 6) + 3", 7.0),16 "Unnecessary parenthesis " to Case("52 / (7 + ((6)) ) + 3", 7.0),17 "Multiple unary expressions" to Case("2 + - - - 5", -3.0),18 "Mixed expressions" to Case("24 / 2 * 3 + -(1/5) + ( 7 * (4 - (-2)))", 77.8),19 ) { (expr, result) ->20 kalkulator.parseAndEvaluate(expr) shouldBeExactly result21 }22 }23})...
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}...
Multiple
Using AI Code Generation
1import io.kotest.matchers.doubles.*2import io.kotest.matchers.doubles.shouldBeGreaterThanOrEqual3import io.kotest.matchers.doubles.shouldBeLessThanOrEqual4import io.kotest.matchers.doubles.shouldBeZero5import io.kotest.matchers.doubles.shouldBeNegativeInfinity6import io.kotest.matchers.doubles.shouldBePositiveInfinity7import io.kotest.matchers.doubles.shouldBeNaN8import io.kotest.matchers.doubles.shouldBeFinite9import io.kotest.matchers.doubles.shouldBeInfinite10import io.kotest.matchers.doubles.shouldBeNormal11import io.kotest.matchers.doubles.shouldBeSubnormal12import io.kotest.matchers.doubles.shouldBeExactly13import io.kotest.matchers.doubles.shouldBeExactlyOne14import io.kotest.matchers.doubles.shouldBeExactlyZero15import io.kotest.matchers.doubles.shouldBeExactlyNegativeZero16import io.kotest.matchers.doubles.shouldBeExactlyPositiveZero17import io.kotest.matchers.doubles.shouldBeExactlyDouble18import io.kotest.matchers.doubles.shouldBeExactlyFloat19import io.kotest.matchers.doubles.shouldBeExactlyLong20import io.kotest.matchers.doubles.shouldBeExactlyInt21import io.kotest.matchers.doubles.shouldBeExactlyShort22import io.kotest.matchers.doubles.shouldBeExactlyByte23import io.kotest.matchers.doubles.shouldBeExactlyUByte24import io.kotest.matchers.doubles.shouldBeExactlyUShort25import io.kotest.matchers.doubles.shouldBeExactlyUInt26import io.kotest.matchers.doubles.shouldBeExactlyULong27import io.kotest.matchers.doubles.shouldBeExactlyChar28import io.kotest.matchers.doubles.shouldBeExactlyBoolean29import io.kotest.matchers.doubles.shouldBeExactlyString30import io.kotest.matchers.doubles.shouldBeExactlyNumber31import io.kotest.matchers.doubles.shouldBeExactlyAny32import io.kotest.matchers.doubles.shouldBeExactlyNothing33import io.kotest.matchers.doubles.shouldBeExactlyUnit34import io.kotest.matchers.doubles.shouldBeExactlyNothing35import io.kotest.matchers.doubles.shouldBeExactlyNothing36import io.kotest.matchers.doubles.should
Multiple
Using AI Code Generation
1assertThat(10.0).isLessThan(20.0)2assertThat(10.0).isGreaterThan(5.0)3assertThat(10.0).isLessThanOrEqualTo(10.0)4assertThat(10.0).isGreaterThanOrEqualTo(10.0)5assertThat(10.0).isBetween(5.0, 20.0)6assertThat(10.0).isNotBetween(5.0, 20.0)7assertThat(10.0).isBetween(5.0, 10.0)8assertThat(10.0).isNotBetween(5.0, 10.0)9assertThat(10.0).isBetween(10.0, 20.0)10assertThat(10.0).isNotBetween(10.0, 20.0)11assertThat(10.0).isBetween(10.0, 10.0)12assertThat(10.0).isNotBetween(10.0, 10.0)13assertThat(10.0).isBetween(5.0, 20.0, true, true)14assertThat(10.0).isNotBetween(5.0, 20.0, true, true)15assertThat(10.0).isBetween(5.0, 10.0, true, true)16assertThat(10.0).isNotBetween(5.0, 10.0, true, true)17assertThat(10.0).isBetween(10.0, 20.0, true, true)18assertThat(10.0).isNotBetween(10.0, 20.0, true, true)19assertThat(10.0).isBetween(10.0, 10.0, true, true)20assertThat(10.0).isNotBetween(10.0, 10.0, true, true)21assertThat(10.0).isBetween(5.0, 20.0, false, false)22assertThat(10.0).isNotBetween(5.0, 20.0, false, false)23assertThat(10.0).isBetween(5.0, 10.0, false, false)24assertThat(10.0).isNotBetween(5.0, 10.0, false, false)
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!!