Best Kotest code snippet using io.kotest.matchers.collections.MatchInOrderSubsetProblem.test
CollectionMatchers.kt
Source:CollectionMatchers.kt
1package io.kotest.matchers.collections2import io.kotest.assertions.ErrorCollectionMode3import io.kotest.assertions.errorCollector4import io.kotest.assertions.print.print5import io.kotest.assertions.runWithMode6import io.kotest.matchers.Matcher7import io.kotest.matchers.MatcherResult8import io.kotest.matchers.neverNullMatcher9fun <T> existInOrder(vararg ps: (T) -> Boolean): Matcher<Collection<T>?> = existInOrder(ps.asList())10/**11 * Assert that a collections contains a subsequence that matches the given subsequence of predicates, possibly with12 * values in between.13 */14fun <T> existInOrder(predicates: List<(T) -> Boolean>): Matcher<Collection<T>?> = neverNullMatcher { actual ->15 require(predicates.isNotEmpty()) { "predicates must not be empty" }16 var subsequenceIndex = 017 val actualIterator = actual.iterator()18 while (actualIterator.hasNext() && subsequenceIndex < predicates.size) {19 if (predicates[subsequenceIndex](actualIterator.next())) subsequenceIndex += 120 }21 MatcherResult(22 subsequenceIndex == predicates.size,23 { "${actual.print().value} did not match the predicates ${predicates.print().value} in order" },24 { "${actual.print().value} should not match the predicates ${predicates.print().value} in order" }25 )26}27fun <T> haveSize(size: Int): Matcher<Collection<T>> = haveSizeMatcher(size)28fun <T> singleElement(t: T): Matcher<Collection<T>> = object : Matcher<Collection<T>> {29 override fun test(value: Collection<T>) = MatcherResult(30 value.size == 1 && value.first() == t,31 { "Collection should be a single element of $t but has ${value.size} elements: ${value.print().value}" },32 { "Collection should not be a single element of $t" }33 )34}35fun <T> singleElement(p: (T) -> Boolean): Matcher<Collection<T>> = object : Matcher<Collection<T>> {36 override fun test(value: Collection<T>): MatcherResult {37 val filteredValue: List<T> = value.filter(p)38 return MatcherResult(39 filteredValue.size == 1,40 { "Collection should have a single element by a given predicate but has ${filteredValue.size} elements: ${value.print().value}" },41 { "Collection should not have a single element by a given predicate" }42 )43 }44}45fun <T : Comparable<T>> beSorted(): Matcher<List<T>> = sorted()46fun <T : Comparable<T>> sorted(): Matcher<List<T>> = sortedBy { it }47fun <T, E : Comparable<E>> beSortedBy(transform: (T) -> E): Matcher<List<T>> = sortedBy(transform)48fun <T, E : Comparable<E>> sortedBy(transform: (T) -> E): Matcher<List<T>> = object : Matcher<List<T>> {49 override fun test(value: List<T>): MatcherResult {50 val failure = value.withIndex().firstOrNull { (i, it) -> i != value.lastIndex && transform(it) > transform(value[i + 1]) }51 val elementMessage = when (failure) {52 null -> ""53 else -> ". Element ${failure.value} at index ${failure.index} was greater than element ${value[failure.index + 1]}"54 }55 return MatcherResult(56 failure == null,57 { "List ${value.print().value} should be sorted$elementMessage" },58 { "List ${value.print().value} should not be sorted" }59 )60 }61}62fun <T> matchEach(vararg fns: (T) -> Unit): Matcher<Collection<T>?> = matchEach(fns.asList())63fun <T> matchInOrder(vararg fns: (T) -> Unit): Matcher<Collection<T>?> = matchInOrder(fns.asList(), allowGaps = false)...
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!!