
commonMain.io.kotest.matchers.collections.matchers.kt Maven / Gradle / Ivy
package io.kotest.matchers.collections
import io.kotest.assertions.show.show
import io.kotest.matchers.*
fun Iterable.shouldHaveElementAt(index: Int, element: T) = toList().shouldHaveElementAt(index, element)
fun Array.shouldHaveElementAt(index: Int, element: T) = asList().shouldHaveElementAt(index, element)
fun List.shouldHaveElementAt(index: Int, element: T) = this should haveElementAt(index, element)
fun Iterable.shouldNotHaveElementAt(index: Int, element: T) = toList().shouldNotHaveElementAt(index, element)
fun Array.shouldNotHaveElementAt(index: Int, element: T) = asList().shouldNotHaveElementAt(index, element)
fun List.shouldNotHaveElementAt(index: Int, element: T) = this shouldNot haveElementAt(index, element)
fun > haveElementAt(index: Int, element: T) = object : Matcher {
override fun test(value: L) =
MatcherResult(
value[index] == element,
{ "Collection should contain ${element.show().value} at index $index" },
{ "Collection should not contain ${element.show().value} at index $index" }
)
}
infix fun Iterable.shouldHaveSingleElement(t: T): Iterable {
toList().shouldHaveSingleElement(t)
return this
}
infix fun Array.shouldHaveSingleElement(t: T): Array {
asList().shouldHaveSingleElement(t)
return this
}
infix fun Iterable.shouldHaveSingleElement(p: (T) -> Boolean): Iterable {
toList().shouldHaveSingleElement(p)
return this
}
infix fun Array.shouldHaveSingleElement(p: (T) -> Boolean) = asList().shouldHaveSingleElement(p)
infix fun Collection.shouldHaveSingleElement(t: T) = this should singleElement(t)
infix fun Collection.shouldHaveSingleElement(p: (T) -> Boolean) = this should singleElement(p)
infix fun Iterable.shouldNotHaveSingleElement(t: T) = toList().shouldNotHaveSingleElement(t)
infix fun Array.shouldNotHaveSingleElement(t: T) = asList().shouldNotHaveSingleElement(t)
infix fun Collection.shouldNotHaveSingleElement(t: T) = this shouldNot singleElement(t)
infix fun Iterable.shouldExist(p: (T) -> Boolean) = toList().shouldExist(p)
infix fun Array.shouldExist(p: (T) -> Boolean) = asList().shouldExist(p)
infix fun Collection.shouldExist(p: (T) -> Boolean) = this should exist(p)
fun exist(p: (T) -> Boolean) = object : Matcher> {
override fun test(value: Collection) = MatcherResult(
value.any { p(it) },
{ "Collection should contain an element that matches the predicate $p" },
{
"Collection should not contain an element that matches the predicate $p"
})
}
fun Iterable.shouldExistInOrder(vararg ps: (T) -> Boolean) = toList().shouldExistInOrder(ps.toList())
fun Array.shouldExistInOrder(vararg ps: (T) -> Boolean) = asList().shouldExistInOrder(ps.toList())
fun List.shouldExistInOrder(vararg ps: (T) -> Boolean) = this.shouldExistInOrder(ps.toList())
infix fun Iterable.shouldExistInOrder(expected: List<(T) -> Boolean>) = toList().shouldExistInOrder(expected)
infix fun Array.shouldExistInOrder(expected: List<(T) -> Boolean>) = asList().shouldExistInOrder(expected)
infix fun List.shouldExistInOrder(expected: List<(T) -> Boolean>) = this should existInOrder(expected)
infix fun Iterable.shouldNotExistInOrder(expected: Iterable<(T) -> Boolean>) =
toList().shouldNotExistInOrder(expected.toList())
infix fun Array.shouldNotExistInOrder(expected: Array<(T) -> Boolean>) =
asList().shouldNotExistInOrder(expected.asList())
infix fun Iterable.shouldNotExistInOrder(expected: List<(T) -> Boolean>) =
toList().shouldNotExistInOrder(expected)
infix fun Array.shouldNotExistInOrder(expected: List<(T) -> Boolean>) = asList().shouldNotExistInOrder(expected)
infix fun List.shouldNotExistInOrder(expected: List<(T) -> Boolean>) = this shouldNot existInOrder(expected)
fun Iterable.shouldContainAnyOf(vararg ts: T) = toList().shouldContainAnyOf(ts)
fun Array.shouldContainAnyOf(vararg ts: T) = asList().shouldContainAnyOf(ts)
fun Collection.shouldContainAnyOf(vararg ts: T) = this should containAnyOf(ts.asList())
fun Iterable.shouldNotContainAnyOf(vararg ts: T) = toList().shouldNotContainAnyOf(ts)
fun Array.shouldNotContainAnyOf(vararg ts: T) = asList().shouldNotContainAnyOf(ts)
fun Collection.shouldNotContainAnyOf(vararg ts: T) = this shouldNot containAnyOf(ts.asList())
infix fun Iterable.shouldContainAnyOf(ts: Collection) = toList().shouldContainAnyOf(ts)
infix fun Array.shouldContainAnyOf(ts: Collection) = asList().shouldContainAnyOf(ts)
infix fun Collection.shouldContainAnyOf(ts: Collection) = this should containAnyOf(ts)
infix fun Iterable.shouldNotContainAnyOf(ts: Collection) = toList().shouldNotContainAnyOf(ts)
infix fun Array.shouldNotContainAnyOf(ts: Collection) = asList().shouldNotContainAnyOf(ts)
infix fun Collection.shouldNotContainAnyOf(ts: Collection) = this shouldNot containAnyOf(ts)
fun containAnyOf(ts: Collection) = object : Matcher> {
override fun test(value: Collection): MatcherResult {
if (ts.isEmpty()) throwEmptyCollectionError()
return MatcherResult(
ts.any { it in value },
{ "Collection should contain any of ${ts.joinToString(separator = ", ", limit = 10) { it.show().value }}" },
{ "Collection should not contain any of ${ts.joinToString(separator = ", ", limit = 10) { it.show().value }}" }
)
}
}
internal fun throwEmptyCollectionError(): Nothing {
throw AssertionError("Asserting content on empty collection. Use Collection.shouldBeEmpty() instead.")
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy