com.github.spoptchev.kotlin.preconditions.PreconditionDSL.kt Maven / Gradle / Ivy
package com.github.spoptchev.kotlin.preconditions
import com.github.spoptchev.kotlin.preconditions.matcher.*
@DslMarker
annotation class PreconditionDSLMarker
typealias EvaluationMethod = (value: Boolean, lazyMessage: () -> Any) -> Unit
@PreconditionDSLMarker
class PreconditionContext : CollectionMatcher, ComparableMatcher, MapMatcher, StringMatcher, ObjectMatcher {
infix fun Precondition.and(precondition: Precondition) = AndPrecondition(this, precondition)
infix fun Precondition.or(precondition: Precondition) = OrPrecondition(this, precondition)
fun not(precondition: Precondition) = NotPrecondition(precondition)
}
fun requireThat(value: T, label: String = "value", preconditionContext: PreconditionContext.() -> Precondition): T =
require(value, label, preconditionContext)
fun require(value: T, label: String = "value", preconditionContext: PreconditionContext.() -> Precondition): T =
assertThat(value, label, ::require, preconditionContext)
fun checkThat(value: T, label: String = "value", preconditionContext: PreconditionContext.() -> Precondition): T =
check(value, label, preconditionContext)
fun check(value: T, label: String = "value", preconditionContext: PreconditionContext.() -> Precondition): T =
assertThat(value, label, ::check, preconditionContext)
private fun assertThat(value: T, label: String, evaluate: EvaluationMethod, preconditionContext: PreconditionContext.() -> Precondition): T =
preconditionContext(PreconditionContext()).let { Assertion(value, label, evaluate).run(it) }
© 2015 - 2024 Weber Informatics LLC | Privacy Policy