By including the EitherValue
trait in our mixin, we get an implicit conversion of the Either
monad. This implicit conversion adds a value method to both left and right of Either
. This allows us to do a left.value
and right.value
. The value
method either returns the selected value from a defined Either
or alternatively throws a TestFailedException
in case the Either
is not defined.
Using this, we are able to test in a single expression, both that the Either
should be left or right (that is, it is defined) and also that it has a certain expected value. Let's look at an example:
firstEither.right.value should be < 5 secondEither.left.value should be ("Big Ploblemo!")
The same can be done using assertions:
assert(firstEither.right.value should be < 5) assert(secondEither.left.value === "Big Ploblemo!")
If we were using a standard right.get
and left.get
method on the Either
to get the value, we would get a NoSuchElementException.
Therefore, using the EitherValue...