Best Kotest code snippet using io.kotest.matchers.collections.matchers.Collection.shouldNotContainAnyOf
CollectionMatchersTest.kt
Source:CollectionMatchersTest.kt
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}...
UniqueDataProvierIT.kt
Source:UniqueDataProvierIT.kt
1package io.github.serpro69.kfaker2import io.github.serpro69.kfaker.provider.Address3import io.kotest.assertions.assertSoftly4import io.kotest.core.spec.style.DescribeSpec5import io.kotest.matchers.collections.beUnique6import io.kotest.matchers.collections.containDuplicates7import io.kotest.matchers.collections.shouldContainAnyOf8import io.kotest.matchers.collections.shouldNotContainAnyOf9import io.kotest.matchers.should10import io.kotest.matchers.shouldBe11import io.kotest.matchers.shouldNot12import io.kotest.matchers.shouldNotBe13@Suppress("UNCHECKED_CAST")14class UniqueDataProviderIT : DescribeSpec({15 describe("unique generation of values enabled for provider through configuration") {16 val config = FakerConfig.builder().create { uniqueGeneratorRetryLimit = 100 }17 // repeat 10 times to make sure values are not included in the collection18 repeat(10) {19 context("collection of values is generated #run$it") {20 val faker = Faker(config)21 faker.unique.configuration { enable(faker::address) }22 val countries = (0..18).map { faker.address.country() }23 val newCountries = (0..30).map { faker.address.country() }24 it("should have unique values") {25 assertSoftly {26 countries should beUnique()27 newCountries should beUnique()28 newCountries shouldNotContainAnyOf countries29 }30 }31 }32 }33 }34 describe("collection of values is used to exclude values from being generated for specific provider") {35 val config = FakerConfig.builder().create { uniqueGeneratorRetryLimit = 100 }36 // repeat 10 times to make sure values are not included in the collection37 repeat(10) {38 val faker = Faker(config)39 faker.unique.configuration { enable(faker::address) }40 val countries = (0..30).map { faker.address.country() }41 context("collection of unique values is generated run#$it") {42 val excludedCountries = listOf(43 "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola"44 )45 faker.unique.configuration { excludeFromFunction(Address::country, excludedCountries) }46 val newCountries = (0..30).map { faker.address.country() }47 val moreExcludedCountries = listOf(48 "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic"49 )50 faker.unique.configuration { excludeFromProvider<Address>(moreExcludedCountries) }51 val moreCountries = (0..30).map { faker.address.country() }52 val excludedCountryCodes = listOf("AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AQ")53 faker.unique.configuration { excludeFromProvider<Address>(excludedCountryCodes) }54 val countryCodes = (0..30).map { faker.address.countryCode() }55 it("excluded values through config should not be included in the generation") {56 assertSoftly {57 newCountries shouldNotContainAnyOf excludedCountries58 moreCountries shouldNotContainAnyOf moreExcludedCountries59 countryCodes shouldNotContainAnyOf excludedCountryCodes60 }61 }62 it("already generated values through config should not be included in the generation") {63 assertSoftly {64 newCountries shouldNotContainAnyOf countries65 moreCountries shouldNotContainAnyOf newCountries66 moreCountries shouldNotContainAnyOf countries67 }68 }69 }70 }71 }72 describe("use collections to exclude values from being generated for all providers") {73 // repeat 10 times to make sure values are not included in the collection74 repeat(10) {75 val faker = Faker()76 val excludedCountries = listOf(77 "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",78 "Angola", "Anguilla", "Antarctica (the territory South of 60 deg S)", "Antigua and Barbuda",79 "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain",80 "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",81 "Bosnia and Herzegovina", "Botswana", "Bouvet Island (Bouvetoya)", "Brazil",82 "British Indian Ocean Territory (Chagos Archipelago)", "Brunei Darussalam", "Bulgaria",83 "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands",84 "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands",85 "Colombia", "Comoros", "Congo", "Congo", "Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia",86 "Cuba", "Cyprus", "Czech Republic",87 )88 val excludedNames = listOf(89 "Abbott", "Abernathy", "Abshire", "Adams", "Altenwerth", "Anderson", "Ankunding", "Armstrong", "Auer",90 "Aufderhar", "Bahringer", "Bailey", "Balistreri", "Barrows", "Bartell", "Bartoletti", "Barton",91 "Bashirian", "Batz", "Bauch", "Baumbach", "Bayer", "Beahan", "Beatty", "Bechtelar", "Becker", "Bednar",92 "Beer", "Beier", "Berge", "Bergnaum", "Bergstrom", "Bernhard", "Bernier", "Bins", "Blanda", "Blick",93 "Block", "Bode", "Boehm", "Bogan", "Bogisich", "Borer", "Bosco", "Botsford", "Boyer", "Boyle",94 "Bradtke", "Brakus", "Braun", "Breitenberg", "Brekke", "Brown", "Bruen", "Buckridge"95 )96 val excludedBicCodes = listOf(97 "AACCGB21", "AACNGB21", "AAFMGB21", "AAHOGB21", "AAHVGB21", "AANLGB21",98 "AANLGB2L", "AAOGGB21", "AAPEGB21", "AAPUGB21", "AAQIGB21", "ABBYGB2L",99 "BCYPGB2LCBB", "BCYPGB2LHGB", "BCYPGB2LHHB", "BCYPGB2LPGB", "BCYPGB2LSSB", "BCYPGB2LMBB"100 )101 val excludeAll = listOf(excludedCountries, excludedNames, excludedBicCodes).flatten()102 faker.unique.configuration {103 enable(faker::address)104 enable(faker::name)105 exclude(excludeAll)106 }107 context("collection of unique values is generated run#$it") {108 val countries = (0..30).map { faker.address.country() }109 val names = (0..30).map { faker.name.lastName() }110 // Unique generation not enabled for Bank111 val bicCodes = (0..30).map { faker.bank.swiftBic() }112 it("should not contain excluded values") {113 assertSoftly {114 countries shouldNotContainAnyOf excludeAll115 names shouldNotContainAnyOf excludeAll116 // Unique generation not enabled for Bank117 bicCodes shouldNot beUnique()118 bicCodes shouldContainAnyOf excludedBicCodes119 }120 }121 }122 }123 }124 describe("use regex patterns to exclude values from being generated for all providers") {125 repeat(10) {126 val faker = Faker()127 faker.unique.configuration {128 // Enable unique generation129 enable(faker::address)130 enable(faker::name)131 // Exclude all values starting with "A"132 exclude { listOf(Regex("^A")) }133 }134 it("should not contain values matching pattern run#$it") {135 val countries = (0..30).map { faker.address.country() }136 val names = (0..30).map { faker.name.lastName() }137 // Unique generation not enabled for Bank138 val bicCodes = (0..30).map { faker.bank.swiftBic() }139 assertSoftly {140 countries.none { s -> s.startsWith("A") } shouldBe true141 countries should beUnique()142 names.none { s -> s.startsWith("A") } shouldBe true143 names should beUnique()144 // Unique generation not enabled for Bank145 bicCodes.any { s -> s.startsWith("A") } shouldBe true146 bicCodes shouldNot beUnique()147 }148 }149 }150 }151 describe("use regex patterns to exclude values from being generated for specific provider") {152 repeat(10) {153 val faker = Faker()154 faker.unique.configuration {155 // Enable unique generation and exclude by patterns156 enable(faker::address) {157 excludeFromProvider<Address> { listOf(Regex("^A")) }158 excludeFromFunction(Address::country) { listOf(Regex("^B")) }159 }160 }161 it("excluded values by pattern should not be included in the generation for the provider run#$it") {162 val countries = (0..30).map { faker.address.country() }163 val cities = (0..30).map { faker.address.city() }164 assertSoftly {165 countries.none { s -> s.startsWith("A") } shouldBe true166 countries.none { s -> s.startsWith("B") } shouldBe true167 cities.none { s -> s.startsWith("A") } shouldBe true168 }169 }170 }171 }172 describe("unique generation of values for category") {173 val config = FakerConfig.builder().create { uniqueGeneratorRetryLimit = 100 }174 context("collection of values is generated") {175 val faker = Faker(config)176 faker.unique.enable(faker::address)177 faker.unique.enable(faker::ancient)178 val countries = (0..20).map { faker.address.country() }179 it("should not contain duplicates") {180 countries should beUnique()181 }182 }183 context("used values are cleared") {184 val faker = Faker(config).also {185 it.unique.enable(it::address)186 }187 val countries = (0..20).map { faker.address.country() }188 faker.unique.clear(faker::address)189 val newCountries = (0..20).map { faker.address.country() }190 it("new collection should not contain duplicates") {191 newCountries should beUnique()192 }193 it("new collection should not equal old collection") {194 newCountries shouldNotBe countries195 }196 }197 context("exclude values from being generated") {198 // repeat 10 times to make sure values are not included in the collection199 repeat(10) {200 val faker = Faker(config)201 faker.unique.enable(faker::address)202 val countries = (0..5).map { faker.address.country() }203 context("collection of values was marked for exclusion run#$it") {204 val excludedCountries = listOf(205 "Afghanistan",206 "Albania",207 "Algeria",208 "American Samoa",209 "Andorra",210 "Angola"211 )212 faker.unique.exclude<Address>("country", excludedCountries)213 val newCountries = (0..20).map { faker.address.country() }214 it("excluded values should not be included in the generation") {215 newCountries shouldNotContainAnyOf excludedCountries216 }217 it("already generated values should not be included in the generation") {218 newCountries shouldNotContainAnyOf countries219 }220 }221 }222 }223 context("unique generation is not enabled for category") {224 val faker = Faker(config)225 context("some values were marked for exclusion") {226 val excludedCountries = listOf(227 "Afghanistan",228 "Albania",229 "Algeria",230 "American Samoa",231 "Andorra",232 "Angola",233 "Anguilla",234 "Antarctica",235 "Antigua And Barbuda",236 "Argentina",237 "Armenia",238 "Aruba",239 "Australia",240 "Austria",241 "Aland Islands",242 "Azerbaijan",243 "Bahamas",244 "Bahrain",245 "Bangladesh",246 "Barbados",247 "Belarus",248 "Belgium",249 "Belize",250 "Benin",251 "Bermuda",252 "Bhutan",253 "Bolivia",254 "Bonaire",255 "Bosnia And Herzegovina",256 "Botswana",257 "Bouvet Island"258 )259 faker.unique.exclude<Address>("country", excludedCountries)260 val countries = (0..100).map { faker.address.country() }261 it("collection can have duplicates") {262 countries should containDuplicates()263 }264 it("collection can have excluded values") {265 countries.any { excludedCountries.contains(it) } shouldBe true266 }267 }268 }269 context("values are generated for another category that is not marked for unique generation") {270 val faker = Faker(config)271 faker.unique.enable(faker::address)272 val animalNames = (0..100).map { faker.animal.name() }273 it("collection can have duplicates") {274 animalNames shouldNot beUnique()275 }276 }277 context("unique generation for category is disabled") {278 val faker = Faker(config)279 faker.unique.enable(faker::address)280 (0..20).map { faker.address.country() }281 faker.unique.disable(faker::address)282 context("collection of values is generated") {283 val countries = (0..100).map { faker.address.country() }284 it("collection can have duplicates") {285 countries shouldNot beUnique()286 }287 }288 }289 context("unique generation is disabled for all categories") {290 val faker = Faker(config)291 faker.unique.enable(faker::address)292 faker.unique.enable(faker::ancient)293 faker.unique.disableAll()294 context("collections of values are generated") {295 val countries = (0..100).map { faker.address.country() }296 val gods = (0..100).map { faker.ancient.god() }297 it("collection can have duplicates") {298 assertSoftly {299 countries shouldNot beUnique()300 gods shouldNot beUnique()301 }302 }303 }304 context("unique generation for a category is re-enabled") {305 faker.unique.enable(faker::address)306 context("collection of values is generated") {307 val countries = (0..20).map { faker.address.country() }308 it("collection should not contain duplicates") {309 countries should beUnique()310 }311 }312 }313 }314 context("unique generation is cleared for all categories") {315 val faker = Faker(config)316 faker.unique.enable(faker::address)317 faker.unique.enable(faker::ancient)318 // Generate some values first319 (0..20).map { faker.address.country() }320 (0..20).map { faker.ancient.hero() }321 faker.unique.clearAll()322 context("collections of values are generated") {323 val countries = (0..20).map { faker.address.country() }324 val heroes = (0..20).map { faker.ancient.hero() }325 it("collections should be unique") {326 assertSoftly {327 countries should beUnique()328 heroes should beUnique()329 }330 }331 }332 }333 }334 describe("local unique generation") {335 val config = FakerConfig.builder().create {336 uniqueGeneratorRetryLimit = 100337 }338 val faker = Faker(config)339 context("unique property prefixes the category function invocation") {340 val countries = (0..20).map {341 faker.address.unique.country()342 }343 it("collection should not contain duplicates") {344 countries should beUnique()345 }346 it("other functions of the same category should not be marked as unique") {347 // This will produce an error if used with `unique` prefix348 // because there is only one value in the dictionary349 (0..10).map { faker.address.defaultCountry() }350 }351 context("values are cleared for the function name") {352 faker.address.unique.clear("country")353 val newCountries = (0..20).map {354 faker.address.unique.country()355 }356 it("collection should not contain duplicates") {357 newCountries should beUnique()358 }359 }360 context("values are cleared for all functions") {361 repeat(20) { faker.address.unique.country() }362 repeat(20) { faker.address.unique.state() }363 faker.address.unique.clearAll()364 it("re-generated collections should be unique") {365 val newCountries = (0..20).map { faker.address.unique.country() }366 val states = (0..20).map { faker.address.unique.state() }367 assertSoftly {368 newCountries should beUnique()369 states should beUnique()370 }371 }372 }373 }374 }375})...
matchers.kt
Source:matchers.kt
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}...
Collection.shouldNotContainAnyOf
Using AI Code Generation
1assertThat(listOf(1, 2, 3)).shouldNotContainAnyOf(listOf(4, 5, 6))2assertThat(listOf(1, 2, 3)).shouldNotContainAnyOf(4, 5, 6)3assertThat(listOf(1, 2, 3)).shouldNotContainAnyOf(listOf(1, 2, 3))4assertThat(listOf(1, 2, 3)).shouldNotContainAnyOf(1, 2, 3)5assertThat(listOf(1, 2, 3)).shouldNotContainAnyOf(listOf(1, 2, 3))6assertThat(listOf(1, 2, 3)).shouldNotContainAnyOf(1, 2, 3)7assertThat(listOf(1, 2, 3)).shouldNotContainAnyOf(listOf(1, 2, 3))8assertThat(listOf(1, 2, 3)).shouldNotContainAnyOf(1, 2, 3)9assertThat(listOf(1, 2, 3)).shouldNotContainAnyOf(listOf(1, 2, 3))10assertThat(listOf(1, 2, 3)).shouldNotContainAnyOf(1, 2, 3)11assertThat(listOf(1, 2, 3)).shouldNotContainAnyOf(listOf(1, 2, 3))12assertThat(listOf(1, 2, 3)).shouldNotContainAnyOf(1, 2, 3)13assertThat(listOf(1, 2, 3)).shouldNotContainAnyOf(listOf(1,
Collection.shouldNotContainAnyOf
Using AI Code Generation
1 shouldNotContainAnyOf(listOf(1, 2, 3), listOf(4, 5, 6)) shouldBe true2 shouldNotContainAnyOf(listOf(1, 2, 3), listOf(4, 5, 6)) shouldBe false3 shouldBeEmpty(listOf()) shouldBe true4 shouldBeEmpty(listOf(1, 2, 3)) shouldBe false5 shouldNotBeEmpty(listOf(1, 2, 3)) shouldBe true6 shouldNotBeEmpty(listOf()) shouldBe false7 shouldBeIn(listOf(1, 2, 3), listOf(1, 2, 3)) shouldBe true8 shouldBeIn(listOf(1, 2, 3), listOf(4, 5, 6)) shouldBe false9 shouldNotBeIn(listOf(1, 2, 3), listOf(4, 5, 6)) shouldBe true10 shouldNotBeIn(listOf(1, 2, 3), listOf(1, 2, 3)) shouldBe false11 shouldHaveAtLeastOneElementThat(listOf(1, 2, 3)) { it % 2 == 0 } shouldBe true12 shouldHaveAtLeastOneElementThat(listOf(1, 2, 3)) { it % 2 == 1 } shouldBe false13 shouldHaveAtLeastOneElement(listOf(1, 2, 3)) shouldBe true14 shouldHaveAtLeastOneElement(listOf()) shouldBe false
Collection.shouldNotContainAnyOf
Using AI Code Generation
11. import io.kotest.matchers.collections.shouldNotContainAnyOf22. listOf(1, 2, 3, 4, 5).shouldNotContainAnyOf(6, 7, 8, 9, 10)33. listOf(1, 2, 3, 4, 5).shouldNotContainAnyOf(6, 7, 8, 9, 10)44. listOf(1, 2, 3, 4, 5).shouldNotContainAnyOf(6, 7, 8, 9, 10)55. listOf(1, 2, 3, 4, 5).shouldNotContainAnyOf(6, 7, 8, 9, 10)66. listOf(1, 2, 3, 4, 5).shouldNotContainAnyOf(6, 7, 8, 9, 10)77. listOf(1, 2, 3, 4, 5).shouldNotContainAnyOf(6, 7, 8, 9, 10)88. listOf(1, 2, 3, 4, 5).shouldNotContainAnyOf(6, 7, 8, 9, 10)99. listOf(1, 2, 3, 4, 5).shouldNotContainAnyOf(6, 7, 8, 9, 10)1010. listOf(1, 2, 3, 4, 5).shouldNotContainAnyOf(6, 7, 8, 9, 10)1111. listOf(1, 2, 3, 4, 5).shouldNotContainAnyOf(6, 7, 8, 9, 10)1212. listOf(1, 2, 3, 4, 5).shouldNotContainAnyOf(6, 7, 8, 9, 10)1313. listOf(1, 2, 3, 4, 5).shouldNotContainAnyOf(6, 7, 8, 9, 10)1414. listOf(1, 2, 3, 4, 5).shouldNotContainAnyOf(6,
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!!