kotest-assertions / io.kotest.assertions / extracting

extracting

fun <K, T> extracting(col: Collection<K>, extractor: K.() -> T): List<T>

extracting pulls property values out of a list of objects for typed bulk assertions on properties.

The simple example shows how extracting helps with disjunct collection assertions:

extracting(persons){ name }
  .shouldContainAll("John Doe", "Samantha Roes")

This is similar to using multiple forOne however allows for a more concise notation.

forOne(persons){ it.name shouldBe "John Doe" }
forOne(persons){ it.name shouldBe "Samantha Rose" }

extracting also allows to define complex return types shown in this elaborate example:

extracting(persons){ Pair(name, age) }
  .shouldContainAll(
    Pair("John Doe", 20),
    Pair("Samantha Roes", 19)
  )

Parameters

col - the collection of objects from which to extract the properties

extractor - the extractor that defines which properties are returned

Author
Hannes Thaller