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

Fix mapcontain (#55) #3965

Open
wants to merge 5 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,7 @@ fun <V> containAnyValues(vararg values: V): Matcher<Map<*, V>> = object : Matche
}
}

fun <K, V> contain(key: K, v: V): Matcher<Map<K, V>> = object : Matcher<Map<K, V>> {
override fun test(value: Map<K, V>) = MatcherResult(
value[key] == v,
{ "Map should contain mapping $key=$v but was ${buildActualValue(value)}" },
{ "Map should not contain mapping $key=$v but was $value" }
)

private fun buildActualValue(map: Map<K, V>) = map[key]?.let { "$key=$it" } ?: map
}
fun <K, V> contain(key: K, v: V): Matcher<Map<K, V>> = mapcontain(key, v)

fun <K, V> containAll(expected: Map<K, V>): Matcher<Map<K, V>> =
MapContainsMatcher(expected, ignoreExtraKeys = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,35 @@ import io.kotest.matchers.should
import io.kotest.matchers.shouldNot

fun <K, V> mapcontain(key: K, v: V) = object : Matcher<Map<K, V>> {
override fun test(value: Map<K, V>) = MatcherResult(
value[key] == v,
{ "Map should contain mapping $key=$v but was ${buildActualValue(value)}" },
{ "Map should not contain mapping $key=$v but was $value" }
)
override fun test(value: Map<K, V>): MatcherResult {
val match = match(value)
return MatcherResult(
match.passed,
{ match.message },
{ "Map should not contain mapping $key=$v but was $value" }
)
}

private fun match(value: Map<K, V>): MapContainResult = when {
key !in value.keys -> MapContainResult(
false,
"Map should contain mapping $key=$v but key was not in the map"
)
key in value.keys && value[key] != v -> MapContainResult(
false,
"Map should contain mapping $key=$v but was ${buildActualValue(value)}"
)
else -> MapContainResult(true, "")
}

private fun buildActualValue(map: Map<K, V>) = map[key]?.let { "$key=$it" } ?: map
}

private data class MapContainResult(
val passed: Boolean,
val message: String
)

fun <K, V> Map<K, V>.shouldContain(key: K, value: V) = this should mapcontain(key, value)
fun <K, V> Map<K, V>.shouldNotContain(key: K, value: V) = this shouldNot mapcontain(key, value)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class MapMatchersTest : WordSpec() {
"contain" should {
"test that a map contains the given pair" {
val map = mapOf(Pair(1, "a"), Pair(2, "b"))
map should contain(1, "a")
map shouldContain(1 to "a")
map.shouldContain(2, "b")
map.shouldNotContain(3, "A")
map shouldContain (1 to "a")
Expand All @@ -70,11 +70,21 @@ class MapMatchersTest : WordSpec() {
}.message.shouldBe("Map should contain mapping 1=c but was 1=a")
shouldThrow<AssertionError> {
map.shouldContain(4, "e")
}.message.shouldBe("Map should contain mapping 4=e but was {1=a, 2=b}")
}.message.shouldBe("Map should contain mapping 4=e but key was not in the map")
shouldThrow<AssertionError> {
map should contain(2, "a")
map shouldContain(2 to "a")
}.message.shouldBe("Map should contain mapping 2=a but was 2=b")
}
"fail for key not in map and null value" {
val map = mapOf("apple" to "green")
shouldThrow<AssertionError> {
map shouldContain("lemon" to null)
}.message.shouldBe("Map should contain mapping lemon=null but key was not in the map")
}
"pass for key not in map and null value" {
val map = mapOf("apple" to "green")
map shouldNotContain("lemon" to null)
}
}

"haveKeys" should {
Expand Down