A collection can be checked for exact element-by-e...
# kotest
k
A collection can be checked for exact element-by-element equality against the expected collection using either of these functions:
Copy code
collection1 shouldBe collection2
collection1 shouldContainExactly collection2
What are the pros and cons of each way of checking?
c
I can't check right now, but if I think the error message with
shouldContainExactly
is better, as it lists which elements are different instead of just listing the two lists in their entirety.
thank you color 1
💯 1
Also the equality of two lists is technically not a requirement of Kotlin collections (it is a requirement of Java collections so Kotlin developers tend to follow it, but it's not garanteed)
l
I think that's it essentially.
shouldBe
will use equality, while
shouldContainExactly
likely iterates over elements
k
Thanks. It's a bit more complicated than that, as
shouldBe
forwards to
IterableEq.equals
so it's not just checking list equality. For example
listOf(1,2) shouldBe setOf(1,2)
is true whereas
listOf(1,2) == setOf(1,2)
is false. But Ivan is right,
shouldContainExactly
is better for large collections as it tells you which elements don't match.
a
also ``shouldContainExactly`` might find an element that is out of order or has a similar partial match