
jvmMain.io.kotest.assertions.arrow.nel.matchers.kt Maven / Gradle / Ivy
package io.kotest.assertions.arrow.nel
import arrow.core.NonEmptyList
import io.kotest.matchers.Matcher
import io.kotest.matchers.MatcherResult
import io.kotest.matchers.should
import io.kotest.matchers.shouldNot
fun NonEmptyList.shouldContainOnlyNulls() = this should containOnlyNulls()
fun NonEmptyList.shouldNotContainOnlyNulls() = this shouldNot containOnlyNulls()
fun containOnlyNulls() = object : Matcher> {
override fun test(value: NonEmptyList) =
MatcherResult(
value.all.all { it == null },
"NonEmptyList should contain only nulls",
"NonEmptyList should not contain only nulls"
)
}
fun NonEmptyList.shouldContainNull() = this should containNull()
fun NonEmptyList.shouldNotContainNull() = this shouldNot containNull()
fun containNull() = object : Matcher> {
override fun test(value: NonEmptyList) =
MatcherResult(
value.all.any { it == null },
"NonEmptyList should contain at least one null",
"NonEmptyList should not contain any nulls"
)
}
fun NonEmptyList.shouldContainElementAt(index: Int, element: T) = this should haveElementAt(index, element)
fun NonEmptyList.shouldNotContainElementAt(index: Int, element: T) = this shouldNot haveElementAt(index, element)
fun haveElementAt(index: Int, element: T) = object : Matcher> {
override fun test(value: NonEmptyList) =
MatcherResult(
value.all[index] == element,
"NonEmptyList should contain $element at index $index",
"NonEmptyList should not contain $element at index $index"
)
}
fun NonEmptyList.shouldContainNoNulls() = this should containNoNulls()
fun NonEmptyList.shouldNotContainNoNulls() = this shouldNot containNoNulls()
fun containNoNulls() = object : Matcher> {
override fun test(value: NonEmptyList) =
MatcherResult(
value.all.all { it != null },
"NonEmptyList should not contain nulls",
"NonEmptyList should have at least one null"
)
}
infix fun NonEmptyList.shouldContain(t: T) = this should contain(t)
infix fun NonEmptyList.shouldNotContain(t: T) = this shouldNot contain(t)
fun contain(t: T) = object : Matcher> {
override fun test(value: NonEmptyList) = MatcherResult(
value.all.contains(t),
"NonEmptyList should contain element $t",
"NonEmptyList should not contain element $t"
)
}
fun NonEmptyList.shouldBeUnique() = this shouldNot haveDuplicates()
fun NonEmptyList.shouldNotBeUnique() = this should haveDuplicates()
fun NonEmptyList.shouldHaveDuplicates() = this should haveDuplicates()
fun NonEmptyList.shouldNotHaveDuplicates() = this shouldNot haveDuplicates()
fun haveDuplicates() = object : Matcher> {
override fun test(value: NonEmptyList) = MatcherResult(
value.all.toSet().size < value.size,
"NonEmptyList should contain duplicates",
"NonEmptyList should not contain duplicates"
)
}
fun NonEmptyList.shouldContainAll(vararg ts: T) = this should containAll(*ts)
fun NonEmptyList.shouldNotContainAll(vararg ts: T) = this shouldNot containAll(*ts)
infix fun NonEmptyList.shouldContainAll(ts: List) = this should containAll(ts)
infix fun NonEmptyList.shouldNotContainAll(ts: List) = this shouldNot containAll(ts)
fun containAll(vararg ts: T) = containAll(ts.asList())
fun containAll(ts: List): Matcher> = object : Matcher> {
override fun test(value: NonEmptyList) = MatcherResult(
ts.all { value.contains(it) },
"NonEmptyList should contain all of ${ts.joinToString(",", limit=10)}",
"NonEmptyList should not contain all of ${ts.joinToString(",", limit=10)}"
)
}
infix fun NonEmptyList.shouldHaveSize(size: Int) = this should haveSize(size)
infix fun NonEmptyList.shouldNotHaveSize(size: Int) = this shouldNot haveSize(size)
fun haveSize(size: Int): Matcher> = object : Matcher> {
override fun test(value: NonEmptyList) =
MatcherResult(
value.size == size,
"NonEmptyList should have size $size but has size ${value.size}",
"NonEmptyList should not have size $size"
)
}
infix fun NonEmptyList.shouldBeSingleElement(t: T) = this should singleElement(t)
infix fun NonEmptyList.shouldNotBeSingleElement(t: T) = this shouldNot singleElement(t)
fun singleElement(t: T): Matcher> = object : Matcher> {
override fun test(value: NonEmptyList) = MatcherResult(
value.size == 1 && value.head == t,
"NonEmptyList should be a single element of $t but has ${value.size} elements",
"NonEmptyList should not be a single element of $t"
)
}
fun > NonEmptyList.shouldBeSorted() = this should beSorted()
fun > NonEmptyList.shouldNotBeSorted() = this shouldNot beSorted()
fun > beSorted(): Matcher> = object : Matcher> {
override fun test(value: NonEmptyList): MatcherResult {
val passed = value.all.sorted() == value.all
val snippet = value.all.joinToString(",", limit=10)
return MatcherResult(
passed,
"NonEmptyList $snippet should be sorted",
"NonEmptyList $snippet should not be sorted"
)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy