Skip to content

Commit

Permalink
Added the tests for the 'withError' function
Browse files Browse the repository at this point in the history
  • Loading branch information
rcardin committed Apr 10, 2024
1 parent 299ff86 commit 24886bb
Show file tree
Hide file tree
Showing 2 changed files with 17 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 @@ -52,5 +52,5 @@ def $catch[A](block: () => A, catchBlock: Throwable => A): A =
def withError[Error, OtherError, A](
transform: OtherError => Error,
block: Raise[OtherError] ?=> () => A
)(using r: Raise[Error]) =
)(using r: Raise[Error]): A =
recover(block, otherError => r.raise(transform(otherError)))
16 changes: 16 additions & 0 deletions src/test/scala/in/rcard/raise4s/RaiseSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,20 @@ class RaiseSpec extends AnyFlatSpec with Matchers {
)
}
}

"withError" should "return the value if it is not an error" in {
val actual = either {
() => withError[Int, String, Int](s => s.length, () => 42)
}

actual should be(Right(42))
}

it should "return the transformed error if the value is an error" in {
val actual = either {
() => withError[Int, String, Int](s => s.length, () => raise("error"))
}

actual should be(Left(5))
}
}

0 comments on commit 24886bb

Please sign in to comment.