From 24886bbdbfdae438312b6cb4d1112f0e25f19b6f Mon Sep 17 00:00:00 2001 From: rcardin Date: Wed, 10 Apr 2024 18:12:04 +0200 Subject: [PATCH] Added the tests for the 'withError' function --- src/main/scala/in/rcard/raise4s/Raise.scala | 2 +- src/test/scala/in/rcard/raise4s/RaiseSpec.scala | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/scala/in/rcard/raise4s/Raise.scala b/src/main/scala/in/rcard/raise4s/Raise.scala index 6f39126..c71ea32 100644 --- a/src/main/scala/in/rcard/raise4s/Raise.scala +++ b/src/main/scala/in/rcard/raise4s/Raise.scala @@ -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))) diff --git a/src/test/scala/in/rcard/raise4s/RaiseSpec.scala b/src/test/scala/in/rcard/raise4s/RaiseSpec.scala index 2791768..f6c3c58 100644 --- a/src/test/scala/in/rcard/raise4s/RaiseSpec.scala +++ b/src/test/scala/in/rcard/raise4s/RaiseSpec.scala @@ -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)) + } }