Kiwi: Fluent assertions for Kotlin projects
Kiwi is multiplatform projects written in pure Kotlin. Except testing it does not use external dependencies
Release
Kiwi is available in SNAPSHOT
repository for now
repositories {
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}
dependencies {
testImplementation 'org.fromsource:kiwi-core-jvm:0.0.2-SNAPSHOT'
}
Kiwi, Hello World!
Below snippet demonstrates usage of Kiwi assertions for standard type like String
@Test
fun `should say hallo to kiwi()` {
"Kiwi, Hello World!".should startWith "Kiwi" contain "Hello" endWith "!"
}
Collections
Kiwi supports List
, Set
and Map
@Test
fun `should combine operator in infix chain`() {
val animals = listOf("kiwi", "hedgehog", "flamingo", "humpback")
animals.should haveSize 4 contain "humpback" have1st "kiwi"
}
Numbers
as well as Byte
, Short
, Int
, Long
, Float
, Double
and JVM based numbers like BigDecimal
, BigNumber
@Test
fun `should check if numer is prime`() {
val number = 7
number.should
.bePositive()
.beGreaterThan(1)
.match { number -> (2 until number).all { number % it != 0} }
}
Dates
LocalDate
and LocalDateTime
@Test
fun `should check if date meets some conditions`() {
val date = LocalDate.of(2020, 11, 14)
date.should
.beInLeapYear()
.beInQ4()
.beBefore(date.plusDays(1))
}
Custom type
... even custom types are supported
data class Animal(val name: String, val weight: Int, val mammal: Boolean) {
fun heavy(): Boolean = weight > 10
}
val kiwi = Animal(name = "kiwi", weight = 1, mammal = true)
val hedgehog = Animal(name = "hedgehog", weight = 2, mammal = true)
val flamingo = Animal(name = "flamingo", weight = 5, mammal = false)
val humpback = Animal(name = "humpback", weight = 5000, mammal = true)
@Test
fun `should apply collection operators for list of custom object`() {
val animals = listOf(kiwi, hedgehog, flamingo, humpback)
animals.should
.haveSize(4)
.contain(flamingo)
.beSorted { it.weight }
.filtered { animal -> animal.mammal }
.matchAny { animal -> animal.heavy() }
}
JsonPath
If you need json path Kiwi will work too. See Roadmap & Future for more details
@Test
fun `should select json paths for json string`() {
val json = """{
"kiwi": "kotlin assertion library",
"github": {
"developers": ["john", "ben"]
}
}"""
json.should
.haveJsonPath("$.kiwi")
.haveJsonPath("$.kiwi", """"kotlin assertion library"""")
.haveJsonPath("$..developers", """["john", "ben"]""")
.haveJsonPath("$..developers[0]", """"john"""")
}
Mix of the operators
Last but not least you can mix all together Different types of operators e.g. Collection
, String
, Numbers
can be used fluently
@Test
fun `should mix different types of operators`() {
val animals = listOf(kiwi, hedgehog, flamingo, humpback)
animals.should
.contain(hedgehog) // Collection operator
.last().name // extract
.should
.match(Regex("[a-z]+")) // String operator
}
Build
Run command below
$ git clone [email protected]/from-source/kiwi.git
$ cd kiwi/
$ ./gradlew clean build
Roadmap & Future
JsonPath evaluation - in progress
Take a look at the JsonPathAcceptanceTest to see which json path selectors are available
@Test
fun `should select first book in the store`() {
val json = """{
"store": {
"details": {
"name": "Books & Books"
},
"books": [
{
"category": "science",
"author": "Douglas Hofstadter",
"title": "I Am a Strange Loop",
"price": 8.95
},
{
"category": "science fiction",
"author": "Stanislaw Lem",
"title": "Solaris",
"isbn": "978-0156027601",
"price": 10.99,
"details": "Published in 1961"
}]
}
}"""
json.should
.haveJsonPath("$.store.details") // check if path exists
.haveJsonPath("$..books[1].category", "science fiction") // check value of path
.haveJsonPath("$..books[0]", """{
"category": "science",
"author": "Douglas Hofstadter",
"title": "I Am a Strange Loop",
"price": 8.95
}""")
}