Skip to content

Commit

Permalink
Merge pull request #1046 from kailuowang/ApplicativeErrorSyntax
Browse files Browse the repository at this point in the history
summon `ApplicativeErrorSyntax` for `F[_]` instead of `F[_, _]`
  • Loading branch information
non committed May 20, 2016
2 parents c134847 + a77beec commit 207fda4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
5 changes: 3 additions & 2 deletions core/src/main/scala/cats/syntax/applicativeError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ trait ApplicativeErrorSyntax {
implicit def applicativeErrorIdSyntax[E](e: E): ApplicativeErrorIdOps[E] =
new ApplicativeErrorIdOps(e)

implicit def applicativeErrorSyntax[F[_, _], E, A](fa: F[E, A])(implicit F: ApplicativeError[F[E, ?], E]): ApplicativeErrorOps[F[E, ?], E, A] =
new ApplicativeErrorOps[F[E, ?], E, A](fa)
implicit def applicativeErrorSyntax[F[_], E, A](fa: F[A])(implicit F: ApplicativeError[F, E]): ApplicativeErrorOps[F, E, A] =
new ApplicativeErrorOps[F, E, A](fa)

}

final class ApplicativeErrorIdOps[E](e: E) {
Expand Down
28 changes: 15 additions & 13 deletions tests/src/test/scala/cats/tests/ApplicativeErrorTests.scala
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
package cats
package tests

import cats.data.{Xor, XorT}
import cats.data.{XorT}


class ApplicativeErrorCheck extends CatsSuite {

type ErrorOr[A] = String Xor A

val failed: String Xor Int =
"Badness".raiseError[ErrorOr, Int]

test("raiseError syntax creates an Xor with the correct type parameters") {
failed should === ("Badness".left[Int])
val failed: Option[Int] =
(()).raiseError[Option, Int]

test("raiseError syntax creates an Option with the correct value") {
failed should === (None: Option[Int])
}

test("handleError syntax transforms an error to a success") {
failed.handleError(error => error.length) should === (7.right)
failed.handleError(_ => 7) should === (Some(7))
}

test("handleErrorWith transforms an error to a success") {
failed.handleErrorWith(error => error.length.right) should === (7.right)
failed.handleErrorWith(_ => Some(7)) should === (Some(7))
}

test("attempt syntax creates a wrapped Xor") {
failed.attempt should === ("Badness".left.right)
failed.attempt should === (Option(().left))
}

test("attemptT syntax creates an XorT") {
failed.attemptT should === (XorT[ErrorOr, String, Int](failed.right))
failed.attemptT should === (XorT[Option, Unit, Int](Option(().left)))
}

test("recover syntax transforms an error to a success") {
failed.recover { case error => error.length } should === (7.right)
failed.recover { case _ => 7 } should === (Some(7))
}

test("recoverWith transforms an error to a success") {
failed.recoverWith { case error => error.length.right } should === (7.right)
failed.recoverWith { case _ => Some(7) } should === (Some(7))
}

}

}

0 comments on commit 207fda4

Please sign in to comment.