-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
fix the following bug - the test below was passing: ``` "fail to detect count mismatch" { sequenceOf("apple", "apple", "orange") should containAllInAnyOrder( sequenceOf("apple", "orange", "orange") ) } ``` * also fix two tests named "for subset, same count with nulls" - they were not validating correct behavior. * also provide more details about what exactly did not match, such as: ``` Sequence should contain the values of [1, 1, 2] in any order, but was [1, 2, 2]. Count Mismatches: For 1: expected count: <2>, but was: <1> For 2: expected count: <1>, but was: <2> ```
- Loading branch information
1 parent
cd60ed6
commit cc6ae07
Showing
4 changed files
with
139 additions
and
5 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
...core/src/commonMain/kotlin/io/kotest/matchers/sequences/UnorderedCollectionsDifference.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,66 @@ | ||
package io.kotest.matchers.sequences | ||
|
||
import io.kotest.assertions.print.print | ||
|
||
internal data class UnorderedCollectionsDifference<T>( | ||
val missingElements: Set<T>, | ||
val extraElements: Set<T>, | ||
val countMismatches: List<CountMismatch<T>> | ||
) { | ||
private val describedMismatches = sequenceOf( | ||
DescribedMismatch(missingElements, "Missing Elements"), | ||
DescribedMismatch(extraElements, "Extra Elements"), | ||
DescribedMismatch(countMismatches, "Count Mismatches") | ||
) | ||
|
||
fun isMatch(): Boolean { | ||
return describedMismatches.all { it.elements.isEmpty() } | ||
} | ||
|
||
override fun toString(): String { | ||
return if (isMatch()) "" else "\n" + describedMismatches | ||
.map { it.toString() } | ||
.filter { it.isNotEmpty() } | ||
.joinToString("\n") | ||
} | ||
|
||
private data class DescribedMismatch<T>( | ||
val elements: Collection<T>, | ||
val description: String | ||
) { | ||
override fun toString(): String { | ||
return if (elements.isEmpty()) "" else | ||
"$description:\n${elements.joinToString("\n") { it.print().value }}" | ||
} | ||
} | ||
|
||
companion object { | ||
fun<T> of(expected: List<T>, value: List<T>): UnorderedCollectionsDifference<T> { | ||
val expectedCounts = expected.counted() | ||
val valueCounts = value.counted() | ||
return UnorderedCollectionsDifference( | ||
missingElements = expectedCounts.keys - valueCounts.keys, | ||
extraElements = valueCounts.keys - expectedCounts.keys, | ||
countMismatches = expectedCounts.mapNotNull { expected -> | ||
val valueCount = valueCounts[expected.key] | ||
valueCount?.let { | ||
if (expected.value == valueCount) null else | ||
CountMismatch(expected.key, expected.value, valueCount) | ||
} | ||
} | ||
) | ||
} | ||
} | ||
|
||
internal data class CountMismatch<T>( | ||
val value: T, | ||
val expectedCount: Int, | ||
val actualCount: Int | ||
) { | ||
init { | ||
require(expectedCount != actualCount) { "Expected count should be different from actual, but both were: $expectedCount" } | ||
} | ||
|
||
override fun toString(): String = " For ${value.print().value}: expected count: <$expectedCount>, but was: <$actualCount>" | ||
} | ||
} |
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
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
52 changes: 52 additions & 0 deletions
52
...mTest/kotlin/com/sksamuel/kotest/matchers/sequences/UnorderedCollectionsDifferenceTest.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,52 @@ | ||
package com.sksamuel.kotest.matchers.sequences | ||
|
||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.sequences.UnorderedCollectionsDifference | ||
import io.kotest.matchers.shouldBe | ||
|
||
class UnorderedCollectionsDifferenceTest: StringSpec() { | ||
init { | ||
"detect match" { | ||
UnorderedCollectionsDifference.of( | ||
expected = listOf("apple", "orange", "apple"), | ||
value = listOf("apple", "apple", "orange"), | ||
).isMatch() shouldBe true | ||
} | ||
"detect missing elements" { | ||
UnorderedCollectionsDifference.of( | ||
expected = listOf("apple", "orange", "apple", "banana"), | ||
value = listOf("apple", "apple", "orange"), | ||
) shouldBe UnorderedCollectionsDifference( | ||
missingElements = setOf("banana"), | ||
extraElements = setOf(), | ||
countMismatches = listOf() | ||
) | ||
} | ||
"detect extra elements" { | ||
UnorderedCollectionsDifference.of( | ||
expected = listOf("apple", "orange", "apple"), | ||
value = listOf("apple", "apple", "orange", "banana"), | ||
) shouldBe UnorderedCollectionsDifference( | ||
missingElements = setOf(), | ||
extraElements = setOf("banana"), | ||
countMismatches = listOf() | ||
) | ||
} | ||
"detect count mismatch" { | ||
UnorderedCollectionsDifference.of( | ||
expected = listOf("apple", "orange", "apple"), | ||
value = listOf("apple", "apple", "orange", "orange"), | ||
) shouldBe UnorderedCollectionsDifference( | ||
missingElements = setOf(), | ||
extraElements = setOf(), | ||
countMismatches = listOf( | ||
UnorderedCollectionsDifference.CountMismatch( | ||
value = "orange", | ||
expectedCount = 1, | ||
actualCount = 2 | ||
) | ||
) | ||
) | ||
} | ||
} | ||
} |