Skip to content

Commit

Permalink
Support right-bias of Either in EitherValues
Browse files Browse the repository at this point in the history
* scala/scala#5135 - `Either` became right-biased
  in Scala 2.12
* scala/scala#6682 - `either.right` becomes a
  deprecated field with Scala 2.13

With Scala 2.13, it's no longer possible to test an `Either`'s right value
using ScalaTest's `EitherValues` without getting a deprecating warning -
ie writing:

```
either.right.value should be > 9
```

will give you this on compile:

```
method right in class Either is deprecated (since 2.13.0): Either is now
right-biased, use methods directly on Either
```

Given that `Either` is now right-biased, this change gives us the ability
to write:

```
either.value should be > 9
```
  • Loading branch information
rtyley committed Feb 13, 2020
1 parent 9d8061c commit ab978ef
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions scalatest/src/main/scala/org/scalatest/EitherValues.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ import org.scalatest.exceptions.StackDepthException
import org.scalatest.exceptions.TestFailedException

/**
* Trait that provides an implicit conversion that adds <code>left.value</code> and <code>right.value</code> methods
* to <code>Either</code>, which will return the selected value of the <code>Either</code> if defined,
* or throw <code>TestFailedException</code> if not.
* Trait that provides an implicit conversion that adds <code>value</code>, <code>left.value</code> and
* <code>right.value</code> methods to <code>Either</code>, which will return the selected value of the
* <code>Either</code> if defined, or throw <code>TestFailedException</code> if not.
*
* <p>
* This construct allows you to express in one statement that an <code>Either</code> should be <em>left</em> or <em>right</em>
* and that its value should meet some expectation. Here's are some examples:
* </p>
*
* <pre class="stHighlight">
* either1.value should be &gt; 9 // Either is right-biased since Scala 2.12)
* either1.right.value should be &gt; 9
* either2.left.value should be ("Muchas problemas")
* </pre>
Expand All @@ -39,6 +40,7 @@ import org.scalatest.exceptions.TestFailedException
* </p>
*
* <pre class="stHighlight">
* assert(either1.value &gt; 9)
* assert(either1.right.value &gt; 9)
* assert(either2.left.value === "Muchas problemas")
* </pre>
Expand Down Expand Up @@ -84,6 +86,14 @@ trait EitherValues {

import scala.language.implicitConversions

/**
* Implicit conversion that adds a (right) biased <code>value</code> method to <code>Either</code>
* (which since Scala 2.12 has been right-biased).
*
* @param either the <code>Either</code> on which to add the <code>value</code> method
*/
implicit def convertEitherToValuable[L, R](either: Either[L, R])(implicit pos: source.Position): RightValuable[L, R] = new RightValuable(either.right, pos)

/**
* Implicit conversion that adds a <code>value</code> method to <code>LeftProjection</code>.
*
Expand Down Expand Up @@ -141,7 +151,7 @@ trait EitherValues {

/**
* Returns the <code>Right</code> value contained in the wrapped <code>RightProjection</code>, if defined as a <code>Right</code>, else throws <code>TestFailedException</code> with
* a detail message indicating the <code>Either</code> was defined as a <code>Right</code>, not a <code>Left</code>.
* a detail message indicating the <code>Either</code> was defined as a <code>Left</code>, not a <code>Right</code>.
*/
def value: R = {
try {
Expand Down

0 comments on commit ab978ef

Please sign in to comment.