Skip to content

Commit

Permalink
collection have size added
Browse files Browse the repository at this point in the history
  • Loading branch information
danseid committed Dec 7, 2012
1 parent 2d1b53f commit 0e95098
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -155,6 +155,13 @@ Matchers to use on ```Collection<T>``` object
listOf("1", "2") should contain item "2" // listOf("1", "2") should contain item "2" //
listOf(1, 2, 3) should contain item 4 // 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 ###Map
Matchers to use on ```Map<K,V>``` object Matchers to use on ```Map<K,V>``` object
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/org/katchers/CollectionMatcher.kt
Expand Up @@ -27,4 +27,12 @@ class CollectionContainMatcher<T>(override val target: Collection<T>): Matcher<C


class CollectionNotContainMatcher<T>(override val target: Collection<T>): Matcher<Collection<T>>{ class CollectionNotContainMatcher<T>(override val target: Collection<T>): Matcher<Collection<T>>{
fun item(el: T) = if(target.contains(el)) fail("$target should not contain $el", "$target contains $el") fun item(el: T) = if(target.contains(el)) fail("$target should not contain $el", "$target contains $el")
}

class CollectionHaveMatcher<T>(override val target: Collection<T>): Matcher<Collection<T>>{
fun size(size: Int) = if(target.size != size) fail("$target should have size $size", "$target has size ${target.size}")
}

class CollectionNotHaveMatcher<T>(override val target: Collection<T>): Matcher<Collection<T>>{
fun size(size: Int) = if(target.size == size) fail("$target should not have size $size", "$target has size ${target.size}")
} }
2 changes: 2 additions & 0 deletions src/main/kotlin/org/katchers/Must.kt
Expand Up @@ -48,6 +48,8 @@ public fun <T: () -> Any>T.must(verb: FAIL): FunctionFailMatcher<T> = FunctionFa
//Collection //Collection
public fun <T>Collection<T>.must(verb: CONTAIN): CollectionContainMatcher<T> = CollectionContainMatcher<T>(this) public fun <T>Collection<T>.must(verb: CONTAIN): CollectionContainMatcher<T> = CollectionContainMatcher<T>(this)
public fun <T>Collection<T>.must(verb: NOTCONTAIN): CollectionNotContainMatcher<T> = CollectionNotContainMatcher<T>(this) public fun <T>Collection<T>.must(verb: NOTCONTAIN): CollectionNotContainMatcher<T> = CollectionNotContainMatcher<T>(this)
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 //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: CONTAIN): MapContainMatcher<R,T> = MapContainMatcher<R,T>(this)
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/org/katchers/Should.kt
Expand Up @@ -51,6 +51,8 @@ public fun <T: () -> Any>T.should(verb: FAIL): FunctionFailMatcher<T> = Function
//Collection //Collection
public fun <T>Collection<T>.should(verb: CONTAIN): CollectionContainMatcher<T> = CollectionContainMatcher<T>(this) public fun <T>Collection<T>.should(verb: CONTAIN): CollectionContainMatcher<T> = CollectionContainMatcher<T>(this)
public fun <T>Collection<T>.should(verb: NOTCONTAIN): CollectionNotContainMatcher<T> = CollectionNotContainMatcher<T>(this) public fun <T>Collection<T>.should(verb: NOTCONTAIN): CollectionNotContainMatcher<T> = CollectionNotContainMatcher<T>(this)
public fun <T>Collection<T>.should(verb: HAVE): CollectionHaveMatcher<T> = CollectionHaveMatcher<T>(this)
public fun <T>Collection<T>.should(verb: NOTHAVE): CollectionNotHaveMatcher<T> = CollectionNotHaveMatcher<T>(this)


//Map //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: CONTAIN): MapContainMatcher<R,T> = MapContainMatcher<R,T>(this)
Expand Down
49 changes: 49 additions & 0 deletions src/test/kotlin/CollectionMatcherTest.kt
@@ -0,0 +1,49 @@
/*
* 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 CollectionMatcherTest {

test fun collectionShouldContainItem() {
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
}

test fun collectionShouldHaveSize() {
listOf(1,2,3) should have size 3
listOf(1).drop(1) should have size 0
{listOf(1,2,3) should have size 2} should fail with assertionError
{listOf(1,2,3) should have size 4} should fail with assertionError

listOf(1,2,3) should !have size 4
listOf(1).drop(1) should !have size 1
{listOf(1,2,3) should !have size 3} should fail with assertionError
{listOf(1).drop(1) should !have size 0} should fail with assertionError

}

}

0 comments on commit 0e95098

Please sign in to comment.