|
| 1 | +package com.baeldung.scala.optiontoeither |
| 2 | + |
| 3 | +import org.scalatest.matchers.should.Matchers |
| 4 | +import org.scalatest.wordspec.AnyWordSpec |
| 5 | + |
| 6 | +sealed trait Error |
| 7 | +case object EmptyOptionValue extends Error |
| 8 | + |
| 9 | +class OptionToEitherUnitTest extends AnyWordSpec with Matchers { |
| 10 | + |
| 11 | + def usingIfElse(option: Option[String]): Either[Error, String] = { |
| 12 | + if (option.isDefined) Right(option.get) else Left(EmptyOptionValue) |
| 13 | + } |
| 14 | + |
| 15 | + def usingPatternMatch(option: Option[String]): Either[Error, String] = { |
| 16 | + option match { |
| 17 | + case Some(value) => Right(value) |
| 18 | + case None => Left(EmptyOptionValue) |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + def usingToRight(option: Option[String]): Either[Error, String] = { |
| 23 | + option.toRight(EmptyOptionValue) |
| 24 | + } |
| 25 | + |
| 26 | + def usingCond(option: Option[String]): Either[Error, String] = { |
| 27 | + Either.cond(option.nonEmpty, option.get, EmptyOptionValue) |
| 28 | + } |
| 29 | + |
| 30 | + def usingFold(option: Option[String]): Either[Error, String] = { |
| 31 | + option.fold(Left(EmptyOptionValue))(Right(_)) |
| 32 | + } |
| 33 | + |
| 34 | + def usingMap(option: Option[String]): Either[Error, String] = { |
| 35 | + option.map(Right(_)).getOrElse(Left(EmptyOptionValue)) |
| 36 | + } |
| 37 | + |
| 38 | + "Option" should { |
| 39 | + "be converted to Either using if else" in { |
| 40 | + val either = usingIfElse(Option("Baeldung")) |
| 41 | + either shouldBe Right("Baeldung") |
| 42 | + val left = usingIfElse(None) |
| 43 | + left shouldBe Left(EmptyOptionValue) |
| 44 | + } |
| 45 | + |
| 46 | + "be converted to Either using pattern matching" in { |
| 47 | + val either = usingPatternMatch(Option("Baeldung")) |
| 48 | + either shouldBe Right("Baeldung") |
| 49 | + val left = usingPatternMatch(None) |
| 50 | + left shouldBe Left(EmptyOptionValue) |
| 51 | + } |
| 52 | + |
| 53 | + "be converted to Either using usingToRight" in { |
| 54 | + val either = usingToRight(Option("Baeldung")) |
| 55 | + either shouldBe Right("Baeldung") |
| 56 | + val left = usingToRight(None) |
| 57 | + left shouldBe Left(EmptyOptionValue) |
| 58 | + } |
| 59 | + |
| 60 | + "be converted to Either using usingCond" in { |
| 61 | + val either = usingCond(Option("Baeldung")) |
| 62 | + either shouldBe Right("Baeldung") |
| 63 | + val left = usingCond(None) |
| 64 | + left shouldBe Left(EmptyOptionValue) |
| 65 | + } |
| 66 | + |
| 67 | + "be converted to Either using fold" in { |
| 68 | + val either = usingFold(Option("Baeldung")) |
| 69 | + either shouldBe Right("Baeldung") |
| 70 | + val left = usingFold(None) |
| 71 | + left shouldBe Left(EmptyOptionValue) |
| 72 | + } |
| 73 | + |
| 74 | + "be converted to Either using map" in { |
| 75 | + val either = usingMap(Option("Baeldung")) |
| 76 | + either shouldBe Right("Baeldung") |
| 77 | + val left = usingMap(None) |
| 78 | + left shouldBe Left(EmptyOptionValue) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments