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

.scalaz-scalatest_2.11.0.3.0.source-code.DisjunctionValues.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.{ -\/, \/, \/- }
import scalaz.syntax.either._

trait DisjunctionValues {
  import scala.language.implicitConversions

  /**
   * Implicit conversion that adds a `value` method to `scalaz.\/`
   *
   * @param disjunction the `scalaz.\/` on which to add the `value` method
   */
  implicit def convertDisjunctionToDisjunctionated[E, T](disjunction: E \/ T) = new Disjunctionable(disjunction)

  // TODO: Fix me as a proper repl session example
  /**
   * Container class for matching success
   * type stuff in `scalaz.\/` 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 `\/-(9)` * * Otherwise your test will fail, indicating that it was left instead of right * * @param disjunction A `scalaz.\/` object to try converting to a `Disjunctionable` * * @see org.scalatest.OptionValues.Valuable */ class Disjunctionable[E, T](disjunction: E \/ T) { def value: T = { disjunction match { case \/-(right) => right case -\/(left) => throw new TestFailedException(sde => Some(s"$left is -\\/, expected \\/-."), None, MonkeyPatch.getStackDepthFun("DisjunctionValues.scala", "value")) } } /** * Allow .leftValue on an \/ to extract the left side. Like .value, but for the left. */ def leftValue: E = { disjunction match { case \/-(right) => throw new TestFailedException(sde => Some(s"$right is \\/-, expected -\\/."), None, MonkeyPatch.getStackDepthFun("DisjunctionValues.scala", "leftValue")) case -\/(left) => left } } } } // TODO: Fix me as a proper repl session example /** * * Companion object for easy importing – rather than mixing in – to allow `DisjunctionValues` operations. * * This will permit you to invoke a `value` method on an instance of a `scalaz.\/`, * which attempts to unwrap the right disjunction * * 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 `\/-(9)` * * Otherwise your test will fail, indicating that it was left instead of right * * @see org.scalatest.OptionValues.Valuable */ object DisjunctionValues extends DisjunctionValues




© 2015 - 2025 Weber Informatics LLC | Privacy Policy