check

inline fun check(value: Boolean)

Throws an IllegalStateException if the value is false.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   var someState: String? = null
fun getStateValue(): String {
    val state = checkNotNull(someState) { "State must be set beforehand" }
    check(state.isNotEmpty()) { "State must be non-empty" }
    // ...
    return state
}

assertFailsWith<IllegalStateException> { getStateValue() }

someState = ""
assertFailsWith<IllegalStateException> { getStateValue() }

someState = "non-empty-state"
assertPrints(getStateValue(), "non-empty-state") 
   //sampleEnd
}

inline fun check(value: Boolean, lazyMessage: () -> Any)

Throws an IllegalStateException with the result of calling lazyMessage if the value is false.

Samples

import samples.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   var someState: String? = null
fun getStateValue(): String {
    val state = checkNotNull(someState) { "State must be set beforehand" }
    check(state.isNotEmpty()) { "State must be non-empty" }
    // ...
    return state
}

assertFailsWith<IllegalStateException> { getStateValue() }

someState = ""
assertFailsWith<IllegalStateException> { getStateValue() }

someState = "non-empty-state"
assertPrints(getStateValue(), "non-empty-state") 
   //sampleEnd
}