strikt.arrow.Validated.kt Maven / Gradle / Ivy
package strikt.arrow
import arrow.core.Validated
import strikt.api.Assertion
/**
* Asserts that the [Validated] is [Validated.Valid]
* @return Assertion builder over the same subject that is now known to be
* a [Validated.Valid].
*/
@Suppress("UNCHECKED_CAST")
fun Assertion.Builder>.isValid() =
assert("should be Valid") {
it.fold({ fail() }, { pass() })
} as Assertion.Builder>
/**
* Asserts that the [Validated] is [Validated.Valid] and that it contains the exact value
* @param value Value to compare to the [Validated]'s wrapped value
* @return Assertion builder over the same subject that is now known to be
* a [Validated.Valid].
*/
@Suppress("UNCHECKED_CAST")
infix fun Assertion.Builder>.isValid(value: A) =
assert("should be Valid") { subject ->
subject.fold({ fail() }, { if (it == value) pass() else fail() })
} as Assertion.Builder>
/**
* Unwraps the containing value of the [Validated.Valid]
* @return Assertion builder over the unwrapped subject
*/
@Deprecated("Use value instead", replaceWith = ReplaceWith("value"))
val Assertion.Builder>.a: Assertion.Builder
get() = value
/**
* Unwraps the containing value of the [Validated.Valid]
* @return Assertion builder over the unwrapped subject
* @see Validated.Valid.value
*/
val Assertion.Builder>.value: Assertion.Builder
@JvmName("validatedValidValue")
get() = get("valid value", Validated.Valid::value)
/**
* Asserts that the [Validated] is [Validated.Invalid]
* @return Assertion builder over the same subject that is now known to be
* a [Validated.Invalid].
*/
@Suppress("UNCHECKED_CAST")
fun Assertion.Builder>.isInvalid() =
assert("should be Invalid") { subject ->
subject.fold({ pass() }, { fail() })
} as Assertion.Builder>
/**
* Asserts that the [Validated] is [Validated.Invalid] and that it contains the exact value
* @param value Value to compare to the [Validated]'s wrapped value
* @return Assertion builder over the same subject that is now known to be
* a [Validated.Invalid].
*/
@Suppress("UNCHECKED_CAST")
infix fun Assertion.Builder>.isInvalid(value: E) =
assert("should be Valid") { subject ->
subject.fold({ if (it == value) pass() else fail() }, { fail() })
} as Assertion.Builder>
/**
* Unwraps the containing value of the [Validated.Invalid]
* @return Assertion builder over the unwrapped subject
*/
@Deprecated("Use value instead", replaceWith = ReplaceWith("value"))
val Assertion.Builder>.e: Assertion.Builder
get() = value
/**
* Unwraps the containing value of the [Validated.Invalid]
* @return Assertion builder over the unwrapped subject
* @see Validated.Invalid.value
*/
val Assertion.Builder>.value: Assertion.Builder
@JvmName("validatedInvalidValue")
get() = get("invalid value", Validated.Invalid::value)