Best Kotest code snippet using io.kotest.matchers.collections.sorted.test
RandomServiceTest.kt
Source:RandomServiceTest.kt
1package io.github.serpro69.kfaker2import io.kotest.assertions.assertSoftly3import io.kotest.assertions.throwables.shouldThrow4import io.kotest.core.spec.style.DescribeSpec5import io.kotest.matchers.collections.shouldBeIn6import io.kotest.matchers.collections.shouldBeSortedWith7import io.kotest.matchers.collections.shouldContain8import io.kotest.matchers.collections.shouldContainAll9import io.kotest.matchers.collections.shouldHaveSize10import io.kotest.matchers.collections.shouldNotBeSortedWith11import io.kotest.matchers.should12import io.kotest.matchers.shouldBe13import io.kotest.matchers.shouldNotBe14import io.kotest.matchers.types.beInstanceOf15import java.util.*16internal class RandomServiceTest : DescribeSpec({17 describe("RandomService instance") {18 val config = fakerConfig { random = Random() }19 val randomService = RandomService(config)20 context("nextInt(bound) fun") {21 val values = List(100) { randomService.nextInt(10) }22 it("return value should be within specified range") {23 values.all { it < 10 } shouldBe true24 }25 }26 context("nextInt(min, max) fun") {27 val values = List(100) { randomService.nextInt(6..8) }28 it("return value should be within specified range") {...
TestSchedule.kt
Source:TestSchedule.kt
...7import edu.illinois.cs.cs125.cisapi.ScheduleYearSemester8import edu.illinois.cs.cs125.cisapi.ScheduleYearSemesterDepartment9import edu.illinois.cs.cs125.cisapi.Section10import edu.illinois.cs.cs125.cisapi.fromXml11import io.kotest.core.spec.style.StringSpec12import io.kotest.matchers.collections.shouldContainInOrder13import io.kotest.matchers.collections.shouldHaveAtLeastSize14import io.kotest.matchers.collections.shouldHaveSize15import io.kotest.matchers.ints.shouldBeGreaterThan16import io.kotest.matchers.shouldBe17import io.kotest.matchers.shouldNotBe18import io.kotest.matchers.string.shouldEndWith19import io.kotest.matchers.string.shouldStartWith20import java.io.File21private fun String.load() = TestSchedule::class.java.getResource("/$this").readText()22@Suppress("BlockingMethodInNonBlockingContext")23class TestSchedule : StringSpec({24 "should load local schedule.xml properly" {25 "schedule.xml".load().fromXml<Schedule>().also { schedule ->26 schedule.calendarYears shouldHaveSize 1727 schedule.calendarYears.map { year -> year.id }.sorted() shouldContainInOrder (2004..2020).toList()28 schedule.calendarYears.forEach {29 it.href.toString() shouldEndWith "${it.year}.xml"30 }31 }32 }33 "should load remote schedule.xml properly" {...
FailingKotestAsserts.kt
Source:FailingKotestAsserts.kt
1package testing.failing2import arrow.core.*3import io.kotest.assertions.arrow.either.shouldBeLeft4import io.kotest.assertions.arrow.either.shouldBeRight5import io.kotest.assertions.arrow.nel.shouldContain6import io.kotest.assertions.arrow.nel.shouldContainNull7import io.kotest.assertions.arrow.option.shouldBeNone8import io.kotest.assertions.arrow.option.shouldBeSome9import io.kotest.assertions.arrow.validation.shouldBeInvalid10import io.kotest.assertions.arrow.validation.shouldBeValid11import io.kotest.assertions.asClue12import io.kotest.assertions.assertSoftly13import io.kotest.assertions.json.*14import io.kotest.assertions.throwables.shouldThrowAny15import io.kotest.assertions.throwables.shouldThrowExactly16import io.kotest.assertions.withClue17import io.kotest.matchers.Matcher18import io.kotest.matchers.MatcherResult19import io.kotest.matchers.booleans.shouldBeFalse20import io.kotest.matchers.booleans.shouldBeTrue21import io.kotest.matchers.collections.shouldBeEmpty22import io.kotest.matchers.collections.shouldBeOneOf23import io.kotest.matchers.collections.shouldBeSameSizeAs24import io.kotest.matchers.collections.shouldBeSorted25import io.kotest.matchers.collections.shouldContain26import io.kotest.matchers.date.shouldBeAfter27import io.kotest.matchers.ints.shouldBeEven28import io.kotest.matchers.ints.shouldBeGreaterThan29import io.kotest.matchers.ints.shouldBeGreaterThanOrEqual30import io.kotest.matchers.ints.shouldBeLessThan31import io.kotest.matchers.ints.shouldBeLessThanOrEqual32import io.kotest.matchers.ints.shouldBeZero33import io.kotest.matchers.maps.shouldBeEmpty34import io.kotest.matchers.maps.shouldContain35import io.kotest.matchers.maps.shouldContainKey36import io.kotest.matchers.maps.shouldContainValue37import io.kotest.matchers.nulls.shouldBeNull38import io.kotest.matchers.should39import io.kotest.matchers.shouldBe40import io.kotest.matchers.shouldNot41import io.kotest.matchers.string.shouldBeBlank42import io.kotest.matchers.string.shouldBeEmpty43import io.kotest.matchers.string.shouldBeUpperCase44import io.kotest.matchers.string.shouldContain45import io.kotest.matchers.string.shouldNotBeBlank46import java.time.LocalDate47import org.junit.jupiter.api.Test48/**49 * Kotest assertions50 *51 * - [Kotest Assertions Documentation](https://kotest.io/assertions/)52 * - [Github](https://github.com/kotest/kotest/)53 */54class FailingKotestAsserts {55 @Test56 fun `General assertions`() {57 assertSoftly {58 "text" shouldBe "txet"59 "hi".shouldBeBlank()60 " ".shouldNotBeBlank()61 "hi".shouldBeEmpty()62 "hi".shouldBeUpperCase()63 "hello".shouldContain("hi")64 false.shouldBeTrue()65 true.shouldBeFalse()66 "not null".shouldBeNull()67 10 shouldBeLessThan 1068 10 shouldBeLessThanOrEqual 969 11 shouldBeGreaterThan 1170 11 shouldBeGreaterThanOrEqual 1271 9.shouldBeEven()72 1.shouldBeZero()73 }74 }75 @Test76 fun `Exception assertions`() {77 assertSoftly {78 shouldThrowExactly<IllegalArgumentException> {79 angryFunction()80 }81 shouldThrowAny {82 "I'm not throwing anything"83 }84 }85 }86 @Test87 fun `Collection assertions`() {88 assertSoftly {89 listOf(1, 2, 3).shouldBeEmpty()90 listOf(1, 2, 3) shouldContain 491 listOf(1, 3, 2).shouldBeSorted()92 listOf(1, 2, 3, 4) shouldBeSameSizeAs listOf(4, 5, 6)93 1 shouldBeOneOf listOf(2, 3)94 mapOf(1 to "one", 2 to "two", 3 to "three").shouldBeEmpty()95 mapOf(1 to "one", 2 to "two", 3 to "three") shouldContainKey 496 mapOf(1 to "one", 2 to "two", 3 to "three") shouldContainValue "five"97 mapOf(1 to "one", 2 to "two", 3 to "three") shouldContain (6 to "six")98 }99 }100 @Test101 fun `Arrow assertions`() {102 assertSoftly {103 val optionNone = none<String>()104 val optionSome = Some("I am something").toOption()105 optionSome.shouldBeNone()106 optionNone.shouldBeSome()107 val rightEither = Either.Right(1)108 val leftEither = Either.Left("ERROR!!")109 leftEither.shouldBeRight()110 leftEither shouldBeRight 1111 rightEither.shouldBeLeft()112 rightEither shouldBeLeft "ERROR!!"113 val nonEmptyList = NonEmptyList.of(1, 2, 3, 4, 5)114 nonEmptyList shouldContain 6115 nonEmptyList.shouldContainNull()116 val valid = Validated.valid()117 val invalid = Validated.invalid()118 invalid.shouldBeValid()119 valid.shouldBeInvalid()120 }121 }122 @Test123 fun `Json assertions`() {124 val jsonString = "{\"test\": \"property\", \"isTest\": true }"125 assertSoftly {126 jsonString shouldMatchJson "{\"test\": \"otherProperty\"}"127 jsonString shouldContainJsonKey "$.anotherTest"128 jsonString shouldNotContainJsonKey "$.test"129 jsonString.shouldContainJsonKeyValue("$.isTest", false)130 }131 }132 @Test133 fun `Custom assertions`() {134 assertSoftly {135 val sentMail = Mail(136 dateCreated = LocalDate.of(2020, 10, 27),137 sent = true, message = "May you have an amazing day"138 )139 val unsentMail = Mail(140 dateCreated = LocalDate.of(2020, 10, 27),141 sent = false, message = "May you have an amazing day"142 )143 // This is possible144 unsentMail.sent should beSent()145 sentMail.sent shouldNot beSent()146 // This is recommended147 unsentMail.shouldBeSent()148 sentMail.shouldNotBeSent()149 }150 }151 @Test152 fun `withClue usage`() {153 val mail = Mail(154 dateCreated = LocalDate.of(2020, 10, 27),155 sent = false, message = "May you have an amazing day"156 )157 withClue("sent field should be true") {158 mail.sent shouldBe true159 }160 mail.asClue {161 it.dateCreated shouldBeAfter LocalDate.of(2020, 10, 26)162 it.sent shouldBe true163 }164 }165 @Test166 fun `asClue usage`() {167 val mail = Mail(168 dateCreated = LocalDate.of(2020, 10, 27),169 sent = false, message = "May you have an amazing day"170 )171 mail.asClue {172 it.dateCreated shouldBeAfter LocalDate.of(2020, 10, 26)173 it.sent shouldBe true174 }175 }176 fun beSent() = object : Matcher<Boolean> {177 override fun test(value: Boolean) = MatcherResult(value, "Mail.sent should be true", "Mail.sent should be false")178 }179 fun Mail.shouldBeSent() = this.sent should beSent()180 fun Mail.shouldNotBeSent() = this.sent shouldNot beSent()181}182data class Mail(val dateCreated: LocalDate, val sent: Boolean, val message: String)183fun angryFunction() {184 throw IllegalStateException("How dare you!")185}...
KotestAsserts.kt
Source:KotestAsserts.kt
1package testing.asserts2import arrow.core.*3import io.kotest.assertions.arrow.either.shouldBeLeft4import io.kotest.assertions.arrow.either.shouldBeRight5import io.kotest.assertions.arrow.nel.shouldContain6import io.kotest.assertions.arrow.nel.shouldContainNull7import io.kotest.assertions.arrow.option.shouldBeNone8import io.kotest.assertions.arrow.option.shouldBeSome9import io.kotest.assertions.arrow.validation.shouldBeInvalid10import io.kotest.assertions.arrow.validation.shouldBeValid11import io.kotest.assertions.asClue12import io.kotest.assertions.json.*13import io.kotest.assertions.throwables.shouldThrowAny14import io.kotest.assertions.throwables.shouldThrowExactly15import io.kotest.assertions.withClue16import io.kotest.matchers.Matcher17import io.kotest.matchers.MatcherResult18import io.kotest.matchers.booleans.shouldBeFalse19import io.kotest.matchers.booleans.shouldBeTrue20import io.kotest.matchers.collections.shouldBeEmpty21import io.kotest.matchers.collections.shouldBeOneOf22import io.kotest.matchers.collections.shouldBeSameSizeAs23import io.kotest.matchers.collections.shouldBeSorted24import io.kotest.matchers.collections.shouldContain25import io.kotest.matchers.date.shouldBeAfter26import io.kotest.matchers.ints.shouldBeEven27import io.kotest.matchers.ints.shouldBeGreaterThan28import io.kotest.matchers.ints.shouldBeGreaterThanOrEqual29import io.kotest.matchers.ints.shouldBeLessThan30import io.kotest.matchers.ints.shouldBeLessThanOrEqual31import io.kotest.matchers.ints.shouldBeZero32import io.kotest.matchers.maps.shouldBeEmpty33import io.kotest.matchers.maps.shouldContain34import io.kotest.matchers.maps.shouldContainKey35import io.kotest.matchers.maps.shouldContainValue36import io.kotest.matchers.nulls.shouldBeNull37import io.kotest.matchers.should38import io.kotest.matchers.shouldBe39import io.kotest.matchers.shouldNot40import io.kotest.matchers.string.shouldBeBlank41import io.kotest.matchers.string.shouldBeEmpty42import io.kotest.matchers.string.shouldBeUpperCase43import io.kotest.matchers.string.shouldContain44import io.kotest.matchers.string.shouldNotBeBlank45import java.time.LocalDate46import org.junit.jupiter.api.Test47/**48 * Kotest assertions49 *50 * - [Kotest Assertions Documentation](https://kotest.io/assertions/)51 * - [Github](https://github.com/kotest/kotest/)52 */53class KotestAsserts {54 @Test55 fun `General assertions`() {56 "text" shouldBe "text"57 " ".shouldBeBlank()58 "hi".shouldNotBeBlank()59 "".shouldBeEmpty()60 "HI".shouldBeUpperCase()61 "hello".shouldContain("ll")62 true.shouldBeTrue()63 false.shouldBeFalse()64 null.shouldBeNull()65 10 shouldBeLessThan 1166 10 shouldBeLessThanOrEqual 1067 11 shouldBeGreaterThan 1068 11 shouldBeGreaterThanOrEqual 1169 10.shouldBeEven()70 0.shouldBeZero()71 }72 @Test73 fun `Exception assertions`() {74 shouldThrowExactly<IllegalStateException> {75 angryFunction()76 }77 shouldThrowAny {78 angryFunction()79 }80 }81 @Test82 fun `Collection assertions`() {83 emptyList<Int>().shouldBeEmpty()84 listOf(1, 2, 3) shouldContain 385 listOf(1, 2, 3).shouldBeSorted()86 listOf(1, 2, 3) shouldBeSameSizeAs listOf(4, 5, 6)87 1 shouldBeOneOf listOf(1, 2, 3)88 emptyMap<Int, String>().shouldBeEmpty()89 mapOf(1 to "one", 2 to "two", 3 to "three") shouldContainKey 190 mapOf(1 to "one", 2 to "two", 3 to "three") shouldContainValue "two"91 mapOf(1 to "one", 2 to "two", 3 to "three") shouldContain (3 to "three")92 }93 @Test94 fun `Arrow assertions`() {95 val optionNone = none<String>()96 val optionSome = Some("I am something").toOption()97 optionNone.shouldBeNone()98 optionSome.shouldBeSome()99 val rightEither = Either.Right(1)100 val leftEither = Either.Left("ERROR!!")101 rightEither.shouldBeRight()102 rightEither shouldBeRight 1103 leftEither.shouldBeLeft()104 leftEither shouldBeLeft "ERROR!!"105 val nonEmptyList = NonEmptyList.of(1, 2, 3, 4, 5, null)106 nonEmptyList shouldContain 1107 nonEmptyList.shouldContainNull()108 val valid = Validated.valid()109 val invalid = Validated.invalid()110 valid.shouldBeValid()111 invalid.shouldBeInvalid()112 }113 @Test114 fun `Json assertions`() {115 val jsonString = "{\"test\": \"property\", \"isTest\": true }"116 jsonString shouldMatchJson jsonString117 jsonString shouldContainJsonKey "$.test"118 jsonString shouldNotContainJsonKey "$.notTest"119 jsonString.shouldContainJsonKeyValue("$.isTest", true)120 }121 @Test122 fun `Custom assertions`() {123 val sentMail = Mail(124 dateCreated = LocalDate.of(2020, 10, 27),125 sent = true, message = "May you have an amazing day"126 )127 val unsentMail = Mail(128 dateCreated = LocalDate.of(2020, 10, 27),129 sent = false, message = "May you have an amazing day"130 )131 // This is possible132 sentMail.sent should beSent()133 unsentMail.sent shouldNot beSent()134 // This is recommended135 sentMail.shouldBeSent()136 unsentMail.shouldNotBeSent()137 }138 @Test139 fun `withClue usage`() {140 val mail = Mail(141 dateCreated = LocalDate.of(2020, 10, 27),142 sent = false, message = "May you have an amazing day"143 )144 withClue("sent field should be false") {145 mail.sent shouldBe false146 }147 mail.asClue {148 it.dateCreated shouldBeAfter LocalDate.of(2020, 10, 26)149 it.sent shouldBe false150 }151 }152 @Test153 fun `asClue usage`() {154 val mail = Mail(155 dateCreated = LocalDate.of(2020, 10, 27),156 sent = false, message = "May you have an amazing day"157 )158 mail.asClue {159 it.dateCreated shouldBeAfter LocalDate.of(2020, 10, 26)160 it.sent shouldBe false161 }162 }163 fun beSent() = object : Matcher<Boolean> {164 override fun test(value: Boolean) = MatcherResult(value, "Mail.sent should be true", "Mail.sent should be false")165 }166 fun Mail.shouldBeSent() = this.sent should beSent()167 fun Mail.shouldNotBeSent() = this.sent shouldNot beSent()168}169data class Mail(val dateCreated: LocalDate, val sent: Boolean, val message: String)170fun angryFunction() {171 throw IllegalStateException("How dare you!")172}...
KotlinTest.kt
Source:KotlinTest.kt
1package com.example.sampletestcode2import io.kotest.core.spec.style.*3import io.kotest.matchers.collections.sorted4import io.kotest.matchers.should5import io.kotest.matchers.shouldBe6import io.kotest.matchers.string.*7import io.kotest.matchers.collections.beEmpty8import io.kotest.matchers.maps.contain9import io.kotest.matchers.maps.haveKey10import io.kotest.matchers.maps.haveValue11class StringSpecTest : StringSpec() {12 // StringSpec를 ìì ë°ì¼ë©°, ë³´íµ init ë¸ë ë´ìì í
ì¤í¸ ì½ë를 ìì±íë¤.13 // init ë¸ë¡ ë´ë¶ì 문ìì´ì í
ì¤í¸ë¥¼ ì¤ëª
íë ë¶ë¶ì´ë©°, ë¸ë ë´ë¶ì ì½ëê° ì¤ì í
ì¤í¸ê° ì§íëë ë¶ë¶ì´ë¤.14 init {15 "문ìì´.lengthê° ë¬¸ìì´ì 길ì´ë¥¼ 리í´í´ì¼ í©ëë¤." {16 "kotlin test".length shouldBe 1117 }18 }19}20class FunSpecTest : FunSpec({21 // FunSpecë í¨ì ííë¡ í
ì¤í¸ ì½ë를 ìì±í ì ìëë¡ ëëë¤.22 // í
ì¤í¸ í¨ìì ë§¤ê° ë³ìë ë¨ì í
ì¤í¸ ì¤ëª
ì´ë©°, í
ì¤í¸ í¨ì ë´ë¶ì ì½ëê° ì¤ì í
ì¤í¸ê° ì§íëë ë¶ë¶ì´ë¤.23 test("문ìì´ì 길ì´ë¥¼ 리í´í´ì¼ í©ëë¤.") {24 "kotlin".length shouldBe 625 "".length shouldBe 026 }27})28class ShouldSpecTest : ShouldSpec({29 // ShouldSpecë FunSpecê³¼ ì ì¬íë¤. ë¤ë§, test ëì should í¤ìë를 ì¬ì©íë¤ë ì°¨ì´ì ì´ ìë¤.30 // ìë should ì ë§¤ê° ë³ìë ë¨ì ì¤ëª
ì´ê³ , í
ì¤í¸ë ìì ë¸ë ë´ë¶ìì ëìíë¤.31 should("문ìì´ì 길ì´ë¥¼ 리í´í´ì¼ í©ëë¤.") {32 "kotlin".length shouldBe 633 "".length shouldBe 034 }35})36class WordSpecTest : WordSpec({37 // String.length ë¶ë¶ì context stringì´ë¤. ì´ë í íê²½ìì í
ì¤í¸ë¥¼ ì§íí ê²ì¸ì§ë¥¼ ë§í´ 주ë ê²ì´ë¤.38 // ìë íê¸ì ìì ì¤ëª
ë¶ë¶ì´ë©°, ì½ë ë¸ë ë´ë¶ìì ì¤ì í
ì¤í¸ê° ëìíë¤.39 "String.length" should {40 "문ìì´ì 길ì´ë¥¼ 리í´í´ì¼ í©ëë¤." {41 "kotlin".length shouldBe 642 "".length shouldBe 043 }44 }45})46class BehaviorSpecTest : BehaviorSpec({47 // BehaviorSpecë BDD (Behaviour Driven Development)48 given("ì ê°ë½") {49 `when`("ì¡ëë¤.") {50 then("ììì 먹ëë¤.") {51 println("ì ê°ë½ì ì¡ê³ , ììì 먹ëë¤.")52 }53 }54 `when`("ëì§ë¤.") {55 then("ì¬ëì´ ë§ëë¤.") {56 println("ì ê°ë½ì ëì§ë©´, ì¬ëì´ ë§ëë¤.")57 }58 }59 }60})61class AnnotationSpecTest : AnnotationSpec() {62 // AnnotationSpecë JUnit ì¤íì¼(PersonTest íì¼ ì°¸ê³ )ë¡ í
ì¤í¸ ì½ë를 ìì±í ì ìë¤.63 @BeforeEach64 fun beforeTest() {65 println("Before Test, Setting")66 }67 @Test68 fun test1() {69 "test".length shouldBe 470 }71 @Test72 fun test2() {73 "test2".length shouldBe 574 }75}76class MatcherTest : StringSpec() {77 init {78 // shouldBeë ëì¼í¨ì ì²´í¬íë Matcher ì´ë¤.79 "hello World" shouldBe haveLength(11) // lengthê° ë§¤ê°ë³ìì ì ë¬ë ê°ì´ì´ì¼ í¨ì ì²´í¬íë¤.80 "hello" should include("ll") // 매ê°ë³ì ê°ì´ í¬í¨ëì´ ìëì§ íì¸íë¤.81 "hello" should endWith("lo") // 매ê°ë³ìì ëì´ í¬í¨ëëì§ íì¸íë¤.82 "hello" should match("he...") // 매ê°ë³ìê° ë§¤ì¹ëëì§ ì²´í¬íë¤.83 "hello".shouldBeLowerCase() // ì문ìë¡ ìì±ë ê²ì´ ë§ëì§ ì²´í¬íë¤.84 val list = emptyList<String>()85 val list2 = listOf("aaa", "bbb", "ccc")86 val map = mapOf<String, String>(Pair("aa", "11"))87 list should beEmpty() // ììê° ë¹ìëì§ íì¸íë¤....
04_PropertyBasedStrategies.kt
Source:04_PropertyBasedStrategies.kt
1package property.based.testing2import io.kotest.core.spec.style.ShouldSpec3import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder4import io.kotest.matchers.collections.shouldHaveSize5import io.kotest.matchers.maps.shouldContain6import io.kotest.matchers.shouldBe7import io.kotest.property.Arb8import io.kotest.property.arbitrary.int9import io.kotest.property.arbitrary.list10import io.kotest.property.checkAll11@Suppress("Unused")12class PropertyBasedStrategies : ShouldSpec({13 context("different paths, same destination") {14 xshould("negate and sort list") {15 checkAll(Arb.list(Arb.int(-10_000, 10_000))) { a ->16 a.map(Int::unaryMinus).sorted() shouldBe a.sorted().map(Int::unaryMinus).reversed()17 }18 }19 xshould("append then reverse is same like reverse then prepend") {20 checkAll(Arb.int(), Arb.list(Arb.int(-10_000, 10_000))) { additionalItem, list ->21 val appendThenReverse = list.appended(additionalItem).asReversed()22 val reversedThenPrepended = list.asReversed().prepended(additionalItem)23 appendThenReverse shouldBe reversedThenPrepended24 }25 }26 }27 context("there and back again") {28 xshould("insert - contains") {29 checkAll<Int, String> { a, b ->30 val myMap = mapOf(a to b)31 myMap shouldContain (a to b)32 }33 }34 xshould("serialization - deserialization") {35 checkAll(Arb.person()) { person ->36 JsonSerializer.fromString(JsonSerializer.toString(person)) shouldBe person37 }38 }39 }40 context("some things never change") {41 xshould("size of sorted list") {42 checkAll<List<Int>> { a ->43 a.sorted() shouldHaveSize a.size44 }45 }46 xshould("contain exactly same elements") {47 checkAll<List<Int>> { a ->48 a.sorted() shouldContainExactlyInAnyOrder a49 }50 }51 }52 context("The more things change, the more they stay the same") {53 xshould("filtering twice is same as filtering once") {54 checkAll<List<Int>> { a ->55 a.filter(::isEven).filter(::isEven) shouldContainExactlyInAnyOrder a.filter(::isEven)56 }57 }58 xshould("trim a string multiple times is same as trim it once") {59 checkAll<String> { a ->60 a.trim().trim().trim() shouldBe a.trim()61 }62 }63 }64 context("Solve a smaller problem first") {65 }66 context("Hard to prove, easy to verify") {67 xshould("prime factor") {68 checkAll(Arb.int(1..100)) { a ->69 a.primeFactors().reduce(Int::times) shouldBe a70 }71 }72 }73 context("The test oracle") {74 // naive vs optimized version75 xshould("test again an other implementation, maybe a not performant one") {76 checkAll<Int, Int> { a, b ->77 add(a, b) shouldBe a + b78 }79 }80 }81})82private fun List<Int>.appended(additionalItem: Int): List<Int> =83 this + additionalItem84private fun List<Int>.prepended(additionalItem: Int): List<Int> =85 also { myList ->86 val intermediateList = myList.toMutableList()87 intermediateList.add(0, additionalItem)88 intermediateList.toList()89 }...
MatcherTest.kt
Source:MatcherTest.kt
1package com.psg.kotest_example2import io.kotest.core.spec.style.StringSpec3import io.kotest.matchers.collections.sorted4import io.kotest.matchers.maps.contain5import io.kotest.matchers.maps.haveKey6import io.kotest.matchers.maps.haveValue7import io.kotest.matchers.should8import io.kotest.matchers.shouldBe9import io.kotest.matchers.string.*10class MatcherTest : StringSpec() {11 init {12 // 'shouldBe' ëì¼í¨ì ì²´í¬íë Matcher ì
ëë¤.13 "hello world" shouldBe haveLength(11) // lengthê° 11ì´ì´ì¼ í¨ì ì²´í¬ í©ëë¤.14 "hello" should include("ll") // íë¼ë¯¸í°ê° í¬í¨ëì´ ìëì§ ì²´í¬ í©ëë¤.15 "hello" should endWith("lo") // íë¼ë¯¸í°ê° ëì í¬í¨ëëì§ ì²´í¬ í©ëë¤.16 "hello" should match("he...") // íë¼ë¯¸í°ê° 매ì¹ëëì§ ì²´í¬ í©ëë¤.17 "hello".shouldBeLowerCase() // ì문ìë¡ ìì±ëìëì§ ì²´í¬ í©ëë¤.18 val list = emptyList<String>()19 val list2 = listOf("aaa", "bbb", "ccc")20 val map = mapOf<String, String>(Pair("aa", "11"))21 list should beEmpty() // ììê° ë¹ìëì§ ì²´í¬ í©ëë¤.22 list2 shouldBe sorted<String>() // í´ë¹ ìë£íì´ ì ë ¬ ëìëì§ ì²´í¬ í©ëë¤.23 map should contain("aa", "11") // í´ë¹ ììê° í¬í¨ëìëì§ ì²´í¬ í©ëë¤....
ExampleUnitTest.kt
Source:ExampleUnitTest.kt
1package pl.gdg.myapplication2import io.kotest.matchers.collections.shouldBeSortedWith3import io.kotest.matchers.collections.shouldNotContain4import io.kotest.matchers.ints.shouldBeExactly5import org.junit.Assert.assertEquals6import org.junit.Test7/**8 * Example local unit test, which will execute on the development machine (host).9 *10 * See [testing documentation](http://d.android.com/tools/testing).11 */12class ExampleUnitTest {13 @Test14 fun addition_isCorrect() {15 assertEquals(4, 2 + 2)16 }17 @Test18 fun `addition is correct`() {19 2 + 2 shouldBeExactly 420 }21 @Test22 fun `list contains odd numbers`() {23 listOf(1, 3, 5) shouldNotContain 724 listOf(1, 3, 5) shouldBeSortedWith Int::compareTo...
test
Using AI Code Generation
1 test("sorted") {2 sorted(listOf(1, 2, 3)) shouldBe listOf(1, 2, 3)3 sorted(listOf(3, 2, 1)) shouldBe listOf(1, 2, 3)4 sorted(listOf(1, 3, 2)) shouldBe listOf(1, 2, 3)5 sorted(listOf(1, 2, 3), reverseOrder()) shouldBe listOf(3, 2, 1)6 }7})8 test("sorted") {9 listOf(1, 2, 3) shouldBeSorted { a, b -> a < b }10 listOf(3, 2, 1) shouldBeSorted { a, b -> a > b }11 listOf(1, 2, 3) shouldBeSorted reverseOrder()12 }13})14 test("sorted") {15 listOf(1, 2, 3) shouldBeSortedWith naturalOrder()16 listOf(3, 2, 1) shouldBeSortedWith reverseOrder()17 }18})19 test("sorted") {20 listOf(1, 2, 3) shouldBeSortedBy { it }21 listOf(3, 2, 1) shouldBeSortedBy { -it }22 }23})24 test("sorted") {25 listOf(1, 2, 3) shouldBeSortedByDescending { -it }26 listOf(3, 2, 1) shouldBeSortedByDescending { it }27 }28})29 test("sorted") {30 listOf(1, 2, 3) shouldBeSortedBy { it }31 listOf(3, 2, 1) shouldBeSortedBy { -it }32 }33})
test
Using AI Code Generation
1list should sorted().reverse()2list should sorted().by { a, b -> a > b }3list should sorted().by { a, b -> a > b }4list should sorted().by { a, b -> a > b }5list should sorted().by { a, b -> a > b }6list should sorted().by { a, b -> a > b }7list should sorted().by { a, b -> a > b }8list should sorted().by { a, b -> a > b }9list should sorted().by { a, b -> a > b }10list should sorted().by { a, b -> a > b }11list should sorted().by { a, b -> a > b }12list should sorted().by { a, b -> a > b }13The sorted matcher also supports a by() method which allows you to specify a comparator function to check the list against
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!!