Skip to content

Option to either conversion #1340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.baeldung.scala.optiontoeither

import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

sealed trait Error
case object EmptyOptionValue extends Error

class OptionToEitherUnitTest extends AnyWordSpec with Matchers {

def usingIfElse(option: Option[String]): Either[Error, String] = {
if (option.isDefined) Right(option.get) else Left(EmptyOptionValue)
}

def usingPatternMatch(option: Option[String]): Either[Error, String] = {
option match {
case Some(value) => Right(value)
case None => Left(EmptyOptionValue)
}
}

def usingToRight(option: Option[String]): Either[Error, String] = {
option.toRight(EmptyOptionValue)
}

def usingCond(option: Option[String]): Either[Error, String] = {
Either.cond(option.nonEmpty, option.get, EmptyOptionValue)
}

def usingFold(option: Option[String]): Either[Error, String] = {
option.fold(Left(EmptyOptionValue))(Right(_))
}

def usingMap(option: Option[String]): Either[Error, String] = {
option.map(Right(_)).getOrElse(Left(EmptyOptionValue))
}

"Option" should {
"be converted to Either using if else" in {
val either = usingIfElse(Option("Baeldung"))
either shouldBe Right("Baeldung")
val left = usingIfElse(None)
left shouldBe Left(EmptyOptionValue)
}

"be converted to Either using pattern matching" in {
val either = usingPatternMatch(Option("Baeldung"))
either shouldBe Right("Baeldung")
val left = usingPatternMatch(None)
left shouldBe Left(EmptyOptionValue)
}

"be converted to Either using usingToRight" in {
val either = usingToRight(Option("Baeldung"))
either shouldBe Right("Baeldung")
val left = usingToRight(None)
left shouldBe Left(EmptyOptionValue)
}

"be converted to Either using usingCond" in {
val either = usingCond(Option("Baeldung"))
either shouldBe Right("Baeldung")
val left = usingCond(None)
left shouldBe Left(EmptyOptionValue)
}

"be converted to Either using fold" in {
val either = usingFold(Option("Baeldung"))
either shouldBe Right("Baeldung")
val left = usingFold(None)
left shouldBe Left(EmptyOptionValue)
}

"be converted to Either using map" in {
val either = usingMap(Option("Baeldung"))
either shouldBe Right("Baeldung")
val left = usingMap(None)
left shouldBe Left(EmptyOptionValue)
}
}

}