How to use Array.shouldHaveElementAt method of io.kotest.matchers.collections.matchers class

Best Kotest code snippet using io.kotest.matchers.collections.matchers.Array.shouldHaveElementAt

CollectionMatchersTest.kt

Source:CollectionMatchersTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.matchers.collections2import io.kotest.assertions.shouldFail3import io.kotest.assertions.throwables.shouldNotThrow4import io.kotest.assertions.throwables.shouldThrow5import io.kotest.assertions.withClue6import io.kotest.core.spec.style.WordSpec7import io.kotest.equals.Equality8import io.kotest.equals.types.byObjectEquality9import io.kotest.matchers.collections.atLeastSize10import io.kotest.matchers.collections.atMostSize11import io.kotest.matchers.collections.beLargerThan12import io.kotest.matchers.collections.beSameSizeAs13import io.kotest.matchers.collections.beSmallerThan14import io.kotest.matchers.collections.contain15import io.kotest.matchers.collections.containDuplicates16import io.kotest.matchers.collections.containNoNulls17import io.kotest.matchers.collections.containNull18import io.kotest.matchers.collections.containOnlyNulls19import io.kotest.matchers.collections.matchInOrder20import io.kotest.matchers.collections.existInOrder21import io.kotest.matchers.collections.haveElementAt22import io.kotest.matchers.collections.haveSize23import io.kotest.matchers.collections.matchEach24import io.kotest.matchers.collections.matchInOrderSubset25import io.kotest.matchers.collections.monotonicallyDecreasing26import io.kotest.matchers.collections.monotonicallyDecreasingWith27import io.kotest.matchers.collections.monotonicallyIncreasing28import io.kotest.matchers.collections.monotonicallyIncreasingWith29import io.kotest.matchers.collections.shouldBeIn30import io.kotest.matchers.collections.shouldBeLargerThan31import io.kotest.matchers.collections.shouldBeMonotonicallyDecreasing32import io.kotest.matchers.collections.shouldBeMonotonicallyDecreasingWith33import io.kotest.matchers.collections.shouldBeMonotonicallyIncreasing34import io.kotest.matchers.collections.shouldBeMonotonicallyIncreasingWith35import io.kotest.matchers.collections.shouldBeSameSizeAs36import io.kotest.matchers.collections.shouldBeSingleton37import io.kotest.matchers.collections.shouldBeSmallerThan38import io.kotest.matchers.collections.shouldBeSorted39import io.kotest.matchers.collections.shouldBeSortedBy40import io.kotest.matchers.collections.shouldBeSortedWith41import io.kotest.matchers.collections.shouldBeStrictlyDecreasing42import io.kotest.matchers.collections.shouldBeStrictlyDecreasingWith43import io.kotest.matchers.collections.shouldBeStrictlyIncreasing44import io.kotest.matchers.collections.shouldBeStrictlyIncreasingWith45import io.kotest.matchers.collections.shouldContainAnyOf46import io.kotest.matchers.collections.shouldContainDuplicates47import io.kotest.matchers.collections.shouldContainNoNulls48import io.kotest.matchers.collections.shouldContainNull49import io.kotest.matchers.collections.shouldContainOnlyNulls50import io.kotest.matchers.collections.shouldExist51import io.kotest.matchers.collections.shouldHaveAtLeastSize52import io.kotest.matchers.collections.shouldHaveAtMostSize53import io.kotest.matchers.collections.shouldHaveElementAt54import io.kotest.matchers.collections.shouldHaveSingleElement55import io.kotest.matchers.collections.shouldHaveSize56import io.kotest.matchers.collections.shouldMatchInOrder57import io.kotest.matchers.collections.shouldMatchInOrderSubset58import io.kotest.matchers.collections.shouldNotBeIn59import io.kotest.matchers.collections.shouldNotBeMonotonicallyDecreasing60import io.kotest.matchers.collections.shouldNotBeMonotonicallyDecreasingWith61import io.kotest.matchers.collections.shouldNotBeMonotonicallyIncreasing62import io.kotest.matchers.collections.shouldNotBeMonotonicallyIncreasingWith63import io.kotest.matchers.collections.shouldNotBeSingleton64import io.kotest.matchers.collections.shouldNotBeSorted65import io.kotest.matchers.collections.shouldNotBeSortedBy66import io.kotest.matchers.collections.shouldNotBeSortedWith67import io.kotest.matchers.collections.shouldNotBeStrictlyDecreasing68import io.kotest.matchers.collections.shouldNotBeStrictlyDecreasingWith69import io.kotest.matchers.collections.shouldNotBeStrictlyIncreasing70import io.kotest.matchers.collections.shouldNotBeStrictlyIncreasingWith71import io.kotest.matchers.collections.shouldNotContainAnyOf72import io.kotest.matchers.collections.shouldNotContainDuplicates73import io.kotest.matchers.collections.shouldNotContainNoNulls74import io.kotest.matchers.collections.shouldNotContainNull75import io.kotest.matchers.collections.shouldNotContainOnlyNulls76import io.kotest.matchers.collections.shouldNotHaveElementAt77import io.kotest.matchers.collections.shouldNotHaveSize78import io.kotest.matchers.collections.shouldNotMatchEach79import io.kotest.matchers.collections.shouldNotMatchInOrder80import io.kotest.matchers.collections.shouldNotMatchInOrderSubset81import io.kotest.matchers.collections.singleElement82import io.kotest.matchers.collections.sorted83import io.kotest.matchers.collections.strictlyDecreasing84import io.kotest.matchers.collections.strictlyDecreasingWith85import io.kotest.matchers.collections.strictlyIncreasing86import io.kotest.matchers.collections.strictlyIncreasingWith87import io.kotest.matchers.ints.shouldBeGreaterThan88import io.kotest.matchers.ints.shouldBeInRange89import io.kotest.matchers.should90import io.kotest.matchers.shouldBe91import io.kotest.matchers.shouldHave92import io.kotest.matchers.shouldNot93import io.kotest.matchers.shouldNotBe94import io.kotest.matchers.shouldNotHave95import io.kotest.matchers.throwable.shouldHaveMessage96class CollectionMatchersTest : WordSpec() {97 private val countdown = (10 downTo 0).toList()98 private val asc = { a: Int, b: Int -> a - b }99 private val desc = { a: Int, b: Int -> b - a }100 init {101 "a descending non-empty list" should {102 "fail to ascend" {103 shouldFail {104 countdown.shouldBeSortedWith(asc)105 }106 }107 "descend" {108 countdown.shouldBeSortedWith(desc)109 }110 "not ascend" {111 countdown.shouldNotBeSortedWith(asc)112 }113 "fail not to descend" {114 shouldFail {115 countdown.shouldNotBeSortedWith(desc)116 }117 }118 }119 "sortedWith" should {120 val items = listOf(121 1 to "I",122 2 to "II",123 4 to "IV",124 5 to "V",125 6 to "VI",126 9 to "IX",127 10 to "X"128 )129 "work on non-Comparable given a Comparator" {130 items.shouldBeSortedWith(Comparator { a, b -> asc(a.first, b.first) })131 }132 "work on non-Comparable given a compare function" {133 items.shouldBeSortedWith { a, b -> asc(a.first, b.first) }134 }135 }136 "haveElementAt" should {137 "test that a collection contains the specified element at the given index" {138 listOf("a", "b", "c") should haveElementAt(1, "b")139 listOf("a", "b", "c") shouldNot haveElementAt(1, "c")140 listOf("a", "b", null) should haveElementAt(2, null)141 listOf("a", "b", null) shouldNot haveElementAt(3, null)142 listOf("a", "b", "c").shouldHaveElementAt(1, "b")143 listOf("a", "b", "c").shouldNotHaveElementAt(1, "c")144 listOf("a", "b", null).shouldHaveElementAt(2, null)145 }146 "support type inference for subtypes of collection" {147 val tests = listOf(148 TestSealed.Test1("test1"),149 TestSealed.Test2(2)150 )151 tests should haveElementAt(0, TestSealed.Test1("test1"))152 tests.shouldHaveElementAt(1, TestSealed.Test2(2))153 }154 }155 "containNull()" should {156 "test that a collection contains at least one null" {157 listOf(1, 2, null) should containNull()158 listOf(null) should containNull()159 listOf(1, 2) shouldNot containNull()160 listOf(1, 2, null).shouldContainNull()161 listOf(null).shouldContainNull()162 listOf(1, 2).shouldNotContainNull()163 }164 }165 "sorted" should {166 "test that a collection is sorted" {167 emptyList<Int>() shouldBe sorted<Int>()168 listOf(1) shouldBe sorted<Int>()169 listOf(1, 2, 3, 4) shouldBe sorted<Int>()170 shouldThrow<AssertionError> {171 listOf(2, 1) shouldBe sorted<Int>()172 }.shouldHaveMessage("List [2, 1] should be sorted. Element 2 at index 0 was greater than element 1")173 listOf(1, 2, 6, 9).shouldBeSorted()174 shouldThrow<AssertionError> {175 listOf(2, 1).shouldBeSorted()176 }.shouldHaveMessage("List [2, 1] should be sorted. Element 2 at index 0 was greater than element 1")177 shouldThrow<AssertionError> {178 listOf(1, 2, 3).shouldNotBeSorted()179 }.shouldHaveMessage("List [1, 2, 3] should not be sorted")180 }181 "restrict items at the error message" {182 val longList = (1..1000).toList()183 shouldThrow<AssertionError> {184 longList.shouldNotBeSorted()185 }.shouldHaveMessage("List [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...and 980 more (set the 'kotest.assertions.collection.print.size' JVM property to see more / less items)] should not be sorted")186 }187 }188 "sortedBy" should {189 val items = listOf(190 1 to "I",191 2 to "II",192 4 to "IV",193 5 to "V",194 6 to "VI",195 9 to "IX",196 10 to "X"197 )198 "compare by the tranformed value" {199 items.shouldBeSortedBy { it.first }200 items.shouldNotBeSortedBy { it.second }201 }202 }203 "shouldBeIncreasing" should {204 "test that a collection is monotonically increasing" {205 listOf(1, 2, 2, 3) shouldBe monotonicallyIncreasing<Int>()206 listOf(6, 5) shouldNotBe monotonicallyIncreasing<Int>()207 listOf(1, 2, 2, 3).shouldBeMonotonicallyIncreasing()208 listOf(6, 5).shouldNotBeMonotonicallyIncreasing()209 }210 "test that a collection is monotonically increasing according to comparator" {211 val comparator = Comparator(desc)212 listOf(3, 2, 2, 1) shouldBe monotonicallyIncreasingWith(comparator)213 listOf(5, 6) shouldNotBe monotonicallyIncreasingWith(comparator)214 listOf(3, 2, 2, 1).shouldBeMonotonicallyIncreasingWith(comparator)215 listOf(5, 6).shouldNotBeMonotonicallyIncreasingWith(comparator)216 }217 "test that a collection is strictly increasing" {218 listOf(1, 2, 3) shouldBe strictlyIncreasing<Int>()219 listOf(1, 2, 2, 3) shouldNotBe strictlyIncreasing<Int>()220 listOf(6, 5) shouldNotBe strictlyIncreasing<Int>()221 listOf(1, 2, 3).shouldBeStrictlyIncreasing()222 listOf(1, 2, 2, 3).shouldNotBeStrictlyIncreasing()223 listOf(6, 5).shouldNotBeStrictlyIncreasing()224 }225 "test that a collection is strictly increasing according to comparator" {226 val comparator = Comparator(desc)227 listOf(3, 2, 1) shouldBe strictlyIncreasingWith(comparator)228 listOf(3, 2, 2, 1) shouldNotBe strictlyIncreasingWith(comparator)229 listOf(5, 6) shouldNotBe strictlyIncreasingWith(comparator)230 listOf(3, 2, 1).shouldBeStrictlyIncreasingWith(comparator)231 listOf(3, 2, 2, 1).shouldNotBeStrictlyIncreasingWith(comparator)232 listOf(5, 6).shouldNotBeStrictlyIncreasingWith(comparator)233 }234 }235 "shouldBeDecreasing" should {236 "test that a collection is monotonically decreasing" {237 listOf(3, 2, 2, -4) shouldBe monotonicallyDecreasing<Int>()238 listOf(5, 6) shouldNotBe monotonicallyDecreasing<Int>()239 listOf(3, 2, 2, -4).shouldBeMonotonicallyDecreasing()240 listOf(5, 6).shouldNotBeMonotonicallyDecreasing()241 }242 "test that a collection is monotonically decreasing according to comparator" {243 val comparator = Comparator(desc)244 listOf(-4, 2, 2, 3) shouldBe monotonicallyDecreasingWith(comparator)245 listOf(6, 5) shouldNotBe monotonicallyDecreasingWith(comparator)246 listOf(-4, 2, 2, 3).shouldBeMonotonicallyDecreasingWith(comparator)247 listOf(6, 5).shouldNotBeMonotonicallyDecreasingWith(comparator)248 }249 "test that a collection is strictly decreasing" {250 listOf(3, 2, -4) shouldBe strictlyDecreasing<Int>()251 listOf(3, 2, 2, -4) shouldNotBe strictlyDecreasing<Int>()252 listOf(5, 6) shouldNotBe strictlyDecreasing<Int>()253 listOf(3, 2, -4).shouldBeStrictlyDecreasing()254 listOf(3, 2, 2, -4).shouldNotBeStrictlyDecreasing()255 listOf(5, 6).shouldNotBeStrictlyDecreasing()256 }257 "test that a collection is strictly decreasing according to comparator" {258 val comparator = Comparator(desc)259 listOf(-4, 2, 3) shouldBe strictlyDecreasingWith(comparator)260 listOf(-4, 2, 2, 3) shouldNotBe strictlyDecreasingWith(comparator)261 listOf(6, 5) shouldNotBe strictlyDecreasingWith(comparator)262 listOf(-4, 2, 3).shouldBeStrictlyDecreasingWith(comparator)263 listOf(-4, 2, 2, 3).shouldNotBeStrictlyDecreasingWith(comparator)264 listOf(6, 5).shouldNotBeStrictlyDecreasingWith(comparator)265 }266 }267 "haveDuplicates" should {268 "test that a collection is unique" {269 listOf(1, 2, 3, 3) should containDuplicates()270 listOf(1, 2, 3, 4) shouldNot containDuplicates()271 listOf(1, 2, 3, 3).shouldContainDuplicates()272 listOf(1, 2, 3, 4).shouldNotContainDuplicates()273 }274 }275 "singleElement" should {276 "test that a collection contains a single given element" {277 listOf(1) shouldBe singleElement(1)278 listOf(1).shouldHaveSingleElement(1)279 shouldThrow<AssertionError> {280 listOf(1) shouldBe singleElement(2)281 }.shouldHaveMessage("Collection should be a single element of 2 but has 1 elements: [1]")282 shouldThrow<AssertionError> {283 listOf(1, 2) shouldBe singleElement(2)284 }.shouldHaveMessage("Collection should be a single element of 2 but has 2 elements: [1, 2]")285 }286 }287 "singleElement with predicate" should {288 "test that a collection contains a single element by given predicate" {289 listOf(1) shouldHave singleElement { e -> e == 1 }290 listOf(1).shouldHaveSingleElement { e -> e == 1 }291 shouldThrow<AssertionError> {292 listOf(1) shouldHave singleElement { e -> e == 2 }293 }.shouldHaveMessage("Collection should have a single element by a given predicate but has 0 elements: [1]")294 shouldThrow<AssertionError> {295 listOf(2, 2) shouldHave singleElement { e -> e == 2 }296 }.shouldHaveMessage("Collection should have a single element by a given predicate but has 2 elements: [2, 2]")297 }298 }299 "should contain element" should {300 "test that a collection contains an element" {301 val col = listOf(1, 2, 3)302 col should contain(2)303 col should contain(2.0) // uses strict num equality = false304 shouldThrow<AssertionError> {305 col should contain(4)306 }.shouldHaveMessage("Collection should contain element 4 based on object equality; but the collection is [1, 2, 3]")307 }308 }309 "should contain element based on a custom equality object" should {310 "test that a collection contains an element" {311 val col = listOf(1, 2, 3.0)312 val verifier = Equality.byObjectEquality<Number>(strictNumberEquality = true)313 col should contain(2, verifier)314 col should contain(3.0, verifier)315 shouldThrow<AssertionError> {316 col should contain(3, verifier)317 }.shouldHaveMessage("Collection should contain element 3 based on object equality; but the collection is [1, 2, 3.0]")318 }319 }320 "shouldBeLargerThan" should {321 "test that a collection is larger than another collection" {322 val col1 = listOf(1, 2, 3)323 val col2 = setOf(1, 2, 3, 4)324 col2.shouldBeLargerThan(col1)325 col2 should beLargerThan(col1)326 col1 shouldNot beLargerThan(col2)327 shouldThrow<AssertionError> {328 col1.shouldBeLargerThan(col2)329 }.shouldHaveMessage("Collection of size 3 should be larger than collection of size 4")330 }331 }332 "shouldBeSmallerThan" should {333 "test that a collection is smaller than another collection" {334 val col1 = listOf(1, 2, 3)335 val col2 = setOf(1, 2, 3, 4)336 col1.shouldBeSmallerThan(col2)337 col1 should beSmallerThan(col2)338 col2 shouldNot beSmallerThan(col1)339 shouldThrow<AssertionError> {340 col2.shouldBeSmallerThan(col1)341 }.shouldHaveMessage("Collection of size 4 should be smaller than collection of size 3")342 }343 }344 "shouldBeSameSizeAs" should {345 "test that a collection is the same size as another collection" {346 val col1 = listOf(1, 2, 3)347 val col2 = setOf(1, 2, 3)348 val col3 = listOf(1, 2, 3, 4)349 col1.shouldBeSameSizeAs(col2)350 col1 should beSameSizeAs(col2)351 col1 shouldNot beSameSizeAs(col3)352 shouldThrow<AssertionError> {353 col1.shouldBeSameSizeAs(col3)354 }.shouldHaveMessage("Collection of size 3 should be the same size as collection of size 4")355 }356 }357 "haveSize" should {358 "test that a collection has a certain size" {359 val col1 = listOf(1, 2, 3)360 col1 should haveSize(3)361 col1.shouldHaveSize(3)362 shouldThrow<AssertionError> {363 col1 should haveSize(2)364 }365 val col2 = emptyList<String>()366 col2 should haveSize(0)367 shouldThrow<AssertionError> {368 col2 should haveSize(1)369 }370 listOf(1, 2, 3).shouldNotHaveSize(1)371 listOf(1, 2, 3).shouldNotHaveSize(4)372 shouldThrow<AssertionError> {373 listOf(1, 2, 3).shouldNotHaveSize(3)374 }.shouldHaveMessage("Collection should not have size 3. Values: [1, 2, 3]")375 }376 }377 "should be singleton" should {378 "pass for collection with a single element" {379 listOf(1).shouldBeSingleton()380 }381 "fail for collection with 0 elements" {382 shouldThrow<AssertionError> {383 listOf<Int>().shouldBeSingleton()384 }.shouldHaveMessage("Collection should have size 1 but has size 0. Values: []")385 }386 "fail for collection with 2+ elements" {387 shouldThrow<AssertionError> {388 listOf(1, 2).shouldBeSingleton()389 }.shouldHaveMessage("Collection should have size 1 but has size 2. Values: [1, 2]")390 shouldThrow<AssertionError> {391 listOf(1, 2, 3, 4).shouldBeSingleton()392 }.shouldHaveMessage("Collection should have size 1 but has size 4. Values: [1, 2, 3, 4]")393 }394 }395 "should be singleton with block" should {396 "pass for collection with a single element" {397 listOf(1).shouldBeSingleton { it shouldBe 1 }398 }399 "fail for collection with 0 elements" {400 shouldThrow<AssertionError> {401 listOf<Int>().shouldBeSingleton { it shouldBe 1 }402 }.shouldHaveMessage("Collection should have size 1 but has size 0. Values: []")403 }404 "fail for collection with a single incorrect elements" {405 shouldThrow<AssertionError> {406 listOf(2).shouldBeSingleton { it shouldBe 1 }407 }.shouldHaveMessage("expected:<1> but was:<2>")408 }409 "fail for collection with 2+ elements" {410 shouldThrow<AssertionError> {411 listOf(1, 2).shouldBeSingleton { it shouldBe 1 }412 }.shouldHaveMessage("Collection should have size 1 but has size 2. Values: [1, 2]")413 shouldThrow<AssertionError> {414 listOf(1, 2, 3, 4).shouldBeSingleton { it shouldBe 1 }415 }.shouldHaveMessage("Collection should have size 1 but has size 4. Values: [1, 2, 3, 4]")416 }417 }418 "should not be singleton" should {419 "pass for collection with 0 elements" {420 listOf<Int>().shouldNotBeSingleton()421 }422 "pass for collection with 2+ elements" {423 listOf(1, 2).shouldNotBeSingleton()424 listOf(1, 2, 3, 4).shouldNotBeSingleton()425 }426 "fail for collection with a single element" {427 shouldThrow<AssertionError> {428 listOf(1).shouldNotBeSingleton()429 }.shouldHaveMessage("Collection should not have size 1. Values: [1]")430 }431 }432 "shouldExist" should {433 "test that a collection contains at least one element that matches a predicate" {434 val list = listOf(1, 2, 3)435 list.shouldExist { it == 2 }436 }437 }438 "shouldHaveAtLeastSize" should {439 "test that a collection has at least a certain number of elements" {440 val list = listOf(1, 2, 3)441 list.shouldHaveAtLeastSize(2)442 list shouldHave atLeastSize(2)443 val set = setOf(1, 2, 3)444 set.shouldHaveAtLeastSize(3)445 set shouldHave atLeastSize(3)446 shouldThrow<AssertionError> {447 list.shouldHaveAtLeastSize(4)448 }.shouldHaveMessage("Collection [1, 2, 3] should contain at least 4 elements")449 shouldThrow<AssertionError> {450 list shouldHave atLeastSize(4)451 }.shouldHaveMessage("Collection [1, 2, 3] should contain at least 4 elements")452 shouldThrow<AssertionError> {453 list shouldNotHave atLeastSize(2)454 }.shouldHaveMessage("Collection [1, 2, 3] should contain less than 2 elements")455 }456 }457 "shouldHaveAtMostSize" should {458 "test that a collection has at least a certain number of elements" {459 val list = listOf(1, 2, 3)460 list.shouldHaveAtMostSize(3)461 list shouldHave atMostSize(3)462 list.shouldHaveAtMostSize(4)463 list shouldHave atMostSize(4)464 val set = setOf(1, 2, 3)465 set.shouldHaveAtMostSize(3)466 set shouldHave atMostSize(3)467 set.shouldHaveAtMostSize(4)468 set shouldHave atMostSize(4)469 shouldThrow<AssertionError> {470 list.shouldHaveAtMostSize(2)471 }.shouldHaveMessage("Collection [1, 2, 3] should contain at most 2 elements")472 shouldThrow<AssertionError> {473 list shouldHave atMostSize(2)474 }.shouldHaveMessage("Collection [1, 2, 3] should contain at most 2 elements")475 shouldThrow<AssertionError> {476 list shouldNotHave atMostSize(4)477 }.shouldHaveMessage("Collection [1, 2, 3] should contain more than 4 elements")478 }479 }480 "containNoNulls" should {481 "test that a collection contains zero nulls" {482 emptyList<String>() should containNoNulls()483 listOf(1, 2, 3) should containNoNulls()484 listOf(null, null, null) shouldNot containNoNulls()485 listOf(1, null, null) shouldNot containNoNulls()486 emptyList<String>().shouldContainNoNulls()487 listOf(1, 2, 3).shouldContainNoNulls()488 listOf(null, null, null).shouldNotContainNoNulls()489 listOf(1, null, null).shouldNotContainNoNulls()490 shouldThrow<AssertionError> {491 listOf(null, null, null).shouldContainNoNulls()492 }.shouldHaveMessage("Collection should not contain nulls")493 shouldThrow<AssertionError> {494 listOf(1, 2, 3).shouldNotContainNoNulls()495 }.shouldHaveMessage("Collection should have at least one null")496 }497 "support type inference for subtypes of collection" {498 val tests = listOf(499 TestSealed.Test1("test1"),500 TestSealed.Test2(2)501 )502 tests should containNoNulls()503 tests.shouldContainNoNulls()504 }505 }506 "containOnlyNulls" should {507 "test that a collection contains only nulls" {508 emptyList<String>() should containOnlyNulls()509 listOf(null, null, null) should containOnlyNulls()510 listOf(1, null, null) shouldNot containOnlyNulls()511 listOf(1, 2, 3) shouldNot containOnlyNulls()512 listOf(null, 1, 2, 3).shouldNotContainOnlyNulls()513 listOf(1, 2, 3).shouldNotContainOnlyNulls()514 listOf(null, null, null).shouldContainOnlyNulls()515 }516 }517 "matchInOrder" should {518 "test that a collection matches the assertions in the given order, duplicates permitted" {519 withClue("Gaps not allowed") {520 shouldFail {521 listOf(1, 2, 2, 3) should matchInOrder(522 { it shouldBe 1 },523 { it shouldBe 2 },524 { it shouldBe 3 }525 )526 }527 }528 arrayOf(2, 2, 3).shouldMatchInOrder(529 { it shouldBe 2 },530 { it shouldBe 2 },531 { it shouldBe 3 },532 )533 }534 "failure shows best result" {535 shouldFail {536 listOf(1, 2, 3, 1, 2, 1, 2).shouldMatchInOrder(537 { it shouldBe 1 },538 { it shouldBe 2 },539 { it shouldBe 1 },540 { it shouldBe 3 },541 )542 }.message shouldBe """543 Expected a sequence of elements to pass the assertions, but failed to match all assertions544 Best result when comparing from index [3], where 3 elements passed, but the following elements failed:545 6 => expected:<3> but was:<2>546 """.trimIndent()547 }548 "Non existing element causes error" {549 shouldThrow<AssertionError> {550 listOf(1, 2, 3).shouldMatchInOrder(551 { it shouldBe 1 },552 { it shouldBe 2 },553 { it shouldBe 6 }554 )555 }556 }557 "out-of-order elements cause error" {558 shouldThrow<AssertionError> {559 listOf(1, 2, 3) should matchInOrder(560 { it shouldBe 2 },561 { it shouldBe 1 },562 { it shouldBe 3 }563 )564 }565 }566 "work with unsorted collections" {567 val actual = listOf(5, 3, 1, 2, 4, 2)568 withClue("should match 4th, 5th and 6th elements ([.., 2, 4, 2])") {569 actual should matchInOrder(570 { it shouldBe 2 },571 { it shouldBeGreaterThan 3 },572 { it shouldBeInRange 2..2 }573 )574 }575 }576 "negation should work" {577 shouldFail {578 listOf(1, 2, 3, 4).shouldNotMatchInOrder(579 { it shouldBe 2 },580 { it shouldBe 3 },581 )582 }.message shouldBe """583 Expected some assertion to fail but all passed584 """.trimIndent()585 listOf(1, 2, 3, 4).shouldNotMatchInOrder(586 { it shouldBe 2 },587 { it shouldBe 4 }588 )589 }590 }591 "matchInOrderSubset" should {592 "test that a collection matches the assertions in the given order without gaps" {593 listOf(1, 1, 2, 2, 3, 3) should matchInOrderSubset(594 { it shouldBe 1 },595 { it shouldBe 2 },596 { it shouldBe 2 },597 { it shouldBe 3 }598 )599 arrayOf(1, 1, 1).shouldMatchInOrderSubset(600 { it shouldBe 1 }601 )602 }603 "Negation should work" {604 shouldFail {605 listOf(1, 2, 3, 4).shouldNotMatchInOrderSubset(606 { it shouldBe 2 },607 { it shouldBe 4 },608 )609 }.message shouldBe """610 Expected some assertion to fail but all passed611 """.trimIndent()612 arrayOf(1, 2, 3, 4).shouldNotMatchInOrder(613 { it shouldBe 4 },614 { it shouldBe 1 }615 )616 }617 "Non existing element causes error" {618 shouldThrow<AssertionError> {619 listOf(1, 1, 2, 2, 3, 3) should matchInOrderSubset(620 { it shouldBe 1 },621 { it shouldBe 2 },622 { it shouldBe 6 }623 )624 }.message shouldBe """625 Expected a sequence of elements to pass the assertions, possibly with gaps between but failed to match all assertions626 Best result when comparing from index [0], where 2 elements passed, but the following elements failed:627 3 => expected:<6> but was:<2>628 4 => expected:<6> but was:<3>629 5 => expected:<6> but was:<3>630 """.trimIndent()631 }632 "out-of-order elements cause error" {633 shouldThrow<AssertionError> {634 listOf(1, 2, 3) should matchInOrderSubset(635 { it shouldBe 2 },636 { it shouldBe 1 },637 { it shouldBe 3 }638 )639 }640 }641 "gaps should be ok" {642 listOf(1, 1, 2, 2, 3, 3) should matchInOrderSubset(643 { it shouldBe 1 },644 { it shouldBe 2 },645 { it shouldBe 3 }646 )647 }648 "work with unsorted collections" {649 val actual = listOf(5, 3, 1, 2, 4, 2)650 withClue("should match 4th, 5th and 6th elements ([.., 2, 4, 2])") {651 actual should matchInOrderSubset(652 { it shouldBe 2 },653 { it shouldBeGreaterThan 3 },654 { it shouldBeInRange 2..2 }655 )656 }657 }658 }659 "matchEach" should {660 "test that a collection matches the assertions in the given order without gaps" {661 listOf(1, 3, 7) should matchEach(662 { it shouldBe 1 },663 { it shouldBeInRange 2..4 },664 { it shouldBeGreaterThan 2 }665 )666 }667 "Negation should work" {668 shouldFail{669 listOf(1, 2).shouldNotMatchEach(670 { it shouldBe 1 },671 { it shouldBe 2 },672 )673 }.message shouldBe """674 Expected some element to fail its assertion, but all passed.675 """.trimIndent()676 arrayOf(1, 2).shouldNotMatchEach(677 { it shouldBe 2 },678 { it shouldBe 1 }679 )680 }681 "No assertion exists for each element" {682 shouldFail {683 listOf(1, -1, 999) should matchEach(684 { it shouldBe 1 }685 )686 }.message shouldBe """687 Expected each element to pass its assertion, but found issues at indexes: [1, 2]688 1 => Element has no corresponding assertion. Only 1 assertions provided689 2 => Element has no corresponding assertion. Only 1 assertions provided690 """.trimIndent()691 }692 "Too many assertions cause error" {693 shouldFail {694 listOf(1, 3, 7) should matchEach(695 { it shouldBe 1 },696 { it shouldBe 3 },697 { it shouldBe 7 },698 { it shouldBe 7 },699 { it shouldBe 7 },700 )701 }.message shouldBe """702 Expected each element to pass its assertion, but found issues at indexes: [3, 4]703 3 => No actual element for assertion at index 3704 4 => No actual element for assertion at index 4705 """.trimIndent()706 }707 "Non matching element causes error" {708 shouldFail {709 listOf(1, 3, 7) should matchEach(710 { it shouldBe 1 },711 { it shouldBeInRange 2..4 },712 { it shouldBeGreaterThan 7 }713 )714 }.message shouldBe """715 Expected each element to pass its assertion, but found issues at indexes: [2]716 2 => 7 should be > 7717 """.trimIndent()718 }719 "out-of-order elements cause error" {720 shouldThrow<AssertionError> {721 setOf(2, 3, 1) should matchEach(722 { it shouldBe 2 },723 { it shouldBe 1 },724 { it shouldBe 3 }725 )726 }.message shouldBe """727 Expected each element to pass its assertion, but found issues at indexes: [1, 2]728 1 => expected:<1> but was:<3>729 2 => expected:<3> but was:<1>730 """.trimIndent()731 }732 "gaps cause errors" {733 shouldThrow<AssertionError> {734 listOf(1, 1, 2, 2, 3, 3) should matchEach(735 { it shouldBe 1 },736 { it shouldBe 2 },737 { it shouldBe 3 }738 )739 }.message shouldBe """740 Expected each element to pass its assertion, but found issues at indexes: [1, 2, 3, 4, 5]741 1 => expected:<2> but was:<1>742 2 => expected:<3> but was:<2>743 3 => Element has no corresponding assertion. Only 3 assertions provided744 4 => Element has no corresponding assertion. Only 3 assertions provided745 5 => Element has no corresponding assertion. Only 3 assertions provided746 """.trimIndent()747 }748 }749 "existInOrder" should {750 "test that a collection matches the predicates in the given order, duplicates permitted" {751 val col = listOf(1, 1, 2, 2, 3, 3)752 col should existInOrder(753 { it == 1 },754 { it == 2 },755 { it == 3 }756 )757 col should existInOrder({ it == 1 })758 shouldThrow<AssertionError> {759 col should existInOrder(760 { it == 1 },761 { it == 2 },762 { it == 6 }763 )764 }765 shouldThrow<AssertionError> {766 col should existInOrder({ it == 4 })767 }768 shouldThrow<AssertionError> {769 col should existInOrder(770 { it == 2 },771 { it == 1 },772 { it == 3 }773 )774 }775 }776 "work with unsorted collections" {777 val actual = listOf(5, 3, 1, 2, 4, 2)778 actual should existInOrder(779 { it == 3 },780 { it == 2 },781 { it == 2 }782 )783 }784 }785 "Contain any" should {786 "Fail when the list is empty" {787 shouldThrow<AssertionError> {788 listOf(1, 2, 3).shouldContainAnyOf(emptyList())789 }.shouldHaveMessage("Asserting content on empty collection. Use Collection.shouldBeEmpty() instead.")790 }791 "Pass when one element is in the list" {792 listOf(1, 2, 3).shouldContainAnyOf(1)793 }794 "Pass when all elements are in the list" {795 listOf(1, 2, 3).shouldContainAnyOf(1, 2, 3)796 }797 "Fail when no element is in the list" {798 shouldThrow<AssertionError> {799 listOf(1, 2, 3).shouldContainAnyOf(4)800 }.shouldHaveMessage("Collection [1, 2, 3] should contain any of [4]")801 }802 }803 "Contain any (negative)" should {804 "Fail when the list is empty" {805 shouldThrow<AssertionError> {806 listOf(1, 2, 3).shouldNotContainAnyOf(emptyList())807 }.shouldHaveMessage("Asserting content on empty collection. Use Collection.shouldBeEmpty() instead.")808 }809 "Pass when no element is present in the list" {810 listOf(1, 2, 3).shouldNotContainAnyOf(4)811 }812 "Fail when one element is in the list" {813 shouldThrow<AssertionError> {814 listOf(1, 2, 3).shouldNotContainAnyOf(1)815 }.shouldHaveMessage("Collection [1, 2, 3] should not contain any of [1]")816 }817 "Fail when all elements are in the list" {818 shouldThrow<AssertionError> {819 listOf(1, 2, 3).shouldNotContainAnyOf(1, 2, 3)820 }.shouldHaveMessage("Collection [1, 2, 3] should not contain any of [1, 2, 3]")821 }822 }823 "Be in" should {824 "Pass when the element is in the list" {825 val foo = Foo("Bar")826 val list = listOf(foo)827 foo shouldBeIn list828 }829 "Fail when the element is not in the list" {830 val foo1 = Foo("Bar")831 val foo2 = Foo("Booz")832 val list = listOf(foo1)833 shouldThrow<AssertionError> {834 foo2.shouldBeIn(list)835 }.shouldHaveMessage("Collection should contain Foo(bar=Booz), but doesn't. Possible values: [Foo(bar=Bar)]")836 }837 "Pass when there's an equal element, but not the same instance in the list" {838 val foo1 = Foo("Bar")839 val foo2 = Foo("Bar")840 val list = listOf(foo1)841 shouldNotThrow<AssertionError> { foo2 shouldBeIn list }842 }843 "Pass when there's an equal element, but not the same instance in the array" {844 val foo1 = Foo("Bar")845 val foo2 = Foo("Bar")846 val list = arrayOf(foo1)847 shouldNotThrow<AssertionError> { foo2 shouldBeIn list }848 }849 "Fail when the list is empty" {850 val foo = Foo("Bar")851 val list = emptyList<Foo>()852 shouldThrow<AssertionError> {853 foo shouldBeIn list854 }.shouldHaveMessage("Asserting content on empty collection. Use Collection.shouldBeEmpty() instead.")855 }856 }857 "Be in (negative)" should {858 "Fail when the element is in the list" {859 val foo = Foo("Bar")860 val list = listOf(foo)861 shouldThrow<AssertionError> {862 foo shouldNotBeIn list863 }.shouldHaveMessage("Collection should not contain Foo(bar=Bar), but does. Forbidden values: [Foo(bar=Bar)]")864 }865 "Pass when the element is not in the list" {866 val foo1 = Foo("Bar")867 val foo2 = Foo("Booz")868 val list = listOf(foo1)869 shouldNotThrow<AssertionError> {870 foo2.shouldNotBeIn(list)871 }872 }873 "Fail when there's an equal element, but not the same instance in the list" {874 val foo1 = Foo("Bar")875 val foo2 = Foo("Bar")876 val list = listOf(foo1)877 shouldThrow<AssertionError> {878 foo2 shouldNotBeIn list879 }.shouldHaveMessage("Collection should not contain Foo(bar=Bar), but does. Forbidden values: [Foo(bar=Bar)]")880 }881 "Fail when the list is empty" {882 val foo = Foo("Bar")883 val list = emptyList<Foo>()884 shouldThrow<AssertionError> {885 foo shouldNotBeIn list886 }.shouldHaveMessage("Asserting content on empty collection. Use Collection.shouldBeEmpty() instead.")887 }888 }889 }890}891private data class Foo(val bar: String)892sealed class TestSealed {893 data class Test1(val value: String) : TestSealed()894 data class Test2(val value: Int) : TestSealed()895}...

Full Screen

Full Screen

SequenceMatchersTest.kt

Source:SequenceMatchersTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.matchers.collections2import io.kotest.assertions.throwables.shouldThrow3import io.kotest.assertions.throwables.shouldThrowAny4import io.kotest.core.spec.style.WordSpec5import io.kotest.core.spec.style.scopes.WordSpecTerminalScope6import io.kotest.core.spec.style.scopes.WordSpecShouldContainerScope7import io.kotest.matchers.sequences.shouldBeLargerThan8import io.kotest.matchers.sequences.shouldBeSameCountAs9import io.kotest.matchers.sequences.shouldBeSmallerThan10import io.kotest.matchers.sequences.shouldBeSorted11import io.kotest.matchers.sequences.shouldBeSortedWith12import io.kotest.matchers.sequences.shouldBeUnique13import io.kotest.matchers.sequences.shouldContain14import io.kotest.matchers.sequences.shouldContainAll15import io.kotest.matchers.sequences.shouldContainAllInAnyOrder16import io.kotest.matchers.sequences.shouldContainDuplicates17import io.kotest.matchers.sequences.shouldContainExactly18import io.kotest.matchers.sequences.shouldContainInOrder19import io.kotest.matchers.sequences.shouldContainNoNulls20import io.kotest.matchers.sequences.shouldContainNull21import io.kotest.matchers.sequences.shouldContainOnlyNulls22import io.kotest.matchers.sequences.shouldExist23import io.kotest.matchers.sequences.shouldHaveAtLeastCount24import io.kotest.matchers.sequences.shouldHaveAtMostCount25import io.kotest.matchers.sequences.shouldHaveCount26import io.kotest.matchers.sequences.shouldHaveElementAt27import io.kotest.matchers.sequences.shouldHaveLowerBound28import io.kotest.matchers.sequences.shouldHaveSingleElement29import io.kotest.matchers.sequences.shouldHaveUpperBound30import io.kotest.matchers.sequences.shouldNotBeSorted31import io.kotest.matchers.sequences.shouldNotBeSortedWith32import io.kotest.matchers.sequences.shouldNotBeUnique33import io.kotest.matchers.sequences.shouldNotContain34import io.kotest.matchers.sequences.shouldNotContainAllInAnyOrder35import io.kotest.matchers.sequences.shouldNotContainExactly36import io.kotest.matchers.sequences.shouldNotContainNoNulls37import io.kotest.matchers.sequences.shouldNotContainNull38import io.kotest.matchers.sequences.shouldNotContainOnlyNulls39import io.kotest.matchers.sequences.shouldNotHaveCount40import io.kotest.matchers.sequences.shouldNotHaveElementAt41class SequenceMatchersTest : WordSpec() {42 /* PassFail */43 private suspend fun WordSpecShouldContainerScope.pass(name: String, test: suspend WordSpecTerminalScope.() -> Unit) {44 ("succeed $name")(test)45 }46 private suspend fun WordSpecShouldContainerScope.succeed(name: String, test: suspend WordSpecTerminalScope.() -> Unit) = pass(name, test)47 fun WordSpecShouldContainerScope.fail(msg: String): Nothing = io.kotest.assertions.fail(msg)48 suspend fun WordSpecShouldContainerScope.fail(name: String, test: () -> Any?) {49 ("fail $name") { shouldThrowAny(test) }50 }51 suspend inline fun <reified E : Throwable> WordSpecShouldContainerScope.abort(name: String, crossinline test: () -> Any?) {52 ("abort $name") { shouldThrow<E>(test) }53 }54 suspend inline fun <reified E : Throwable> WordSpecShouldContainerScope.`throw`(name: String, crossinline test: () -> Any?) = abort<E>(55 name,56 test)57 /* sample data */58 val empty = emptySequence<Int>()59 val single = sequenceOf(0)60 val nulls = sequenceOf<Int?>(null, null, null, null)61 val sparse = sequenceOf(null, null, null, 3)62 val countup = (0..10).asSequence()63 val countdown = (10 downTo 0).asSequence()64 val unique = sequenceOf(3, 2, 1)65 val repeating = sequenceOf(1, 2, 3, 1, 2, 3)66 val asc = { a: Int, b: Int -> a - b }67 val desc = { a: Int, b: Int -> b - a }68 /* tests */69 init {70 /* count */71 "have count" should {72 succeed("for empty when 0") {73 empty.shouldHaveCount(0)74 }75 fail("for empty when non-zero") {76 empty.shouldHaveCount(1)77 }78 succeed("for single when 1") {79 single.shouldHaveCount(1)80 }81 fail("for single when 0") {82 single.shouldHaveCount(0)83 }84 "match count() for multiple" {85 sparse.shouldHaveCount(sparse.count())86 }87 fail("to mis-match count() for multiple") {88 sparse.shouldHaveCount(sparse.count() - 1)89 }90 }91 "not have count" should {92 fail("for empty when non-zero") {93 empty.shouldNotHaveCount(0)94 }95 succeed("for empty when non-zero") {96 empty.shouldNotHaveCount(1)97 }98 fail("for single when 1") {99 single.shouldNotHaveCount(1)100 }101 succeed("for single when 0") {102 single.shouldNotHaveCount(0)103 }104 fail("to match count() for multiple") {105 sparse.shouldNotHaveCount(sparse.count())106 }107 "mis-match count() for multiple" {108 sparse.shouldNotHaveCount(sparse.count() - 1)109 }110 }111 "larger than" should {112 fail("for empty") {113 empty.shouldBeLargerThan(single)114 }115 succeed("with empty") {116 single.shouldBeLargerThan(empty)117 }118 fail("for smaller") {119 nulls.shouldBeLargerThan(countup)120 }121 fail("for same count") {122 countup.shouldBeLargerThan(countdown)123 }124 succeed("for larger") {125 countup.shouldBeLargerThan(nulls)126 }127 }128 "smaller than" should {129 succeed("for empty") {130 empty.shouldBeSmallerThan(single)131 }132 fail("with empty") {133 single.shouldBeSmallerThan(empty)134 }135 succeed("for smaller") {136 nulls.shouldBeSmallerThan(countup)137 }138 fail("for same count") {139 countup.shouldBeSmallerThan(countdown)140 }141 fail("for larger") {142 countup.shouldBeSmallerThan(nulls)143 }144 }145 "same count" should {146 fail("for empty with any") {147 empty.shouldBeSameCountAs(single)148 }149 fail("for any with empty") {150 nulls.shouldBeSameCountAs(empty)151 }152 fail("for smaller") {153 nulls.shouldBeSameCountAs(countup)154 }155 succeed("with same count") {156 countup.shouldBeSameCountAs(countdown)157 }158 fail("for larger") {159 countup.shouldBeSmallerThan(nulls)160 }161 }162 "at least count" should {163 succeed("for empty with -1") {164 empty.shouldHaveAtLeastCount(-1)165 }166 succeed("for any with -1") {167 countup.shouldHaveAtLeastCount(-1)168 }169 succeed("for empty with 0") {170 empty.shouldHaveAtLeastCount(0)171 }172 fail("for empty with 1") {173 empty.shouldHaveAtLeastCount(1)174 }175 succeed("for smaller count") {176 single.shouldHaveAtLeastCount(0)177 }178 succeed("for same count") {179 nulls.shouldHaveAtLeastCount(nulls.count())180 }181 fail("for larger count") {182 countup.shouldHaveAtLeastCount(countup.count() + 1)183 }184 }185 "at most count" should {186 fail("for empty with -1") {187 empty.shouldHaveAtMostCount(-1)188 }189 succeed("for empty with 0") {190 empty.shouldHaveAtMostCount(0)191 }192 succeed("for empty with 1") {193 empty.shouldHaveAtMostCount(1)194 }195 fail("for smaller count") {196 countup.shouldHaveAtMostCount(countup.count() - 1)197 }198 succeed("for same count") {199 countup.shouldHaveAtMostCount(countup.count())200 }201 succeed("for larger count") {202 countup.shouldHaveAtMostCount(countup.count() + 1)203 }204 }205 /* contain */206 /** null */207 "contain only nulls" should {208 succeed("for empty") {209 empty.shouldContainOnlyNulls()210 }211 fail("for single") {212 single.shouldContainOnlyNulls()213 }214 succeed("for nulls") {215 nulls.shouldContainOnlyNulls()216 }217 fail("for sparse") {218 sparse.shouldContainOnlyNulls()219 }220 }221 "not contain only nulls" should {222 fail("for empty") {223 empty.shouldNotContainOnlyNulls()224 }225 "fail for single" {226 single.shouldNotContainOnlyNulls()227 }228 fail("for nulls") {229 nulls.shouldNotContainOnlyNulls()230 }231 succeed("for sparse") {232 sparse.shouldNotContainOnlyNulls()233 }234 }235 "contain a null" should {236 fail("for empty") {237 empty.shouldContainNull()238 }239 fail("for non-nulls") {240 single.shouldContainNull()241 }242 succeed("for nulls") {243 nulls.shouldContainNull()244 }245 succeed("for sparse") {246 sparse.shouldContainNull()247 }248 }249 "not contain a null" should {250 succeed("for empty") {251 empty.shouldNotContainNull()252 }253 succeed("for non-nulls") {254 single.shouldNotContainNull()255 }256 fail("for nulls") {257 nulls.shouldNotContainNull()258 }259 fail("for sparse") {260 sparse.shouldNotContainNull()261 }262 }263 "contain no nulls" should {264 succeed("for empty") {265 empty.shouldContainNoNulls()266 }267 succeed("for non-nulls") {268 single.shouldContainNoNulls()269 }270 fail("for nulls") {271 nulls.shouldContainNoNulls()272 }273 fail("for sparse") {274 sparse.shouldContainNoNulls()275 }276 }277 "not contain no nulls" should {278 fail("for empty") {279 empty.shouldNotContainNoNulls()280 }281 fail("for non-nulls") {282 single.shouldNotContainNoNulls()283 }284 succeed("for nulls") {285 nulls.shouldNotContainNoNulls()286 }287 succeed("for sparse") {288 sparse.shouldNotContainNoNulls()289 }290 }291 /** single-value */292 "single element" should {293 fail("for empty") {294 empty.shouldHaveSingleElement(null)295 }296 succeed("for single") {297 single.shouldHaveSingleElement(single.first())298 }299 fail("for multiple") {300 nulls.shouldHaveSingleElement(null)301 }302 }303 "have element at" should {304 abort<IndexOutOfBoundsException>("for empty") {305 empty.shouldHaveElementAt(empty.count(), 0)306 }307 abort<IndexOutOfBoundsException>("when an element after the end is requested") {308 nulls.shouldHaveElementAt(nulls.count(), 0)309 }310 succeed("when the sequence has the element") {311 countup.shouldHaveElementAt(10, 10)312 }313 fail("when the sequence doesn't have the element") {314 countdown.shouldHaveElementAt(10, 10)315 }316 }317 "not have element at" should {318 abort<IndexOutOfBoundsException>("for empty") {319 empty.shouldNotHaveElementAt(empty.count(), 0)320 }321 abort<IndexOutOfBoundsException>("when an element after the end is requested") {322 nulls.shouldNotHaveElementAt(nulls.count(), 0)323 }324 fail("when the sequence has the element") {325 countup.shouldNotHaveElementAt(10, 10)326 }327 succeed("when the sequence doesn't have the element") {328 countdown.shouldNotHaveElementAt(10, 10)329 }330 }331 "contain" should {332 fail("for empty") {333 empty.shouldContain(0)334 }335 succeed("when the sequence contains the value") {336 countup.shouldContain(2)337 }338 fail("when the sequence doesn't contain the value") {339 sparse.shouldContain(2)340 }341 }342 "not contain" should {343 succeed("for empty") {344 empty.shouldNotContain(0)345 }346 fail("when the sequence contains the value") {347 countup.shouldNotContain(2)348 }349 succeed("when the sequence doesn't contain the value") {350 sparse.shouldNotContain(2)351 }352 }353 "exist" should {354 fail("for empty") {355 empty.shouldExist { true }356 }357 succeed("when always true") {358 single.shouldExist { true }359 }360 fail("when always false") {361 countup.shouldExist { false }362 }363 succeed("when matches at least one") {364 countdown.shouldExist { it % 5 == 4 }365 }366 fail("when matches none") {367 countdown.shouldExist { it > 20 }368 }369 }370 /** multiple-value */371 "contain all" should {372 succeed("for empty with empty") {373 empty.shouldContainAll(empty)374 }375 succeed("for empty with empty (variadic)") {376 empty.shouldContainAll()377 }378 fail("for empty with any other") {379 empty.shouldContainAll(single)380 }381 succeed("for any with empty") {382 single.shouldContainAll(empty)383 }384 succeed("for any with empty (variadic)") {385 single.shouldContainAll()386 }387 succeed("for subset of nulls") {388 sparse.shouldContainAll(nulls)389 }390 succeed("for subset of nulls (variadic)") {391 sparse.shouldContainAll(null, null)392 }393 succeed("for subset in order (variadic)") {394 countdown.shouldContainAll(2, 3, 5, 7)395 }396 succeed("for subset not in order (variadic)") {397 countdown.shouldContainAll(2, 5, 3, 7)398 }399 succeed("for same elements") {400 repeating.shouldContainAll(unique)401 }402 succeed("for same elements (variadic)") {403 repeating.shouldContainAll(2, 3, 1)404 }405 succeed("for same elements, repeated") {406 unique.shouldContainAll(repeating)407 }408 succeed("for same elements, repeated (variadic)") {409 unique.shouldContainAll(1, 2, 3, 1, 2, 3)410 }411 }412 "contain exactly empty" should {413 succeed("for empty") {414 empty.shouldContainExactly(sequenceOf<Int>())415 }416 succeed("for empty (variadic)") {417 empty.shouldContainExactly()418 }419 fail("for single") {420 single.shouldContainExactly(empty)421 }422 "fail for single (variadic)" {423 shouldThrowAny {424 single.shouldContainExactly()425 }426 }427 fail("for multiple") {428 nulls.shouldContainExactly(empty)429 }430 fail("for multiple (variadic)") {431 nulls.shouldContainExactly()432 }433 }434 "contain exactly non-empty" should {435 val nonempty = sparse;436 fail("for empty") {437 empty.shouldContainExactly(nonempty)438 }439 fail("for empty (variadic)") {440 empty.shouldContainExactly(*nonempty.toList().toTypedArray())441 }442 succeed("for same") {443 sparse.shouldContainExactly(nonempty)444 }445 succeed("for same (variadic)") {446 sparse.shouldContainExactly(*sparse.toList().toTypedArray())447 }448 fail("for another of different size") {449 countup.shouldContainExactly(nonempty)450 }451 fail("for another of different size (variadic)") {452 countup.shouldContainExactly(*nonempty.toList().toTypedArray())453 }454 fail("for another of same size") {455 nulls.shouldContainExactly(nonempty)456 }457 fail("for another of same size (variadic)") {458 nulls.shouldContainExactly(*nonempty.toList().toTypedArray())459 }460 fail("for same elements but different order") {461 repeating.shouldContainExactly(unique + unique)462 }463 fail("for same elements but different order (variadic)") {464 repeating.shouldContainExactly(1, 1, 2, 2, 3, 3)465 }466 }467 "not contain exactly empty" should {468 fail("for empty") {469 empty.shouldNotContainExactly(sequenceOf<Int>())470 }471 succeed("for single") {472 single.shouldNotContainExactly(empty)473 }474 succeed("for multiple") {475 nulls.shouldNotContainExactly(empty)476 }477 }478 "not contain exactly non-empty" should {479 val nonempty = sparse;480 succeed("for empty") {481 empty.shouldNotContainExactly(nonempty)482 }483 fail("for same") {484 sparse.shouldNotContainExactly(nonempty)485 }486 succeed("for another of different size") {487 countup.shouldNotContainExactly(nonempty)488 }489 succeed("for another of same size") {490 nulls.shouldNotContainExactly(nonempty)491 }492 succeed("for same elements but different order") {493 repeating.shouldNotContainExactly(unique + unique)494 }495 succeed("for same elements but different order (variadic)") {496 repeating.shouldNotContainExactly(1, 1, 2, 2, 3, 3)497 }498 succeed("for single traversable equal sequence") {499 var count1 = 0500 var count2 = 0501 val seq1 = generateSequence { if(count1 < 5) count1++ else null }502 val seq2 = generateSequence { if(count2 < 5) count2++ else null }503 seq1.shouldContainExactly(seq2)504 }505 fail("for single traversable unequal sequence") {506 var count1 = 0507 var count2 = 0508 val seq1 = generateSequence { if(count1 < 5) count1++ else null }509 val seq2 = generateSequence { if(count2 < 6) count2++ else null }510 seq1.shouldContainExactly(seq2)511 }512 }513 "contain in any order" should {514 succeed("for empty with empty") {515 empty.shouldContainAllInAnyOrder(empty)516 }517 fail("for empty with any other") {518 empty.shouldContainAllInAnyOrder(nulls)519 }520 succeed("when elements are same") {521 countdown.shouldContainAllInAnyOrder(countup)522 }523 fail("for overlapping sequence") {524 countup.shouldContainAllInAnyOrder((5..15).asSequence())525 }526 succeed("for subset, same count with nulls") {527 sparse.shouldContainAllInAnyOrder(nulls)528 }529 succeed("for subset, same count") {530 repeating.shouldContainAllInAnyOrder(unique + unique)531 }532 succeed("for subset, same count (variadic)") {533 repeating.shouldContainAllInAnyOrder(1, 1, 2, 2, 3, 3)534 }535 fail("for subset, different count with nulls") {536 sparse.shouldContainAllInAnyOrder(sparse.toSet().asSequence())537 }538 fail("for same, different count") {539 repeating.shouldContainAllInAnyOrder(unique)540 }541 }542 "not contain in any order" should {543 fail("for empty with empty") {544 empty.shouldNotContainAllInAnyOrder(empty)545 }546 succeed("for empty with any other") {547 empty.shouldNotContainAllInAnyOrder(nulls)548 }549 fail("when elements are same") {550 countdown.shouldNotContainAllInAnyOrder(countup)551 }552 succeed("for overlapping sequence") {553 countup.shouldNotContainAllInAnyOrder((5..15).asSequence())554 }555 fail("for subset, same count with nulls") {556 sparse.shouldNotContainAllInAnyOrder(nulls)557 }558 fail("for subset, same count") {559 repeating.shouldNotContainAllInAnyOrder(unique + unique)560 }561 fail("for subset, same count (variadic)") {562 repeating.shouldNotContainAllInAnyOrder(1, 1, 2, 2, 3, 3)563 }564 succeed("for subset, different count with nulls") {565 sparse.shouldNotContainAllInAnyOrder(sparse.toSet().asSequence())566 }567 succeed("for same, different count") {568 repeating.shouldNotContainAllInAnyOrder(unique)569 }570 }571 "contain in order" should {572 "with empty" {573 shouldThrowAny {574 countup.shouldContainInOrder(empty)575 }576 }577 fail("with empty (variadic)") {578 countup.shouldContainInOrder()579 }580 fail("for overlapping sequence") {581 countup.shouldContainInOrder((5..15).asSequence())582 }583 fail("for overlapping sequence (variadic)") {584 countup.shouldContainInOrder(*(5..15).toList().toTypedArray())585 }586 succeed("for subset in order") {587 countup.shouldContainInOrder(sequenceOf(2, 3, 5, 7))588 }589 succeed("for subset in order (variadic)") {590 countup.shouldContainInOrder(2, 3, 5, 7)591 }592 succeed("for subset in order with repeats") {593 repeating.shouldContainInOrder(sequenceOf(1, 3, 1, 2))594 }595 succeed("for subset in order with repeats (variadic)") {596 repeating.shouldContainInOrder(1, 3, 1, 2)597 }598 fail("for subset in order with too many repeats") {599 repeating.shouldContainInOrder(sequenceOf(1, 3, 1, 2, 2))600 }601 fail("for subset in order with too many repeats (variadic)") {602 repeating.shouldContainInOrder(1, 3, 1, 2, 2)603 }604 fail("for subset not in order") {605 countup.shouldContainInOrder(sequenceOf(2, 5, 3, 7))606 }607 fail("for subset not in order (variadic)") {608 countup.shouldContainInOrder(2, 5, 3, 7)609 }610 }611 /** unique */612 "unique" should {613 succeed("for empty") {614 empty.shouldBeUnique()615 }616 succeed("for single") {617 single.shouldBeUnique()618 }619 fail("with repeated nulls") {620 sparse.shouldBeUnique()621 }622 fail("with repeats") {623 repeating.shouldBeUnique()624 }625 succeed("for multiple unique") {626 countup.shouldBeUnique()627 }628 }629 "not unique" should {630 fail("for empty") {631 empty.shouldNotBeUnique()632 }633 fail("for single") {634 single.shouldNotBeUnique()635 }636 succeed("with repeated nulls") {637 sparse.shouldNotBeUnique()638 }639 succeed("with repeats") {640 repeating.shouldNotBeUnique()641 }642 fail("for multiple unique") {643 countup.shouldNotBeUnique()644 }645 }646 "duplicates" should {647 fail("for empty") {648 empty.shouldContainDuplicates()649 }650 fail("for single") {651 single.shouldContainDuplicates()652 }653 succeed("with repeated nulls") {654 sparse.shouldContainDuplicates()655 }656 succeed("with repeats") {657 repeating.shouldNotBeUnique()658 }659 fail("for multiple unique") {660 countup.shouldContainDuplicates()661 }662 }663 /* comparable */664 /** bound */665 "have an upper bound" should {666 succeed("for empty") {667 empty.shouldHaveUpperBound(Int.MIN_VALUE)668 }669 succeed("for single") {670 single.shouldHaveUpperBound(0)671 }672 fail("for single with wrong bound") {673 single.shouldHaveUpperBound(-1)674 }675 succeed("for multiple") {676 countup.shouldHaveUpperBound(countup.maxOrNull() ?: Int.MAX_VALUE)677 }678 fail("for multiple with wrong bound") {679 countup.shouldHaveUpperBound((countup.maxOrNull() ?: Int.MAX_VALUE) - 1)680 }681 }682 "have a lower bound" should {683 succeed("for empty") {684 empty.shouldHaveLowerBound(Int.MAX_VALUE)685 }686 succeed("for single") {687 single.shouldHaveLowerBound(0)688 }689 fail("for single with wrong bound") {690 single.shouldHaveLowerBound(1)691 }692 succeed("for multiple") {693 countup.shouldHaveLowerBound(countup.minOrNull() ?: Int.MIN_VALUE)694 }695 fail("for multiple with wrong bound") {696 countup.shouldHaveLowerBound((countup.minOrNull() ?: Int.MIN_VALUE) + 1)697 }698 }699 /** sort */700 "sorted" should {701 succeed("for empty") {702 empty.shouldBeSorted()703 }704 succeed("for single") {705 single.shouldBeSorted()706 }707 fail("for repeating") {708 repeating.shouldBeSorted()709 }710 succeed("for count-up") {711 countup.shouldBeSorted()712 }713 fail("for count-down") {714 countdown.shouldBeSorted()715 }716 }717 "not sorted" should {718 fail("for empty") {719 empty.shouldNotBeSorted()720 }721 fail("for single") {722 single.shouldNotBeSorted()723 }724 succeed("for repeating") {725 repeating.shouldNotBeSorted()726 }727 fail("for count-up") {728 countup.shouldNotBeSorted()729 }730 succeed("for count-down") {731 countdown.shouldNotBeSorted()732 }733 }734 "sorted ascending" should {735 val dir = asc736 succeed("for empty") {737 empty.shouldBeSortedWith(dir)738 }739 succeed("for single") {740 single.shouldBeSortedWith(dir)741 }742 fail("for repeating") {743 repeating.shouldBeSortedWith(dir)744 }745 succeed("for count-up") {746 countup.shouldBeSortedWith(dir)747 }748 fail("for count-down") {749 countdown.shouldBeSortedWith(dir)750 }751 }752 "sorted descending" should {753 val dir = desc754 succeed("for empty") {755 empty.shouldBeSortedWith(dir)756 }757 succeed("for single") {758 single.shouldBeSortedWith(dir)759 }760 fail("for repeating") {761 repeating.shouldBeSortedWith(dir)762 }763 fail("for count-up") {764 countup.shouldBeSortedWith(dir)765 }766 succeed("for count-down") {767 countdown.shouldBeSortedWith(dir)768 }769 }770 "not sorted ascending" should {771 val dir = asc772 fail("for empty") {773 empty.shouldNotBeSortedWith(dir)774 }775 fail("for single") {776 single.shouldNotBeSortedWith(dir)777 }778 succeed("for repeating") {779 repeating.shouldNotBeSortedWith(dir)780 }781 fail("for count-up") {782 countup.shouldNotBeSortedWith(dir)783 }784 succeed("for count-down") {785 countdown.shouldNotBeSortedWith(dir)786 }787 }788 "not sorted descending" should {789 val dir = desc790 fail("for empty") {791 empty.shouldNotBeSortedWith(dir)792 }793 fail("for single") {794 single.shouldNotBeSortedWith(dir)795 }796 succeed("for repeating") {797 repeating.shouldNotBeSortedWith(dir)798 }799 succeed("for count-up") {800 countup.shouldNotBeSortedWith(dir)801 }802 fail("for count-down") {803 countdown.shouldNotBeSortedWith(dir)804 }805 }806 }807}...

Full Screen

Full Screen

matchers.kt

Source:matchers.kt Github

copy

Full Screen

1package io.kotest.matchers.collections2import io.kotest.assertions.print.print3import io.kotest.matchers.*4fun <T> Iterable<T>.shouldHaveElementAt(index: Int, element: T) = toList().shouldHaveElementAt(index, element)5fun <T> Array<T>.shouldHaveElementAt(index: Int, element: T) = asList().shouldHaveElementAt(index, element)6fun <T> List<T>.shouldHaveElementAt(index: Int, element: T) = this should haveElementAt(index, element)7fun <T> Iterable<T>.shouldNotHaveElementAt(index: Int, element: T) = toList().shouldNotHaveElementAt(index, element)8fun <T> Array<T>.shouldNotHaveElementAt(index: Int, element: T) = asList().shouldNotHaveElementAt(index, element)9fun <T> List<T>.shouldNotHaveElementAt(index: Int, element: T) = this shouldNot haveElementAt(index, element)10fun <T, L : List<T>> haveElementAt(index: Int, element: T) = object : Matcher<L> {11 override fun test(value: L) =12 MatcherResult(13 index < value.size && value[index] == element,14 { "Collection ${value.print().value} should contain ${element.print().value} at index $index" },15 { "Collection ${value.print().value} should not contain ${element.print().value} at index $index" }16 )17}18infix fun <T> Iterable<T>.shouldHaveSingleElement(t: T): Iterable<T> {19 toList().shouldHaveSingleElement(t)20 return this21}22infix fun <T> Array<T>.shouldHaveSingleElement(t: T): Array<T> {23 asList().shouldHaveSingleElement(t)24 return this25}26infix fun <T> Iterable<T>.shouldHaveSingleElement(p: (T) -> Boolean): Iterable<T> {27 toList().shouldHaveSingleElement(p)28 return this29}30infix fun <T> Array<T>.shouldHaveSingleElement(p: (T) -> Boolean) = asList().shouldHaveSingleElement(p)31infix fun <T> Collection<T>.shouldHaveSingleElement(t: T) = this should singleElement(t)32infix fun <T> Collection<T>.shouldHaveSingleElement(p: (T) -> Boolean) = this should singleElement(p)33infix fun <T> Iterable<T>.shouldNotHaveSingleElement(t: T) = toList().shouldNotHaveSingleElement(t)34infix fun <T> Array<T>.shouldNotHaveSingleElement(t: T) = asList().shouldNotHaveSingleElement(t)35infix fun <T> Collection<T>.shouldNotHaveSingleElement(t: T) = this shouldNot singleElement(t)36infix fun <T> Iterable<T>.shouldExist(p: (T) -> Boolean) = toList().shouldExist(p)37infix fun <T> Array<T>.shouldExist(p: (T) -> Boolean) = asList().shouldExist(p)38infix fun <T> Collection<T>.shouldExist(p: (T) -> Boolean) = this should exist(p)39fun <T> exist(p: (T) -> Boolean) = object : Matcher<Collection<T>> {40 override fun test(value: Collection<T>) = MatcherResult(41 value.any { p(it) },42 { "Collection ${value.print().value} should contain an element that matches the predicate $p" },43 {44 "Collection ${value.print().value} should not contain an element that matches the predicate $p"45 })46}47fun <T> Iterable<T>.shouldMatchInOrder(vararg assertions: (T) -> Unit) = toList().shouldMatchInOrder(assertions.toList())48fun <T> Array<T>.shouldMatchInOrder(vararg assertions: (T) -> Unit) = asList().shouldMatchInOrder(assertions.toList())49fun <T> List<T>.shouldMatchInOrder(vararg assertions: (T) -> Unit) = this.shouldMatchInOrder(assertions.toList())50infix fun <T> Iterable<T>.shouldMatchInOrder(assertions: List<(T) -> Unit>) = toList().shouldMatchInOrder(assertions)51infix fun <T> Array<T>.shouldMatchInOrder(assertions: List<(T) -> Unit>) = asList().shouldMatchInOrder(assertions)52infix fun <T> List<T>.shouldMatchInOrder(assertions: List<(T) -> Unit>) = this should matchInOrder(assertions.toList(), allowGaps = false)53fun <T> Iterable<T>.shouldNotMatchInOrder(vararg assertions: (T) -> Unit) = toList().shouldNotMatchInOrder(assertions.toList())54fun <T> Array<T>.shouldNotMatchInOrder(vararg assertions: (T) -> Unit) = asList().shouldNotMatchInOrder(assertions.toList())55fun <T> List<T>.shouldNotMatchInOrder(vararg assertions: (T) -> Unit) = this.shouldNotMatchInOrder(assertions.toList())56infix fun <T> Iterable<T>.shouldNotMatchInOrder(assertions: List<(T) -> Unit>) = toList().shouldNotMatchInOrder(assertions)57infix fun <T> Array<T>.shouldNotMatchInOrder(assertions: List<(T) -> Unit>) = asList().shouldNotMatchInOrder(assertions)58infix fun <T> List<T>.shouldNotMatchInOrder(assertions: List<(T) -> Unit>) = this shouldNot matchInOrder(assertions.toList(), allowGaps = false)59fun <T> Iterable<T>.shouldMatchInOrderSubset(vararg assertions: (T) -> Unit) = toList().shouldMatchInOrderSubset(assertions.toList())60fun <T> Array<T>.shouldMatchInOrderSubset(vararg assertions: (T) -> Unit) = asList().shouldMatchInOrderSubset(assertions.toList())61fun <T> List<T>.shouldMatchInOrderSubset(vararg assertions: (T) -> Unit) = this.shouldMatchInOrderSubset(assertions.toList())62infix fun <T> Iterable<T>.shouldMatchInOrderSubset(assertions: List<(T) -> Unit>) = toList().shouldMatchInOrderSubset(assertions)63infix fun <T> Array<T>.shouldMatchInOrderSubset(assertions: List<(T) -> Unit>) = asList().shouldMatchInOrderSubset(assertions)64infix fun <T> List<T>.shouldMatchInOrderSubset(assertions: List<(T) -> Unit>) = this should matchInOrder(assertions.toList(), allowGaps = true)65fun <T> Iterable<T>.shouldNotMatchInOrderSubset(vararg assertions: (T) -> Unit) = toList().shouldNotMatchInOrderSubset(assertions.toList())66fun <T> Array<T>.shouldNotMatchInOrderSubset(vararg assertions: (T) -> Unit) = asList().shouldNotMatchInOrderSubset(assertions.toList())67fun <T> List<T>.shouldNotMatchInOrderSubset(vararg assertions: (T) -> Unit) = this.shouldNotMatchInOrderSubset(assertions.toList())68infix fun <T> Iterable<T>.shouldNotMatchInOrderSubset(assertions: List<(T) -> Unit>) = toList().shouldNotMatchInOrderSubset(assertions)69infix fun <T> Array<T>.shouldNotMatchInOrderSubset(assertions: List<(T) -> Unit>) = asList().shouldNotMatchInOrderSubset(assertions)70infix fun <T> List<T>.shouldNotMatchInOrderSubset(assertions: List<(T) -> Unit>) = this shouldNot matchInOrder(assertions.toList(), allowGaps = true)71fun <T> Iterable<T>.shouldMatchEach(vararg assertions: (T) -> Unit) = toList().shouldMatchEach(assertions.toList())72fun <T> Array<T>.shouldMatchEach(vararg assertions: (T) -> Unit) = asList().shouldMatchEach(assertions.toList())73fun <T> List<T>.shouldMatchEach(vararg assertions: (T) -> Unit) = this.shouldMatchEach(assertions.toList())74infix fun <T> Iterable<T>.shouldMatchEach(assertions: List<(T) -> Unit>) = toList().shouldMatchEach(assertions)75infix fun <T> Array<T>.shouldMatchEach(assertions: List<(T) -> Unit>) = asList().shouldMatchEach(assertions)76infix fun <T> List<T>.shouldMatchEach(assertions: List<(T) -> Unit>) = this should matchEach(assertions.toList())77fun <T> Iterable<T>.shouldNotMatchEach(vararg assertions: (T) -> Unit) = toList().shouldNotMatchEach(assertions.toList())78fun <T> Array<T>.shouldNotMatchEach(vararg assertions: (T) -> Unit) = asList().shouldNotMatchEach(assertions.toList())79fun <T> List<T>.shouldNotMatchEach(vararg assertions: (T) -> Unit) = this.shouldNotMatchEach(assertions.toList())80infix fun <T> Iterable<T>.shouldNotMatchEach(assertions: List<(T) -> Unit>) = toList().shouldNotMatchEach(assertions)81infix fun <T> Array<T>.shouldNotMatchEach(assertions: List<(T) -> Unit>) = asList().shouldNotMatchEach(assertions)82infix fun <T> List<T>.shouldNotMatchEach(assertions: List<(T) -> Unit>) = this shouldNot matchEach(assertions.toList())83fun <T> Iterable<T>.shouldExistInOrder(vararg ps: (T) -> Boolean) = toList().shouldExistInOrder(ps.toList())84fun <T> Array<T>.shouldExistInOrder(vararg ps: (T) -> Boolean) = asList().shouldExistInOrder(ps.toList())85fun <T> List<T>.shouldExistInOrder(vararg ps: (T) -> Boolean) = this.shouldExistInOrder(ps.toList())86infix fun <T> Iterable<T>.shouldExistInOrder(expected: List<(T) -> Boolean>) = toList().shouldExistInOrder(expected)87infix fun <T> Array<T>.shouldExistInOrder(expected: List<(T) -> Boolean>) = asList().shouldExistInOrder(expected)88infix fun <T> List<T>.shouldExistInOrder(expected: List<(T) -> Boolean>) = this should existInOrder(expected)89infix fun <T> Iterable<T>.shouldNotExistInOrder(expected: Iterable<(T) -> Boolean>) =90 toList().shouldNotExistInOrder(expected.toList())91infix fun <T> Array<T>.shouldNotExistInOrder(expected: Array<(T) -> Boolean>) =92 asList().shouldNotExistInOrder(expected.asList())93infix fun <T> Iterable<T>.shouldNotExistInOrder(expected: List<(T) -> Boolean>) =94 toList().shouldNotExistInOrder(expected)95infix fun <T> Array<T>.shouldNotExistInOrder(expected: List<(T) -> Boolean>) = asList().shouldNotExistInOrder(expected)96infix fun <T> List<T>.shouldNotExistInOrder(expected: List<(T) -> Boolean>) = this shouldNot existInOrder(expected)97fun <T> Iterable<T>.shouldContainAnyOf(vararg ts: T) = toList().shouldContainAnyOf(ts)98fun <T> Array<T>.shouldContainAnyOf(vararg ts: T) = asList().shouldContainAnyOf(ts)99fun <T> Collection<T>.shouldContainAnyOf(vararg ts: T) = this should containAnyOf(ts.asList())100fun <T> Iterable<T>.shouldNotContainAnyOf(vararg ts: T) = toList().shouldNotContainAnyOf(ts)101fun <T> Array<T>.shouldNotContainAnyOf(vararg ts: T) = asList().shouldNotContainAnyOf(ts)102fun <T> Collection<T>.shouldNotContainAnyOf(vararg ts: T) = this shouldNot containAnyOf(ts.asList())103infix fun <T> Iterable<T>.shouldContainAnyOf(ts: Collection<T>) = toList().shouldContainAnyOf(ts)104infix fun <T> Array<T>.shouldContainAnyOf(ts: Collection<T>) = asList().shouldContainAnyOf(ts)105infix fun <T> Collection<T>.shouldContainAnyOf(ts: Collection<T>) = this should containAnyOf(ts)106infix fun <T> Iterable<T>.shouldNotContainAnyOf(ts: Collection<T>) = toList().shouldNotContainAnyOf(ts)107infix fun <T> Array<T>.shouldNotContainAnyOf(ts: Collection<T>) = asList().shouldNotContainAnyOf(ts)108infix fun <T> Collection<T>.shouldNotContainAnyOf(ts: Collection<T>) = this shouldNot containAnyOf(ts)109fun <T> containAnyOf(ts: Collection<T>) = object : Matcher<Collection<T>> {110 override fun test(value: Collection<T>): MatcherResult {111 if (ts.isEmpty()) throwEmptyCollectionError()112 return MatcherResult(113 ts.any { it in value },114 { "Collection ${value.print().value} should contain any of ${ts.print().value}" },115 { "Collection ${value.print().value} should not contain any of ${ts.print().value}" }116 )117 }118}119internal fun throwEmptyCollectionError(): Nothing {120 throw AssertionError("Asserting content on empty collection. Use Collection.shouldBeEmpty() instead.")121}...

Full Screen

Full Screen

ArgumentsTest.kt

Source:ArgumentsTest.kt Github

copy

Full Screen

1package de.qualersoft.robotframework.gradleplugin.utils2import io.kotest.assertions.assertSoftly3import io.kotest.core.spec.style.AnnotationSpec4import io.kotest.matchers.Matcher5import io.kotest.matchers.MatcherResult6import io.kotest.matchers.collections.shouldHaveElementAt7import io.kotest.matchers.collections.shouldHaveSize8import io.kotest.matchers.should9import io.kotest.matchers.shouldNot10import java.io.File11class ArgumentsTest : AnnotationSpec() {12 private lateinit var sut: Arguments13 @BeforeEach14 fun setupTest() {15 sut = Arguments()16 }17 @Test18 fun givenNewArgumentsThenResultIsEmpty() {19 sut.shouldBeEmpty()20 }21 @Test22 fun whenAddingAnOptionalFileOfNullThenResultIsEmpty() {23 sut.addOptionalFile(null, "a")24 sut.shouldBeEmpty()25 }26 @Test27 fun whenAddingAnOptionalNonNullFileThenResultIsNotEmpty() {28 sut.addOptionalFile(File("./test"), "a")29 assertSoftly {30 sut.shouldNotBeEmpty()31 val arr = sut.toArray()32 arr.shouldHaveSize(2)33 arr.shouldHaveElementAt(0, "a")34 arr.shouldHaveElementAt(1, File("./test").path)35 }36 }37 @Test38 fun whenAddingNullFileThenItsConvertedToNone() {39 sut.addFileToArguments(null, "f")40 assertSoftly {41 sut.shouldNotBeEmpty()42 val arr = sut.toArray()43 arr.shouldHaveSize(2)44 arr.shouldHaveElementAt(0, "f")45 arr.shouldHaveElementAt(1, "NONE")46 }47 }48 @Test49 fun whenAddingNonEmptyFileThenItWillBeInResult() {50 sut.addFileToArguments(File("./test"), "f")51 assertSoftly {52 sut.shouldNotBeEmpty()53 val arr = sut.toArray()54 arr.shouldHaveSize(2)55 arr.shouldHaveElementAt(0, "f")56 arr.shouldHaveElementAt(1, File("./test").path)57 }58 }59 @Test60 fun whenAddingEmptyFileThenItsNotInResult() {61 sut.addFileToArguments(File(""), "f")62 sut.shouldBeEmpty()63 }64 @Test65 fun whenAddingNullStringThenItsNotInResult() {66 sut.addNonEmptyStringToArguments(null, "s")67 sut.shouldBeEmpty()68 }69 @Test70 fun whenAddingEmptyStringThenItsNotInResult() {71 sut.addNonEmptyStringToArguments("", "s")72 sut.shouldBeEmpty()73 }74 @Test75 fun whenAddingNonEmptyStringThenItsInResult() {76 sut.addNonEmptyStringToArguments("notEmpty", "s")77 assertSoftly {78 sut.shouldNotBeEmpty()79 val arr = sut.toArray()80 arr.shouldHaveSize(2)81 arr.shouldHaveElementAt(0, "s")82 arr.shouldHaveElementAt(1, "notEmpty")83 }84 }85 @Test86 fun whenAddingEmptyMapThenItsNotInResult() {87 sut.addMapToArguments(mapOf(), "m")88 sut.shouldBeEmpty()89 }90 @Test91 fun whenAddingMapThenItsInResult() {92 sut.addMapToArguments(mapOf("key" to "val"), "m")93 assertSoftly {94 sut.shouldNotBeEmpty()95 val arr = sut.toArray()96 arr.shouldHaveSize(2)97 arr.shouldHaveElementAt(0, "m")98 arr.shouldHaveElementAt(1, "key:val")99 }100 }101 @Test102 fun whenAddingMultiMapThenItsInResult() {103 sut.addMapToArguments(mapOf("key1" to "val1", "key2" to "val2"), "m")104 assertSoftly {105 sut.shouldNotBeEmpty()106 val arr = sut.toArray()107 arr.shouldHaveSize(4)108 arr.shouldHaveElementAt(0, "m")109 arr.shouldHaveElementAt(1, "key1:val1")110 arr.shouldHaveElementAt(2, "m")111 arr.shouldHaveElementAt(3, "key2:val2")112 }113 }114 @Test115 fun whenAddingNullFlagThenItsNotInResult() {116 sut.addFlagToArguments(null, "b")117 sut.shouldBeEmpty()118 }119 @Test120 fun whenAddingFalseFlagThenItsNotInResult() {121 sut.addFlagToArguments(false, "b")122 sut.shouldBeEmpty()123 }124 @Test125 fun whenAddingTrueFlagThenItsInResult() {126 sut.addFlagToArguments(true, "b")127 assertSoftly {128 sut.shouldNotBeEmpty()129 val arr = sut.toArray()130 arr.shouldHaveSize(1)131 arr.shouldHaveElementAt(0, "b")132 }133 }134 @Test135 fun whenAddingOptionalNullStringThenItsNotInResult() {136 sut.addStringToArguments(null, "s")137 sut.shouldBeEmpty()138 }139 @Test140 fun whenAddingOptionalEmptyStringThenItsInResult() {141 sut.addStringToArguments("", "s")142 assertSoftly {143 sut.shouldNotBeEmpty()144 val arr = sut.toArray()145 arr.shouldHaveSize(2)146 arr.shouldHaveElementAt(0, "s")147 arr.shouldHaveElementAt(1, "")148 }149 }150 @Test151 fun whenAddingOptionalNonEmptyStringThenItsInResult() {152 sut.addStringToArguments("NotEmpty", "s")153 assertSoftly {154 sut.shouldNotBeEmpty()155 val arr = sut.toArray()156 arr.shouldHaveSize(2)157 arr.shouldHaveElementAt(0, "s")158 arr.shouldHaveElementAt(1, "NotEmpty")159 }160 }161 @Test162 fun whenAddNullStringListThenItsNotInResult() {163 sut.addListToArguments(null as String?, "s")164 sut.shouldBeEmpty()165 }166 @Test167 fun whenAddStringListThenItsInResult() {168 sut.addListToArguments("aString", "s")169 assertSoftly {170 sut.shouldNotBeEmpty()171 val arr = sut.toArray()172 arr.shouldHaveSize(2)173 arr.shouldHaveElementAt(0, "s")174 arr.shouldHaveElementAt(1, "aString")175 }176 }177 @Test178 fun whenAddingMultiStringListThenEachIsInResult() {179 sut.addListToArguments("str1, str2", "s")180 assertSoftly {181 sut.shouldNotBeEmpty()182 val arr = sut.toArray()183 arr.shouldHaveSize(4)184 arr.shouldHaveElementAt(0, "s")185 arr.shouldHaveElementAt(1, "str1")186 arr.shouldHaveElementAt(2, "s")187 arr.shouldHaveElementAt(3, "str2")188 }189 }190 @Test191 fun whenAddingNullListThenItsNotInResult() {192 sut.addListToArguments(null as List<String?>?, "ls")193 sut.shouldBeEmpty()194 }195 @Test196 fun whenAddingEmptyListThenItsNotInResult() {197 sut.addListToArguments(listOf(), "ls")198 sut.shouldBeEmpty()199 }200 @Test201 fun whenAddingListWithNullThenItsNotInResult() {202 sut.addListToArguments(listOf<String?>(null), "ls")203 sut.shouldBeEmpty()204 }205 @Test206 fun whenAddingListWithEmptyThenItsNotInResult() {207 sut.addListToArguments(listOf(""), "ls")208 sut.shouldBeEmpty()209 }210 @Test211 fun whenAddingListThenItsInResult() {212 sut.addListToArguments(listOf("str"), "ls")213 assertSoftly {214 sut.shouldNotBeEmpty()215 val arr = sut.toArray()216 arr.shouldHaveSize(2)217 arr.shouldHaveElementAt(0, "ls")218 arr.shouldHaveElementAt(1, "str")219 }220 }221 @Test222 fun whenAddingListWithMoreElemsThenEachIsInResult() {223 sut.addListToArguments(listOf("str1", "str2"), "ls")224 assertSoftly {225 sut.shouldNotBeEmpty()226 val arr = sut.toArray()227 arr.shouldHaveSize(4)228 arr.shouldHaveElementAt(0, "ls")229 arr.shouldHaveElementAt(1, "str1")230 arr.shouldHaveElementAt(2, "ls")231 arr.shouldHaveElementAt(3, "str2")232 }233 }234 @Test235 fun whenAddingNullFileListThenItsNotInResult() {236 sut.addFileListToArguments(null, "fl")237 sut.shouldBeEmpty()238 }239 @Test240 fun whenAddingEmptyFileListThenItsNotInResult() {241 sut.addFileListToArguments(listOf(), "fl")242 sut.shouldBeEmpty()243 }244 @Test245 fun whenAddingFileListWithEmptyFileThenItsNotInResult() {246 sut.addFileListToArguments(listOf(File("")), "fl")247 sut.shouldBeEmpty()248 }249 @Test250 fun whenAddingFileListWithFileThenItsInResult() {251 sut.addFileListToArguments(listOf(File("./test")), "fl")252 assertSoftly {253 sut.shouldNotBeEmpty()254 val arr = sut.toArray()255 arr.shouldHaveSize(2)256 arr.shouldHaveElementAt(0, "fl")257 arr.shouldHaveElementAt(1, File("./test").path)258 }259 }260 // <editor-fold desc="Helper extensions">261 private fun beEmpty() = object : Matcher<Arguments> {262 override fun test(value: Arguments) = MatcherResult(263 value.toArray().isEmpty(),264 "Arguments $value should be empty",265 "String $value should not be empty"266 )267 }268 private fun Arguments.shouldBeEmpty() = this should beEmpty()269 private fun Arguments.shouldNotBeEmpty() = this shouldNot beEmpty()270 // </editor-fold>271}...

Full Screen

Full Screen

Array.shouldHaveElementAt

Using AI Code Generation

copy

Full Screen

1val array = arrayOf(1, 2, 3)2array.shouldHaveElementAt(1, 2)3val array = arrayOf(1, 2, 3)4array.shouldNotHaveElementAt(1, 4)5val array = arrayOf(1)6array.shouldHaveSingleElement(1)7val array = arrayOf(1)8array.shouldNotHaveSingleElement(2)9val array = arrayOf()10array.shouldBeEmpty()11val array = arrayOf(1)12array.shouldNotBeEmpty()13val array = arrayOf(1, 2, 3)14array.shouldBeSorted()15val array = arrayOf(1, 2, 3)16array.shouldNotBeSorted()17val array = arrayOf(1, 2, 3)18array.shouldBeSortedWith(compareBy { it })19val array = arrayOf(1, 2, 3)20array.shouldNotBeSortedWith(compareBy { it })21val array = arrayOf(1, 2, 3)22array.shouldBeSortedBy { it }23val array = arrayOf(1, 2, 3)24array.shouldNotBeSortedBy { it }

Full Screen

Full Screen

Array.shouldHaveElementAt

Using AI Code Generation

copy

Full Screen

1import io.kotest.matchers.collections.shouldHaveElementAt2val array = arrayOf(1, 2, 3, 4, 5)3array.shouldHaveElementAt(0, 1)4array.shouldHaveElementAt(1, 2)5array.shouldHaveElementAt(2, 3)6array.shouldHaveElementAt(3, 4)7array.shouldHaveElementAt(4, 5)8import io.kotest.matchers.collections.shouldNotHaveElementAt9val array = arrayOf(1, 2, 3, 4, 5)10array.shouldNotHaveElementAt(0, 6)11array.shouldNotHaveElementAt(1, 7)12array.shouldNotHaveElementAt(2, 8)13array.shouldNotHaveElementAt(3, 9)14array.shouldNotHaveElementAt(4, 10)15import io.kotest.matchers.collections.shouldHaveElements16val array = arrayOf(1, 2, 3, 4, 5)17array.shouldHaveElements(1, 2, 3, 4, 5)18import io.kotest.matchers.collections.shouldNotHaveElements19val array = arrayOf(1, 2, 3, 4, 5)20array.shouldNotHaveElements(6, 7, 8, 9, 10)21import io.kotest.matchers.collections.shouldHaveSameSizeAs22val array = arrayOf(1, 2, 3, 4, 5)23array.shouldHaveSameSizeAs(arrayOf(1, 2, 3, 4, 5))24import io.kotest.matchers.collections.shouldHaveSize25val array = arrayOf(1, 2, 3, 4, 5)26array.shouldHaveSize(5)

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful