How to use Array.forSingle method of io.kotest.inspectors.Inspectors class

Best Kotest code snippet using io.kotest.inspectors.Inspectors.Array.forSingle

CollectionInspectorsTest.kt

Source:CollectionInspectorsTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.inspectors2import io.kotest.assertions.assertSoftly3import io.kotest.assertions.shouldFail4import io.kotest.assertions.throwables.shouldThrow5import io.kotest.assertions.throwables.shouldThrowAny6import io.kotest.core.spec.style.WordSpec7import io.kotest.inspectors.forAll8import io.kotest.inspectors.forAny9import io.kotest.inspectors.forAtLeastOne10import io.kotest.inspectors.forAtMostOne11import io.kotest.inspectors.forExactly12import io.kotest.inspectors.forNone13import io.kotest.inspectors.forOne14import io.kotest.inspectors.forSingle15import io.kotest.inspectors.forSome16import io.kotest.matchers.comparables.beGreaterThan17import io.kotest.matchers.comparables.beLessThan18import io.kotest.matchers.ints.shouldBeGreaterThan19import io.kotest.matchers.ints.shouldBeLessThan20import io.kotest.matchers.should21import io.kotest.matchers.shouldBe22@Suppress("ConstantConditionIf")23class CollectionInspectorsTest : WordSpec() {24 private val list = listOf(1, 2, 3, 4, 5)25 private val array = arrayOf(1, 2, 3, 4, 5)26 data class DummyEntry(27 val id: Int,28 val name: String,29 )30 init {31 "forAll" should {32 "pass if all elements of an array pass" {33 array.forAll {34 it.shouldBeGreaterThan(0)35 }36 }37 "pass if all elements of a collection pass" {38 list.forAll {39 it.shouldBeGreaterThan(0)40 }41 }42 "return itself" {43 array.forAll {44 it.shouldBeGreaterThan(0)45 }.forAll {46 it.shouldBeGreaterThan(0)47 }48 list.forAll {49 it.shouldBeGreaterThan(0)50 }.forAll {51 it.shouldBeGreaterThan(0)52 }53 }54 "fail when an exception is thrown inside an array" {55 shouldThrowAny {56 array.forAll {57 if (true) throw NullPointerException()58 }59 }.message shouldBe "0 elements passed but expected 5\n" +60 "\n" +61 "The following elements passed:\n" +62 "--none--\n" +63 "\n" +64 "The following elements failed:\n" +65 "1 => java.lang.NullPointerException\n" +66 "2 => java.lang.NullPointerException\n" +67 "3 => java.lang.NullPointerException\n" +68 "4 => java.lang.NullPointerException\n" +69 "5 => java.lang.NullPointerException"70 }71 "fail when an exception is thrown inside a list" {72 shouldThrowAny {73 list.forAll {74 if (true) throw NullPointerException()75 }76 }.message shouldBe "0 elements passed but expected 5\n" +77 "\n" +78 "The following elements passed:\n" +79 "--none--\n" +80 "\n" +81 "The following elements failed:\n" +82 "1 => java.lang.NullPointerException\n" +83 "2 => java.lang.NullPointerException\n" +84 "3 => java.lang.NullPointerException\n" +85 "4 => java.lang.NullPointerException\n" +86 "5 => java.lang.NullPointerException"87 }88 }89 "forNone" should {90 "pass if no elements pass fn test for a list" {91 list.forNone {92 it shouldBe 1093 }94 }95 "pass if no elements pass fn test for an array" {96 array.forNone {97 it shouldBe 1098 }99 }100 "pass if an element throws an exception" {101 val items = listOf(1, 2, 3)102 items.forNone {103 if (true) throw NullPointerException()104 }105 }106 "return itself" {107 list.forNone {108 it shouldBe 10109 }.forNone {110 it shouldBe 10111 }112 array.forNone {113 it shouldBe 10114 }.forNone {115 it shouldBe 10116 }117 }118 "fail if one elements passes fn test" {119 shouldThrow<AssertionError> {120 list.forNone {121 it shouldBe 4122 }123 }.message shouldBe """1 elements passed but expected 0124The following elements passed:1254126The following elements failed:1271 => expected:<4> but was:<1>1282 => expected:<4> but was:<2>1293 => expected:<4> but was:<3>1305 => expected:<4> but was:<5>"""131 }132 "fail if all elements pass fn test" {133 shouldThrow<AssertionError> {134 list.forNone {135 it should beGreaterThan(0)136 }137 }.message shouldBe """5 elements passed but expected 0138The following elements passed:13911402141314241435144The following elements failed:145--none--"""146 }147 "work inside assertSoftly block" {148 val dummyEntries = listOf(149 DummyEntry(id = 1, name = "first"),150 DummyEntry(id = 2, name = "second"),151 )152 assertSoftly(dummyEntries) {153 forNone {154 it.id shouldBe 3155 it.name shouldBe "third"156 }157 }158 }159 }160 "forSome" should {161 "pass if one elements pass test" {162 list.forSome {163 it shouldBe 3164 }165 }166 "pass if size-1 elements pass test" {167 list.forSome {168 it should beGreaterThan(1)169 }170 }171 "return itself" {172 list.forSome {173 it shouldBe 3174 }.forSome {175 it shouldBe 3176 }177 array.forSome {178 it shouldBe 3179 }.forSome {180 it shouldBe 3181 }182 }183 "fail if no elements pass test" {184 shouldThrow<AssertionError> {185 array.forSome {186 it should beLessThan(0)187 }188 }.message shouldBe """No elements passed but expected at least one189The following elements passed:190--none--191The following elements failed:1921 => 1 should be < 01932 => 2 should be < 01943 => 3 should be < 01954 => 4 should be < 01965 => 5 should be < 0"""197 }198 "fail if all elements pass test" {199 shouldThrow<AssertionError> {200 list.forSome {201 it should beGreaterThan(0)202 }203 }.message shouldBe """All elements passed but expected < 5204The following elements passed:20512062207320842095210The following elements failed:211--none--"""212 }213 "work inside assertSoftly block" {214 val dummyEntries = listOf(215 DummyEntry(id = 1, name = "first"),216 DummyEntry(id = 1, name = "first"),217 DummyEntry(id = 2, name = "second"),218 )219 assertSoftly(dummyEntries) {220 forSome {221 it.id shouldBe 1222 it.name shouldBe "first"223 }224 }225 }226 }227 "forOne" should {228 "pass if one elements pass test" {229 list.forOne {230 it shouldBe 3231 }232 }233 "return itself" {234 list.forOne {235 it shouldBe 3236 }.forOne {237 it shouldBe 3238 }239 array.forOne {240 it shouldBe 3241 }.forOne {242 it shouldBe 3243 }244 }245 "fail if > 1 elements pass test" {246 shouldThrow<AssertionError> {247 list.forOne {248 it should beGreaterThan(2)249 }250 }.message shouldBe """3 elements passed but expected 1251The following elements passed:252325342545255The following elements failed:2561 => 1 should be > 22572 => 2 should be > 2"""258 }259 "fail if no elements pass test" {260 shouldThrow<AssertionError> {261 array.forOne {262 it shouldBe 22263 }264 }.message shouldBe """0 elements passed but expected 1265The following elements passed:266--none--267The following elements failed:2681 => expected:<22> but was:<1>2692 => expected:<22> but was:<2>2703 => expected:<22> but was:<3>2714 => expected:<22> but was:<4>2725 => expected:<22> but was:<5>"""273 }274 "work inside assertSoftly block" {275 val dummyEntries = listOf(276 DummyEntry(id = 1, name = "first"),277 DummyEntry(id = 2, name = "second"),278 )279 assertSoftly(dummyEntries) {280 forOne {281 it.id shouldBe 1282 it.name shouldBe "first"283 }284 }285 }286 }287 "forAny" should {288 "pass if one elements pass test" {289 list.forAny {290 it shouldBe 3291 }292 }293 "pass if at least elements pass test" {294 list.forAny {295 it should beGreaterThan(2)296 }297 }298 "return itself" {299 list.forAny {300 it shouldBe 3301 }.forAny {302 it shouldBe 3303 }304 array.forAny {305 it shouldBe 3306 }.forAny {307 it shouldBe 3308 }309 }310 "fail if no elements pass test" {311 shouldThrow<AssertionError> {312 array.forAny {313 it shouldBe 6314 }315 }.message shouldBe """0 elements passed but expected at least 1316The following elements passed:317--none--318The following elements failed:3191 => expected:<6> but was:<1>3202 => expected:<6> but was:<2>3213 => expected:<6> but was:<3>3224 => expected:<6> but was:<4>3235 => expected:<6> but was:<5>"""324 }325 "work inside assertSoftly block" {326 val dummyEntries = listOf(327 DummyEntry(id = 1, name = "first"),328 DummyEntry(id = 2, name = "second"),329 )330 assertSoftly(dummyEntries) {331 forAny {332 it.id shouldBe 1333 it.name shouldBe "first"334 }335 }336 }337 }338 "forExactly" should {339 "pass if exactly k elements pass" {340 list.forExactly(2) {341 it should beLessThan(3)342 }343 }344 "fail if more elements pass test" {345 shouldThrow<AssertionError> {346 list.forExactly(2) {347 it should beGreaterThan(2)348 }349 }.message shouldBe """3 elements passed but expected 2350The following elements passed:351335243535354The following elements failed:3551 => 1 should be > 23562 => 2 should be > 2"""357 }358 "fail if less elements pass test" {359 shouldThrow<AssertionError> {360 array.forExactly(2) {361 it should beLessThan(2)362 }363 }.message shouldBe """1 elements passed but expected 2364The following elements passed:3651366The following elements failed:3672 => 2 should be < 23683 => 3 should be < 23694 => 4 should be < 23705 => 5 should be < 2"""371 }372 "fail if no elements pass test" {373 shouldThrow<AssertionError> {374 array.forExactly(2) {375 it shouldBe 33376 }377 }.message shouldBe """0 elements passed but expected 2378The following elements passed:379--none--380The following elements failed:3811 => expected:<33> but was:<1>3822 => expected:<33> but was:<2>3833 => expected:<33> but was:<3>3844 => expected:<33> but was:<4>3855 => expected:<33> but was:<5>"""386 }387 }388 "forAtMostOne" should {389 "pass if one elements pass test" {390 list.forAtMostOne {391 it shouldBe 3392 }393 }394 "fail if 2 elements pass test" {395 shouldThrow<AssertionError> {396 array.forAtMostOne {397 it should beGreaterThan(3)398 }399 }.message shouldBe """2 elements passed but expected at most 1400The following elements passed:40144025403The following elements failed:4041 => 1 should be > 34052 => 2 should be > 34063 => 3 should be > 3"""407 }408 "work inside assertSoftly block" {409 val dummyEntries = listOf(410 DummyEntry(id = 1, name = "first"),411 DummyEntry(id = 2, name = "second"),412 )413 assertSoftly(dummyEntries) {414 forAtMostOne {415 it.id shouldBe 1416 it.name shouldBe "first"417 }418 }419 }420 }421 "forAtLeastOne" should {422 "pass if one elements pass test" {423 list.forAtLeastOne {424 it shouldBe 3425 }426 }427 "fail if no elements pass test" {428 shouldThrow<AssertionError> {429 array.forAtLeastOne {430 it shouldBe 22431 }432 }.message shouldBe """0 elements passed but expected at least 1433The following elements passed:434--none--435The following elements failed:4361 => expected:<22> but was:<1>4372 => expected:<22> but was:<2>4383 => expected:<22> but was:<3>4394 => expected:<22> but was:<4>4405 => expected:<22> but was:<5>"""441 }442 "work inside assertSoftly block" {443 val dummyEntries = listOf(444 DummyEntry(id = 1, name = "first"),445 DummyEntry(id = 2, name = "second"),446 )447 assertSoftly(dummyEntries) {448 forAtLeastOne {449 it.id shouldBe 1450 it.name shouldBe "first"451 }452 }453 }454 }455 "forSingle" should {456 "pass list is singular, and the single element pass" {457 listOf(1).forSingle {458 it shouldBeLessThan 3459 }460 }461 "return the single element on success" {462 listOf(1).forSingle { it shouldBeLessThan 3 } shouldBe 1463 }464 "fail if collection consists of multiple elements" {465 shouldFail {466 listOf(467 DummyEntry(id = 1, name = "first"),468 DummyEntry(id = 2, name = "second"),469 ).forSingle {470 it.id shouldBe 1471 }472 }.message shouldBe """473 Expected a single element in the collection, but found 2.474 The following elements passed:475 DummyEntry(id=1, name=first)476 The following elements failed:477 DummyEntry(id=2, name=second) => expected:<1> but was:<2>478 """.trimIndent()479 }480 "fail for empty collection" {481 shouldFail {482 arrayOf<Int>().forSingle {483 it shouldBe 3484 }485 }.message shouldBe """486 Expected a single element in the collection, but it was empty.487 """.trimIndent()488 }489 "fail if single element doesn't match" {490 shouldFail {491 arrayOf(2).forSingle {492 it shouldBe 3493 }494 }.message shouldBe """495 Expected a single element to pass, but it failed.496 The following elements passed:497 --none--498 The following elements failed:499 2 => expected:<3> but was:<2>500 """.trimIndent()501 }502 "work inside assertSoftly block" {503 val dummyEntries = listOf(504 DummyEntry(id = 1, name = "first"),505 )506 assertSoftly(dummyEntries) {507 forSingle {508 it.id shouldBe 1509 it.name shouldBe "first"510 }511 }512 }513 }514 }515}...

