Skip to content
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

Add reject (partial function) method to MonadError #2194

Merged
merged 4 commits into from
Mar 16, 2018
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions core/src/main/scala/cats/syntax/monadError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ final class MonadErrorOps[F[_], E, A](val fa: F[A]) extends AnyVal {
def ensureOr(error: A => E)(predicate: A => Boolean)(implicit F: MonadError[F, E]): F[A] =
F.ensureOr(fa)(error)(predicate)

def reject(pf: PartialFunction[A, E])(implicit F: MonadError[F, E]): F[A] =
F.flatMap(fa) { a =>
pf.andThen(F.raiseError[A]).applyOrElse(a, (_: A) => fa)
}

def adaptError(pf: PartialFunction[E, E])(implicit F: MonadError[F, E]): F[A] =
F.adaptError(fa)(pf)
}
Expand Down
47 changes: 35 additions & 12 deletions tests/src/test/scala/cats/tests/MonadErrorSuite.scala
Original file line number Diff line number Diff line change
@@ -1,36 +1,59 @@
package cats
package tests

import scala.util.{Failure, Success, Try}

class MonadErrorSuite extends CatsSuite {

val successful: Option[Int] = 42.some
val failed: Option[Int] = None
implicit val eqThrow: Eq[Throwable] = Eq.fromUniversalEquals

val successful: Try[Int] = Success(42)
val failedValue: Throwable = new IllegalArgumentException("default failure")
val otherValue: Throwable = new IllegalStateException("other failure")
val failed: Try[Int] = Failure(failedValue)

test("ensure raises an error if the predicate fails") {
successful.ensure(())(i => false) should === (None)
successful.ensure(failedValue)(_ => false) should === (failed)
}

test("ensure returns the successful value if the predicate succeeds") {
successful.ensure(())(i => true) should === (successful)
successful.ensure(failedValue)(_ => true) should === (successful)
}

test("ensure returns the failure, when applied to a failure") {
failed.ensure(())(i => false) should === (failed)
failed.ensure(())(i => true) should === (failed)
test("ensure returns the original failure, when applied to a failure") {
failed.ensure(otherValue)(_ => false) should === (failed)
failed.ensure(otherValue)(_ => true) should === (failed)
}

test("ensureOr raises an error if the predicate fails") {
successful.ensureOr(_ => ())(_ => false) should === (None)
successful.ensureOr(_ => failedValue)(_ => false) should === (failed)
}

test("ensureOr returns the successful value if the predicate succeeds") {
successful.ensureOr(_ => ())(_ => true) should === (successful)
successful.ensureOr(_ => failedValue)(_ => true) should === (successful)
}

test("ensureOr returns the original failure, when applied to a failure") {
failed.ensureOr(_ => otherValue)(_ => false) should === (failed)
failed.ensureOr(_ => otherValue)(_ => true) should === (failed)
}

test("ensureOr returns the failure, when applied to a failure") {
failed.ensureOr(_ => ())(_ => false) should === (failed)
failed.ensureOr(_ => ())(_ => true) should === (failed)
test("ensureP returns the successful value if the partial function is not defined") {
successful.reject {
case i if i < 0 => failedValue
} should === (successful)
}

test("ensureP returns the original failure, when applied to a failure") {
failed.reject {
case i if i < 0 => otherValue
} should === (failed)
}

test("ensureP raises an error if the partial function is defined") {
successful.reject {
case i if i > 0 => failedValue
} should === (failed)
}

}