-
Notifications
You must be signed in to change notification settings - Fork 647
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Sequence.shouldContainExactly(...) (#3743)
Previously, in the case of a failure, simply `toString()` on the involved sequences has been invoked to build the failure message. However, this just printed the (internal) sequence class name. Now, the consumed and compared elements are collected in separate lists and then included in the failure message. So the failure contains all elements up the point, when a difference has been detected. This approach even works for infinite sequences. Fixes #3742
- Loading branch information
Showing
2 changed files
with
61 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...ns-core/src/jvmTest/kotlin/com/sksamuel/kotest/matchers/sequences/SequenceMatchersTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.sksamuel.kotest.matchers.sequences | ||
|
||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.sequences.shouldContainExactly | ||
import io.kotest.matchers.sequences.shouldNotContainExactly | ||
import io.kotest.matchers.shouldBe | ||
|
||
class SequenceMatchersTest : StringSpec({ | ||
|
||
"contain exactly" { | ||
sequenceOf(1, 2, 3).shouldContainExactly(1, 2, 3) | ||
sequenceOf(1).shouldContainExactly(1) | ||
emptySequence<Any>().shouldContainExactly() | ||
|
||
shouldThrow<AssertionError> { | ||
sequenceOf(1, 2, 3).shouldContainExactly(1, 3, 5) | ||
}.message shouldBe """ | ||
Sequence should contain exactly 1, 3, ... but was 1, 2, .... | ||
Expected 3 at index 1 but found 2.""".trimIndent() | ||
|
||
shouldThrow<AssertionError> { | ||
sequenceOf(1, 2, 3).shouldContainExactly(1, 2, 3, 4) | ||
}.message shouldBe """ | ||
Sequence should contain exactly 1, 2, 3, 4 but was 1, 2, 3. | ||
Actual sequence has less elements than expected sequence: expected 4 at index 3.""".trimIndent() | ||
|
||
shouldThrow<AssertionError> { | ||
sequenceOf(1, 2, 3).shouldContainExactly(1, 2) | ||
}.message shouldBe """ | ||
Sequence should contain exactly 1, 2 but was 1, 2, 3. | ||
Actual sequence has more elements than expected sequence: found 3 at index 2.""".trimIndent() | ||
|
||
shouldThrow<AssertionError> { | ||
generateSequence(1) { it + 1 }.shouldContainExactly(1, 2, 3) | ||
}.message shouldBe """ | ||
Sequence should contain exactly 1, 2, 3 but was 1, 2, 3, 4, .... | ||
Actual sequence has more elements than expected sequence: found 4 at index 3.""".trimIndent() | ||
|
||
shouldThrow<AssertionError> { | ||
sequenceOf(1, 2, 3).shouldNotContainExactly(1, 2, 3) | ||
}.message shouldBe "Sequence should not contain exactly 1, 2, 3" | ||
} | ||
}) |