Full Screen

Full Screen

Inspectors.kt

Source:Inspectors.kt Github

copy

Full Screen

1package io.kotest.inspectors2import io.kotest.assertions.failure3inline fun <K, V, C : Map<K, V>> C.forAllValues(fn: (V) -> Unit): C = apply { values.forAll(fn) }4inline fun <K, V, C : Map<K, V>> C.forAllKeys(fn: (K) -> Unit): C = apply { keys.forAll(fn) }5inline fun <K, V, C : Map<K, V>> C.forAll(fn: (Map.Entry<K, V>) -> Unit): C = apply {6 val results = runTests(this, fn)7 val passed = results.filterIsInstance<ElementPass<Map.Entry<K, V>>>()8 if (passed.size < this.size) {9 val msg = "${passed.size} elements passed but expected ${this.size}"10 buildAssertionError(msg, results)11 }12}13inline fun CharSequence.forAll(fn: (Char) -> Unit): CharSequence = apply { toList().forAll(fn) }14inline fun <T> Sequence<T>.forAll(fn: (T) -> Unit): Sequence<T> = apply { toList().forAll(fn) }15inline fun <T> Array<T>.forAll(fn: (T) -> Unit): Array<T> = apply { asList().forAll(fn) }16inline fun <T, C : Collection<T>> C.forAll(fn: (T) -> Unit): C = apply {17 val results = runTests(this, fn)18 val passed = results.filterIsInstance<ElementPass<T>>()19 if (passed.size < this.size) {20 val msg = "${passed.size} elements passed but expected ${this.size}"21 buildAssertionError(msg, results)22 }23}24inline fun <K, V, C : Map<K, V>> C.forOneValue(fn: (V) -> Unit): C = apply { values.forExactly(1, fn) }25inline fun <K, V, C : Map<K, V>> C.forOneKey(fn: (K) -> Unit): C = apply { keys.forExactly(1, fn) }26inline fun <K, V, C : Map<K, V>> C.forOne(fn: (Map.Entry<K, V>) -> Unit): C = apply { forExactly(1, fn) }27inline fun CharSequence.forOne(fn: (Char) -> Unit): CharSequence = apply { toList().forOne(fn) }28inline fun <T> Sequence<T>.forOne(fn: (T) -> Unit): Sequence<T> = apply { toList().forOne(fn) }29inline fun <T> Array<T>.forOne(fn: (T) -> Unit): Array<T> = apply { asList().forOne(fn) }30inline fun <T, C : Collection<T>> C.forOne(fn: (T) -> Unit): C = forExactly(1, fn)31inline fun <K, V, C : Map<K, V>> C.forValuesExactly(k: Int, fn: (V) -> Unit): C = apply { values.forExactly(k, fn) }32inline fun <K, V, C : Map<K, V>> C.forKeysExactly(k: Int, fn: (K) -> Unit): C = apply { keys.forExactly(k, fn) }33inline fun <K, V, C : Map<K, V>> C.forExactly(k: Int, fn: (Map.Entry<K, V>) -> Unit): C = apply {34 val results = runTests(this, fn)35 val passed = results.filterIsInstance<ElementPass<Map.Entry<K, V>>>()36 if (passed.size != k) {37 val msg = "${passed.size} elements passed but expected $k"38 buildAssertionError(msg, results)39 }40}41inline fun CharSequence.forExactly(k: Int, fn: (Char) -> Unit): CharSequence = apply { toList().forExactly(k, fn) }42inline fun <T> Sequence<T>.forExactly(k: Int, fn: (T) -> Unit): Sequence<T> = apply { toList().forExactly(k, fn) }43inline fun <T> Array<T>.forExactly(k: Int, fn: (T) -> Unit): Array<T> = apply { toList().forExactly(k, fn) }44inline fun <T, C : Collection<T>> C.forExactly(k: Int, fn: (T) -> Unit): C = apply {45 val results = runTests(this, fn)46 val passed = results.filterIsInstance<ElementPass<T>>()47 if (passed.size != k) {48 val msg = "${passed.size} elements passed but expected $k"49 buildAssertionError(msg, results)50 }51}52inline fun <K, V, C : Map<K, V>> C.forSomeValues(fn: (V) -> Unit): C = apply { values.forSome(fn) }53inline fun <K, V, C : Map<K, V>> C.forSomeKeys(fn: (K) -> Unit): C = apply { keys.forSome(fn) }54inline fun <K, V, C : Map<K, V>> C.forSome(fn: (Map.Entry<K, V>) -> Unit): C = apply {55 val results = runTests(this, fn)56 val passed = results.filterIsInstance<ElementPass<Map.Entry<K, V>>>()57 if (passed.isEmpty()) {58 buildAssertionError("No elements passed but expected at least one", results)59 } else if (passed.size == size) {60 buildAssertionError("All elements passed but expected < $size", results)61 }62}63inline fun CharSequence.forSome(fn: (Char) -> Unit): CharSequence = apply { toList().forSome(fn) }64inline fun <T> Sequence<T>.forSome(fn: (T) -> Unit): Sequence<T> = apply { toList().forSome(fn) }65inline fun <T> Array<T>.forSome(fn: (T) -> Unit): Array<T> = apply { toList().forSome(fn) }66inline fun <T, C : Collection<T>> C.forSome(fn: (T) -> Unit): C = apply {67 val results = runTests(this, fn)68 val passed = results.filterIsInstance<ElementPass<T>>()69 if (passed.isEmpty()) {70 buildAssertionError("No elements passed but expected at least one", results)71 } else if (passed.size == size) {72 buildAssertionError("All elements passed but expected < $size", results)73 }74}75inline fun <K, V, C : Map<K, V>> C.forAnyValue(fn: (V) -> Unit): C = apply { values.forAny(fn) }76inline fun <K, V, C : Map<K, V>> C.forAnyKey(fn: (K) -> Unit): C = apply { keys.forAny(fn) }77inline fun <K, V, C : Map<K, V>> C.forAny(fn: (Map.Entry<K, V>) -> Unit): C = apply { forAtLeastOne(fn) }78inline fun <T> Sequence<T>.forAny(fn: (T) -> Unit): Sequence<T> = apply { toList().forAny(fn) }79inline fun CharSequence.forAny(fn: (Char) -> Unit): CharSequence = apply { toList().forAny(fn) }80inline fun <T> Array<T>.forAny(fn: (T) -> Unit): Array<T> = apply { toList().forAny(fn) }81inline fun <T, C : Collection<T>> C.forAny(fn: (T) -> Unit): C = apply { forAtLeastOne(fn) }82inline fun <K, V, C : Map<K, V>> C.forAtLeastOneValue(fn: (V) -> Unit): C = apply { values.forAtLeastOne(fn) }83inline fun <K, V, C : Map<K, V>> C.forAtLeastOneKey(fn: (K) -> Unit): C = apply { keys.forAtLeastOne(fn) }84inline fun <K, V, C : Map<K, V>> C.forAtLeastOne(fn: (Map.Entry<K, V>) -> Unit): C = apply { forAtLeast(1, fn) }85inline fun CharSequence.forAtLeastOne(fn: (Char) -> Unit): CharSequence = apply { toList().forAtLeast(1, fn) }86inline fun <T> Sequence<T>.forAtLeastOne(fn: (T) -> Unit): Sequence<T> = apply { toList().forAtLeastOne(fn) }87inline fun <T> Array<T>.forAtLeastOne(fn: (T) -> Unit): Array<T> = apply { toList().forAtLeastOne(fn) }88inline fun <T, C : Collection<T>> C.forAtLeastOne(f: (T) -> Unit) = forAtLeast(1, f)89inline fun <K, V, C : Map<K, V>> C.forValuesAtLeast(k: Int, fn: (V) -> Unit): C = apply { values.forAtLeast(k, fn) }90inline fun <K, V, C : Map<K, V>> C.forKeysAtLeast(k: Int, fn: (K) -> Unit): C = apply { keys.forAtLeast(k, fn) }91inline fun <K, V, C : Map<K, V>> C.forAtLeast(k: Int, fn: (Map.Entry<K, V>) -> Unit): C = apply {92 val results = runTests(this, fn)93 val passed = results.filterIsInstance<ElementPass<Map.Entry<K, V>>>()94 if (passed.size < k) {95 val msg = "${passed.size} elements passed but expected at least $k"96 buildAssertionError(msg, results)97 }98}99inline fun CharSequence.forAtLeast(k: Int, fn: (Char) -> Unit): CharSequence = apply { toList().forAtLeast(k, fn) }100inline fun <T> Sequence<T>.forAtLeast(k: Int, fn: (T) -> Unit): Sequence<T> = apply { toList().forAtLeast(k, fn) }101inline fun <T> Array<T>.forAtLeast(k: Int, fn: (T) -> Unit): Array<T> = apply { toList().forAtLeast(k, fn) }102inline fun <T, C : Collection<T>> C.forAtLeast(k: Int, fn: (T) -> Unit): C = apply {103 val results = runTests(this, fn)104 val passed = results.filterIsInstance<ElementPass<T>>()105 if (passed.size < k) {106 val msg = "${passed.size} elements passed but expected at least $k"107 buildAssertionError(msg, results)108 }109}110inline fun <K, V, C : Map<K, V>> C.forAtMostOneValue(fn: (V) -> Unit): C = apply { values.forAtMostOne(fn) }111inline fun <K, V, C : Map<K, V>> C.forAtMostOneKey(fn: (K) -> Unit): C = apply { keys.forAtMostOne(fn) }112inline fun <K, V, C : Map<K, V>> C.forAtMostOne(fn: (Map.Entry<K, V>) -> Unit): C = apply { forAtMost(1, fn) }113inline fun CharSequence.forAtMostOne(fn: (Char) -> Unit): CharSequence = apply { toList().forAtMost(1, fn) }114inline fun <T> Sequence<T>.forAtMostOne(fn: (T) -> Unit): Sequence<T> = apply { toList().forAtMostOne(fn) }115inline fun <T> Array<T>.forAtMostOne(fn: (T) -> Unit): Array<T> = apply { toList().forAtMostOne(fn) }116inline fun <T, C : Collection<T>> C.forAtMostOne(fn: (T) -> Unit) = forAtMost(1, fn)117inline fun <K, V, C : Map<K, V>> C.forValuesAtMost(k: Int, fn: (V) -> Unit): C = apply { values.forAtMost(k, fn) }118inline fun <K, V, C : Map<K, V>> C.forKeysAtMost(k: Int, fn: (K) -> Unit): C = apply { keys.forAtMost(k, fn) }119inline fun <K, V, C : Map<K, V>> C.forAtMost(k: Int, fn: (Map.Entry<K, V>) -> Unit): C = apply {120 val results = runTests(this, fn)121 val passed = results.filterIsInstance<ElementPass<Map.Entry<K, V>>>()122 if (passed.size > k) {123 val msg = "${passed.size} elements passed but expected at most $k"124 buildAssertionError(msg, results)125 }126}127inline fun CharSequence.forAtMost(k: Int, fn: (Char) -> Unit): CharSequence = apply { toList().forAtMost(k, fn) }128inline fun <T> Sequence<T>.forAtMost(k: Int, fn: (T) -> Unit): Sequence<T> = apply { toList().forAtMost(k, fn) }129inline fun <T> Array<T>.forAtMost(k: Int, fn: (T) -> Unit): Array<T> = apply { toList().forAtMost(k, fn) }130inline fun <T, C : Collection<T>> C.forAtMost(k: Int, fn: (T) -> Unit): C = apply {131 val results = runTests(this, fn)132 val passed = results.filterIsInstance<ElementPass<T>>()133 if (passed.size > k) {134 val msg = "${passed.size} elements passed but expected at most $k"135 buildAssertionError(msg, results)136 }137}138inline fun <K, V, C : Map<K, V>> C.forNoneValue(fn: (V) -> Unit): C = apply { values.forNone(fn) }139inline fun <K, V, C : Map<K, V>> C.forNoneKey(fn: (K) -> Unit): C = apply { keys.forNone(fn) }140inline fun <K, V, C : Map<K, V>> C.forNone(fn: (Map.Entry<K, V>) -> Unit): C = apply {141 val results = runTests(this, fn)142 val passed = results.filterIsInstance<ElementPass<Map.Entry<K, V>>>()143 if (passed.isNotEmpty()) {144 val msg = "${passed.size} elements passed but expected ${0}"145 buildAssertionError(msg, results)146 }147}148inline fun CharSequence.forNone(fn: (Char) -> Unit): CharSequence = apply { toList().forNone(fn) }149inline fun <T> Sequence<T>.forNone(fn: (T) -> Unit): Sequence<T> = apply { toList().forNone(fn) }150inline fun <T> Array<T>.forNone(fn: (T) -> Unit): Array<T> = apply { toList().forNone(fn) }151inline fun <T, C : Collection<T>> C.forNone(f: (T) -> Unit): C = apply {152 val results = runTests(this, f)153 val passed = results.filterIsInstance<ElementPass<T>>()154 if (passed.isNotEmpty()) {155 val msg = "${passed.size} elements passed but expected ${0}"156 buildAssertionError(msg, results)157 }158}159/**160 * Checks that [Sequence] consists of a single element, which passes the given assertion block [fn]161 * and returns the element162 * */163fun <T> Sequence<T>.forSingle(fn: (T) -> Unit): T = toList().forSingle(fn)164/**165 * Checks that [Array] consists of a single element, which passes the given assertion block [fn]166 * and returns the element167 * */168fun <T> Array<T>.forSingle(fn: (T) -> Unit): T = toList().forSingle(fn)169/**170 * Checks that [Collection] consists of a single element, which passes the given assertion block [fn]171 * and returns the element172 * */173fun <T, C : Collection<T>> C.forSingle(f: (T) -> Unit): T = run {174 val results = runTests(this, f)175 when (results.size) {176 1 -> when (results[0]) {177 is ElementPass<T> -> results[0].value()178 else -> buildAssertionError("Expected a single element to pass, but it failed.", results)179 }180 0 -> throw failure("Expected a single element in the collection, but it was empty.")181 else -> buildAssertionError("Expected a single element in the collection, but found ${results.size}.", results)182 }183}...

Full Screen

Full Screen

Array.forSingle

Using AI Code Generation

copy

Full Screen

1val array = arrayOf(1, 2, 3, 4)2array.forSingle { it % 2 == 0 } shouldBe 23val array = arrayOf(1, 2, 3, 4)4array.forAtLeastOne { it % 2 == 0 } shouldBe 25val array = arrayOf(1, 2, 3, 4)6array.forAtMostOne { it % 2 == 0 } shouldBe 27val array = arrayOf(1, 2, 3, 4)8array.forExactlyOne { it % 2 == 0 } shouldBe 29val array = arrayOf(1, 2, 3, 4)10array.forNone { it % 2 == 0 } shouldBe 211val array = arrayOf(1, 2, 3, 4)12array.forAny { it % 2 == 0 } shouldBe 213val array = arrayOf(1, 2, 3, 4)14array.forNone { it % 2 == 0 } shouldBe 215val array = arrayOf(1, 2, 3, 4)16array.forAtLeastOne { it % 2 == 0 } shouldBe 217val array = arrayOf(1, 2, 3, 4)18array.forAtMostOne { it % 2 == 0 } shouldBe 219val array = arrayOf(1, 2,

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