Permalink
Browse files

map contain key value matchers added

  • Loading branch information...
1 parent 0e95098 commit bc72b07390292452e796f946f0ef88a91f938208 @danseid committed Dec 10, 2012
Showing with 28 additions and 1 deletion.
  1. +7 −0 src/main/kotlin/org/katchers/MapContainMatcher.kt
  2. +21 −1 src/test/kotlin/MapMatcherTest.kt
@@ -23,9 +23,16 @@ package org.katchers
class MapContainMatcher<K,V>(override val target: Map<K,V>): Matcher<Map<K,V>>{
private inline fun item(key: K, value: V) = if(!(value == target.get(key))) fail("$target contains ($key=$value)", "$target doesn't contain ($key=$value)")
inline fun item(el: Pair<K,V>) = item(el.first, el.second)
+ inline fun key(key: K) = if(!target.containsKey(key)) fail("$target contains key $key", "$target doesn't contain key $key")
+ inline fun value(value: V) = if(!target.containsValue(value)) fail("$target contains value $value", "$target doesn't contain value $value")
+
}
class MapNotContainMatcher<K,V>(override val target: Map<K,V>): Matcher<Map<K,V>>{
private inline fun item(key: K, value: V) = if(value == target.get(key)) fail("$target should not contain ($key=$value)", "$target contains ($key=$value)")
inline fun item(el: Pair<K,V>) = item(el.first, el.second)
+ inline fun key(key: K) = if(target.containsKey(key)) fail("$target should not contain key $key", "$target contains key $key")
+ inline fun value(value: V) = if(target.containsValue(value)) fail("$target should not contain value $value", "$target contains value $value")
+
+
}
@@ -30,7 +30,27 @@ public class MapMatcherTest {
mapOf(1 to "1", 2 to "2", 3 to "3") should !contain item (1 to "2")
mapOf(1 to 2, 2 to 3, 4 to 5) should !contain item (1 to 3)
{mapOf(1 to 2, 2 to 3, 4 to 5) should !contain item (1 to 2)} should fail with assertionError
+ }
+
+ test fun mapShouldContainKey() {
+ mapOf(1 to 2, 3 to 4, 5 to 6) should contain key 1
+ mapOf("1" to 2) should contain key "1"
+ {mapOf(1 to 2) should contain key 2} should fail with assertionError
+
+ mapOf(1 to 2, 3 to 4, 5 to 6) should !contain key 2
+ mapOf("1" to 2) should !contain key "2"
+ {mapOf(1 to 2) should !contain key 1} should fail with assertionError
+ }
+
+ test fun mapShouldContainValue() {
+ mapOf(1 to 2, 3 to 4, 5 to 6) should contain value 2
+ mapOf("1" to 2) should contain value 2
+ {mapOf(1 to 2) should contain value 1} should fail with assertionError
+
+ mapOf(1 to 2, 3 to 4, 5 to 6) should !contain value 1
+ mapOf("1" to 2) should !contain value 1
+ {mapOf(1 to 2) should !contain value 2} should fail with assertionError
}
-}
+}

0 comments on commit bc72b07

Please sign in to comment.