Permalink
Browse files

moved examples to wiki

  • Loading branch information...
1 parent bc72b07 commit 384f1033ecefd83b48d4fb45d07df4bf40f213ba @danseid committed Dec 10, 2012
View
157 README.md
@@ -25,159 +25,4 @@ Every verb can be negated by __!__ operator
1 should !be equal 2
-##List of katchers
-All examples are using __should__, but it can also be replaced with __must__
-###Any
-Any matchers may be used on any type of object.
-####be | !be equal
-Equal <matchfunction> should only be used on types, that implement equals function correctly
-```kotlin
-1 should be equal 1 //✔
-2 should !be equal 1 //✔
-"string" should be equal "string" //✔
-"string" should be equal "str" //✘
-```
-
-####be | !be any
-Match the result to any object in a list
- ```kotlin
- 1 should be any of(1,2,3) //✔ of is a convenient function to create a list
- 1 should be any arrayList(1,2,3) //✔ same as above
- "abc" should be any of("cab", "bac", "abc") //✔
- 4 should !be any of(1,2,3) //✔
- 4 should !be any of (1,2,4) //✘
- ```
-
-####match | !match condition
-Match condition to any object
-```kotlin
-1 should match condition {this > 0 && this < 2} //✔
-
-object book {
- val title = "Title"
- val author = "Author"
-}
-
-book should match condition { title == "Title" } //✔
-book should match condition { title == "Title" && author == "Author" } //✔
-book should !match condition { title == "T" && author == "Author" } //✔
-book should !match condition { title == "Title" && author == "Author" //✘
-```
-###Number
-Matchers to use on number objects like Int, Float, Double ...
-#### be | !be gt |greaterThan
-```kotlin
- 1 should be gt 0.0 //✔
- 1 should be greaterThan 0 //✔ same as above
- 1 should !be gt 1 //✔
- 1.9 should !be gt 2 //✔
- 1 should be gt 1 //✘
- 1 should be gt 2 //✘
-```
-#### be | !be gte | greaterOrEqualThan
-```kotlin
- 1 should be gte 0 //✔
- 1 should be greaterOrEqualThan 0 //✔ same as above
- 1 should be gte 1 //✔
- 1 should !be gte 2 //✔
- 1 should !be gte 1 //✘
- 1 should be gte 2 //✘
-```
-#### be | !be lt | lessThan
-```kotlin
- 1.5 should be lt 2 //✔
- 1 should be lessThan 2 //✔ same as above
- 1 should !be lt 1 //✔
- 1 should !be lt 0 //✔
- 1 should be lt 1 //✘
- 1 should be lt 0 //✘
-```
-#### be | !be lte | lessOrEqualThan
-```kotlin
- 1 should be lte 2 //✔
- 1 should be lessOrEqualThan 2 //✔ same as above
- 1 should be lte 1 //✔
- 1 should !be lte 0 //✔
- 1 should !be lte 1 //✘
- 1 should be lte 0 //✘
-```
-#### be | !be in range()
-```kotlin
- 1 should be in range(1..2) //✔
- 1 should be in range(1.0..2.0) //✔
- 1.1 should be in range(1..2) //✔
- 1 should !be in range(1.1..2.0) //✔
- 1.0 should !be in range(1.1..2.0) //✔
- 1.99 should !be in range(2..3) //✔
- 1.99 should !be in range(1.991..1.992) //✔
- 1 should !be in range(0.9..0.99); //✔
-```
-
-###String
-Matchers to use on string objects
-####have | !have length
-```kotlin
- "string" should have length 6 //✔
- "" should !have length 1 //✔
- "string" should have length 0 //✘
-```
-####start | !start with
-```kotlin
- "string" should start with "str" //✔
- "string" should !start with "tring" //✔
- "string" should start with "ing" //✘
-```
-####end | !end with
-```kotlin
- "string" should end with "ing" //✔
- "string" should !end with "in" //✔
- "string" should end with "strin" //✘
-```
-####contain | !contain any
-```kotlin
- "string" should contain any of("s", "t", "a") //✔
- "string" should !contain any of("a", "b", "c") //✔
- "string" should contain any of("a", "b", "c") //✘
-```
-####contain | !contain all
-```kotlin
- "string" should contain all of("s", "t", "r") //✔
- "string" should !contain all of("a", "b", "c") //✔
- "string" should contain all of("s", "t", "a") //✘
-```
-
-###Collection
-Matchers to use on ```Collection<T>``` object
-####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 //✘
-```
-####have | !have size
-```kotlin
- listOf(1,2,3) should have size 3 //✔
- listOf(1,2,3) should !have size 4 //✔
- listOf(1,2,3) should !have size 3 //✘
- listOf(1,2,3) should have size 2 //✘
-```
-
-###Map
-Matchers to use on ```Map<K,V>``` object
-####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
-####fail | !fail with
-```kotlin
- {throw NullPointerException()} should fail with NullPointerException() //✔
- {1 should be equal 1} should !fail with AssertionError() //✔
- {"string" should be equal "string"} should fail with AssertionError() //✘
-```
-
+###For more examples see [wiki](https://github.com/danseid/katchers/wiki)
@@ -25,14 +25,19 @@ class MapContainMatcher<K,V>(override val target: Map<K,V>): Matcher<Map<K,V>>{
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")
+}
+class MapHaveMatcher<K,V>(override val target: Map<K,V>): Matcher<Map<K,V>>{
+ fun size(size: Int) = if(target.size != size) fail("$target should have size $size", "$target has size ${target.size}")
+}
+class MapNotHaveMatcher<K,V>(override val target: Map<K,V>): Matcher<Map<K,V>>{
+ fun size(size: Int) = if(target.size == size) fail("$target should not have size $size", "$target has size ${target.size}")
}
@@ -20,7 +20,6 @@ package org.katchers
* @since 2012/11/21
*/
-// 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)
@@ -41,6 +40,7 @@ public fun String.must(val verb: NOTCONTAIN): StringNotContainMatcher = StringNo
public inline fun Number.must(val verb: BE): NumberBeMatcher = NumberBeMatcher(this)
public inline fun Number.must(val verb: NOTBE): NumberNotBeMatcher = NumberNotBeMatcher(this)
+
//() -> 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)
@@ -51,6 +51,8 @@ public fun <T>Collection<T>.must(verb: NOTCONTAIN): CollectionNotContainMatcher<
public fun <T>Collection<T>.must(verb: HAVE): CollectionHaveMatcher<T> = CollectionHaveMatcher<T>(this)
public fun <T>Collection<T>.must(verb: NOTHAVE): CollectionNotHaveMatcher<T> = CollectionNotHaveMatcher<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)
+//Map
+public fun <K,V>Map<K,V>.must(verb: CONTAIN): MapContainMatcher<K,V> = MapContainMatcher<K,V>(this)
+public fun <K,V>Map<K,V>.must(verb: NOTCONTAIN): MapNotContainMatcher<K,V> = MapNotContainMatcher<K,V>(this)
+public fun <K,V>Map<K,V>.must(verb: HAVE): MapHaveMatcher<K,V> = MapHaveMatcher<K,V>(this)
+public fun <K,V>Map<K,V>.must(verb: NOTHAVE): MapNotHaveMatcher<K,V> = MapNotHaveMatcher<K,V>(this)
@@ -55,9 +55,10 @@ public fun <T>Collection<T>.should(verb: HAVE): CollectionHaveMatcher<T> = Colle
public fun <T>Collection<T>.should(verb: NOTHAVE): CollectionNotHaveMatcher<T> = CollectionNotHaveMatcher<T>(this)
//Map
-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 <K,V>Map<K,V>.should(verb: CONTAIN): MapContainMatcher<K,V> = MapContainMatcher<K,V>(this)
+public fun <K,V>Map<K,V>.should(verb: NOTCONTAIN): MapNotContainMatcher<K,V> = MapNotContainMatcher<K,V>(this)
+public fun <K,V>Map<K,V>.should(verb: HAVE): MapHaveMatcher<K,V> = MapHaveMatcher<K,V>(this)
+public fun <K,V>Map<K,V>.should(verb: NOTHAVE): MapNotHaveMatcher<K,V> = MapNotHaveMatcher<K,V>(this)
@@ -25,31 +25,43 @@ 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 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
+ { mapOf(1 to 2, 2 to 3, 4 to 5) should !contain item (1 to 2) } should fail with assertionError
+ }
+
+ test fun mapShouldhaveSize() {
+ mapOf(1 to 1) should have size 1
+ mapOf(1 to 1) should !have size 0
+
+ { mapOf(1 to 1) should have size 0 } should fail with assertionError
+ { mapOf(1 to 1) should have size 2 } should fail with assertionError
+ { mapOf(1 to 1) should !have size 1 } 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) 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
+ { 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) 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
+ { mapOf(1 to 2) should !contain value 2 } should fail with assertionError
}

0 comments on commit 384f103

Please sign in to comment.