Klitos Kyriacou
07/19/2023, 11:41 AMlistOf(0, 0, 1).forAll { it shouldBe 0 }
the failure message says that one element fails:
2 elements passed but expected 3
The following elements passed:
0
0
The following elements failed:
1 => expected:<0> but was:<1>
Is it possible to make it tell me which element failed (it was element at index 2 in the above case)?CLOVIS
07/19/2023, 1:00 PM```The following elements passed:
0
0
The following elements failed:
1 ```As you can see, the first two passed, and the third one failed. I'm not sure what more information you want?
Klitos Kyriacou
07/19/2023, 1:23 PMThe following elements failed:
1 (at index 2)
For comparison with other libraries:
Strikt: expectThat(listOf(0, 0, 1)).all { this isEqualTo 0 }
▼ Expect that [0, 0, 1]:
✗ all elements match:
▼ 0:
✓ is equal to 0
▼ 0:
✓ is equal to 0
▼ 1:
✗ is equal to 0
found 1
AssertK: assertThat(listOf(0, 0, 1)).each { it.isEqualTo(0) }
expected [[2]]:<[0]> but was:<[1]> ([0, 0, 1])
solonovamax
07/19/2023, 7:34 PMkotlin
listOf(0, 0, 0).shouldContainOnly(0) // passes
listOf(0, 0, 0) shouldContainOnly listOf(0) // passes
listOf(0, 0, 0) shouldContainOnly listOf(0, 0) // passes
listOf(0, 0, 0) shouldContainOnly listOf(0, 1) // fails with "Some elements were missing: [1]"
listOf(0, 0, 0, 1, 2) shouldContainOnly listOf(0, 1) // fails with "Some elements were unexpected: [2]"
alternatively, there is also:
- shouldContainExactly
, for requiring that it contains the given values in the exact order.
- shouldContainExactlyInAnyOrder
, for requiring it contains the given values in any order.
- shouldContainAll
, for requiring that it contains at least one of all the values specified.
- shouldContainDuplicates
, for ensuring that it contains duplicates
You can also check out CollectionMatchersTest (https://github.com/kotest/kotest/blob/master/kotest-assertions/kotest-assertions-core/src/jvmTest/kotlin/com/sksamuel/kotest/matchers/collections/CollectionMatchersTest.kt) for examples of more collection matchersKlitos Kyriacou
07/20/2023, 8:43 AMshouldContainOnly
is more succinct than my previous usage of forAll
, but it still doesn't tell you at which index it had found a non-matching element.solonovamax
07/21/2023, 4:37 PMkotlin
listOf(0, 0, 0).forEachIndexed { index, it ->
withClue("index $index") {
it shouldBe 0
}
}
then, the clue will show the index when it fails. However, this may end up being kind of messy.Emil Kantis
07/21/2023, 9:07 PM