Files
functional-programming-snake/scala/test/M10Extensions.test.scala
T

53 lines
1.8 KiB
Scala

package snake
import Fixtures.*
import M09Helpers.drawn
class M10ExtensionsSuite extends munit.FunSuite:
cases("m10-update").foreach { c =>
test(s"update: ${c("name").str}") {
val a = c("args")
assertEquals(Game.update(gameState(a(0)), action(a(1))), gameState(c("expected")))
}
}
cases("m10-moves-every").foreach { c =>
test(s"movesEvery: ${c("name").str}") {
assertEquals(Game.movesEvery(c("args")(0).num.toInt), c("expected").num.toInt)
}
}
cases("m10-input").foreach { c =>
test(s"keyToAction: ${c("name").str}") {
assertEquals(Input.keyToAction(c("args")(0).str), opt(c("expected"))(action))
}
}
private def time(t: Long): Event = Event.Time(t)
private val frameTimes = List(25L, 50L, 75L, 100L, 125L, 150L, 175L, 200L)
test("the animation clock runs every 25 ms") { assertEquals(App.FRAME_MS, 25L) }
test("speedActions turns the clock into Frame actions every 25 ms") {
assertEquals(App.speedActions.run(frameTimes.map(time)), List.fill(8)(Action.Frame))
}
test("speedActions still turns keys into actions, including pause and restart") {
assertEquals(
App.speedActions.run(List(Event.KeyDown(" "), Event.KeyDown("r"), Event.KeyDown("ArrowUp"))),
List(Action.TogglePause, Action.Restart, Action.Turn(Direction.Up))
)
}
test("main (seed 42) draws a moved snake after eight 25 ms frames") {
val traced = IO.trace(App.main, 42)
assertEquals(traced.ops, List(Op.RandomSeed))
val (first, afterFirst) = traced.result.step(time(0))
assertEquals(drawn(first.head), View.view(Spawn.newGame(42)))
val drawings = afterFirst.run(frameTimes.map(time))
assertEquals(drawings.length, 8)
val start = Spawn.newGame(42)
val moved = start.copy(snake = start.snake.map(p => p.copy(x = p.x + 1)), frame = 8)
assertEquals(drawn(drawings(7)), View.view(moved))
}