Skip to content

Commit

Permalink
Merge pull request #1127 from johnynek/oscar/tryharder
Browse files Browse the repository at this point in the history
Add Xor.toTry
  • Loading branch information
ceedubs authored Jun 15, 2016
2 parents 3c8f03c + 2eebf31 commit ae309c1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/data/Xor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ sealed abstract class Xor[+A, +B] extends Product with Serializable {

def toList: List[B] = fold(_ => Nil, _ :: Nil)

def toTry(implicit ev: A <:< Throwable): Try[B] = fold(a => Failure(ev(a)), Success(_))

def toValidated: Validated[A,B] = fold(Validated.Invalid.apply, Validated.Valid.apply)

/** Returns a [[ValidatedNel]] representation of this disjunction with the `Left` value
Expand Down
8 changes: 8 additions & 0 deletions tests/src/test/scala/cats/tests/XorTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ class XorTests extends CatsSuite {
}
}

test("toTry then fromTry is identity") {
implicit def eqTh: Eq[Throwable] = Eq.allEqual

forAll { (x: Throwable Xor String) =>
Xor.fromTry(x.toTry) should === (x)
}
}

test("isLeft consistency") {
forAll { (x: Int Xor String) =>
x.isLeft should === (x.toEither.isLeft)
Expand Down

0 comments on commit ae309c1

Please sign in to comment.