Skip to content

Commit

Permalink
ShouldExistInOrderTest.kt (kotest#30) (kotest#3872)
Browse files Browse the repository at this point in the history
provide more details when `shouldExistInOrder` fails. For instance,
print out "Predicate number 1 did not match", as shown in the following
example:
```
        "fail with clear message" {
             shouldThrowAny {
                listOf(1, 2).shouldExistInOrder(
                   { i: Int -> i < 2 },
                   { i: Int -> i < 2 }
                )
             }.message shouldBe "[1, 2] did not match the predicates [(kotlin.Int) -> kotlin.Boolean, (kotlin.Int) -> kotlin.Boolean] in order. Predicate at index 1 did not match."
          }
```

---------

Co-authored-by: Emil Kantis <emil.kantis@protonmail.com>
  • Loading branch information
AlexCue987 and Kantis authored Feb 24, 2024
1 parent 7d7a517 commit ee24971
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fun <T> existInOrder(predicates: List<(T) -> Boolean>): Matcher<Collection<T>?>

MatcherResult(
subsequenceIndex == predicates.size,
{ "${actual.print().value} did not match the predicates ${predicates.print().value} in order" },
{ "${actual.print().value} did not match the predicates ${predicates.print().value} in order. Predicate at index $subsequenceIndex did not match." },
{ "${actual.print().value} should not match the predicates ${predicates.print().value} in order" }
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.sksamuel.kotest.matchers.collections

import io.kotest.assertions.throwables.shouldThrowAny
import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.collections.shouldExistInOrder
import io.kotest.matchers.shouldBe

class ShouldExistInOrderTest: WordSpec() {
init {
"shouldExistInOrder" should {
"pass" {
listOf(1, 2).shouldExistInOrder(
{ i: Int -> i < 2 },
{ i: Int -> i < 3 }
)
}
"fail with clear message" {
shouldThrowAny {
listOf(1, 2).shouldExistInOrder(
{ i: Int -> i < 2 },
{ i: Int -> i < 2 }
)
}.message shouldBe "[1, 2] did not match the predicates [(kotlin.Int) -> kotlin.Boolean, (kotlin.Int) -> kotlin.Boolean] in order. Predicate at index 1 did not match."
}
}
}
}

0 comments on commit ee24971

Please sign in to comment.