Best Kotest code snippet using io.kotest.inspectors.Inspectors.Sequence.forAny
matchers.kt
Source:matchers.kt
1@file:Suppress("unused")2package io.kotest.matchers.sequences3import io.kotest.assertions.eq.eq4import io.kotest.assertions.print.print5import io.kotest.matchers.*6private fun <T> Sequence<T>.toString(limit: Int = 10) = this.joinToString(", ", limit = limit)7/*8How should infinite sequences be detected, and how should they be dealt with?9Sequence<T>.count() may run through the whole sequence (sequences from `generateSequence()` do so), so isn't always a safe way to detect infinite sequences.10For now, the documentation should mention that infinite sequences will cause these matchers never to halt.11*/12fun <T> Sequence<T>.shouldContainOnlyNulls() = this should containOnlyNulls()13fun <T> Sequence<T>.shouldNotContainOnlyNulls() = this shouldNot containOnlyNulls()14fun <T> containOnlyNulls() = object : Matcher<Sequence<T>> {15 override fun test(value: Sequence<T>) =16 MatcherResult(17 value.all { it == null },18 { "Sequence should contain only nulls" },19 {20 "Sequence should not contain only nulls"21 })22}23fun <T> Sequence<T>.shouldContainNull() = this should containNull()24fun <T> Sequence<T>.shouldNotContainNull() = this shouldNot containNull()25fun <T> containNull() = object : Matcher<Sequence<T>> {26 override fun test(value: Sequence<T>) =27 MatcherResult(28 value.any { it == null },29 { "Sequence should contain at least one null" },30 {31 "Sequence should not contain any nulls"32 })33}34fun <T> Sequence<T>.shouldHaveElementAt(index: Int, element: T) = this should haveElementAt(index, element)35fun <T> Sequence<T>.shouldNotHaveElementAt(index: Int, element: T) = this shouldNot haveElementAt(index, element)36fun <T, S : Sequence<T>> haveElementAt(index: Int, element: T) = object : Matcher<S> {37 override fun test(value: S) =38 MatcherResult(39 value.elementAt(index) == element,40 { "Sequence should contain $element at index $index" },41 { "Sequence should not contain $element at index $index" }42 )43}44fun <T> Sequence<T>.shouldContainNoNulls() = this should containNoNulls()45fun <T> Sequence<T>.shouldNotContainNoNulls() = this shouldNot containNoNulls()46fun <T> containNoNulls() = object : Matcher<Sequence<T>> {47 override fun test(value: Sequence<T>) =48 MatcherResult(49 value.all { it != null },50 { "Sequence should not contain nulls" },51 { "Sequence should have at least one null" }52 )53}54infix fun <T, C : Sequence<T>> C.shouldContain(t: T) = this should contain(t)55infix fun <T, C : Sequence<T>> C.shouldNotContain(t: T) = this shouldNot contain(t)56fun <T, C : Sequence<T>> contain(t: T) = object : Matcher<C> {57 override fun test(value: C) = MatcherResult(58 value.contains(t),59 { "Sequence should contain element $t" },60 { "Sequence should not contain element $t" }61 )62}63infix fun <T, C : Sequence<T>> C?.shouldNotContainExactly(expected: C) = this shouldNot containExactly(expected)64fun <T, C : Sequence<T>> C?.shouldNotContainExactly(vararg expected: T) = this shouldNot containExactly(*expected)65infix fun <T, C : Sequence<T>> C?.shouldContainExactly(expected: C) = this should containExactly(expected)66fun <T, C : Sequence<T>> C?.shouldContainExactly(vararg expected: T) = this should containExactly(*expected)67fun <T> containExactly(vararg expected: T): Matcher<Sequence<T>?> = containExactly(expected.asSequence())68/** Assert that a sequence contains exactly the given values and nothing else, in order. */69fun <T, C : Sequence<T>> containExactly(expected: C): Matcher<C?> = neverNullMatcher { value ->70 val actualIterator = value.withIndex().iterator()71 val expectedIterator = expected.withIndex().iterator()72 var passed = true73 var failMessage = "Sequence should contain exactly $expected but was $value."74 while (passed && actualIterator.hasNext() && expectedIterator.hasNext()) {75 val actualElement = actualIterator.next()76 val expectedElement = expectedIterator.next()77 if (eq(actualElement.value, expectedElement.value) != null) {78 failMessage += " (expected ${expectedElement.value.print().value} at ${expectedElement.index} but found ${actualElement.value.print().value})"79 passed = false80 }81 }82 if (passed && actualIterator.hasNext()) {83 failMessage += "\nActual sequence has more element than Expected sequence"84 passed = false85 }86 if (passed && expectedIterator.hasNext()) {87 failMessage += "\nExpected sequence has more element than Actual sequence"88 passed = false89 }90 MatcherResult(91 passed,92 { failMessage },93 {94 "Sequence should not be exactly $expected"95 })96}97@Deprecated("use shouldNotContainAllInAnyOrder", ReplaceWith("shouldNotContainAllInAnyOrder"))98infix fun <T, C : Sequence<T>> C?.shouldNotContainExactlyInAnyOrder(expected: C) =99 this shouldNot containAllInAnyOrder(expected)100@Deprecated("use shouldNotContainAllInAnyOrder", ReplaceWith("shouldNotContainAllInAnyOrder"))101fun <T, C : Sequence<T>> C?.shouldNotContainExactlyInAnyOrder(vararg expected: T) =102 this shouldNot containAllInAnyOrder(*expected)103@Deprecated("use shouldContainAllInAnyOrder", ReplaceWith("shouldContainAllInAnyOrder"))104infix fun <T, C : Sequence<T>> C?.shouldContainExactlyInAnyOrder(expected: C) =105 this should containAllInAnyOrder(expected)106@Deprecated("use shouldContainAllInAnyOrder", ReplaceWith("shouldContainAllInAnyOrder"))107fun <T, C : Sequence<T>> C?.shouldContainExactlyInAnyOrder(vararg expected: T) =108 this should containAllInAnyOrder(*expected)109@Deprecated("use containAllInAnyOrder", ReplaceWith("containAllInAnyOrder"))110fun <T> containExactlyInAnyOrder(vararg expected: T): Matcher<Sequence<T>?> =111 containAllInAnyOrder(expected.asSequence())112@Deprecated("use containAllInAnyOrder", ReplaceWith("containAllInAnyOrder"))113 /** Assert that a sequence contains the given values and nothing else, in any order. */114fun <T, C : Sequence<T>> containExactlyInAnyOrder(expected: C): Matcher<C?> = containAllInAnyOrder(expected)115infix fun <T, C : Sequence<T>> C?.shouldNotContainAllInAnyOrder(expected: C) =116 this shouldNot containAllInAnyOrder(expected)117fun <T, C : Sequence<T>> C?.shouldNotContainAllInAnyOrder(vararg expected: T) =118 this shouldNot containAllInAnyOrder(*expected)119infix fun <T, C : Sequence<T>> C?.shouldContainAllInAnyOrder(expected: C) =120 this should containAllInAnyOrder(expected)121fun <T, C : Sequence<T>> C?.shouldContainAllInAnyOrder(vararg expected: T) =122 this should containAllInAnyOrder(*expected)123fun <T> containAllInAnyOrder(vararg expected: T): Matcher<Sequence<T>?> =124 containAllInAnyOrder(expected.asSequence())125/** Assert that a sequence contains all the given values and nothing else, in any order. */126fun <T, C : Sequence<T>> containAllInAnyOrder(expected: C): Matcher<C?> = neverNullMatcher { value ->127 val passed = value.count() == expected.count() && expected.all { value.contains(it) }128 MatcherResult(129 passed,130 { "Sequence should contain the values of $expected in any order, but was $value" },131 { "Sequence should not contain the values of $expected in any order" }132 )133}134infix fun <T : Comparable<T>, C : Sequence<T>> C.shouldHaveUpperBound(t: T) = this should haveUpperBound(t)135fun <T : Comparable<T>, C : Sequence<T>> haveUpperBound(t: T) = object : Matcher<C> {136 override fun test(value: C) = MatcherResult(137 (value.maxOrNull() ?: t) <= t,138 { "Sequence should have upper bound $t" },139 { "Sequence should not have upper bound $t" }140 )141}142infix fun <T : Comparable<T>, C : Sequence<T>> C.shouldHaveLowerBound(t: T) = this should haveLowerBound(t)143fun <T : Comparable<T>, C : Sequence<T>> haveLowerBound(t: T) = object : Matcher<C> {144 override fun test(value: C) = MatcherResult(145 (value.minOrNull() ?: t) >= t,146 { "Sequence should have lower bound $t" },147 { "Sequence should not have lower bound $t" }148 )149}150fun <T> Sequence<T>.shouldBeUnique() = this should beUnique()151fun <T> Sequence<T>.shouldNotBeUnique() = this shouldNot beUnique()152fun <T> beUnique() = object : Matcher<Sequence<T>> {153 override fun test(value: Sequence<T>) = MatcherResult(154 value.toSet().size == value.count(),155 { "Sequence should be Unique" },156 { "Sequence should contain at least one duplicate element" }157 )158}159fun <T> Sequence<T>.shouldContainDuplicates() = this should containDuplicates()160fun <T> Sequence<T>.shouldNotContainDuplicates() = this shouldNot containDuplicates()161fun <T> containDuplicates() = object : Matcher<Sequence<T>> {162 override fun test(value: Sequence<T>) = MatcherResult(163 value.toSet().size < value.count(),164 { "Sequence should contain duplicates" },165 { "Sequence should not contain duplicates" }166 )167}168fun <T : Comparable<T>> Sequence<T>.shouldBeSorted() = this should beSorted()169fun <T : Comparable<T>> Sequence<T>.shouldNotBeSorted() = this shouldNot beSorted()170fun <T : Comparable<T>> beSorted(): Matcher<Sequence<T>> = sorted()171fun <T : Comparable<T>> sorted(): Matcher<Sequence<T>> = object : Matcher<Sequence<T>> {172 override fun test(value: Sequence<T>): MatcherResult {173 @Suppress("UNUSED_DESTRUCTURED_PARAMETER_ENTRY")174 val failure = value.zipWithNext().withIndex().firstOrNull { (i, it) -> it.first > it.second }175 val snippet = value.joinToString(",", limit = 10)176 val elementMessage = when (failure) {177 null -> ""178 else -> ". Element ${failure.value.first} at index ${failure.index} was greater than element ${failure.value.second}"179 }180 return MatcherResult(181 failure == null,182 { "Sequence $snippet should be sorted$elementMessage" },183 { "Sequence $snippet should not be sorted" }184 )185 }186}187infix fun <T> Sequence<T>.shouldBeSortedWith(comparator: Comparator<in T>) = this should beSortedWith(comparator)188infix fun <T> Sequence<T>.shouldBeSortedWith(cmp: (T, T) -> Int) = this should beSortedWith(cmp)189fun <T> beSortedWith(comparator: Comparator<in T>): Matcher<Sequence<T>> = sortedWith(comparator)190fun <T> beSortedWith(cmp: (T, T) -> Int): Matcher<Sequence<T>> = sortedWith(cmp)191fun <T> sortedWith(comparator: Comparator<in T>): Matcher<Sequence<T>> = sortedWith { a, b ->192 comparator.compare(a, b)193}194fun <T> sortedWith(cmp: (T, T) -> Int): Matcher<Sequence<T>> = object : Matcher<Sequence<T>> {195 override fun test(value: Sequence<T>): MatcherResult {196 @Suppress("UNUSED_DESTRUCTURED_PARAMETER_ENTRY")197 val failure = value.zipWithNext().withIndex().firstOrNull { (i, it) -> cmp(it.first, it.second) > 0 }198 val snippet = value.joinToString(",", limit = 10)199 val elementMessage = when (failure) {200 null -> ""201 else -> ". Element ${failure.value.first} at index ${failure.index} shouldn't precede element ${failure.value.second}"202 }203 return MatcherResult(204 failure == null,205 { "Sequence $snippet should be sorted$elementMessage" },206 { "Sequence $snippet should not be sorted" }207 )208 }209}210infix fun <T> Sequence<T>.shouldNotBeSortedWith(comparator: Comparator<in T>) = this shouldNot beSortedWith(comparator)211infix fun <T> Sequence<T>.shouldNotBeSortedWith(cmp: (T, T) -> Int) = this shouldNot beSortedWith(cmp)212infix fun <T> Sequence<T>.shouldHaveSingleElement(t: T) = this should singleElement(t)213infix fun <T> Sequence<T>.shouldNotHaveSingleElement(t: T) = this shouldNot singleElement(t)214fun <T> singleElement(t: T) = object : Matcher<Sequence<T>> {215 override fun test(value: Sequence<T>) = MatcherResult(216 value.count() == 1 && value.first() == t,217 { "Sequence should be a single element of $t but has ${value.count()} elements" },218 { "Sequence should not be a single element of $t" }219 )220}221infix fun <T> Sequence<T>.shouldHaveCount(count: Int) = this should haveCount(count)222infix fun <T> Sequence<T>.shouldNotHaveCount(count: Int) = this shouldNot haveCount(223 count224)225infix fun <T> Sequence<T>.shouldHaveSize(size: Int) = this should haveCount(size)226infix fun <T> Sequence<T>.shouldNotHaveSize(size: Int) = this shouldNot haveCount(size)227//fun <T> haveSize(size: Int) = haveCount(size)228fun <T> haveSize(size: Int): Matcher<Sequence<T>> = haveCount(size)229fun <T> haveCount(count: Int): Matcher<Sequence<T>> = object : Matcher<Sequence<T>> {230 override fun test(value: Sequence<T>) =231 MatcherResult(232 value.count() == count,233 { "Sequence should have count $count but has count ${value.count()}" },234 { "Sequence should not have count $count" }235 )236}237infix fun <T, U> Sequence<T>.shouldBeLargerThan(other: Sequence<U>) = this should beLargerThan(other)238fun <T, U> beLargerThan(other: Sequence<U>) = object : Matcher<Sequence<T>> {239 override fun test(value: Sequence<T>) = MatcherResult(240 value.count() > other.count(),241 { "Sequence of count ${value.count()} should be larger than sequence of count ${other.count()}" },242 { "Sequence of count ${value.count()} should not be larger than sequence of count ${other.count()}" }243 )244}245infix fun <T, U> Sequence<T>.shouldBeSmallerThan(other: Sequence<U>) = this should beSmallerThan(other)246fun <T, U> beSmallerThan(other: Sequence<U>) = object : Matcher<Sequence<T>> {247 override fun test(value: Sequence<T>) = MatcherResult(248 value.count() < other.count(),249 { "Sequence of count ${value.count()} should be smaller than sequence of count ${other.count()}" },250 { "Sequence of count ${value.count()} should not be smaller than sequence of count ${other.count()}" }251 )252}253infix fun <T, U> Sequence<T>.shouldBeSameCountAs(other: Sequence<U>) = this should beSameCountAs(254 other255)256fun <T, U> beSameCountAs(other: Sequence<U>) = object : Matcher<Sequence<T>> {257 override fun test(value: Sequence<T>) = MatcherResult(258 value.count() == other.count(),259 { "Sequence of count ${value.count()} should be the same count as sequence of count ${other.count()}" },260 { "Sequence of count ${value.count()} should not be the same count as sequence of count ${other.count()}" }261 )262}263infix fun <T, U> Sequence<T>.shouldBeSameSizeAs(other: Sequence<U>) = this.shouldBeSameCountAs(other)264infix fun <T> Sequence<T>.shouldHaveAtLeastCount(n: Int) = this shouldHave atLeastCount(n)265fun <T> atLeastCount(n: Int) = object : Matcher<Sequence<T>> {266 override fun test(value: Sequence<T>) = MatcherResult(267 value.count() >= n,268 { "Sequence should contain at least $n elements" },269 { "Sequence should contain less than $n elements" }270 )271}272infix fun <T> Sequence<T>.shouldHaveAtLeastSize(n: Int) = this.shouldHaveAtLeastCount(n)273infix fun <T> Sequence<T>.shouldHaveAtMostCount(n: Int) = this shouldHave atMostCount(n)274fun <T> atMostCount(n: Int) = object : Matcher<Sequence<T>> {275 override fun test(value: Sequence<T>) = MatcherResult(276 value.count() <= n,277 { "Sequence should contain at most $n elements" },278 { "Sequence should contain more than $n elements" }279 )280}281infix fun <T> Sequence<T>.shouldHaveAtMostSize(n: Int) = this shouldHave atMostCount(n)282@Deprecated(283 "Use `forAny` inspection instead",284 ReplaceWith(285 "forAny { p() shouldBe true }",286 "io.kotest.matchers.shouldBe",287 "io.kotest.inspectors.forAny",288 )289)290infix fun <T> Sequence<T>.shouldExist(p: (T) -> Boolean) = this should exist(p)291fun <T> exist(p: (T) -> Boolean) = object : Matcher<Sequence<T>> {292 override fun test(value: Sequence<T>) = MatcherResult(293 value.any { p(it) },294 { "Sequence should contain an element that matches the predicate $p" },295 { "Sequence should not contain an element that matches the predicate $p" }296 )297}298fun <T : Comparable<T>> Sequence<T>.shouldContainInOrder(vararg ts: T) =299 this should containsInOrder(ts.asSequence())300infix fun <T : Comparable<T>> Sequence<T>.shouldContainInOrder(expected: Sequence<T>) =301 this should containsInOrder(expected)302fun <T : Comparable<T>> Sequence<T>.shouldNotContainInOrder(expected: Sequence<T>) =303 this shouldNot containsInOrder(expected)304/** Assert that a sequence contains a given subsequence, possibly with values in between. */305fun <T> containsInOrder(subsequence: Sequence<T>): Matcher<Sequence<T>?> = neverNullMatcher { actual ->306 val subsequenceCount = subsequence.count()307 require(subsequenceCount > 0) { "expected values must not be empty" }308 var subsequenceIndex = 0309 val actualIterator = actual.iterator()310 while (actualIterator.hasNext() && subsequenceIndex < subsequenceCount) {311 if (actualIterator.next() == subsequence.elementAt(subsequenceIndex)) subsequenceIndex += 1312 }313 MatcherResult(314 subsequenceIndex == subsequence.count(),315 { "[$actual] did not contain the elements [$subsequence] in order" },316 { "[$actual] should not contain the elements [$subsequence] in order" }317 )318}319fun <T> Sequence<T>.shouldBeEmpty() = this should beEmpty()320fun <T> Sequence<T>.shouldNotBeEmpty() = this shouldNot beEmpty()321fun <T> beEmpty(): Matcher<Sequence<T>> = object : Matcher<Sequence<T>> {322 override fun test(value: Sequence<T>): MatcherResult = MatcherResult(323 value.count() == 0,324 { "Sequence should be empty" },325 { "Sequence should not be empty" }326 )327}328fun <T> Sequence<T>.shouldContainAll(vararg ts: T) = this should containAll(ts.toList())329infix fun <T> Sequence<T>.shouldContainAll(ts: Collection<T>) = this should containAll(ts.toList())330infix fun <T> Sequence<T>.shouldContainAll(ts: Sequence<T>) = this should containAll(ts)331fun <T> Sequence<T>.shouldNotContainAll(vararg ts: T) = this shouldNot containAll(ts.asList())332infix fun <T> Sequence<T>.shouldNotContainAll(ts: Collection<T>) = this shouldNot containAll(ts.toList())333infix fun <T> Sequence<T>.shouldNotContainAll(ts: Sequence<T>) = this shouldNot containAll(ts)334fun <T> containAll(ts: Sequence<T>): Matcher<Sequence<T>> = containAll(ts.toList())335fun <T> containAll(ts: List<T>): Matcher<Sequence<T>> = object : Matcher<Sequence<T>> {336 override fun test(value: Sequence<T>): MatcherResult {337 val remaining = ts.toMutableSet()338 val iter = value.iterator()339 while (remaining.isNotEmpty() && iter.hasNext()) {340 remaining.remove(iter.next())341 }342 return MatcherResult(343 remaining.isEmpty(),344 { "Sequence should contain all of ${value.joinToString(",", limit = 10)}" },345 { "Sequence should not contain all of ${value.joinToString(",", limit = 10)}" }346 )347 }348}...
InspectorAliasTest.kt
Source:InspectorAliasTest.kt
1package com.sksamuel.kotest2import io.kotest.assertions.throwables.shouldThrowAny3import io.kotest.core.spec.style.FunSpec4import io.kotest.inspectors.shouldForAll5import io.kotest.inspectors.shouldForAny6import io.kotest.inspectors.shouldForAtLeast7import io.kotest.inspectors.shouldForAtLeastOne8import io.kotest.inspectors.shouldForAtMost9import io.kotest.inspectors.shouldForAtMostOne10import io.kotest.inspectors.shouldForExactly11import io.kotest.inspectors.shouldForNone12import io.kotest.inspectors.shouldForOne13import io.kotest.inspectors.shouldForSome14import io.kotest.matchers.comparables.shouldBeGreaterThan15import io.kotest.matchers.ints.shouldBeLessThan16import io.kotest.matchers.shouldBe17class InspectorAliasTest : FunSpec({18 val array = arrayOf(1, 2, 3)19 val list = listOf(1, 2, 3)20 val sequence = sequenceOf(1, 2, 3)21 context("forAll") {22 fun block(x: Int) = x shouldBeGreaterThan 023 test("array") {24 array.shouldForAll {25 it shouldBeLessThan 426 }27 shouldThrowAny {28 array.shouldForAll {29 it shouldBeLessThan 330 }31 }32 }33 test("list") {34 list.shouldForAll(::block)35 shouldThrowAny {36 list.shouldForAll {37 it shouldBeLessThan 338 }39 }40 }41 test("sequence") {42 sequence.shouldForAll(::block)43 shouldThrowAny {44 sequence.shouldForAll {45 it shouldBeLessThan 346 }47 }48 }49 }50 context("forOne") {51 fun block(x: Int) = x shouldBe 252 test("array") {53 array.shouldForOne(::block)54 shouldThrowAny {55 array.shouldForOne {56 it shouldBeLessThan 157 }58 }59 }60 test("list") {61 list.shouldForOne(::block)62 shouldThrowAny {63 list.shouldForOne {64 it shouldBeLessThan 165 }66 }67 }68 test("sequence") {69 sequence.shouldForOne(::block)70 shouldThrowAny {71 sequence.shouldForOne {72 it shouldBeLessThan 173 }74 }75 }76 }77 context("forExactly") {78 fun block(x: Int) = x shouldBeGreaterThan 179 val n = 280 test("array") {81 array.shouldForExactly(n, ::block)82 shouldThrowAny {83 array.shouldForExactly(n) {84 it shouldBeLessThan 185 }86 }87 }88 test("list") {89 list.shouldForExactly(n, ::block)90 shouldThrowAny {91 list.shouldForExactly(n) {92 it shouldBeLessThan 193 }94 }95 }96 test("sequence") {97 sequence.shouldForExactly(n, ::block)98 shouldThrowAny {99 sequence.shouldForExactly(n) {100 it shouldBeLessThan 1101 }102 }103 }104 }105 context("forSome") {106 fun block(x: Int) = x shouldBeGreaterThan 2107 test("array") {108 array.shouldForSome(::block)109 shouldThrowAny {110 array.shouldForSome {111 it shouldBeLessThan 1112 }113 }114 }115 test("list") {116 list.shouldForSome(::block)117 shouldThrowAny {118 list.shouldForSome {119 it shouldBeLessThan 1120 }121 }122 }123 test("sequence") {124 sequence.shouldForSome(::block)125 shouldThrowAny {126 sequence.shouldForSome {127 it shouldBeLessThan 1128 }129 }130 }131 }132 context("forAny") {133 fun block(x: Int) = x shouldBeGreaterThan 0134 test("array") {135 array.shouldForAny(::block)136 shouldThrowAny {137 array.shouldForAny {138 it shouldBeLessThan 1139 }140 }141 }142 test("list") {143 list.shouldForAny(::block)144 shouldThrowAny {145 list.shouldForAny {146 it shouldBeLessThan 1147 }148 }149 }150 test("sequence") {151 sequence.shouldForAny(::block)152 shouldThrowAny {153 sequence.shouldForAny {154 it shouldBeLessThan 1155 }156 }157 }158 }159 context("forAtLeast") {160 fun block(x: Int) = x shouldBeGreaterThan 0161 val n = 3162 test("array") {163 array.shouldForAtLeast(n, ::block)164 shouldThrowAny {165 array.shouldForAtLeast(n) {166 it shouldBeLessThan 3167 }168 }169 }170 test("list") {171 list.shouldForAtLeast(n, ::block)172 shouldThrowAny {173 list.shouldForAtLeast(n) {174 it shouldBeLessThan 3175 }176 }177 }178 test("sequence") {179 sequence.shouldForAtLeast(n, ::block)180 shouldThrowAny {181 sequence.shouldForAtLeast(n) {182 it shouldBeLessThan 3183 }184 }185 }186 }187 context("forAtLeastOne") {188 fun block(x: Int) = x shouldBeGreaterThan 0189 test("array") {190 array.shouldForAtLeastOne(::block)191 shouldThrowAny {192 array.shouldForAtLeastOne {193 it shouldBeLessThan 1194 }195 }196 }197 test("list") {198 list.shouldForAtLeastOne(::block)199 shouldThrowAny {200 list.shouldForAtLeastOne {201 it shouldBeLessThan 1202 }203 }204 }205 test("sequence") {206 sequence.shouldForAtLeastOne(::block)207 shouldThrowAny {208 sequence.shouldForAtLeastOne {209 it shouldBeLessThan 1210 }211 }212 }213 }214 context("forAtMost") {215 fun block(x: Int) = x shouldBeGreaterThan 0216 test("array") {217 val arr = arrayOf(0, 0, 1)218 arr.shouldForAtMost(1, ::block)219 shouldThrowAny {220 arr.shouldForAtMost(1) {221 it shouldBeLessThan 3222 }223 }224 }225 test("list") {226 val l = listOf(0, 1, 1)227 l.shouldForAtMost(2, ::block)228 shouldThrowAny {229 l.shouldForAtMost(2) {230 it shouldBeLessThan 3231 }232 }233 }234 test("sequence") {235 sequence.shouldForAtMost(3, ::block)236 shouldThrowAny {237 sequence.shouldForAtMost(2) {238 it shouldBeLessThan 4239 }240 }241 }242 }243 context("forNone") {244 fun block(x: Int) = x shouldBeLessThan 1245 test("array") {246 array.shouldForNone(::block)247 shouldThrowAny {248 array.shouldForNone {249 it shouldBeLessThan 4250 }251 }252 }253 test("list") {254 list.shouldForNone(::block)255 shouldThrowAny {256 list.shouldForNone {257 it shouldBeLessThan 4258 }259 }260 }261 test("sequence") {262 sequence.shouldForNone(::block)263 shouldThrowAny {264 sequence.shouldForNone {265 it shouldBeLessThan 4266 }267 }268 }269 }270 context("forAtMostOne") {271 fun block(x: Int) = x shouldBe 1272 test("array") {273 array.shouldForAtMostOne(::block)274 }275 test("list") {276 list.shouldForAtMostOne(::block)277 }278 test("sequence") {279 sequence.shouldForAtMostOne(::block)280 }281 }282})...
InspectorsTest.kt
Source:InspectorsTest.kt
1package com.sksamuel.kotest2import io.kotest.assertions.throwables.shouldThrow3import io.kotest.core.spec.style.WordSpec4import io.kotest.inspectors.forAny5import io.kotest.inspectors.forExactly6import io.kotest.inspectors.forNone7import io.kotest.inspectors.forOne8import io.kotest.inspectors.forSome9import io.kotest.matchers.comparables.beGreaterThan10import io.kotest.matchers.comparables.beLessThan11import io.kotest.matchers.should12import io.kotest.matchers.shouldBe13import io.kotest.matchers.shouldNotBe14class InspectorsTest : WordSpec() {15 private val list = listOf(1, 2, 3, 4, 5)16 private val array = arrayOf(1, 2, 3, 4, 5)17 private val charSequence = "charSequence"18 init {19 "forNone" should {20 "pass if no elements pass fn test for a list" {21 list.forNone {22 it shouldBe 1023 }24 }25 "pass if no elements pass fn test for a char sequence" {26 charSequence.forNone {27 it shouldBe 'x'28 }29 }30 "pass if no elements pass fn test for an array" {31 array.forNone {32 it shouldBe 1033 }34 }35 "fail if one elements passes fn test" {36 shouldThrow<AssertionError> {37 list.forNone {38 it shouldBe 439 }40 }.message shouldBe """1 elements passed but expected 041The following elements passed:42443The following elements failed:441 => expected:<4> but was:<1>452 => expected:<4> but was:<2>463 => expected:<4> but was:<3>475 => expected:<4> but was:<5>"""48 }49 "fail if all elements pass fn test" {50 shouldThrow<AssertionError> {51 list.forNone {52 it should beGreaterThan(0)53 }54 }.message shouldBe """5 elements passed but expected 055The following elements passed:56157258359460561The following elements failed:62--none--"""63 }64 }65 "forSome" should {66 "pass if one elements pass test" {67 list.forSome {68 it shouldBe 369 }70 }71 "pass if size-1 elements pass test" {72 list.forSome {73 it should beGreaterThan(1)74 }75 }76 "pass if two elements pass test for a char sequence" {77 charSequence.forSome {78 it shouldBe 'c'79 }80 }81 "fail if no elements pass test" {82 shouldThrow<AssertionError> {83 array.forSome {84 it should beLessThan(0)85 }86 }.message shouldBe """No elements passed but expected at least one87The following elements passed:88--none--89The following elements failed:901 => 1 should be < 0912 => 2 should be < 0923 => 3 should be < 0934 => 4 should be < 0945 => 5 should be < 0"""95 }96 "fail if all elements pass test" {97 shouldThrow<AssertionError> {98 list.forSome {99 it should beGreaterThan(0)100 }101 }.message shouldBe """All elements passed but expected < 5102The following elements passed:10311042105310641075108The following elements failed:109--none--"""110 }111 }112 "forOne" should {113 "pass if one elements pass test" {114 list.forOne {115 it shouldBe 3116 }117 }118 "fail if all elements pass test for a char sequence" {119 shouldThrow<AssertionError> {120 charSequence.forOne { t ->121 t shouldNotBe 'X'122 }123 }.message shouldBe """12 elements passed but expected 1124The following elements passed:125c126h127a128r129S130e131q132u133e134n135... and 2 more passed elements136The following elements failed:137--none--"""138 }139 "fail if > 1 elements pass test" {140 shouldThrow<AssertionError> {141 list.forOne { t ->142 t should beGreaterThan(2)143 }144 }.message shouldBe """3 elements passed but expected 1145The following elements passed:146314741485149The following elements failed:1501 => 1 should be > 21512 => 2 should be > 2"""152 }153 "fail if no elements pass test" {154 shouldThrow<AssertionError> {155 array.forOne { t ->156 t shouldBe 22157 }158 }.message shouldBe """0 elements passed but expected 1159The following elements passed:160--none--161The following elements failed:1621 => expected:<22> but was:<1>1632 => expected:<22> but was:<2>1643 => expected:<22> but was:<3>1654 => expected:<22> but was:<4>1665 => expected:<22> but was:<5>"""167 }168 }169 "forAny" should {170 "pass if one elements pass test" {171 list.forAny { t ->172 t shouldBe 3173 }174 }175 "pass if at least elements pass test" {176 list.forAny { t ->177 t should beGreaterThan(2)178 }179 }180 "pass if all elements pass test for a char sequence" {181 charSequence.forAny {182 it shouldNotBe 'X'183 }184 }185 "fail if no elements pass test" {186 shouldThrow<AssertionError> {187 array.forAny { t ->188 t shouldBe 6189 }190 }.message shouldBe """0 elements passed but expected at least 1191The following elements passed:192--none--193The following elements failed:1941 => expected:<6> but was:<1>1952 => expected:<6> but was:<2>1963 => expected:<6> but was:<3>1974 => expected:<6> but was:<4>1985 => expected:<6> but was:<5>"""199 }200 }201 "forExactly" should {202 "pass if exactly k elements pass" {203 list.forExactly(2) { t ->204 t should beLessThan(3)205 }206 }207 "pass if exactly k elements pass for a char sequence" {208 charSequence.forExactly(1) {209 it shouldBe 'h'210 }211 }212 "fail if more elements pass test" {213 shouldThrow<AssertionError> {214 list.forExactly(2) { t ->215 t should beGreaterThan(2)216 }217 }.message shouldBe """3 elements passed but expected 2218The following elements passed:219322042215222The following elements failed:2231 => 1 should be > 22242 => 2 should be > 2"""225 }226 "fail if less elements pass test" {227 shouldThrow<AssertionError> {228 array.forExactly(2) { t ->229 t should beLessThan(2)230 }231 }.message shouldBe """1 elements passed but expected 2232The following elements passed:2331234The following elements failed:2352 => 2 should be < 22363 => 3 should be < 22374 => 4 should be < 22385 => 5 should be < 2"""239 }240 "fail if no elements pass test" {241 shouldThrow<AssertionError> {242 list.forExactly(2) { t ->243 t shouldBe 33244 }245 }.message shouldBe """0 elements passed but expected 2246The following elements passed:247--none--248The following elements failed:2491 => expected:<33> but was:<1>2502 => expected:<33> but was:<2>2513 => expected:<33> but was:<3>2524 => expected:<33> but was:<4>2535 => expected:<33> but was:<5>"""254 }255 }256 }257}...
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}...
InspectorAliases.kt
Source:InspectorAliases.kt
1package io.kotest.inspectors2/** Alias for [Sequence.forAll] */3fun <T> Sequence<T>.shouldForAll(fn: (T) -> Unit) = forAll(fn)4/** Alias for [Array.forAll] */5fun <T> Array<T>.shouldForAll(fn: (T) -> Unit) = forAll(fn)6/** Alias for [Collection.forAll] */7fun <T> Collection<T>.shouldForAll(fn: (T) -> Unit) = forAll(fn)8/** Alias for [Sequence.forOne] */9fun <T> Sequence<T>.shouldForOne(fn: (T) -> Unit) = forOne(fn)10/** Alias for [Array.forOne] */11fun <T> Array<T>.shouldForOne(fn: (T) -> Unit) = forOne(fn)12/** Alias for [Collection.forOne] */13fun <T> Collection<T>.shouldForOne(fn: (T) -> Unit) = forOne(fn)14/** Alias for [Sequence.forExactly] */15fun <T> Sequence<T>.shouldForExactly(k: Int, fn: (T) -> Unit) = forExactly(k, fn)16/** Alias for [Array.forExactly] */17fun <T> Array<T>.shouldForExactly(k: Int, fn: (T) -> Unit) = forExactly(k, fn)18/** Alias for [Collection.forExactly] */19fun <T> Collection<T>.shouldForExactly(k: Int, fn: (T) -> Unit) = forExactly(k, fn)20/** Alias for [Sequence.forSome] */21fun <T> Sequence<T>.shouldForSome(fn: (T) -> Unit) = forSome(fn)22/** Alias for [Array.forSome] */23fun <T> Array<T>.shouldForSome(fn: (T) -> Unit) = forSome(fn)24/** Alias for [Collection.forSome] */25fun <T> Collection<T>.shouldForSome(fn: (T) -> Unit) = forSome(fn)26/** Alias for [Sequence.forAny] */27fun <T> Sequence<T>.shouldForAny(fn: (T) -> Unit) = forAny(fn)28/** Alias for [Array.forAny] */29fun <T> Array<T>.shouldForAny(fn: (T) -> Unit) = forAny(fn)30/** Alias for [Collection.forAny] */31fun <T> Collection<T>.shouldForAny(fn: (T) -> Unit) = forAny(fn)32/** Alias for [Sequence.forAtLeastOne] */33fun <T> Sequence<T>.shouldForAtLeastOne(fn: (T) -> Unit) = forAtLeastOne(fn)34/** Alias for [Array.forAtLeastOne] */35fun <T> Array<T>.shouldForAtLeastOne(fn: (T) -> Unit) = forAtLeastOne(fn)36/** Alias for [Collection.forAtLeastOne] */37fun <T> Collection<T>.shouldForAtLeastOne(fn: (T) -> Unit) = forAtLeastOne(fn)38/** Alias for [Sequence.forAtLeast] */39fun <T> Sequence<T>.shouldForAtLeast(k: Int, fn: (T) -> Unit) = forAtLeast(k, fn)40/** Alias for [Array.forAtLeast] */41fun <T> Array<T>.shouldForAtLeast(k: Int, fn: (T) -> Unit) = forAtLeast(k, fn)42/** Alias for [Collection.forAtLeast] */43fun <T> Collection<T>.shouldForAtLeast(k: Int, fn: (T) -> Unit) = forAtLeast(k, fn)44/** Alias for [Sequence.forAtMostOne] */45fun <T> Sequence<T>.shouldForAtMostOne(fn: (T) -> Unit) = forAtMostOne(fn)46/** Alias for [Array.forAtMostOne] */47fun <T> Array<T>.shouldForAtMostOne(fn: (T) -> Unit) = forAtMostOne(fn)48/** Alias for [Collection.forAtMostOne] */49fun <T> Collection<T>.shouldForAtMostOne(fn: (T) -> Unit) = forAtMostOne(fn)50/** Alias for [Sequence.forAtMost] */51fun <T> Sequence<T>.shouldForAtMost(k: Int, fn: (T) -> Unit) = forAtMost(k, fn)52/** Alias for [Array.forAtMost] */53fun <T> Array<T>.shouldForAtMost(k: Int, fn: (T) -> Unit) = forAtMost(k, fn)54/** Alias for [Collection.forAtMost] */55fun <T> Collection<T>.shouldForAtMost(k: Int, fn: (T) -> Unit) = forAtMost(k, fn)56/** Alias for [Sequence.forNone] */57fun <T> Sequence<T>.shouldForNone(fn: (T) -> Unit) = forNone(fn)58/** Alias for [Array.forNone] */59fun <T> Array<T>.shouldForNone(fn: (T) -> Unit) = forNone(fn)60/** Alias for [Collection.forNone] */61fun <T> Collection<T>.shouldForNone(fn: (T) -> Unit) = forNone(fn)...
Sequence.forAny
Using AI Code Generation
1val list = listOf ( 1 , 2 , 3 , 4 , 5 , 6 )2Sequence . forAny ( list ) { it should beGreaterThan ( 1 ) }3val list = listOf ( 1 , 2 , 3 , 4 , 5 , 6 )4Sequence . forAll ( list ) { it should beGreaterThan ( 1 ) }5val list = listOf ( 1 , 2 , 3 , 4 , 5 , 6 )6Sequence . forNone ( list ) { it should beGreaterThan ( 1 ) }7val list = listOf ( 1 , 2 , 3 , 4 , 5 , 6 )8Sequence . forOne ( list ) { it should beGreaterThan ( 1 ) }9val list = listOf ( 1 , 2 , 3 , 4 , 5 , 6 )10Sequence . forNone ( list ) { it should beGreaterThan ( 1 ) }11val list = listOf ( 1 , 2 , 3 , 4 , 5 , 6 )12Sequence . forExactly ( list , 3 ) { it should beGreaterThan ( 1 ) }13val list = listOf ( 1 , 2 , 3 , 4 , 5 , 6 )14Sequence . forAtLeast ( list , 3 ) { it should beGreaterThan ( 1 ) }15val list = listOf ( 1 , 2 , 3 , 4 , 5 , 6 )16Sequence . forAtMost ( list , 3 ) { it should beGreaterThan ( 1 ) }
Sequence.forAny
Using AI Code Generation
1val list = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)2Sequence.forAny(list) { it > 5 } shouldBe true3Sequence.forAny(list) { it > 10 } shouldBe false4val list = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)5Sequence.forAll(list) { it > 5 } shouldBe false6Sequence.forAll(list) { it > 0 } shouldBe true7Sequence.forAll(list) { it > 10 } shouldBe false8val list = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)9Sequence.forNone(list) { it > 10 } shouldBe true10Sequence.forNone(list) { it > 5 } shouldBe false11val list = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)12Sequence.forOne(list) { it > 5 } shouldBe true13Sequence.forOne(list) { it > 10 } shouldBe false14val list = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)15Sequence.forExactly(2, list) { it > 5 } shouldBe true16Sequence.forExactly(3, list) { it > 5 } shouldBe false
Sequence.forAny
Using AI Code Generation
1@DisplayName("Test forAny method of Inspectors class")2class ForAnyTest {3 fun `test forAny method of Inspectors class`() {4 val list = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)5 list.forAny {6 }7 }8}
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!!