Best Kotest code snippet using io.kotest.matchers.doubles.Positive.positive
DoubleMatchersTest.kt
Source:DoubleMatchersTest.kt
...14import io.kotest.matchers.doubles.gte15import io.kotest.matchers.doubles.lt16import io.kotest.matchers.doubles.lte17import io.kotest.matchers.doubles.negative18import io.kotest.matchers.doubles.positive19import io.kotest.matchers.doubles.shouldBeBetween20import 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()311 it shouldMatchLessThanOrEqual it.slightlyGreater()312 }313 }314 "Positive Infinity" {315 checkAll(100, nonMinNorMaxValueDoubles) {316 it shouldMatchLessThanOrEqual POSITIVE_INFINITY317 }318 }319 }320 "Should not be less than or equal" - {321 "Any number smaller than itself" {322 checkAll(100, nonMinNorMaxValueDoubles) {323 it shouldNotMatchLessThanOrEqual it.slightlySmaller()324 it shouldNotMatchLessThanOrEqual it.muchSmaller()325 }326 }327 "Negative Infinity" {328 checkAll(100, nonMinNorMaxValueDoubles) {329 it shouldNotMatchLessThanOrEqual NEGATIVE_INFINITY330 }331 }332 "NaN" {333 checkAll(100, nonMinNorMaxValueDoubles) {334 it shouldNotMatchLessThanOrEqual NaN335 }336 }337 }338 }339 "The non-numeric double" - {340 "NaN" {341 "Should not be less than or equal" - {342 "Any numeric double" {343 checkAll(100, nonMinNorMaxValueDoubles) {344 NaN shouldNotMatchLessThanOrEqual it345 }346 }347 "Positive Infinity" {348 NaN shouldNotMatchLessThanOrEqual POSITIVE_INFINITY349 }350 "Negative Infinity" {351 NaN shouldNotMatchLessThanOrEqual NEGATIVE_INFINITY352 }353 "Itself" {354 NaN shouldNotMatchLessThanOrEqual NaN355 }356 }357 }358 "Positive Infinity" - {359 "Should be less than or equal" - {360 "Positive Infinity" {361 POSITIVE_INFINITY shouldMatchLessThanOrEqual POSITIVE_INFINITY362 }363 }364 "Should not be less than or equal" - {365 "Any numeric double" {366 checkAll(100, nonMinNorMaxValueDoubles) {367 POSITIVE_INFINITY shouldNotMatchLessThanOrEqual it368 }369 }370 "Negative Infinity" {371 POSITIVE_INFINITY shouldNotMatchLessThanOrEqual NEGATIVE_INFINITY372 }373 "NaN" {374 POSITIVE_INFINITY shouldNotMatchLessThanOrEqual NaN375 }376 }377 }378 "Negative Infinity" - {379 "Should be less than or equal" - {380 "Any numeric double" {381 checkAll(100, nonMinNorMaxValueDoubles) {382 NEGATIVE_INFINITY shouldMatchLessThanOrEqual it383 }384 }385 "Positive Infinity" {386 NEGATIVE_INFINITY shouldMatchLessThanOrEqual POSITIVE_INFINITY387 }388 "Itself" {389 NEGATIVE_INFINITY shouldMatchLessThanOrEqual NEGATIVE_INFINITY390 }391 }392 "Should not be less than or equal" - {393 "NaN" {394 NEGATIVE_INFINITY shouldNotMatchLessThanOrEqual NaN395 }396 }397 }398 }399 }400 "Greater than matcher" - {401 "Every numeric double" - {402 "Should be greater than" - {403 "Numbers smaller than itself" {404 checkAll(100, nonMinNorMaxValueDoubles) {405 it shouldMatchGreaterThan it.slightlySmaller()406 it shouldMatchGreaterThan it.muchSmaller()407 }408 }409 "Negative infinity" {410 checkAll(100, nonMinNorMaxValueDoubles) {411 it shouldMatchGreaterThan NEGATIVE_INFINITY412 }413 }414 }415 "Should not be greater than" - {416 "Itself" {417 checkAll(100, nonMinNorMaxValueDoubles) {418 it shouldNotMatchGreaterThan it419 }420 }421 "Numbers greater than itself" {422 checkAll(100, nonMinNorMaxValueDoubles) {423 it shouldNotMatchGreaterThan it.slightlyGreater()424 it shouldNotMatchGreaterThan it.muchGreater()425 }426 }427 "NaN" {428 checkAll(100, nonMinNorMaxValueDoubles) {429 it shouldNotMatchGreaterThan NaN430 }431 }432 "Positive Infinity" {433 checkAll(100, nonMinNorMaxValueDoubles) {434 it shouldNotMatchGreaterThan POSITIVE_INFINITY435 }436 }437 }438 }439 "The non-numeric double" - {440 "NaN" - {441 "Should not be greater than" - {442 "Itself" {443 NaN shouldNotMatchGreaterThan NaN444 }445 "Any numeric double" {446 checkAll(100, numericDoubles) {447 NaN shouldNotMatchGreaterThan it448 }449 }450 "Positive Infinity" {451 NaN shouldNotMatchGreaterThan POSITIVE_INFINITY452 }453 "Negative Infinity" {454 NaN shouldNotMatchGreaterThan NEGATIVE_INFINITY455 }456 }457 }458 }459 }460 "Greater than or equal matcher" - {461 "Every numeric double" - {462 "Should be greater than or equal to" - {463 "Itself" {464 checkAll(100, nonMinNorMaxValueDoubles) {465 it shouldMatchGreaterThanOrEqual it466 }467 }468 "Numbers smaller than itself" {469 checkAll(100, nonMinNorMaxValueDoubles) {470 it shouldMatchGreaterThanOrEqual it.slightlySmaller()471 it shouldMatchGreaterThanOrEqual it.muchSmaller()472 }473 }474 "Negative Infinity" {475 checkAll(100, nonMinNorMaxValueDoubles) {476 it shouldMatchGreaterThanOrEqual NEGATIVE_INFINITY477 }478 }479 }480 "Should not be greater than or equal to" - {481 "Numbers bigger than itself" {482 checkAll(100, nonMinNorMaxValueDoubles) {483 it shouldNotMatchGreaterThanOrEqual it.slightlyGreater()484 it shouldNotMatchGreaterThanOrEqual it.muchGreater()485 }486 }487 "Positive Infinity" {488 checkAll(100, nonMinNorMaxValueDoubles) {489 it shouldNotMatchGreaterThanOrEqual POSITIVE_INFINITY490 }491 }492 "NaN" {493 checkAll(100, nonMinNorMaxValueDoubles) {494 it shouldNotMatchGreaterThanOrEqual NaN495 }496 }497 }498 }499 "The non-numeric double" - {500 "NaN" - {501 "Should not be greater than or equal to" - {502 "Itself" {503 NaN shouldNotMatchGreaterThanOrEqual NaN504 }505 "Any numeric double" {506 checkAll(100, numericDoubles) {507 NaN shouldNotMatchGreaterThanOrEqual it508 }509 }510 "Positive Infinity" {511 NaN shouldNotMatchGreaterThanOrEqual POSITIVE_INFINITY512 }513 "Negative Infinity" {514 NaN shouldNotMatchGreaterThanOrEqual NEGATIVE_INFINITY515 }516 }517 }518 "Positive Infinity" - {519 "Should be greater than or equal to" - {520 "Itself" {521 POSITIVE_INFINITY shouldMatchGreaterThanOrEqual POSITIVE_INFINITY522 }523 "Negative Infinity" {524 POSITIVE_INFINITY shouldMatchGreaterThanOrEqual NEGATIVE_INFINITY525 }526 "Any numeric double" {527 checkAll(100, numericDoubles) {528 POSITIVE_INFINITY shouldMatchGreaterThanOrEqual it529 }530 }531 }532 "Should not be greater than or equal to" - {533 "NaN" {534 POSITIVE_INFINITY shouldNotMatchGreaterThanOrEqual NaN535 }536 }537 }538 "Negative Infinity" - {539 "Should be greater than or equal to" - {540 "Itself" {541 NEGATIVE_INFINITY shouldMatchGreaterThanOrEqual NEGATIVE_INFINITY542 }543 }544 "Should not be greater than or equal to" - {545 "Any numeric double" {546 checkAll(100, numericDoubles) {547 NEGATIVE_INFINITY shouldNotMatchGreaterThanOrEqual it548 }549 }550 "Positive Infinity" {551 NEGATIVE_INFINITY shouldNotMatchGreaterThanOrEqual POSITIVE_INFINITY552 }553 "NaN" {554 NEGATIVE_INFINITY shouldNotMatchGreaterThanOrEqual NaN555 }556 }557 }558 }559 }560 "NaN matcher" - {561 "Every numeric double" - {562 "Should not be NaN" {563 checkAll(100, numericDoubles) {564 it.shouldNotMatchNaN()565 }566 }567 }568 "The non-numeric double" - {569 "NaN" - {570 "Should match NaN" {571 NaN.shouldMatchNaN()572 }573 }574 "Positive Infinity" - {575 "Should not match NaN" {576 POSITIVE_INFINITY.shouldNotMatchNaN()577 }578 }579 "Negative Infinity" - {580 "Should not match NaN" {581 NEGATIVE_INFINITY.shouldNotMatchNaN()582 }583 }584 }585 }586 "Positive Infinity matcher" - {587 "Any numeric double" - {588 "Should not match positive infinity" {589 checkAll(100, numericDoubles) {590 it.shouldNotMatchPositiveInfinity()591 }592 }593 }594 "The non-numeric double" - {595 "Positive Infinity" - {596 "Should match positive infinity" {597 POSITIVE_INFINITY.shouldMatchPositiveInfinity()598 }599 }600 "Negative Infinity" - {601 "Should not match positive infinity" {602 NEGATIVE_INFINITY.shouldNotMatchPositiveInfinity()603 }604 }605 "NaN" - {606 "Should not match positive infinity" {607 NaN.shouldNotMatchPositiveInfinity()608 }609 }610 }611 }612 "Negative Infinity matcher" - {613 "Any numeric double" - {614 "Should not match negative infinity" {615 checkAll(100, numericDoubles) {616 it.shouldNotMatchNegativeInfinity()617 }618 }619 }620 "The non-numeric double" - {621 "Negative Infinity" - {622 "Should match negative infinity" {623 NEGATIVE_INFINITY.shouldMatchNegativeInfinity()624 }625 }626 "Positive Infinity" - {627 "Should not match negative infinity" {628 POSITIVE_INFINITY.shouldNotMatchNegativeInfinity()629 }630 }631 "NaN" - {632 "Should not match negative infinity" {633 NaN.shouldNotMatchNegativeInfinity()634 }635 }636 }637 }638 "shouldBeZero" {639 (0.0).shouldBeZero()640 (-0.1).shouldNotBeZero()641 (0.1).shouldNotBeZero()642 MIN_VALUE.shouldNotBeZero()643 MAX_VALUE.shouldNotBeZero()644 NaN.shouldNotBeZero()645 POSITIVE_INFINITY.shouldNotBeZero()646 NEGATIVE_INFINITY.shouldNotBeZero()647 }648 }649 private fun shouldThrowAssertionError(message: String, vararg expression: () -> Any?) {650 expression.forEach {651 val exception = shouldThrow<AssertionError>(it)652 exception.message shouldBe message653 }654 }655 private fun Double.shouldMatchBetween(a: Double, b: Double, tolerance: Double) {656 this.shouldBeBetween(a, b, tolerance)657 this shouldBe between(a, b, tolerance)658 this.shouldThrowExceptionOnNotBetween(a, b, tolerance)659 }660 private fun Double.shouldNotMatchBetween(a: Double, b: Double, tolerance: Double) {661 this.shouldNotBeBetween(a, b, tolerance)662 this shouldNotBe between(a, b, tolerance)663 this.shouldThrowExceptionOnBetween(a, b, tolerance)664 }665 private fun Double.shouldThrowExceptionOnBetween(a: Double, b: Double, tolerance: Double) {666 when {667 this < a -> this.shouldThrowMinimumExceptionOnBetween(a, b, tolerance)668 this > b -> this.shouldThrowMaximumExceptionOnBetween(a, b, tolerance)669 else -> throw IllegalStateException()670 }671 }672 private fun Double.shouldThrowMinimumExceptionOnBetween(a: Double, b: Double, tolerance: Double) {673 val message = "$this should be bigger than $a"674 shouldThrowExceptionOnBetween(a, b, tolerance, message)675 }676 private fun Double.shouldThrowMaximumExceptionOnBetween(a: Double, b: Double, tolerance: Double) {677 val message = "$this should be smaller than $b"678 shouldThrowExceptionOnBetween(a, b, tolerance, message)679 }680 private fun Double.shouldThrowExceptionOnBetween(681 a: Double,682 b: Double,683 tolerance: Double,684 message: String = "$this should be smaller than $b and bigger than $a"685 ) {686 shouldThrowAssertionError(message,687 { this.shouldBeBetween(a, b, tolerance) },688 { this shouldBe between(a, b, tolerance) })689 }690 private fun Double.shouldThrowExceptionOnNotBetween(691 a: Double,692 b: Double,693 tolerance: Double,694 message: String = "$this should not be smaller than $b and should not be bigger than $a"695 ) {696 shouldThrowAssertionError(message,697 { this.shouldNotBeBetween(a, b, tolerance) },698 { this shouldNotBe between(a, b, tolerance) })699 }700 private infix fun Double.shouldMatchLessThan(x: Double) {701 this shouldBe lt(x)702 this shouldBeLessThan x703 this should beLessThan(x)704 this shouldThrowExceptionOnNotLessThan x705 }706 private infix fun Double.shouldThrowExceptionOnNotLessThan(x: Double) {707 shouldThrowAssertionError("$this should not be < $x",708 { this shouldNotBe lt(x) },709 { this shouldNotBeLessThan x },710 { this shouldNot beLessThan(x) })711 }712 private infix fun Double.shouldNotMatchLessThan(x: Double) {713 this shouldNotBe lt(x)714 this shouldNotBeLessThan x715 this shouldNot beLessThan(x)716 this shouldThrowExceptionOnLessThan x717 }718 private infix fun Double.shouldThrowExceptionOnLessThan(x: Double) {719 shouldThrowAssertionError("$this should be < $x",720 { this shouldBe lt(x) },721 { this shouldBeLessThan x },722 { this should beLessThan(x) }723 )724 }725 private fun Double.shouldMatchPositive() {726 this.shouldBePositive()727 this shouldBe positive()728 this.shouldThrowExceptionOnNotPositive()729 }730 private fun Double.shouldThrowExceptionOnNotPositive() {731 shouldThrowAssertionError("$this should not be > 0.0",732 { this shouldNotBe positive() },733 { this.shouldNotBePositive() }734 )735 }736 private fun Double.shouldNotMatchPositive() {737 this.shouldNotBePositive()738 this shouldNotBe positive()739 this.shouldThrowExceptionOnPositive()740 }741 private fun Double.shouldThrowExceptionOnPositive() {742 shouldThrowAssertionError("$this should be > 0.0",743 { this shouldBe positive() },744 { this.shouldBePositive() }745 )746 }747 private fun Double.shouldMatchNegative() {748 this.shouldBeNegative()749 this shouldBe negative()750 this.shouldThrowExceptionOnNotNegative()751 }752 private fun Double.shouldThrowExceptionOnNotNegative() {753 shouldThrowAssertionError("$this should not be < 0.0",754 { this shouldNotBe negative() },755 { this.shouldNotBeNegative() }756 )757 }...
DoubleExactlyTest.kt
Source:DoubleExactlyTest.kt
1package com.sksamuel.kotest.matchers.doubles2import io.kotest.assertions.throwables.shouldThrow3import io.kotest.core.spec.style.FreeSpec4import io.kotest.matchers.doubles.exactly5import io.kotest.matchers.doubles.shouldBeExactly6import io.kotest.matchers.doubles.shouldNotBeExactly7import io.kotest.matchers.shouldBe8import io.kotest.matchers.shouldNotBe9import io.kotest.property.checkAll10class DoubleExactlyTest : FreeSpec() {11 init {12 "for every numeric Double" - {13 "Should be exactly itself" {14 checkAll(100, numericDoubles) {15 it shouldExactlyMatch it16 }17 }18 "Should not be exactly" - {19 "Any number smaller than itself" {20 checkAll(100, nonMinNorMaxValueDoubles) {21 it shouldNotMatchExactly it.slightlySmaller()22 }23 }24 "Any number bigger than itself" {25 checkAll(100, nonMinNorMaxValueDoubles) {26 it shouldNotMatchExactly it.slightlyGreater()27 }28 }29 "Anything that's not numeric" {30 checkAll(100, numericDoubles) {31 nonNumericDoubles.forEach { nonNumeric ->32 it shouldNotMatchExactly nonNumeric33 }34 }35 }36 }37 }38 "For non-numeric doubles" - {39 "NaN" - {40 "Should not be exactly" - {41 "Any number" {42 checkAll(100, numericDoubles) {43 Double.NaN shouldNotMatchExactly it44 }45 }46 "NaN" {47 Double.NaN shouldNotMatchExactly Double.NaN48 }49 "The infinities" {50 Double.Companion.NaN shouldNotMatchExactly Double.Companion.POSITIVE_INFINITY51 Double.Companion.NaN shouldNotMatchExactly Double.Companion.NEGATIVE_INFINITY52 }53 }54 }55 "Positive Infinity" - {56 "Should be exactly" - {57 "Itself" {58 Double.Companion.POSITIVE_INFINITY shouldExactlyMatch Double.Companion.POSITIVE_INFINITY59 }60 }61 "Should not be exactly" - {62 "Any numeric double" {63 checkAll(100, numericDoubles) {64 Double.POSITIVE_INFINITY shouldNotMatchExactly it65 }66 }67 "Any other non-numeric double" {68 Double.Companion.POSITIVE_INFINITY shouldNotMatchExactly Double.Companion.NEGATIVE_INFINITY69 Double.Companion.POSITIVE_INFINITY shouldNotMatchExactly Double.Companion.NaN70 }71 }72 }73 "Negative Infinity" - {74 "Should be exactly" - {75 "Itself" {76 Double.Companion.NEGATIVE_INFINITY shouldExactlyMatch Double.Companion.NEGATIVE_INFINITY77 }78 }79 "Should not be exactly" - {80 "Any numeric double" {81 checkAll(100, numericDoubles) {82 Double.NEGATIVE_INFINITY shouldNotMatchExactly it83 }84 }85 "Any other non-numeric double" {86 Double.Companion.NEGATIVE_INFINITY shouldNotMatchExactly Double.Companion.POSITIVE_INFINITY87 Double.Companion.NEGATIVE_INFINITY shouldNotMatchExactly Double.Companion.NaN88 }89 }90 }91 }92 }93 private infix fun Double.shouldExactlyMatch(other: Double) {94 this shouldBeExactly other95 this shouldBe exactly(other)96 this shouldThrowExceptionOnNotExactly other97 }98 private infix fun Double.shouldNotMatchExactly(other: Double) {99 this shouldNotBe exactly(other)100 this shouldNotBeExactly other101 this shouldThrowExceptionOnExactly other102 }103 private infix fun Double.shouldThrowExceptionOnNotExactly(other: Double) {104 shouldThrowAssertionError("$this should not equal $other",105 { this shouldNotBeExactly other },106 { this shouldNotBe exactly(other) }107 )108 }109 private infix fun Double.shouldThrowExceptionOnExactly(other: Double) {110 shouldThrowAssertionError("$this is not equal to expected value $other",111 { this shouldBeExactly other },112 { this shouldBe exactly(other) }113 )114 }115 private fun shouldThrowAssertionError(message: String, vararg expression: () -> Any?) {116 expression.forEach {117 val exception = shouldThrow<AssertionError>(it)118 exception.message shouldBe message119 }120 }121}...
DoublesSpec.kt
Source:DoublesSpec.kt
2import io.kotest.core.spec.style.DescribeSpec3import io.kotest.matchers.should4import io.monkeypatch.kaval.core.validator.Doubles.inRange5import io.monkeypatch.kaval.core.validator.Doubles.negative6import io.monkeypatch.kaval.core.validator.Doubles.positive7import io.monkeypatch.kaval.core.validator.Doubles.strictlyNegative8import io.monkeypatch.kaval.core.validator.Doubles.strictlyPositive9import io.monkeypatch.kaval.kotlintest.beInvalidWithReason10import io.monkeypatch.kaval.kotlintest.beValid11class DoublesSpec : DescribeSpec() {12 init {13 describe("inRange") {14 val validator = inRange((5.0)..(10.0))15 it("inRange should reject a lower number") {16 val result = validator(0.0)17 result should beInvalidWithReason(18 "requires to be in range 5.0..10.0, got 0.0"19 )20 }21 it("inRange should reject a higher number") {22 val result = validator(42.0)23 result should beInvalidWithReason(24 "requires to be in range 5.0..10.0, got 42.0"25 )26 }27 it("inRange should accept value lower bound") {28 val result = validator(5.0)29 result should beValid()30 }31 it("inRange should accept in range") {32 val result = validator(10.0)33 result should beValid()34 }35 it("inRange should accept value upper bound") {36 val result = validator(7.0)37 result should beValid()38 }39 }40 describe("strictlyPositive") {41 val validator = strictlyPositive42 it("strictlyPositive should reject a negative number") {43 val result = validator(-1.0)44 result should beInvalidWithReason(45 "requires to be strictly positive"46 )47 }48 it("strictlyPositive should reject zero") {49 val result = validator(0.0)50 result should beInvalidWithReason(51 "requires to be strictly positive"52 )53 }54 it("strictlyPositive should accept a positive number") {55 val result = validator(5.0)56 result should beValid()57 }58 }59 describe("positive") {60 val validator = positive61 it("positive should reject a negative number") {62 val result = validator(-1.0)63 result should beInvalidWithReason(64 "requires to be positive"65 )66 }67 it("positive should reject zero") {68 val result = validator(0.0)69 result should beValid()70 }71 it("positive should accept a positive number") {72 val result = validator(5.0)73 result should beValid()74 }75 }76 describe("strictlyNegative") {77 val validator = strictlyNegative78 it("strictlyNegative should reject a positive number") {79 val result = validator(1.0)80 result should beInvalidWithReason(81 "requires to be strictly negative"82 )83 }84 it("strictlyNegative should reject zero") {85 val result = validator(0.0)86 result should beInvalidWithReason(87 "requires to be strictly negative"88 )89 }90 it("strictlyNegative should accept a negative number") {91 val result = validator(-5.0)92 result should beValid()...
DoubleNumberTests.kt
Source:DoubleNumberTests.kt
1package com.github.jvmusin.universalconverter.number2import io.kotest.core.spec.style.StringSpec3import io.kotest.matchers.booleans.shouldBeFalse4import io.kotest.matchers.booleans.shouldBeTrue5import io.kotest.matchers.doubles.plusOrMinus6import io.kotest.matchers.doubles.shouldBePositiveInfinity7import io.kotest.matchers.shouldBe8class DoubleNumberTests : StringSpec({9 "3 * 9.1 = 18.3" {10 (DoubleNumber(3.0) * DoubleNumber(9.1)).value shouldBe (27.3 plusOrMinus 1e-10)11 }12 "3 / 2 = 1.5" {13 (DoubleNumber(3.0) / DoubleNumber(2.0)) shouldBe DoubleNumber(1.5)14 }15 "3 / 0 = +INF" {16 (DoubleNumber(3.0) / DoubleNumber(0.0)).value.shouldBePositiveInfinity()17 }18 "обÑаÑное к 5 = 0.2" {19 DoubleNumber(5.0).inverse() shouldBe DoubleNumber(0.2)20 }21 "обÑаÑное к 0 = +INF" {22 DoubleNumber(0.0).inverse().value.shouldBePositiveInfinity()23 }24 "5 положиÑелÑно" {25 DoubleNumber(5.0).isPositive.shouldBeTrue()26 }27 "1e-30 положиÑелÑно" {28 DoubleNumber(1e-30).isPositive.shouldBeTrue()29 }30 "0 не положиÑелÑно" {31 DoubleNumber(0.0).isPositive.shouldBeFalse()32 }33 "Double.MIN_VALUE положиÑелÑно" {34 DoubleNumber(Double.MIN_VALUE).isPositive.shouldBeTrue()35 }36 "-3 не положиÑелÑно" {37 DoubleNumber(-3.0).isPositive.shouldBeFalse()38 }39 "13.565 в ÑÑÑÐ¾ÐºÑ Ð¸ в double даÑÑ Ñо же Ñамое ÑиÑло" {40 val x = 13.56541 DoubleNumber(x).toString().toDouble() shouldBe x42 }43 // Kind of acceptance tests below44 "1/3 * 1000 в ÑÑÑÐ¾ÐºÑ Ð¾ÑдаÑÑ 34 знаÑаÑие ÑиÑÑÑ Ð¸ окÑÑглÑÐµÑ Ð²Ð½Ð¸Ð·" {45 val x = DoubleNumber(1.0).divideBy(DoubleNumber(3.0)).multiplyBy(DoubleNumber(1000.0))46 x.toString() shouldBe "333.3333333333333143855270463973284"47 }48 "2/3 * 1000 в ÑÑÑÐ¾ÐºÑ Ð¾ÑдаÑÑ 34 знаÑаÑие ÑиÑÑÑ Ð¸ окÑÑглÑÐµÑ Ð²Ð²ÐµÑÑ
" {49 val x = DoubleNumber(2.0).divideBy(DoubleNumber(3.0)).multiplyBy(DoubleNumber(1000.0))50 x.toString() shouldBe "666.6666666666666287710540927946568"51 }52})...
DoubleToleranceTest.kt
Source:DoubleToleranceTest.kt
1package com.sksamuel.kotest.matchers.doubles2import io.kotest.assertions.throwables.shouldThrow3import io.kotest.assertions.throwables.shouldThrowAny4import io.kotest.core.spec.style.FunSpec5import io.kotest.matchers.doubles.percent6import io.kotest.matchers.doubles.plusOrMinus7import io.kotest.matchers.doubles.shouldBeWithinPercentageOf8import io.kotest.matchers.shouldBe9import io.kotest.matchers.shouldNotBe10import io.kotest.property.Arb11import io.kotest.property.arbitrary.bind12import io.kotest.property.arbitrary.double13import io.kotest.property.arbitrary.numericDouble14import io.kotest.property.checkAll15class DoubleToleranceTest : FunSpec({16 test("double with tolerance should include tolerance in error message") {17 shouldThrowAny {18 1.0 shouldBe (1.5 plusOrMinus 0.4)19 }.message shouldBe "1.0 should be equal to 1.5 within tolerance of 0.4 (lowest acceptable value is 1.1; highest acceptable value is 1.9)"20 }21 test("infinite double with finite tolerance should equal the same infinite double") {22 checkAll(Arb.numericDouble(min = 0.0)) { eps ->23 Double.NEGATIVE_INFINITY shouldBe (Double.NEGATIVE_INFINITY plusOrMinus eps)24 Double.POSITIVE_INFINITY shouldBe (Double.POSITIVE_INFINITY plusOrMinus eps)25 }26 }27 test("Allow for percentage tolerance") {28 1.5 shouldBe (1.0 plusOrMinus 50.percent)29 1.5 shouldNotBe (2.0 plusOrMinus 10.percent)30 }31 context("Percentage") {32 test("Match equal numbers") {33 Arb.bind(Arb.double(), Arb.double(0.0, 5.0)) { value, percentage ->34 value.shouldBeWithinPercentageOf(value, percentage)35 }36 }37 test("Refuse negative percentage") {38 shouldThrow<IllegalArgumentException> {39 1.0.shouldBeWithinPercentageOf(1.0, -0.1)40 }41 }42 test("Match close enough numbers") {43 Arb.bind(Arb.double(), Arb.double(0.0, 5.0)) { value, percentage ->44 value.shouldBeWithinPercentageOf((value - value.times(percentage / 100)), percentage)45 value.shouldBeWithinPercentageOf((value + value.times(percentage / 100)), percentage)46 }47 }48 }49})...
BLEDistanceUnscentedKalmanFilterTest.kt
Source:BLEDistanceUnscentedKalmanFilterTest.kt
1package uk.nhs.riskscore.internal2import io.kotest.core.spec.style.StringSpec3import io.kotest.matchers.booleans.shouldBeTrue4import io.kotest.matchers.doubles.shouldBeGreaterThanOrEqual5import io.kotest.matchers.ints.shouldBeExactly6import io.kotest.property.arbitrary.orNull7import io.kotest.property.checkAll8import uk.nhs.riskscore.internal.kotest.bleDistanceUnscentedKalmanFilter9import uk.nhs.riskscore.internal.kotest.nonEmptyList10import uk.nhs.riskscore.internal.kotest.smallNumericDoubles11import uk.nhs.riskscore.internal.kotest.smallPositiveDoubles12internal class BLEDistanceUnscentedKalmanFilterTest : StringSpec({13 "transition function is non-negative" {14 checkAll(15 smallNumericDoubles,16 smallNumericDoubles,17 bleDistanceUnscentedKalmanFilter) { state, noise, filter ->18 val result = filter.transitionFunction(state, noise)19 result shouldBeGreaterThanOrEqual 0.020 }21 }22 "smooth function returns the same number of elements as the input" {23 checkAll(24 100,25 bleDistanceUnscentedKalmanFilter,26 smallPositiveDoubles.orNull(0.1).nonEmptyList(2)27 ) { filter, attenutations ->28 filter.smooth(attenutations).size shouldBeExactly attenutations.size29 }30 }31 "smooth function returns finite results" {32 checkAll(33 100,34 bleDistanceUnscentedKalmanFilter,35 smallPositiveDoubles.orNull(0.1).nonEmptyList(2)36 ) { filter, attenutations ->37 filter.smooth(attenutations).all { (mean, covariance) ->38 mean.isFinite() && covariance.isFinite()39 }.shouldBeTrue()40 }41 }42})...
QuadraticRiskScoreTest.kt
Source:QuadraticRiskScoreTest.kt
1package uk.nhs.riskscore.internal2import io.kotest.core.spec.style.StringSpec3import io.kotest.matchers.doubles.shouldBeLessThanOrEqual4import io.kotest.property.checkAll5import uk.nhs.riskscore.internal.kotest.nonEmptyList6import uk.nhs.riskscore.internal.kotest.smallPositiveDoubles7internal class QuadraticRiskScoreTest : StringSpec({8 "risk score is bounded by sum of durations" {9 checkAll(10 smallPositiveDoubles,11 smallPositiveDoubles.nonEmptyList(),12 smallPositiveDoubles.nonEmptyList()13 ) { minDistance, durations, distances ->14 val score = QuadraticRiskScore(minDistance).calculate(15 durations = durations,16 distances = distances,17 shouldNormalize = false18 )19 score shouldBeLessThanOrEqual durations.sum()20 }21 }22 "normalized risk score is bounded above by 1" {23 checkAll(24 smallPositiveDoubles,25 smallPositiveDoubles.nonEmptyList(),26 smallPositiveDoubles.nonEmptyList()27 ) { minDistance, durations, distances ->28 val score = QuadraticRiskScore(minDistance).calculate(29 durations = durations,30 distances = distances,31 shouldNormalize = true32 )33 score shouldBeLessThanOrEqual 1.034 }35 }36})...
Positive.kt
Source:Positive.kt
...3import io.kotest.matchers.MatcherResult4import io.kotest.matchers.shouldBe5import io.kotest.matchers.shouldNotBe6/**7 * Asserts that this [Double] is positive8 *9 * Verifies that this [Double] is strictly greater than 0.0.10 *11 * Opposite of [Double.shouldNotBePositive]12 *13 * ```14 * 0.1.shouldBePositive() // Assertion passes15 * (-0.1).shouldBePositive() // Assertion fails16 * ```17 *18 * @see [Double.shouldNotBeNegative]19 */20fun Double.shouldBePositive() = this shouldBe positive()21/**22 * Asserts that this [Double] is not positive23 *24 * Verifies that this [Double] is not strictly greater than 0.0.25 *26 * Opposite of [Double.shouldBePositive]27 *28 * ```29 * 0.1.shouldNotBePositive() // Assertion fails30 * (-0.1).shouldNotBePositive() // Assertion passes31 * ```32 *33 * @see [Double.shouldBeNegative]34 */35fun Double.shouldNotBePositive() = this shouldNotBe positive()36fun positive() = object : Matcher<Double> {37 override fun test(value: Double) = MatcherResult(value > 0.0, "$value should be > 0.0", "$value should not be > 0.0")38}...
positive
Using AI Code Generation
1 fun doubleMatchers() {2- 1.0 should beLessThan(2.0)3- 1.0 should beLessThanOrEqualTo(2.0)4- 1.0 should beGreaterThan(0.0)5- 1.0 should beGreaterThanOrEqualTo(0.0)6- 1.0 should beBetween(0.0, 2.0)7- 1.0 should beBetweenClosed(0.0, 2.0)8- 1.0 should beBetweenInclusive(0.0, 2.0)9- 1.0 should beBetweenClosedInclusive(0.0, 2.0)10- 1.0 shouldBeBetween(0.0, 2.0)11- 1.0 shouldBeBetweenInclusive(0.0, 2.0)12- 1.0 shouldBeBetweenClosed(0.0, 2.0)13- 1.0 shouldBeBetweenClosedInclusive(0.0, 2.0)14- 1.0 shouldBeBetween(0.0, 2.0, true, true)15- 1.0 shouldBeBetween(0.0, 2.0, true, false)16- 1.0 shouldBeBetween(0.0, 2.0, false, true)17- 1.0 shouldBeBetween(0.0, 2.0, false, false)18- 1.0 shouldBeBetweenClosed(0.0, 2.0, true, true)19- 1.0 shouldBeBetweenClosed(0.0, 2.0, true, false)20- 1.0 shouldBeBetweenClosed(0.0, 2.0, false, true)21- 1.0 shouldBeBetweenClosed(0.0, 2.0, false, false)
positive
Using AI Code Generation
1 45 infix fun Double.shouldBePositive() = this shouldBe Positive2 46 infix fun Double.shouldNotBePositive() = this shouldNotBe Positive3 48 infix fun Double.shouldBeNegative() = this shouldBe Negative4 49 infix fun Double.shouldNotBeNegative() = this shouldNotBe Negative5 51 infix fun Double.shouldBeZero() = this shouldBe Zero6 52 infix fun Double.shouldNotBeZero() = this shouldNotBe Zero7 54 infix fun Double.shouldBeNonPositive() = this shouldBe NonPositive8 55 infix fun Double.shouldNotBeNonPositive() = this shouldNotBe NonPositive9 57 infix fun Double.shouldBeNonNegative() = this shouldBe NonNegative10 58 infix fun Double.shouldNotBeNonNegative() = this shouldNotBe NonNegative11 60 infix fun Double.shouldBePositiveInfinity() = this shouldBe PositiveInfinity12 61 infix fun Double.shouldNotBePositiveInfinity() = this shouldNotBe PositiveInfinity13 63 infix fun Double.shouldBeNegativeInfinity() = this shouldBe NegativeInfinity14 64 infix fun Double.shouldNotBeNegativeInfinity() = this shouldNotBe NegativeInfinity15 66 infix fun Double.shouldBeNaN() = this shouldBe NaN16 67 infix fun Double.shouldNotBeNaN() = this shouldNotBe NaN17 69 infix fun Double.shouldBeFinite() = this shouldBe Finite18 70 infix fun Double.shouldNotBeFinite() = this shouldNotBe Finite19 72 infix fun Double.shouldBeNormal() = this shouldBe Normal20 73 infix fun Double.shouldNotBeNormal() = this shouldNotBe Normal21 75 infix fun Double.shouldBeSubnormal() = this shouldBe Subnormal22 76 infix fun Double.shouldNotBeSubnormal() = this shouldNotBe Subnormal23 78 infix fun Double.shouldBeZeroOrPositive() = this shouldBe ZeroOrPositive24 79 infix fun Double.shouldNotBeZeroOrPositive() = this shouldNotBe ZeroOrPositive
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!!