feat(scala): m07 io and m08 stream with tests

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Lucas Winther
2026-10-29 00:12:08 +01:00
co-authored by Claude Opus 5.5
parent 56bc6b4cf7
commit bd0c9fcc0c
6 changed files with 446 additions and 0 deletions
+99
View File
@@ -0,0 +1,99 @@
package snake
import IO.*
class M07IOSuite extends munit.FunSuite:
private def rectAt(x: Long): Shape = Shape.Rect(x.toInt, 0, 1, 1, "#000000")
private val r1 = rectAt(0)
private val t1 = Shape.Text(1, 2, "hi", "#ffffff", 16, "left")
private def drawOp(shape: Shape): Op = Op.Draw(shape)
private val seedOp: Op = Op.RandomSeed
test("trace of pure(3) has result 3 and no ops") {
assertEquals(trace(pure(3), 0), Traced(3, Nil))
}
test("randomSeed is answered with the seed given to trace") {
assertEquals(trace(randomSeed, 9).result, 9L)
}
test("randomSeed records a RandomSeed op") {
assertEquals(trace(randomSeed, 9).ops, List(seedOp))
}
test("effect(op) is an Effect whose continuation is pure") {
effect(seedOp) match
case IO.Effect(op, k) =>
assertEquals(op, seedOp)
assertEquals(k(OpResult.Seed(5)), IO.Pure(OpResult.Seed(5)))
case other => fail(s"expected an Effect, got $other")
}
test("draw records a Draw op and produces ()") {
assertEquals(trace(draw(r1), 0), Traced((), List(drawOp(r1))))
}
test("drawScene draws the shapes in order") {
assertEquals(trace(drawScene(List(r1, t1)), 0).ops, List(drawOp(r1), drawOp(t1)))
}
test("drawScene of an empty scene has no ops") {
assertEquals(trace(drawScene(Nil), 0).ops, Nil)
}
test("sequence of nothing has no ops") {
assertEquals(trace(sequence(Nil), 0), Traced((), Nil))
}
test("sequence runs effects left to right") {
assertEquals(
trace(sequence(List(randomSeed.map(_ => ()), draw(t1), draw(r1))), 0).ops,
List(seedOp, drawOp(t1), drawOp(r1))
)
}
test("map transforms the result without adding ops") {
assertEquals(trace(randomSeed.map(_ + 1), 41), Traced(42L, List(seedOp)))
}
test("flatMap passes the result of the first IO to the second") {
assertEquals(
trace(randomSeed.flatMap(s => draw(rectAt(s))), 7).ops,
List(seedOp, drawOp(rectAt(7)))
)
}
test("building io.map(throwingF) does not throw") {
val boom: Long => Long = _ => throw new RuntimeException("boom")
randomSeed.map(boom) // only builds a description
}
test("tracing io.map(throwingF) runs f and throws") {
val boom: Long => Long = _ => throw new RuntimeException("boom")
interceptMessage[RuntimeException]("boom")(trace(randomSeed.map(boom), 0))
}
// Monad laws, checked by interpreting both sides and comparing Traced(result, ops).
private val programs: List[(String, () => IO[Long])] = List(
"a pure value" -> (() => pure(4L)),
"randomSeed" -> (() => randomSeed),
"a draw followed by reading the seed" -> (() => draw(r1).flatMap(_ => randomSeed))
)
private val f: Long => IO[Long] = n => draw(rectAt(n)).flatMap(_ => pure(n * 2))
private val g: Long => IO[Long] = n => randomSeed.map(_ + n)
programs.foreach { (name, program) =>
test(s"left identity holds for $name") {
val a = trace(program(), 5).result
assertEquals(trace(pure(a).flatMap(f), 5), trace(f(a), 5))
}
test(s"right identity holds for $name") {
assertEquals(trace(program().flatMap(pure), 5), trace(program(), 5))
}
test(s"associativity holds for $name") {
assertEquals(
trace(program().flatMap(f).flatMap(g), 5),
trace(program().flatMap(x => f(x).flatMap(g)), 5)
)
}
}
+88
View File
@@ -0,0 +1,88 @@
package snake
import Stream.{keyDowns, keyUps, interval}
class M08StreamSuite extends munit.FunSuite:
private def time(t: Long): Event = Event.Time(t)
private def down(key: String): Event = Event.KeyDown(key)
private def up(key: String): Event = Event.KeyUp(key)
test("keyDowns emits keys of KeyDown events only") {
assertEquals(keyDowns.run(List(down("a"), time(5), up("a"), down("b"))), List("a", "b"))
}
test("keyUps emits keys of KeyUp events only") {
assertEquals(keyUps.run(List(down("a"), time(5), up("a"), down("b"), up("b"))), List("a", "b"))
}
test("run of no events is empty") {
assertEquals(keyDowns.run(Nil), Nil)
}
test("interval emits each multiple once as time passes") {
assertEquals(interval(10).run(List(time(5), time(10), time(35))), List(10L, 20L, 30L))
}
test("interval emits several multiples after one big time jump") {
assertEquals(interval(10).run(List(time(35))), List(10L, 20L, 30L))
}
test("interval ignores key events") {
assertEquals(interval(10).run(List(down("a"), up("a"))), Nil)
}
test("map transforms every output") {
assertEquals(keyDowns.map(_ + "!").run(List(down("a"), down("b"))), List("a!", "b!"))
}
test("filter keeps outputs satisfying the predicate") {
assertEquals(keyDowns.filter(_ != "a").run(List(down("a"), down("b"))), List("b"))
}
test("filterMap transforms outputs and drops Nones") {
assertEquals(
keyDowns.filterMap(k => if k == "a" then None else Some(k.toUpperCase)).run(List(down("a"), down("b"))),
List("B")
)
}
test("merge puts outputs of the first stream before the second at the same event") {
assertEquals(
keyDowns.map("first " + _).merge(keyDowns.map("second " + _)).run(List(down("x"))),
List("first x", "second x")
)
}
test("merge interleaves sources by event order") {
assertEquals(
interval(10).merge(keyDowns.map(_ => -1L)).run(List(time(10), down("a"), time(20))),
List(10L, -1L, 20L)
)
}
test("scan emits each new accumulator, not the initial value") {
assertEquals(keyDowns.scan(0)((n, _) => n + 1).run(List(down("a"), up("a"), down("b"), down("c"))), List(1, 2, 3))
}
test("scan passes the output to the folding function") {
assertEquals(keyDowns.scan("")(_ + _).run(List(down("a"), down("b"))), List("a", "ab"))
}
test("startWith emits the value once, before the outputs of the first step") {
assertEquals(keyDowns.startWith("init").run(List(down("a"), down("b"))), List("init", "a", "b"))
}
test("startWith emits at the first event even if that step has no outputs") {
assertEquals(keyDowns.startWith("init").run(List(time(1), down("a"))), List("init", "a"))
}
test("startWith emits nothing when no event ever arrives") {
assertEquals(keyDowns.startWith("init").run(Nil), Nil)
}
test("running the same stream value twice gives the same outputs") {
val s = interval(10).merge(keyDowns.map(_ => 100L)).scan(0L)(_ + _)
val events = List(time(25), down("a"), time(40))
assertEquals(s.run(events), s.run(events))
assertEquals(s.run(events), List(10L, 30L, 130L, 160L, 200L))
}