-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ensureP (partial function) method to MonadError (#2194)
* Add ensureP method to MonadError * Rename to `reject`. Fix Test. * Use applyOrElse to potentially avoid evaluating PF twice.
- Loading branch information
1 parent
06041fb
commit 3938d1d
Showing
2 changed files
with
40 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |