@@ -3,7 +3,10 @@ package ox
33import org .scalatest .exceptions .TestFailedException
44import org .scalatest .flatspec .AnyFlatSpec
55import org .scalatest .matchers .should .Matchers
6- import ox .either .{fail , ok , orThrow }
6+ import ox .either .catching
7+ import ox .either .fail
8+ import ox .either .ok
9+ import ox .either .orThrow
710
811import scala .util .boundary .Label
912
@@ -116,24 +119,24 @@ class EitherTest extends AnyFlatSpec with Matchers:
116119 e.getMessage should include(" The enclosing `either` call uses a different error type." )
117120 }
118121
119- it should " catch exceptions" in {
120- either.catching (throw new RuntimeException (" boom" )).left.map(_.getMessage) shouldBe Left (" boom" )
122+ it should " catch non fatal exceptions" in {
123+ either.catchingNonFatal (throw new RuntimeException (" boom" )).left.map(_.getMessage) shouldBe Left (" boom" )
121124 }
122125
123126 it should " not catch fatal exceptions" in {
124- val e = intercept[InterruptedException ](either.catching (throw new InterruptedException ()))
127+ val e = intercept[InterruptedException ](either.catchingNonFatal (throw new InterruptedException ()))
125128
126129 e shouldBe a[InterruptedException ]
127130 }
128131
129- it should " provide an either scope when catching" in {
132+ it should " provide an either scope when catching non fatal exceptions " in {
130133 val val1 : Either [Throwable , Int ] = Left (ComparableException (" oh no" ))
131134
132- either.catching (val1.ok()) shouldBe Left (ComparableException (" oh no" ))
135+ either.catchingNonFatal (val1.ok()) shouldBe Left (ComparableException (" oh no" ))
133136 }
134137
135- it should " report a proper compilation error when wrong error type is used for ok() in catching block" in {
136- val e = intercept[TestFailedException ](assertCompiles(""" either.catching (fail1.ok())""" ))
138+ it should " report a proper compilation error when wrong error type is used for ok() in catchingNonFatal block" in {
139+ val e = intercept[TestFailedException ](assertCompiles(""" either.catchingNonFatal (fail1.ok())""" ))
137140
138141 e.getMessage should include(" The enclosing `either` call uses a different error type." )
139142 }
@@ -174,6 +177,25 @@ class EitherTest extends AnyFlatSpec with Matchers:
174177 intercept[RuntimeException ](v.orThrow).getMessage shouldBe " boom!"
175178 }
176179
180+ " catching" should " catch given exceptions only" in {
181+ val e = new IllegalArgumentException (" boom" )
182+ (throw e).catching[IllegalArgumentException ] shouldBe Left (e)
183+ }
184+
185+ it should " catch parent exceptions" in {
186+ val e = new IllegalArgumentException (" boom" )
187+ (throw e).catching[Exception ] shouldBe Left (e)
188+ }
189+
190+ it should " not catch non-given exceptions" in {
191+ val e = new IllegalArgumentException (" boom" )
192+ intercept[IllegalArgumentException ]((throw e).catching[IllegalStateException ]) shouldBe e
193+ }
194+
195+ it should " return successful results as Right-values" in {
196+ 10 .catching[Exception ] shouldBe Right (10 )
197+ }
198+
177199 private transparent inline def receivesNoEitherNestingError (inline code : String ): Unit =
178200 val errs = scala.compiletime.testing.typeCheckErrors(code)
179201 if ! errs
0 commit comments