Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

haveElementAt-more-detail (#49) #3933

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,31 @@ fun <T> Array<T>.shouldNotHaveElementAt(index: Int, element: T) = asList().shoul
fun <T> List<T>.shouldNotHaveElementAt(index: Int, element: T) = this shouldNot haveElementAt(index, element)

fun <T, L : List<T>> haveElementAt(index: Int, element: T) = object : Matcher<L> {
override fun test(value: L) =
MatcherResult(
index < value.size && value[index] == element,
{ "Collection ${value.print().value} should contain ${element.print().value} at index $index" },
override fun test(value: L): MatcherResult {
val passed = index < value.size && value[index] == element
val listTooShortMsg = if(index < value.size) "" else "But it is too short: only ${value.size} elements"
val unexpectedElementMsg = when {
passed -> ""
index < value.size -> "Expected: <${value[index].print().value}>, but was <${element.print().value}>"
else -> ""
}
val indexesForElement = value.mapIndexedNotNull { index, current ->
if(current == element) index else null
}
val indexesForElementMsg = if(passed || indexesForElement.isEmpty())
""
else "Element was found at index(es): ${indexesForElement.print().value}"
val additionalDescriptions = listOf(listTooShortMsg, unexpectedElementMsg, indexesForElementMsg).filter {
it.isNotEmpty()
}
val additionalDescriptionsMsg = if(additionalDescriptions.isEmpty()) ""
else "\n${additionalDescriptions.joinToString("\n")}"
return MatcherResult(
passed,
{ "Collection ${value.print().value} should contain ${element.print().value} at index $index$additionalDescriptionsMsg" },
{ "Collection ${value.print().value} should not contain ${element.print().value} at index $index" }
)
}
}

infix fun <T> Iterable<T>.shouldExist(p: (T) -> Boolean) = toList().shouldExist(p)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldHave
import io.kotest.matchers.shouldNot
import io.kotest.matchers.shouldNotHave
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.throwable.shouldHaveMessage

class CollectionMatchersTest : WordSpec() {
Expand Down Expand Up @@ -143,6 +144,40 @@ class CollectionMatchersTest : WordSpec() {
tests should haveElementAt(0, TestSealed.Test1("test1"))
tests.shouldHaveElementAt(1, TestSealed.Test2(2))
}
"print if list is too short" {
shouldThrowAny {
listOf("a", "b", "c").shouldHaveElementAt(3, "d")
}.message shouldBe """
|Collection ["a", "b", "c"] should contain "d" at index 3
|But it is too short: only 3 elements
""".trimMargin()
}
"print if element does not match" {
shouldThrowAny {
listOf("a", "b", "c").shouldHaveElementAt(2, "d")
}.message shouldBe """
|Collection ["a", "b", "c"] should contain "d" at index 2
|Expected: <"c">, but was <"d">
""".trimMargin()
}
"print if element found at another index" {
shouldThrowAny {
listOf("a", "b", "c").shouldHaveElementAt(2, "b")
}.message shouldBe """
|Collection ["a", "b", "c"] should contain "b" at index 2
|Expected: <"c">, but was <"b">
|Element was found at index(es): [1]
""".trimMargin()
}
"print if element found at multiple other indexes" {
shouldThrowAny {
listOf("a", "b", "c", "b").shouldHaveElementAt(2, "b")
}.message shouldBe """
|Collection ["a", "b", "c", "b"] should contain "b" at index 2
|Expected: <"c">, but was <"b">
|Element was found at index(es): [1, 3]
""".trimMargin()
}
}

"containNull()" should {
Expand Down