Skip to content

Commit

Permalink
Should contain exactly in any order count mismatch draft (#19) (#3845)
Browse files Browse the repository at this point in the history
modify `containExactlyInAnyOrder` so that it explicitly prints out when
count of duplicates is different, such as:
```
            shouldThrow<AssertionError> {
               listOf(1, 2, 2, 3).shouldContainExactlyInAnyOrder(listOf(1, 2, 3, 3))
            }.shouldHaveMessage(
               """
                  Collection should contain [1, 2, 3, 3] in any order, but was [1, 2, 2, 3]
                  CountMismatches: Key="2", expected count: 1, but was: 2, Key="3", expected count: 2, but was: 1
               """.trimIndent()
```
  • Loading branch information
AlexCue987 committed Jan 24, 2024
1 parent f524b62 commit ebcbd0e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,19 @@ fun <T, C : Collection<T>> containExactlyInAnyOrder(
val extra = actual.filterNot { t ->
expected.any { verifier?.verify(it, t)?.areEqual() ?: (t == it) }
}
val countMismatch = countMismatch(expectedGroupedCounts, valueGroupedCounts)

val failureMessage = {
buildString {
append("Collection should contain ${expected.print().value} in any order, but was ${actual.print().value}")
appendLine()
appendMissingAndExtra(missing, extra)
appendLine()
if(missing.isNotEmpty() || extra.isNotEmpty()) {
appendLine()
}
if(countMismatch.isNotEmpty()) {
append("CountMismatches: ${countMismatch.joinToString(", ")}")
}
}
}

Expand All @@ -147,3 +153,22 @@ fun <T, C : Collection<T>> containExactlyInAnyOrder(
negatedFailureMessage
)
}

internal fun<T> countMismatch(expectedCounts: Map<T, Int>, actualCounts: Map<T, Int>) =
actualCounts.entries.mapNotNull { actualEntry ->
expectedCounts[actualEntry.key]?.let { expectedValue ->
if(actualEntry.value != expectedValue)
CountMismatch(actualEntry.key, expectedValue, actualEntry.value)
else null
}
}

internal data class CountMismatch<T>(val key: T, val expectedCount: Int, val actualCount: Int) {
init {
require(expectedCount >= 0 && actualCount >= 0) {
"Both expected and actual count should be non-negative, but expected was: $expectedCount and actual: was: $actualCount"
}
}

override fun toString(): String = "Key=\"${key}\", expected count: $expectedCount, but was: $actualCount"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package com.sksamuel.kotest.matchers.collections
import io.kotest.assertions.shouldFailWithMessage
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.collections.CountMismatch
import io.kotest.matchers.collections.containExactly
import io.kotest.matchers.collections.containExactlyInAnyOrder
import io.kotest.matchers.collections.countMismatch
import io.kotest.matchers.collections.shouldBeEmpty
import io.kotest.matchers.collections.shouldContainExactly
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
import io.kotest.matchers.collections.shouldNotContainExactly
Expand Down Expand Up @@ -292,6 +295,28 @@ class ShouldContainExactlyTest : WordSpec() {
)
}

"print count mismatches for not null keys" {
shouldThrow<AssertionError> {
listOf(1, 2, 2, 3).shouldContainExactlyInAnyOrder(listOf(1, 2, 3, 3))
}.shouldHaveMessage(
"""
Collection should contain [1, 2, 3, 3] in any order, but was [1, 2, 2, 3]
CountMismatches: Key="2", expected count: 1, but was: 2, Key="3", expected count: 2, but was: 1
""".trimIndent()
)
}

"print count mismatches for nullable keys" {
shouldThrow<AssertionError> {
listOf(1, null, null, 3).shouldContainExactlyInAnyOrder(listOf(1, null, 3, 3))
}.shouldHaveMessage(
"""
Collection should contain [1, <null>, 3, 3] in any order, but was [1, <null>, <null>, 3]
CountMismatches: Key="null", expected count: 1, but was: 2, Key="3", expected count: 2, but was: 1
""".trimIndent()
)
}

"disambiguate when using optional expected value" {
val actual: List<String> = listOf("A", "B", "C")
val expected: List<String>? = listOf("A", "B", "C")
Expand All @@ -304,6 +329,29 @@ class ShouldContainExactlyTest : WordSpec() {
}
}
}

"countMismatch" should {
"return empty list for a complete match" {
val counts = mapOf("apple" to 1, "orange" to 2)
countMismatch(counts, counts).shouldBeEmpty()
}
"return differences for not null key" {
countMismatch(
mapOf("apple" to 1, "orange" to 2, "banana" to 3),
mapOf("apple" to 2, "orange" to 2, "peach" to 1)
) shouldBe listOf(
CountMismatch("apple", 1, 2)
)
}
"return differences for null key" {
countMismatch(
mapOf(null to 1, "orange" to 2, "banana" to 3),
mapOf(null to 2, "orange" to 2, "peach" to 1)
) shouldBe listOf(
CountMismatch(null, 1, 2)
)
}
}
}

companion object {
Expand Down

0 comments on commit ebcbd0e

Please sign in to comment.