Permalink
Browse files

Iterable/Map should contain item matcher added

  • Loading branch information...
1 parent 61e4923 commit 316d1ce72bef0229f4e1a2b9071f5cfaedf929f0 @danseid committed Dec 7, 2012
View
@@ -146,6 +146,24 @@ Matchers to use on string objects
"string" should contain all of("s", "t", "a") //✘
```
+###Iterable
+Matchers to use on Iterable<T>
+####contain | !contain item
+```kotlin
+ listOf(1, 2, 3) should contain item 3 //✔
+ setOf(1, 2, 3) should contain item 2 //✔
+ listOf("1", "2") should contain item "2" //✔
+ listOf(1, 2, 3) should contain item 4
+```
+
+###Map
+Matchers to use on Map<K,V>
+####contain | !contain item
+```kotlin
+ mapOf(1 to "1", 2 to "2", 3 to "3") should contain item (1 to "1") //✔
+ mapOf(1 to 2, 2 to 3, 4 to 5) should contain item (1 to 2) //✔
+ mapOf(1 to 2, 2 to 3, 4 to 5) should contain item (1 to 3) //✘
+```
###Function
Matchers to use on "() -> Any" function
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2012 Daniel Seidler
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.katchers
+
+/**
+ * @author Daniel Seidler
+ * @since 2012/12/07
+ */
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2012 Daniel Seidler
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.katchers
+
+/**
+ * @author Daniel Seidler
+ * @since 2012/12/06
+ */
+
+class IterableContainMatcher<T>(override val target: Iterable<T>): Matcher<Iterable<T>>{
+ fun item(el: T) = if(!target.contains(el)) fail("$target contains $el", "$target doesn't contain $el")
+}
+
+class IterableNotContainMatcher<T>(override val target: Iterable<T>): Matcher<Iterable<T>>{
+ fun item(el: T) = if(target.contains(el)) fail("$target should not contain $el", "$target contains $el")
+}
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2012 Daniel Seidler
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.katchers
+
+/**
+ * @author Daniel Seidler
+ * @since 2012/12/06
+ */
+
+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)
+}
+
+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)
+}
@@ -22,6 +22,7 @@ package org.katchers
// Any must be
// Any must
+
public fun <T>T.must(val verb: BE): AnyBeMatcher<T> = AnyBeMatcher(this)
public fun <T>T.must(val verb: NOTBE): AnyNotBeMatcher<T> = AnyNotBeMatcher(this)
public fun <T>T.must(val verb: MATCH): AnyMatchMatcher<T> = AnyMatchMatcher(this)
@@ -45,3 +46,13 @@ public inline fun Number.must(val verb: NOTBE): NumberNotBeMatcher = NumberNotBe
//() -> Unit must fail
public fun <T: () -> Any>T.must(verb: NOTFAIL): FunctionNotFailMatcher<T> = FunctionNotFailMatcher(this)
public fun <T: () -> Any>T.must(verb: FAIL): FunctionFailMatcher<T> = FunctionFailMatcher(this)
+
+
+//Iterables
+public fun <T>Iterable<T>.must(verb: CONTAIN): IterableContainMatcher<T> = IterableContainMatcher<T>(this)
+public fun <T>Iterable<T>.must(verb: NOTCONTAIN): IterableNotContainMatcher<T> = IterableNotContainMatcher<T>(this)
+
+
+//Maps
+public fun <R,T>Map<R,T>.must(verb: CONTAIN): MapContainMatcher<R,T> = MapContainMatcher<R,T>(this)
+public fun <R,T>Map<R,T>.must(verb: NOTCONTAIN): MapNotContainMatcher<R,T> = MapNotContainMatcher<R,T>(this)
@@ -48,3 +48,30 @@ public inline fun Number.should(val verb: NOTBE): NumberNotBeMatcher = NumberNot
public fun <T: () -> Any>T.should(verb: NOTFAIL): FunctionNotFailMatcher<T> = FunctionNotFailMatcher(this)
public fun <T: () -> Any>T.should(verb: FAIL): FunctionFailMatcher<T> = FunctionFailMatcher(this)
+
+//Iterables
+public fun <T>Iterable<T>.should(verb: CONTAIN): IterableContainMatcher<T> = IterableContainMatcher<T>(this)
+public fun <T>Iterable<T>.should(verb: NOTCONTAIN): IterableNotContainMatcher<T> = IterableNotContainMatcher<T>(this)
+
+
+//Maps
+public fun <R,T>Map<R,T>.should(verb: CONTAIN): MapContainMatcher<R,T> = MapContainMatcher<R,T>(this)
+public fun <R,T>Map<R,T>.should(verb: NOTCONTAIN): MapNotContainMatcher<R,T> = MapNotContainMatcher<R,T>(this)
+
+
+
+
+
+
+
+
+
+//public fun <T>Iterable<T>.should(func: ContainFunction<T>) {
+// this should contain item func.item
+//}
+//
+//class ContainFunction<T>(val item: T) {
+//
+//}
+//
+//fun contain<T>(val item: T) = ContainFunction(item)
@@ -29,6 +29,10 @@ Suite.SuiteClasses(
javaClass<FunctionMatcherTest>(),
javaClass<NumberMatcherTest>(),
javaClass<NumbersTest>(),
- javaClass<NumberRangesMatcherTest>()
+ javaClass<NumberRangesMatcherTest>(),
+ javaClass<IterableMatcherTest>(),
+ javaClass<MapMatcherTest>()
+
+
)
public class AllTest {}
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2012 Daniel Seidler
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+import org.junit.Test as test
+import org.katchers.*
+
+/**
+ * @author Daniel Seidler
+ * @since 2012/12/06
+ */
+public class IterableMatcherTest {
+
+ test fun listContainsElement() {
+ listOf(1, 2, 3) should contain item 3
+ setOf(1, 2, 3) should contain item 2
+ listOf("1", "2") should contain item "2"
+ { listOf(1, 2, 3) should contain item 4 } should fail with assertionError
+
+ listOf(1, 2, 3) should !contain item 4
+ listOf("1", "2") should !contain item "3"
+ { listOf(1, 2, 3) should !contain item 3 } should fail with assertionError
+ }
+
+}
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2012 Daniel Seidler
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+import org.junit.Test as test
+import org.katchers.*
+
+/**
+ * @author Daniel Seidler
+ * @since 2012/12/06
+ */
+public class MapMatcherTest {
+
+ test fun mapShouldContainItem() {
+ mapOf(1 to "1", 2 to "2", 3 to "3") should contain item (1 to "1")
+ mapOf(1 to 2, 2 to 3, 4 to 5) should contain item (1 to 2)
+ {mapOf(1 to 2, 2 to 3, 4 to 5) should contain item (1 to 3)} should fail with assertionError
+
+ 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
+
+
+ }
+}
@@ -26,13 +26,13 @@ public class NumbersTest {
* Test Number.compare function
*/
test fun compareNumbers() {
- val oneD = 1.0 as Double
+ val oneD = 1.0
val oneF = oneD.toFloat()
val oneI = oneD.toInt()
val oneL = oneD.toLong()
val oneS = oneD.toShort()
val oneB = oneD.toByte()
- val onePointOneD = 1.1 as Double
+ val onePointOneD = 1.1
val onePointOneF = (1.1).toFloat()
val oneList: List<Number> = arrayList<Number>(oneD, oneF, oneI, oneL, oneS, oneB)
@@ -24,7 +24,6 @@ public class ShouldTest {
test fun shouldBeAnyMatcher() {
assertTrue(Any().should(be) is AnyBeMatcher<Any>)
assertTrue(Any().should(!be) is AnyNotBeMatcher<Any>)
- val a = {(x: Int) -> x > 0}
}
test fun shouldBeStringMatcher() {

0 comments on commit 316d1ce

Please sign in to comment.