From e093bbde416ba1f81dd3c59895757a726a4c4958 Mon Sep 17 00:00:00 2001 From: Kabir Idris Date: Tue, 25 Sep 2018 18:10:26 +0300 Subject: [PATCH] added Convenience functions for Ior NonEmptyChain (#2522) --- core/src/main/scala/cats/data/Ior.scala | 7 ++++++- tests/src/test/scala/cats/tests/IorSuite.scala | 14 +++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/cats/data/Ior.scala b/core/src/main/scala/cats/data/Ior.scala index 97d6afa780..31893ff984 100644 --- a/core/src/main/scala/cats/data/Ior.scala +++ b/core/src/main/scala/cats/data/Ior.scala @@ -138,7 +138,7 @@ sealed abstract class Ior[+A, +B] extends Product with Serializable { ) } -object Ior extends IorInstances with IorFunctions { +object Ior extends IorInstances with IorFunctions with IorFunctions2 { final case class Left[+A](a: A) extends (A Ior Nothing) final case class Right[+B](b: B) extends (Nothing Ior B) final case class Both[+A, +B](a: A, b: B) extends (A Ior B) @@ -354,3 +354,8 @@ private[data] sealed trait IorFunctions { case Right(b) => right(b) } } + +private[data] sealed trait IorFunctions2{ + def leftNec[A, B](a: A): IorNec[A, B] = Ior.left(NonEmptyChain.one(a)) + def bothNec[A, B](a: A, b: B): IorNec[A, B] = Ior.both(NonEmptyChain.one(a), b) +} diff --git a/tests/src/test/scala/cats/tests/IorSuite.scala b/tests/src/test/scala/cats/tests/IorSuite.scala index ac84db49ca..9260ae4ffa 100644 --- a/tests/src/test/scala/cats/tests/IorSuite.scala +++ b/tests/src/test/scala/cats/tests/IorSuite.scala @@ -3,7 +3,7 @@ package tests import cats.kernel.laws.discipline.SemigroupTests import cats.laws.discipline.{BifunctorTests, BitraverseTests, SemigroupalTests, MonadErrorTests, SerializableTests, TraverseTests} -import cats.data.{Ior, NonEmptyList, EitherT} +import cats.data.{Ior,NonEmptyChain, NonEmptyList, EitherT} import cats.laws.discipline.arbitrary._ import org.scalacheck.Arbitrary._ @@ -221,12 +221,24 @@ class IorSuite extends CatsSuite { } } + test("leftNec") { + forAll { (x: String) => + Ior.leftNec(x).left should === (Some(NonEmptyChain.one(x))) + } + } + test("bothNel") { forAll { (x: Int, y: String) => Ior.bothNel(y, x).onlyBoth should === (Some((NonEmptyList.one(y), x))) } } + test("bothNec") { + forAll { (x: Int, y: String) => + Ior.bothNec(y, x).onlyBoth should === (Some((NonEmptyChain.one(y), x))) + } + } + test("getOrElse consistent with Option getOrElse") { forAll { (x: Int Ior String, default: String) => x.getOrElse(default) should === (x.toOption.getOrElse(default))