Files
functional-programming-snake/scala/test/M04Spawn.test.scala
T
2026-10-29 00:07:27 +01:00

53 lines
1.7 KiB
Scala

package snake
import Fixtures.*
class M04SpawnSuite extends munit.FunSuite:
cases("m04-free-cells").foreach { c =>
test(s"freeCells: ${c("name").str}") {
assertEquals(Spawn.freeCells(gameState(c("args")(0))), list(c("expected"))(point))
}
}
cases("m04-spawn").foreach { c =>
test(s"spawnApple: ${c("name").str}") {
assertEquals(Spawn.spawnApple(gameState(c("args")(0))), gameState(c("expected")))
}
}
cases("m04-new-game").foreach { c =>
test(s"newGame: ${c("name").str}") {
assertEquals(Spawn.newGame(c("args")(0).num.toLong), gameState(c("expected")))
}
}
cases("m04-tick").foreach { c =>
test(s"tick: ${c("name").str}") {
assertEquals(Spawn.tick(gameState(c("args")(0))), gameState(c("expected")))
}
}
// 3x2 board, one-cell snake, about to eat the apple at (1,0)
private val eating = GameState(3, 2, List(Point(0, 0)), Direction.Right, Direction.Right,
Some(Point(1, 0)), 0L, 0, Status.Playing, 0)
test("tick: eating the apple spawns a new one in the same step") {
// snake becomes (1,0),(0,0); free cells are (2,0),(0,1),(1,1),(2,1); seed 0 draws 1144304738 % 4 = 2 -> (1,1)
assertEquals(
Spawn.tick(eating),
eating.copy(snake = List(Point(1, 0), Point(0, 0)), apple = Some(Point(1, 1)), score = 1, seed = 1831565813L)
)
}
test("tick is spawnApple after advance") {
assertEquals(Spawn.tick(eating), Spawn.spawnApple(Rules.advance(eating)))
}
test("tick leaves the seed alone when no apple needs spawning") {
assertEquals(Spawn.tick(eating.copy(apple = Some(Point(2, 1)))).seed, 0L)
}
test("newGame is reproducible: same seed, same game") {
assertEquals(Spawn.newGame(42L), Spawn.newGame(42L))
}