Skip to content

Commit

Permalink
Working on Failure Intro example #bruce #time 30m
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce Eckel committed Jul 21, 2024
1 parent 9a1d582 commit 502b588
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/test/scala/experiments/ShortCircuitingFailure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,53 @@ package experiments
import zio.*
import zio.test.*

def matchTo(n: Int, limit: Int) =
def testLimit(n: Int, limit: Int) =
println(s"testLimit($n, $limit)")
if n < limit then
ZIO.succeed(s"Passed $n")
else
println("n >= limit: testLimit failed")
ZIO.fail(s"Failed at $n")

def completeToShortCircuit(n: Int) =
def shortCircuiting(n: Int) =
defer:
val r1 =
matchTo(0, n).run
printLine(r1).run
val r2 =
matchTo(1, n).run
printLine(r2).run
val r3 =
matchTo(2, n).run
printLine(r3).run
val r1 = testLimit(0, n).run
printLine(s"-> n: $n, r1: $r1").run
val r2 = testLimit(1, n).run
printLine(s"-> n: $n, r2: $r2").run
val r3 = testLimit(2, n).run
printLine(s"-> n: $n, r3: $r3").run
// TODO Decide whether to just return r3 instead of a new string here
ZIO.succeed(s"Passed all steps").run
// ZIO.succeed(s"Passed all steps").run
r3

object ShortCircuitingFailure extends ZIOSpecDefault:

object ShortCircuitingFailure1 extends ZIOAppDefault:
def run =
defer:
val result0 = shortCircuiting(0).flip.run
printLine(s"result0: $result0").run
val result1 = shortCircuiting(1).flip.run
printLine(s"result1: $result1").run
val result2 = shortCircuiting(2).flip.run
printLine(s"result2: $result2").run

object ShortCircuitingFailure2 extends ZIOSpecDefault:
def spec =
suite("Suite of Tests")(
test("one"):
defer:
val result = completeToShortCircuit(1).flip.run
val result = shortCircuiting(1).flip.run
assertTrue(result == "Failed at 1")
,
test("two"):
defer:
val result = completeToShortCircuit(2).flip.run
val result = shortCircuiting(2).flip.run
assertTrue(result == "Failed at 2")
,
test("three"):
defer:
val result = completeToShortCircuit(3).run
val result = shortCircuiting(3).run
assertTrue(result == "Passed all steps")
,
) @@ TestAspect.sequential
end ShortCircuitingFailure

0 comments on commit 502b588

Please sign in to comment.