Files
functional-programming-snake/scala/test/M08Stream.test.scala
T
2026-10-29 00:12:08 +01:00

89 lines
3.1 KiB
Scala

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))
}