Skip to content

Commit

Permalink
Added tests for the 'recover' function
Browse files Browse the repository at this point in the history
  • Loading branch information
rcardin committed Apr 10, 2024
1 parent b625e4a commit 9eb4b57
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/scala/in/rcard/raise4s/Raise.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ def ensureNotNull[B, Error](value: B, raise: () => Error)(using r: Raise[Error])
* @return
* The result of the block or the fallback value
*/
def recover[Error, A](block: Raise[Error] ?=> () => A)(recover: Error => A): A =
def recover[Error, A](block: Raise[Error] ?=> () => A, recover: Error => A): A =
fold(block, ex => throw ex, recover, identity)
27 changes: 27 additions & 0 deletions src/test/scala/in/rcard/raise4s/RaiseSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,31 @@ class RaiseSpec extends AnyFlatSpec with Matchers {
)
actual should be(43)
}

"recover" should "return the value if it is not an error" in {
val actual = recover(
() => 42,
error => 43
)

actual should be(42)
}

it should "return the recovery value if the value is an error" in {
val actual = recover(
() => raise("error"),
error => 43
)

actual should be(43)
}

it should "rethrow the exception" in {
assertThrows[RuntimeException] {
recover(
() => throw new RuntimeException("error"),
error => 43
)
}
}
}

0 comments on commit 9eb4b57

Please sign in to comment.