Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add similarity to haveValues and haveValue (#48) #3931

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -29,23 +29,51 @@ fun <K> haveKeys(vararg keys: K): Matcher<Map<K, Any?>> = object : Matcher<Map<K
}

fun <V> haveValue(v: V): Matcher<Map<*, V>> = object : Matcher<Map<*, V>> {
override fun test(value: Map<*, V>) = MatcherResult(
value.containsValue(v),
{ "Map should contain value $v" },
{ "Map should not contain value $v" })
override fun test(value: Map<*, V>): MatcherResult {
val passed = value.containsValue(v)
val possibleMatchesDescription = possibleMatchesForMissingElements(
setOf(v),
value.values.toSet(),
"value"
)
return MatcherResult(
passed,
{ "Map should contain value $v$possibleMatchesDescription" },
{ "Map should not contain value $v" })
}
}

fun <V> haveValues(vararg values: V): Matcher<Map<*, V>> = object : Matcher<Map<*, V>> {
override fun test(value: Map<*, V>): MatcherResult {
val valuesNotPresentInMap = values.filterNot { value.containsValue(it) }
val possibleMatchesDescription = possibleMatchesForMissingElements(
valuesNotPresentInMap.toSet(),
value.values.toSet(),
"values"
)
return MatcherResult(
valuesNotPresentInMap.isEmpty(),
{ "Map did not contain the values ${values.joinToString(", ")}" },
{ "Map did not contain the values ${valuesNotPresentInMap.joinToString(", ")}$possibleMatchesDescription" },
{ "Map should not contain the values ${values.joinToString(", ")}" }
)
}
}

internal fun <K> possibleMatchesForMissingElements(
unexpected: Set<K>,
expected: Set<K>,
elementTypeDescription: String
): String {
val possibleMatches = unexpected.
map {
possibleMatchesDescription(expected, it)
}.filter {
it.isNotEmpty()
}.joinToString("\n")
return if (possibleMatches.isEmpty()) ""
else "\nPossible matches for missing $elementTypeDescription:\n$possibleMatches"
}

fun <K> containAnyKeys(vararg keys: K): Matcher<Map<K, Any?>> = object : Matcher<Map<K, Any?>> {
override fun test(value: Map<K, Any?>): MatcherResult {
val passed = keys.any { value.containsKey(it) }
Expand Down
Expand Up @@ -55,6 +55,23 @@ class MapMatchersTest : WordSpec() {
map.shouldContainValue("c")
}.message.shouldBe("Map should contain value c")
}
"find similarities for values not found" {
shouldThrow<AssertionError> {
mapOf(
1 to sweetGreenApple,
2 to sweetRedApple,
3 to sourYellowLemon
).shouldContainValue(sweetGreenPear)
}.message.shouldBe("""
|Map should contain value Fruit(name=pear, color=green, taste=sweet)
|Possible matches for missing value:
|
| expected: Fruit(name=apple, color=green, taste=sweet),
| but was: Fruit(name=pear, color=green, taste=sweet),
| The following fields did not match:
| "name" expected: <"apple">, but was: <"pear">
""".trimMargin())
}
}

"contain" should {
Expand Down Expand Up @@ -116,6 +133,24 @@ class MapMatchersTest : WordSpec() {
map.shouldNotContainValues("a", "b")
}.message.shouldBe("Map should not contain the values a, b")
}

"find similarities for values not found" {
shouldThrow<AssertionError> {
mapOf(
1 to sweetGreenApple,
2 to sweetRedApple,
3 to sourYellowLemon
).shouldContainValues(sweetGreenApple, sweetGreenPear)
}.message.shouldBe("""
|Map did not contain the values Fruit(name=pear, color=green, taste=sweet)
|Possible matches for missing values:
|
| expected: Fruit(name=apple, color=green, taste=sweet),
| but was: Fruit(name=pear, color=green, taste=sweet),
| The following fields did not match:
| "name" expected: <"apple">, but was: <"pear">
""".trimMargin())
}
}

"containAnyKeys" should {
Expand Down