Best Kotest code snippet using io.kotest.inspectors.Inspectors.Array.forAll
CollectionInspectorsTest.kt
Source:CollectionInspectorsTest.kt
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}...
Inspectors.kt
Source:Inspectors.kt
1package com.github.shwaka.kotest.inspectors2import io.kotest.inspectors.ElementPass3import io.kotest.inspectors.runTests4fun <T> Sequence<T>.forAll(fn: (T) -> Unit) = toList().forAll(fn)5fun <T> Array<T>.forAll(fn: (T) -> Unit) = asList().forAll(fn)6fun <T> Collection<T>.forAll(fn: (T) -> Unit) {7 val results = runTests(this, fn)8 val passed = results.filterIsInstance<ElementPass<T>>()9 if (passed.size < this.size) {10 val msg = "${passed.size} elements passed but expected ${this.size}"11 buildAssertionError(msg, results)12 }13}14fun <T> Sequence<T>.forOne(fn: (T) -> Unit) = toList().forOne(fn)15fun <T> Array<T>.forOne(fn: (T) -> Unit) = asList().forOne(fn)16fun <T> Collection<T>.forOne(fn: (T) -> Unit) = forExactly(1, fn)17fun <T> Sequence<T>.forExactly(k: Int, fn: (T) -> Unit) = toList().forExactly(k, fn)18fun <T> Array<T>.forExactly(k: Int, fn: (T) -> Unit) = toList().forExactly(k, fn)19fun <T> Collection<T>.forExactly(k: Int, fn: (T) -> Unit) {20 val results = runTests(this, fn)21 val passed = results.filterIsInstance<ElementPass<T>>()22 if (passed.size != k) {23 val msg = "${passed.size} elements passed but expected $k"24 buildAssertionError(msg, results)25 }26}27fun <T> Sequence<T>.forSome(fn: (T) -> Unit) = toList().forSome(fn)28fun <T> Array<T>.forSome(fn: (T) -> Unit) = toList().forSome(fn)29fun <T> Collection<T>.forSome(fn: (T) -> Unit) {30 val results = runTests(this, fn)31 val passed = results.filterIsInstance<ElementPass<T>>()32 if (passed.isEmpty()) {33 buildAssertionError("No elements passed but expected at least one", results)34 } else if (passed.size == size) {35 buildAssertionError("All elements passed but expected < $size", results)36 }37}38fun <T> Sequence<T>.forAny(fn: (T) -> Unit) = toList().forAny(fn)39fun <T> Array<T>.forAny(fn: (T) -> Unit) = toList().forAny(fn)40fun <T> Collection<T>.forAny(fn: (T) -> Unit) = forAtLeastOne(fn)41fun <T> Sequence<T>.forAtLeastOne(fn: (T) -> Unit) = toList().forAtLeastOne(fn)42fun <T> Array<T>.forAtLeastOne(fn: (T) -> Unit) = toList().forAtLeastOne(fn)43fun <T> Collection<T>.forAtLeastOne(f: (T) -> Unit) = forAtLeast(1, f)44fun <T> Sequence<T>.forAtLeast(k: Int, fn: (T) -> Unit) = toList().forAtLeast(k, fn)45fun <T> Array<T>.forAtLeast(k: Int, fn: (T) -> Unit) = toList().forAtLeast(k, fn)46fun <T> Collection<T>.forAtLeast(k: Int, fn: (T) -> Unit) {47 val results = runTests(this, fn)48 val passed = results.filterIsInstance<ElementPass<T>>()49 if (passed.size < k) {50 val msg = "${passed.size} elements passed but expected at least $k"51 buildAssertionError(msg, results)52 }53}54fun <T> Sequence<T>.forAtMostOne(fn: (T) -> Unit) = toList().forAtMostOne(fn)55fun <T> Array<T>.forAtMostOne(fn: (T) -> Unit) = toList().forAtMostOne(fn)56fun <T> Collection<T>.forAtMostOne(fn: (T) -> Unit) = forAtMost(1, fn)57fun <T> Sequence<T>.forAtMost(k: Int, fn: (T) -> Unit) = toList().forAtMost(k, fn)58fun <T> Array<T>.forAtMost(k: Int, fn: (T) -> Unit) = toList().forAtMost(k, fn)59fun <T> Collection<T>.forAtMost(k: Int, fn: (T) -> Unit) {60 val results = runTests(this, fn)61 val passed = results.filterIsInstance<ElementPass<T>>()62 if (passed.size > k) {63 val msg = "${passed.size} elements passed but expected at most $k"64 buildAssertionError(msg, results)65 }66}67fun <T> Sequence<T>.forNone(fn: (T) -> Unit) = toList().forNone(fn)68fun <T> Array<T>.forNone(fn: (T) -> Unit) = toList().forNone(fn)69fun <T> Collection<T>.forNone(f: (T) -> Unit) {70 val results = runTests(this, f)71 val passed = results.filterIsInstance<ElementPass<T>>()72 if (passed.isNotEmpty()) {73 val msg = "${passed.size} elements passed but expected ${0}"74 buildAssertionError(msg, results)75 }76}...
synchronized list concurrent write and read - integration.kt
Source:synchronized list concurrent write and read - integration.kt
1package amber.collections2import amber.coroutines.joinAllJobs3import amber.sync.SyncMode4import amber.sync.Synchronized5import io.kotest.core.spec.style.AnnotationSpec6import io.kotest.data.forAll7import io.kotest.data.headers8import io.kotest.data.row9import io.kotest.data.table10import io.kotest.inspectors.forAll11import io.kotest.matchers.concurrent.shouldCompleteWithin12import io.kotest.matchers.shouldBe13import kotlinx.coroutines.GlobalScope14import kotlinx.coroutines.coroutineScope15import kotlinx.coroutines.runBlocking16import java.util.concurrent.TimeUnit17import amber.collections.SyncMutableList as SyncList18class `synchronized list concurrent write and read - integration` : AnnotationSpec() {19 private suspend fun testWith(it: Int, b: SyncMode) {20 val factor = it21 val list = SyncList<String>(synchronized = Synchronized(b))22 coroutineScope<Unit> {23 this.joinAllJobs {24 repeat(factor) {25 GlobalScope.launch {26 repeat(factor) {27 list.add("test")28 }29 }30 }31 }32 }33 list.size shouldBe factor * factor34 coroutineScope<Unit> {35 this.joinAllJobs {36 repeat(factor) {37 GlobalScope.launch {38 repeat(factor) {39 list.synchronized<Unit> {40 list.remove(list.last())41 }42 }43 }44 }45 }46 }47 list.size shouldBe 048 }49 @Test50 suspend fun `async add and remove SyncMode_UNSAFE_EXECUTOR`() {51 shouldCompleteWithin(10, TimeUnit.SECONDS) {52 table(53 headers("factor", "syncMode"), row(arrayOf(10, 20, 30), SyncMode.UNSAFE_EXECUTOR)54 ).forAll { a, b ->55 a.forAll {56 runBlocking { testWith(it, b) }57 }58 }59 }60 }61 @Test62 suspend fun `async add and remove SyncMode_SAFE_EXECUTOR`() {63 shouldCompleteWithin(10, TimeUnit.SECONDS) {64 table(65 headers("factor", "syncMode"), row(arrayOf(10, 20, 30), SyncMode.SAFE_EXECUTOR)66 ).forAll { a, b ->67 a.forAll {68 runBlocking { testWith(it, b) }69 }70 }71 }72 }73 @Test74 suspend fun `async add and remove SyncMode_HEAVY_EXECUTOR`() {75 shouldCompleteWithin(10, TimeUnit.SECONDS) {76 table(77 headers("factor", "syncMode"), row(arrayOf(10, 20, 30), SyncMode.HEAVY_EXECUTOR)78 ).forAll { a, b ->79 a.forAll {80 runBlocking { testWith(it, b) }81 }82 }83 }84 }85 @Test86 suspend fun `async add and remove SyncMode_KOTLIN`() {87 shouldCompleteWithin(10, TimeUnit.SECONDS) {88 table(89 headers("factor", "syncMode"), row(arrayOf(10, 20, 30, 100), SyncMode.KOTLIN)90 ).forAll { a, b ->91 a.forAll {92 runBlocking { testWith(it, b) }93 }94 }95 }96 }97}...
sizes.kt
Source:sizes.kt
1package com.sksamuel.kotest.inspectors2import io.kotest.assertions.shouldFail3import io.kotest.assertions.throwables.shouldThrowAny4import io.kotest.core.spec.style.FunSpec5import io.kotest.extensions.system.withSystemProperty6import io.kotest.inspectors.forAll7import io.kotest.matchers.ints.shouldBeLessThanOrEqual8import io.kotest.matchers.shouldBe9class InspectorSizeTests : FunSpec({10 test("should error with large failure count #938") {11 shouldFail {12 List(10_000) { it }.forAll {13 it shouldBe -114 }15 }16 }17 test("passed results are truncated when passed list length is over 10") {18 shouldThrowAny {19 (1..13).toList().forAll {20 it.shouldBeLessThanOrEqual(12)21 }22 }.message shouldBe "12 elements passed but expected 13\n" +23 "\n" +24 "The following elements passed:\n" +25 "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" +26 "... and 2 more passed elements\n" +27 "\n" +28 "The following elements failed:\n" +29 "13 => 13 should be <= 12"30 }31 test("failed results are truncated when failed array size is over 10") {32 shouldThrowAny {33 arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12).forAll {34 if (System.currentTimeMillis() > 0) throw NullPointerException()35 }36 }.message shouldBe "0 elements passed but expected 12\n" +37 "\n" +38 "The following elements passed:\n" +39 "--none--\n" +40 "\n" +41 "The following elements failed:\n" +42 "1 => java.lang.NullPointerException\n" +43 "2 => java.lang.NullPointerException\n" +44 "3 => java.lang.NullPointerException\n" +45 "4 => java.lang.NullPointerException\n" +46 "5 => java.lang.NullPointerException\n" +47 "6 => java.lang.NullPointerException\n" +48 "7 => java.lang.NullPointerException\n" +49 "8 => java.lang.NullPointerException\n" +50 "9 => java.lang.NullPointerException\n" +51 "10 => java.lang.NullPointerException\n" +52 "... and 2 more failed elements"53 }54 test("max results is controllable by sys prop") {55 withSystemProperty("kotest.assertions.output.max", "3") {56 shouldThrowAny {57 arrayOf(1, 2, 3, 4, 5).forAll {58 if (System.currentTimeMillis() > 0) throw NullPointerException()59 }60 }.message shouldBe "0 elements passed but expected 5\n" +61 "\n" +62 "The following elements passed:\n" +63 "--none--\n" +64 "\n" +65 "The following elements failed:\n" +66 "1 => java.lang.NullPointerException\n" +67 "2 => java.lang.NullPointerException\n" +68 "3 => java.lang.NullPointerException\n" +69 "... and 2 more failed elements"70 }71 }72})...
AppKtTest.kt
Source:AppKtTest.kt
1package eu.kowalcze.michal.kotlin.cron.api.cmdline2import io.kotest.core.spec.style.StringSpec3import io.kotest.inspectors.forAll4import io.kotest.matchers.shouldBe5import java.io.ByteArrayOutputStream6import java.io.PrintStream7class AppKtTest : StringSpec({8 "should provide proper summary"{9 listOf(10 "1 2 3 4 5 a command is not as important" to """minute 111hour 212day of month 313month 414day of week 515command a command is not as important16""",17 ).forAll { (line, expectedOutput) ->18 // given19 val out = ByteArrayOutputStream()20 val app = App(PrintStream(out))21 // when22 app.printSummary(line)23 // then24 String(out.toByteArray()) shouldBe expectedOutput25 }26 }27 "should display error information"{28 listOf(29 "1 2 3 4 X command" to "ERROR: Provided value: 'X' at index:5 is not recognizable by any known parser\n",30 ).forAll { (line, expectedOutput) ->31 // given32 val out = ByteArrayOutputStream()33 val app = App(PrintStream(out))34 // when35 app.printSummary(line)36 // then37 String(out.toByteArray()) shouldBe expectedOutput38 }39 }40})...
NumbersTestWithInspectors.kt
Source:NumbersTestWithInspectors.kt
1import io.kotest.core.spec.style.StringSpec2import io.kotest.inspectors.*3import io.kotest.matchers.ints.shouldBeGreaterThanOrEqual4import io.kotest.matchers.shouldBe5class NumbersTestWithInspectors : StringSpec({6 val numbers = Array(10) { it + 1 }7 "all are non-negative" {8 numbers.forAll { it shouldBeGreaterThanOrEqual 0 }9 }10 "none is zero" { numbers.forNone { it shouldBe 0 } }11 "a single 10" { numbers.forOne { it shouldBe 10 } }12 "at most one 0" { numbers.forAtMostOne { it shouldBe 0 } }13 "at least one odd number" {14 numbers.forAtLeastOne { it % 2 shouldBe 1 }15 }16 "at most five odd numbers" {17 numbers.forAtMost(5) { it % 2 shouldBe 1 }18 }19 "at least three even numbers" {20 numbers.forAtLeast(3) { it % 2 shouldBe 0 }21 }22 "some numbers are odd" { numbers.forAny { it % 2 shouldBe 1 } }23 "some but not all numbers are even" {24 numbers.forSome { it % 2 shouldBe 0 }25 }26 "exactly five numbers are even" {27 numbers.forExactly(5) { it % 2 shouldBe 0 }28 }29})...
R0ACTest.kt
Source:R0ACTest.kt
1package at.jku.isse.clones.harness2import at.jku.isse.clones.harness.classes.R0AC3import io.kotest.core.spec.style.StringSpec4import io.kotest.inspectors.forAll5import io.kotest.matchers.collections.shouldHaveSize6import io.kotest.matchers.shouldBe7class R0ACTest : StringSpec({8 "should run through"{9 R0AC.run()10 }11 "equal results"{12 val result = R0AC.targets(R0AC.allDevs).map { targets ->13 val results = R0AC.requests.map {14 targets.invoke(null, it.l, it.r)15 }16 Pair(targets, results)17 }18 val ref = result[0].second.flatMap{ (it as IntArray).toList()}19 result.forAll {20 it.second shouldHaveSize 10021 it.second.flatMap { (it as IntArray).toList() } shouldBe ref22 }23 }24})...
NumberTestWithForAll.kt
Source:NumberTestWithForAll.kt
1import io.kotest.assertions.assertSoftly2import io.kotest.core.spec.style.StringSpec3import io.kotest.inspectors.forAll4import io.kotest.matchers.ints.shouldBeGreaterThan5import io.kotest.matchers.ints.shouldBeLessThan6class NumberTestWithForAll : StringSpec({7 val numbers = Array(10) { it + 1 }8 "invalid numbers" {9 assertSoftly {10 numbers.forAll { it shouldBeLessThan 5 }11 numbers.forAll { it shouldBeGreaterThan 3 }12 }13 }14})...
Array.forAll
Using AI Code Generation
1val list = listOf(1, 2, 3, 4, 5)2list.forAll { it should beLessThan(6) }3val array = arrayOf(1, 2, 3, 4, 5)4array.forAll { it should beLessThan(6) }5val iterable = listOf(1, 2, 3, 4, 5).asIterable()6iterable.forAll { it should beLessThan(6) }7val sequence = listOf(1, 2, 3, 4, 5).asSequence()8sequence.forAll { it should beLessThan(6) }9val array = arrayOf(1, 2, 3, 4, 5)10array.forNone { it should beLessThan(6) }11val iterable = listOf(1, 2, 3, 4, 5).asIterable()12iterable.forNone { it should beLessThan(6) }13val array = arrayOf(1, 2, 3, 4, 5)14array.forOne { it should beLessThan(6) }15val iterable = listOf(1, 2, 3, 4, 5).asIterable()16iterable.forOne { it should beLessThan(6) }17val array = arrayOf(1, 2, 3, 4, 5)18array.forOne { it should beLessThan(6) }19val iterable = listOf(1, 2, 3, 4
Array.forAll
Using AI Code Generation
1val names = listOf("John", "Jane", "Mary")2forAll(names) { name ->3name should startWith("J")4}5val names = listOf("John", "Jane", "Mary")6forAll(names) { name ->7name should startWith("J")8}9val names = listOf("John", "Jane", "Mary")10forAll(names) { name ->11name should startWith("J")12}13val names = listOf("John", "Jane", "Mary")14forAll(names) { name ->15name should startWith("J")16}17val names = listOf("John", "Jane", "Mary")18forAll(names) { name ->19name should startWith("J")20}21val names = listOf("John", "Jane", "Mary")22forAll(names) { name ->23name should startWith("J")24}25val names = listOf("John", "Jane", "Mary")26forAll(names) { name ->27name should startWith("J")28}29val names = listOf("John", "Jane", "Mary")30forAll(names) { name ->31name should startWith("J")32}33val names = listOf("John", "Jane", "Mary")34forAll(names) { name ->35name should startWith("J")36}37val names = listOf("John", "Jane", "Mary")38forAll(names) { name ->39name should startWith("J")40}41val names = listOf("John", "
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!!