All Downloads are FREE. Search and download functionalities are using the official Maven repository.

strikt.arrow.Either.kt Maven / Gradle / Ivy

package strikt.arrow

import arrow.core.Either
import strikt.api.Assertion

/**
 * Asserts that the [Either] is [Either.Right]
 * @return Assertion builder over the same subject that is now known to be
 * a [Either.Right].
 */
@Suppress("UNCHECKED_CAST")
fun  Assertion.Builder>.isRight() =
  assert("should be Right") { subject ->
    when (subject) {
      is Either.Right -> pass()
      is Either.Left -> fail()
    }
  } as Assertion.Builder>

/**
 * Asserts that the [Either] is [Either.Right] and that it contains the exact value
 * @param value Value to compare to the [Either]'s wrapped value
 * @return Assertion builder over the same subject that is now known to be
 * a [Either.Right].
 */
@Suppress("UNCHECKED_CAST")
infix fun  Assertion.Builder>.isRight(value: R) =
  assert("should be Right($value)") { subject ->
    when (subject) {
      is Either.Right ->
        if (subject.value == value) {
          pass()
        } else {
          fail()
        }

      else -> fail()
    }
  } as Assertion.Builder>

/**
 * Unwraps the containing value of the [Either.Right]
 * @return Assertion builder over the unwrapped subject
 */
@Deprecated("Use value instead", replaceWith = ReplaceWith("value"))
val  Assertion.Builder>.b: Assertion.Builder
  get() = value

/**
 * Unwraps the containing value of the [Either.Right]
 * @return Assertion builder over the unwrapped subject
 * @see Either.Right.value
 */
val  Assertion.Builder>.value: Assertion.Builder
  @JvmName("EitherRightValue")
  get() = get("right value", Either.Right::value)

/**
 * Asserts that the [Either] is [Either.Left]
 * @return Assertion builder over the same subject that is now known to be
 * a [Either.Left].
 */
@Suppress("UNCHECKED_CAST")
fun  Assertion.Builder>.isLeft() =
  assert("should be Left") { subject ->
    when {
      subject.isRight() -> fail()
      subject.isLeft() -> pass()
    }
  } as Assertion.Builder>

/**
 * Asserts that the [Either] is [Either.Left] and that it contains the exact value
 * @param value Value to compare to the [Either]'s wrapped value
 * @return Assertion builder over the same subject that is now known to be
 * a [Either.Left].
 */
@Suppress("UNCHECKED_CAST")
infix fun  Assertion.Builder>.isLeft(value: L) =
  assert("should be Left($value)") { subject ->
    when (subject) {
      is Either.Left -> {
        if (subject.value == value) {
          pass()
        } else {
          fail()
        }
      }
      else -> fail()
    }
  } as Assertion.Builder>

/**
 * Unwraps the containing value of the [Either.Left]
 * @return Assertion builder over the unwrapped subject
 * @see Either.Left.value
 */
@Deprecated("Use value instead", replaceWith = ReplaceWith("value"))
val  Assertion.Builder>.a: Assertion.Builder
  get() = value

/**
 * Unwraps the containing value of the [Either.Left]
 * @return Assertion builder over the unwrapped subject
 */
val  Assertion.Builder>.value: Assertion.Builder
  @JvmName("EitherLeftValue")
  get() = get("left value", Either.Left::value)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy