Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add search for similar items to containAll (#3909)
add search for similar items to `containAll`, which provides more insight into what exactly is not matched, for instance: ``` shouldThrowAny { listOf(sweetGreenApple, sweetGreenPear, sourYellowLemon).shouldContainAll( listOf(sweetGreenApple, sweetRedApple) ) }.shouldHaveMessage(""" |Collection should contain all of [Fruit(name=apple, color=green, taste=sweet), Fruit(name=apple, color=red, taste=sweet)] but was missing [Fruit(name=apple, color=red, taste=sweet)]Possible matches: | expected: Fruit(name=apple, color=green, taste=sweet), | but was: Fruit(name=apple, color=red, taste=sweet), | The following fields did not match: | "color" expected: <"green">, but was: <"red"> """.trimMargin()) ``` Co-authored-by: Sam <sam@sksamuel.com>
- Loading branch information
1 parent
3d46d16
commit bad750c
Showing
5 changed files
with
79 additions
and
1 deletion.
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
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
14 changes: 14 additions & 0 deletions
14
...t-assertions-core/src/jvmTest/kotlin/com/sksamuel/kotest/matchers/collections/TestData.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,14 @@ | ||
package com.sksamuel.kotest.matchers.collections | ||
|
||
data class Fruit( | ||
val name: String, | ||
val color: String, | ||
val taste: String | ||
) | ||
|
||
val sweetGreenApple = Fruit("apple", "green", "sweet") | ||
val sweetRedApple = Fruit("apple", "red", "sweet") | ||
val sweetGreenPear = Fruit("pear", "green", "sweet") | ||
val sourYellowLemon = Fruit("lemon", "yellow", "sour") | ||
val tartRedCherry = Fruit("cherry", "red", "tart") | ||
val bitterPurplePlum = Fruit("plum", "purple", "bitter") |
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
23 changes: 23 additions & 0 deletions
23
...est-assertions-shared/src/commonMain/kotlin/io/kotest/similarity/PossibleMatchesForSet.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,23 @@ | ||
package io.kotest.similarity | ||
|
||
import io.kotest.equals.Equality | ||
|
||
fun<T> possibleMatchesForSet( | ||
passed: Boolean, | ||
expected: Set<T>, | ||
actual: Set<T>, | ||
verifier: Equality<T>? | ||
): String { | ||
return when { | ||
passed -> "" | ||
actual.isEmpty() -> "" | ||
Equality.default<T>().name() == (verifier?.name() ?: Equality.default<T>().name()) -> { | ||
val possibleMatches = actual.map { | ||
possibleMatchesDescription(expected, it) | ||
}.filter { it.isNotEmpty() } | ||
if(possibleMatches.isEmpty()) "" | ||
else "Possible matches:${possibleMatches.joinToString("\n")}" | ||
} | ||
else -> "" | ||
} | ||
} |