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

.scalaz-scalatest_2.11.0.3.0.source-code.ValidationValues.scala Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package org.typelevel.scalatest

import org.scalatest.exceptions.TestFailedException
import org.scalatest.typelevel.patch.MonkeyPatch

import scalaz.{ Validation, Success, Failure}
import scalaz.syntax.validation._

trait ValidationValues {
  import scala.language.implicitConversions

  /**
   * Implicit conversion that adds a `value` method to `scalaz.Validation`
   *
   * @param validation the `scalaz.Validation` on which to add the `value` method
   */
  implicit def convertValidationToValidationable[E, T](validation: Validation[E, T]): Validationable[E, T] = new Validationable(validation)

  // TODO: Fix me as a proper repl session example
  /**
   * Container class for matching success
   * type stuff in `scalaz.Validation` containers,
   * similar to `org.scalatest.OptionValues.Valuable`
   *
   * Meant to allow you to make statements like:
   *
   * 
   *   result.value should be > 15
   * 
* * Where it only matches if result is `Success(9)` * * Otherwise your test will fail, indicating that it was left instead of right * * @param validation A `scalaz.Validation` object to try converting to a `Validationable` * * @see org.scalatest.OptionValues.Valuable */ class Validationable[E, T](validation: Validation[E, T]) { def value: T = { validation match { case Success(right) => right case Failure(left) => throw new TestFailedException(sde => Some(s"$left is Failure, expected Success."), None, MonkeyPatch.getStackDepthFun("ValidationValues.scala", "value")) } } /** * Allow .leftValue on an Validation to extract the left side. Like .value, but for the left. */ def leftValue: E = { validation match { case Success(right) => throw new TestFailedException(sde => Some(s"$right is Success, expected Failure."), None, MonkeyPatch.getStackDepthFun("ValidationValues.scala", "leftValue")) case Failure(left) => left } } } } // TODO: Fix me as a proper repl session example /** * * Companion object for easy importing – rather than mixing in – to allow `ValidationValues` operations. * * This will permit you to invoke a `value` method on an instance of a `scalaz.Validation`, * which attempts to unwrap the right validation * * Similar to `org.scalatest.OptionValues.Valuable` * * Meant to allow you to make statements like: * *
 *   result.value should be > 15
 * 
* * Where it only matches if result is `Success(9)` * * Otherwise your test will fail, indicating that it was left instead of right * * @see org.scalatest.OptionValues.Valuable */ object ValidationValues extends ValidationValues




© 2015 - 2025 Weber Informatics LLC | Privacy Policy