Best Kotest code snippet using io.kotest.matchers.collections.sorted
ReasonableDefaultElementsTest.kt
Source:ReasonableDefaultElementsTest.kt
...92 }93 }94 }95 "All default elements" should {96 "be sorted" {97 allDefaultElements98 .keys99 .toList()100 .shouldBeSorted()101 Elements.allElements()102 .keys103 .toList()104 .shouldBeSorted()105 Elements.allCommandingElements()106 .keys107 .toList()108 .shouldBeSorted()109 }110 "have a name" {...
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() // ììê° ë¹ìëì§ íì¸íë¤.88 list2 shouldBe sorted<String>() // í´ë¹ ìë£íì´ ì ë ¬ëì´ ìëì§ íì¸íë¤.89 map should contain("aa", "11") // í´ë¹ ììê° í¬í¨ëì´ ìëì§ íì¸íë¤.90 map should haveKey("aa") // í´ë¹ í¤ ê°ì´ í¬í¨ëì´ ìëì§ íì¸íë¤.91 map should haveValue("11") // í´ë¹ value ê°ì´ í¬í¨ëì´ ìëì§ íì¸íë¤.92 }93}...
Nel.kt
Source:Nel.kt
1package io.kotest.assertions.arrow.core2import arrow.core.NonEmptyList3import io.kotest.matchers.collections.shouldBeSorted4import io.kotest.matchers.collections.shouldNotBeSorted5import io.kotest.matchers.collections.shouldBeUnique6import io.kotest.matchers.collections.shouldContain7import io.kotest.matchers.collections.shouldContainAll8import io.kotest.matchers.collections.shouldContainDuplicates9import io.kotest.matchers.collections.shouldContainNoNulls10import io.kotest.matchers.collections.shouldContainNull11import io.kotest.matchers.collections.shouldContainOnlyNulls12import io.kotest.matchers.collections.shouldHaveElementAt13import io.kotest.matchers.collections.shouldHaveSingleElement14import io.kotest.matchers.collections.shouldHaveSize15import io.kotest.matchers.collections.shouldNotHaveSize16import io.kotest.matchers.collections.shouldNotBeUnique17import io.kotest.matchers.collections.shouldNotContain18import io.kotest.matchers.collections.shouldNotContainAll19import io.kotest.matchers.collections.shouldNotContainDuplicates20import io.kotest.matchers.collections.shouldNotContainNoNulls21import io.kotest.matchers.collections.shouldNotContainNull22import io.kotest.matchers.collections.shouldNotContainOnlyNulls23import io.kotest.matchers.collections.shouldNotHaveElementAt24import io.kotest.matchers.collections.shouldNotHaveSingleElement25public fun <A> NonEmptyList<A>.shouldContainOnlyNulls(): NonEmptyList<A> =26 apply { all.shouldContainOnlyNulls() }27public fun <A> NonEmptyList<A>.shouldNotContainOnlyNulls(): NonEmptyList<A> =28 apply { all.shouldNotContainOnlyNulls() }29public fun <A> NonEmptyList<A>.shouldContainNull(): NonEmptyList<A> =30 apply { all.shouldContainNull() }31public fun <A> NonEmptyList<A>.shouldNotContainNull(): NonEmptyList<A> =32 apply { all.shouldNotContainNull() }33public fun <A> NonEmptyList<A>.shouldHaveElementAt(index: Int, element: A): Unit =34 all.shouldHaveElementAt(index, element)35public fun <A> NonEmptyList<A>.shouldNotHaveElementAt(index: Int, element: A): Unit =36 all.shouldNotHaveElementAt(index, element)37public fun <A> NonEmptyList<A>.shouldContainNoNulls(): NonEmptyList<A> =38 apply { all.shouldContainNoNulls() }39public fun <A> NonEmptyList<A>.shouldNotContainNoNulls(): NonEmptyList<A> =40 apply { all.shouldNotContainNoNulls() }41public infix fun <A> NonEmptyList<A>.shouldContain(a: A): Unit {42 all.shouldContain(a)43}44public infix fun <A> NonEmptyList<A>.shouldNotContain(a: A): Unit {45 all.shouldNotContain(a)46}47public fun <A> NonEmptyList<A>.shouldBeUnique(): NonEmptyList<A> =48 apply { all.shouldBeUnique() }49public fun <A> NonEmptyList<A>.shouldNotBeUnique(): NonEmptyList<A> =50 apply { all.shouldNotBeUnique() }51public fun <A> NonEmptyList<A>.shouldContainDuplicates(): NonEmptyList<A> =52 apply { all.shouldContainDuplicates() }53public fun <A> NonEmptyList<A>.shouldNotContainDuplicates(): NonEmptyList<A> =54 apply { all.shouldNotContainDuplicates() }55public fun <A> NonEmptyList<A>.shouldContainAll(vararg ts: A): Unit =56 all.shouldContainAll(*ts)57public fun <A> NonEmptyList<A>.shouldNotContainAll(vararg ts: A): Unit =58 all.shouldNotContainAll(*ts)59public infix fun <A> NonEmptyList<A>.shouldContainAll(ts: List<A>): Unit =60 all.shouldContainAll(ts)61public infix fun <A> NonEmptyList<A>.shouldNotContainAll(ts: List<A>): Unit =62 all.shouldNotContainAll(ts)63public infix fun <A> NonEmptyList<A>.shouldHaveSize(size: Int): NonEmptyList<A> =64 apply { all.shouldHaveSize(size) }65public infix fun <A> NonEmptyList<A>.shouldNotHaveSize(size: Int): NonEmptyList<A> =66 apply { all.shouldNotHaveSize(size) }67public infix fun <A> NonEmptyList<A>.shouldHaveSingleElement(a: A): Unit =68 all.shouldHaveSingleElement(a)69public infix fun <A> NonEmptyList<A>.shouldNotHaveSingleElement(a: A): Unit =70 all.shouldNotHaveSingleElement(a)71public fun <A : Comparable<A>> NonEmptyList<A>.shouldBeSorted(): NonEmptyList<A> =72 apply { all.shouldBeSorted() }73public fun <A : Comparable<A>> NonEmptyList<A>.shouldNotBeSorted(): NonEmptyList<A> =74 apply { all.shouldNotBeSorted() }...
04_PropertyBasedStrategies.kt
Source:04_PropertyBasedStrategies.kt
...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 }...
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") // í´ë¹ ììê° í¬í¨ëìëì§ ì²´í¬ í©ëë¤.24 map should haveKey("aa") // í´ë¹ keyê° í¬í¨ëìëì§ ì²´í¬ í©ëë¤.25 map should haveValue("11") // í´ë¹ valueê° í¬í¨ëìëì§ ì²´í¬ í©ëë¤.26 }27}...
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::compareTo25 }26}...
sorted
Using AI Code Generation
1val expected = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)2val actual = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).shuffled()3actual should sorted()4val expected = listOf(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)5val actual = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).shuffled()6actual should sorted(reverseOrder())7val expected = listOf("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")8val actual = listOf("a", "b", "c", "d", "e", "f", "g", "h", "i", "j").shuffled()9actual should sorted()10val expected = listOf("j", "i", "h", "g", "f", "e", "d", "c", "b", "a")11val actual = listOf("a", "b", "c", "d", "e", "f", "g", "h", "i", "j").shuffled()12actual should sorted(reverseOrder())13val expected = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)14val actual = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).shuffled()15actual should sortedWith(compareBy { it })16val expected = listOf(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
sorted
Using AI Code Generation
1import io.kotest.matchers.collections.*2val list = listOf(1, 2, 3, 4, 5)3list shouldContainAll listOf(1, 2, 3)4list shouldContainInOrder listOf(1, 2, 3)5list shouldContainExactlyInOrder listOf(1, 2, 3)6list shouldContainAllInOrder listOf(1, 2, 3)7list shouldContainExactlyAllInOrder listOf(1, 2, 3)8list shouldContainNone listOf(6, 7, 8)9list shouldContainExactly listOf(1, 2, 3, 4, 5)10list shouldContainSame listOf(5, 4, 3, 2, 1)11list shouldContainExactlyInAnyOrder listOf(5, 4, 3, 2, 1)12list shouldContainSameInAnyOrder listOf(5, 4, 3, 2, 1)13list shouldContainExactlyInAnyOrder listOf(5, 4, 3, 2, 1)14list shouldContainSameInAnyOrder listOf(5, 4, 3, 2, 1)15list shouldContainExactlyInAnyOrder listOf(5, 4, 3, 2, 1)16list shouldContainSameInAnyOrder listOf(5, 4, 3, 2, 1)17list shouldContainExactlyInAnyOrder listOf(5, 4, 3, 2, 1)18list shouldContainSameInAnyOrder listOf(5, 4, 3, 2, 1)19list shouldContainExactlyInAnyOrder listOf(5, 4, 3, 2, 1)20list shouldContainSameInAnyOrder listOf(5, 4, 3, 2, 1)21list shouldContainExactlyInAnyOrder listOf(5, 4, 3, 2, 1)22list shouldContainSameInAnyOrder listOf(5, 4, 3, 2, 1)23list shouldContainExactlyInAnyOrder listOf(5, 4, 3, 2, 1)24list shouldContainSameInAnyOrder listOf(5, 4, 3, 2, 1)25list shouldContainExactlyInAnyOrder listOf(5
sorted
Using AI Code Generation
1val list = listOf(1, 2, 3, 4, 5, 6)2list should beSorted()3list should beSorted(reverseOrder())4list should beSortedBy { it % 3 }5list should beSortedBy(reverseOrder()) { it % 3 }6list should beSortedBy { it % 3 }.using(reverseOrder())7list should beSortedBy(reverseOrder()) { it % 3 }.using(reverseOrder())8list should beSortedBy { it % 3 }.using(reverseOrder())9list should beSortedBy(reverseOrder()) { it % 3 }.using(reverseOrder())10list should beSortedBy { it % 3 }11list should beSortedBy(reverseOrder()) { it % 3 }12list should beSortedBy { it % 3 }.using(reverseOrder())13list should beSortedBy(reverseOrder()) { it % 3 }.using(reverseOrder())14list should beSortedBy { it % 3 }15list should beSortedBy(reverseOrder()) { it % 3 }16list should beSortedBy { it % 3 }.using(reverseOrder())17list should beSortedBy(reverseOrder()) { it % 3 }.using(reverseOrder())18list should beSortedBy { it % 3 }19list should beSortedBy(reverseOrder()) { it % 3 }20list should beSortedBy { it % 3 }.using(reverseOrder())21list should beSortedBy(reverseOrder()) { it % 3 }.using(reverseOrder())22list should beSortedBy { it % 3 }23list should beSortedBy(reverseOrder()) { it % 3 }24list should beSortedBy { it % 3 }.using(reverseOrder())25list should beSortedBy(reverseOrder()) { it % 3 }.using(reverseOrder())26list should beSortedBy { it % 3 }27list should beSortedBy(reverseOrder()) { it % 3 }28list should beSortedBy { it % 3 }.using(reverseOrder())29list should beSortedBy(reverseOrder()) { it % 3 }.using(reverseOrder())30list should beSortedBy { it % 3 }31list should beSortedBy(reverseOrder()) { it % 3 }32list should beSortedBy { it % 3 }.using(reverseOrder())33list should beSortedBy(reverseOrder()) { it % 3 }.using(reverseOrder())34list should beSortedBy { it % 3 }35list should beSortedBy(reverseOrder()) { it % 3 }
sorted
Using AI Code Generation
1+data class Person(val name: String, val age: Int)2+fun main() {3+ val people = listOf(Person("Alice", 29), Person("Bob", 31))4+ people should sorted(Person::age)5+ people should sorted(Person::age, reverseOrder())6+ people should sorted(Person::name)7+}8+data class Person(val name: String, val age: Int)9+fun main() {10+ val people = listOf(Person("Alice", 29), Person("Bob", 31))11+ people should sorted(Person::age)12+ people should sorted(Person::age, reverseOrder())13+ people should sorted(Person::name)14+}15+data class Person(val name: String, val age: Int)16+fun main() {17+ val people = listOf(Person("Alice", 29), Person("Bob", 31))18+ people should sorted(Person::age)19+ people should sorted(Person::age, reverseOrder())20+ people should sorted(Person::name)21+}22+data class Person(val name: String, val age: Int)23+fun main() {24+ val people = listOf(Person("Alice",
sorted
Using AI Code Generation
1 test("sorting") {2 val list = listOf(3, 2, 1)3 list should beSorted()4 }5 test("sorting") {6 val list = listOf(3, 2, 1)7 list should beSorted()8 }9 test("sorting") {10 val list = listOf(3, 2, 1)11 list should beSorted()12 }13 test("sorting") {14 val list = listOf(3, 2, 1)15 list should beSorted()16 }17 test("sorting") {18 val list = listOf(3, 2, 1)19 list should beSorted()20 }21 test("sorting") {22 val list = listOf(3, 2, 1)23 list should beSorted()24 }25 test("sorting") {26 val list = listOf(3, 2, 1)27 list should beSorted()28 }29 test("sorting") {30 val list = listOf(3, 2, 1)31 list should beSorted()32 }33 test("sorting") {34 val list = listOf(3, 2, 1)35 list should beSorted()36 }37 test("sorting") {38 val list = listOf(3, 2, 1)39 list should beSorted()40 }41 test("sorting") {42 val list = listOf(3, 2, 1)43 list should beSorted()44 }
sorted
Using AI Code Generation
1val list = listOf(1, 2, 3, 4, 5)2list.shouldBeSorted()3list.shouldBeSorted(reverseOrder())4list.shouldBeSortedDescending()5val collection = listOf(1, 2, 3, 4, 5)6collection.shouldBeSorted()7collection.shouldBeSorted(reverseOrder())8collection.shouldBeSortedDescending()9val array = arrayOf(1, 2, 3, 4, 5)10array.shouldBeSorted()11array.shouldBeSorted(reverseOrder())12array.shouldBeSortedDescending()13val sequence = listOf(1, 2, 3, 4, 5).asSequence()14sequence.shouldBeSorted()15sequence.shouldBeSorted(reverseOrder())16sequence.shouldBeSortedDescending()17val map = mapOf(1 to "one", 2 to "two", 3 to "three")18map.shouldBeSorted()19map.shouldBeSortedBy { it.key }20map.shouldBeSortedBy { it.value }21map.shouldBeSortedBy { it.value.length }22map.shouldBeSortedBy { it.value.length }.reversed()23map.shouldBeSortedBy { it.value.length }.withReverseOrder()24map.shouldBeSortedBy { it.value.length }.withReverseOrder().reversed()25map.shouldBeSortedBy { it.value.length }.withReverseOrder().reversed().reversed()26map.shouldBeSortedBy { it.value.length }.withReverseOrder().reversed().reversed().withReverseOrder()27map.shouldBeSortedBy { it.value.length }.withReverseOrder().reversed().reversed().withReverseOrder().reversed()28val map = mapOf(1 to "one", 2 to "two", 3 to "three")29map.shouldBeSorted()30map.shouldBeSortedBy { it.key }
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!!