Katchers - matcher library


#1

A simple matcher library to build test expressions in kotlin tests. For better understanding check out the examples: http://github.com/danseid/katchers/wiki/Quick-reference


#2

Looks cool. Do the expressions simply throw an exception when they fail? How would you go about integrating this with something like jUnit?


#3

Yes, i'm using kotlin.test.fail() function, that is throwing an AssertionError. So you are able to use katchers with jUnit.

jUnit Test

import org.junit.Test as test
import org.katchers.*
 
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
 
    }
 
}

#4

Perfect.


#5

Looks really cool!