Skip to content

fardavide/Assert4K

Repository files navigation

Assert4k

METHOD LINE COMPLEXITY

Assert4k is a Kotlin Multiplatform assertion DSL. It currently supports Jvm and Js only, but many other platform will come soon.

The focus of Assert4k is to take advantage of the power of Kotlin for create a beautiful human readable assertion DSL. It covers a big variety of use cases and ensure you to able to write your assertion in the same way you would write to a friend. Forgot about commas and parenthesis!

All the code is available under the same package assert4k.*, include it in your IDE settings for avoid to search what you need between the suggestions.

( Preferences -> Editor -> Code Style -> Kotlin -> Packages to Use with import '* )

assert that developing `is` `fun`

Usage

Include in your Gradle dependencies:

testImplementation("studio.forface:assert4k:<last_version>")

and start having fun!

There are many alternatives for fits everyone's tastes. Like:

  • `null` or Null
  • `not equals` or notEquals

For fail with a custom message, use / "This is my message", it applies to almost every assertion.

assert that greeting equals "good night" / "It's too early for sleep!!" 

Extensibility - custom assertions

infix fun Asserter<User>.`older than`(age: Int) =
    assert that (value.age > age) / { 
        "User too young: Expected age to be greater than <$age> but is <$value>" 
    }

assert that someUser `older than` 15
assert that someUser *{
    it `older than` 15
    +name equals "Davide"
}

Some assertions

General (any.kt and null.kt)
assert that MyClass("Hello") equals MyClass("Hello")

assert that today.isSunny

assert that { tomorrow.isSunny }

assert that myObject `is` `null`
Strings (string.kt)
assert that "bye bye" contains "bye"

assert that "bye bye" `contains no case` "BYE"

assert that "bye bye" contains ("BYE" no case)
Numbers (number.kt)
assert that 5 `greater than` 5.1f

assert that 5 equals "5"

assert that 1.5 between 1 .. 2

assert that 1.5 between 1.4 .. 1.6
Collections (collection.kt)
assert that listOf(1, 2, 3) contains 3

assert that emptyList<Int> `is` empty

assert that null `is` empty or `null`
Catch exception(fail.kt)
assert that fails { throw Exception() }

assert that fails<IllegalStateException> { throw IllegalStateException() }

assert that fails { throw Exception("Oops!") } with "Oops!"

assert that coFails {
  delay(100)
  throw Exception()
}
Multiple assertions
assert that User(name = "Davide", age = 29) *{
    it equals User("Davide", 29)

    +name `equals no case` "davide"
    // invoke operator can be used on some types, like number, which already
    // have the `unaryPlus` as language feature
    +age() between 25 .. 35
}

About

Kotlin multiplatform assertions

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages