All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.io.kotest.matchers.collections.matchers.kt Maven / Gradle / Ivy

package io.kotest.matchers.collections

import io.kotest.assertions.print.print
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(
         index < value.size && value[index] == element,
         { "Collection ${value.print().value} should contain ${element.print().value} at index $index" },
         { "Collection ${value.print().value} should not contain ${element.print().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 ${value.print().value} should contain an element that matches the predicate $p" },
      {
         "Collection ${value.print().value} should not contain an element that matches the predicate $p"
      })
}

fun  Iterable.shouldMatchInOrder(vararg assertions: (T) -> Unit) = toList().shouldMatchInOrder(assertions.toList())
fun  Array.shouldMatchInOrder(vararg assertions: (T) -> Unit) = asList().shouldMatchInOrder(assertions.toList())
fun  List.shouldMatchInOrder(vararg assertions: (T) -> Unit) = this.shouldMatchInOrder(assertions.toList())
infix fun  Iterable.shouldMatchInOrder(assertions: List<(T) -> Unit>) = toList().shouldMatchInOrder(assertions)
infix fun  Array.shouldMatchInOrder(assertions: List<(T) -> Unit>) = asList().shouldMatchInOrder(assertions)
infix fun  List.shouldMatchInOrder(assertions: List<(T) -> Unit>) = this should matchInOrder(assertions.toList(), allowGaps = false)
fun  Iterable.shouldNotMatchInOrder(vararg assertions: (T) -> Unit) = toList().shouldNotMatchInOrder(assertions.toList())
fun  Array.shouldNotMatchInOrder(vararg assertions: (T) -> Unit) = asList().shouldNotMatchInOrder(assertions.toList())
fun  List.shouldNotMatchInOrder(vararg assertions: (T) -> Unit) = this.shouldNotMatchInOrder(assertions.toList())
infix fun  Iterable.shouldNotMatchInOrder(assertions: List<(T) -> Unit>) = toList().shouldNotMatchInOrder(assertions)
infix fun  Array.shouldNotMatchInOrder(assertions: List<(T) -> Unit>) = asList().shouldNotMatchInOrder(assertions)
infix fun  List.shouldNotMatchInOrder(assertions: List<(T) -> Unit>) = this shouldNot matchInOrder(assertions.toList(), allowGaps = false)

fun  Iterable.shouldMatchInOrderSubset(vararg assertions: (T) -> Unit) = toList().shouldMatchInOrderSubset(assertions.toList())
fun  Array.shouldMatchInOrderSubset(vararg assertions: (T) -> Unit) = asList().shouldMatchInOrderSubset(assertions.toList())
fun  List.shouldMatchInOrderSubset(vararg assertions: (T) -> Unit) = this.shouldMatchInOrderSubset(assertions.toList())
infix fun  Iterable.shouldMatchInOrderSubset(assertions: List<(T) -> Unit>) = toList().shouldMatchInOrderSubset(assertions)
infix fun  Array.shouldMatchInOrderSubset(assertions: List<(T) -> Unit>) = asList().shouldMatchInOrderSubset(assertions)
infix fun  List.shouldMatchInOrderSubset(assertions: List<(T) -> Unit>) = this should matchInOrder(assertions.toList(), allowGaps = true)
fun  Iterable.shouldNotMatchInOrderSubset(vararg assertions: (T) -> Unit) = toList().shouldNotMatchInOrderSubset(assertions.toList())
fun  Array.shouldNotMatchInOrderSubset(vararg assertions: (T) -> Unit) = asList().shouldNotMatchInOrderSubset(assertions.toList())
fun  List.shouldNotMatchInOrderSubset(vararg assertions: (T) -> Unit) = this.shouldNotMatchInOrderSubset(assertions.toList())
infix fun  Iterable.shouldNotMatchInOrderSubset(assertions: List<(T) -> Unit>) = toList().shouldNotMatchInOrderSubset(assertions)
infix fun  Array.shouldNotMatchInOrderSubset(assertions: List<(T) -> Unit>) = asList().shouldNotMatchInOrderSubset(assertions)
infix fun  List.shouldNotMatchInOrderSubset(assertions: List<(T) -> Unit>) = this shouldNot matchInOrder(assertions.toList(), allowGaps = true)

fun  Iterable.shouldMatchEach(vararg assertions: (T) -> Unit) = toList().shouldMatchEach(assertions.toList())
fun  Array.shouldMatchEach(vararg assertions: (T) -> Unit) = asList().shouldMatchEach(assertions.toList())
fun  List.shouldMatchEach(vararg assertions: (T) -> Unit) = this.shouldMatchEach(assertions.toList())
infix fun  Iterable.shouldMatchEach(assertions: List<(T) -> Unit>) = toList().shouldMatchEach(assertions)
infix fun  Array.shouldMatchEach(assertions: List<(T) -> Unit>) = asList().shouldMatchEach(assertions)
infix fun  List.shouldMatchEach(assertions: List<(T) -> Unit>) = this should matchEach(assertions.toList())
fun  Iterable.shouldNotMatchEach(vararg assertions: (T) -> Unit) = toList().shouldNotMatchEach(assertions.toList())
fun  Array.shouldNotMatchEach(vararg assertions: (T) -> Unit) = asList().shouldNotMatchEach(assertions.toList())
fun  List.shouldNotMatchEach(vararg assertions: (T) -> Unit) = this.shouldNotMatchEach(assertions.toList())
infix fun  Iterable.shouldNotMatchEach(assertions: List<(T) -> Unit>) = toList().shouldNotMatchEach(assertions)
infix fun  Array.shouldNotMatchEach(assertions: List<(T) -> Unit>) = asList().shouldNotMatchEach(assertions)
infix fun  List.shouldNotMatchEach(assertions: List<(T) -> Unit>) = this shouldNot matchEach(assertions.toList())

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 ${value.print().value} should contain any of ${ts.print().value}" },
         { "Collection ${value.print().value} should not contain any of ${ts.print().value}" }
      )
   }
}



internal fun throwEmptyCollectionError(): Nothing {
   throw AssertionError("Asserting content on empty collection. Use Collection.shouldBeEmpty() instead.")
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy