Files
functional-programming-snake/scala/test/Fixtures.test.scala
T
2026-10-28 23:34:21 +01:00

19 lines
717 B
Scala

package snake
import java.nio.file.{Files, Paths}
/** Loads shared fixtures from ../fixtures/<name>.json.
* Typed decoders (point, direction, status, action, state, opt) arrive with the model (Task 11). */
object Fixtures:
private val dir = Paths.get(sys.props.getOrElse("user.dir", ".")).resolve("..").resolve("fixtures").normalize
def load(name: String): ujson.Value =
ujson.read(Files.readString(dir.resolve(s"$name.json")))
/** The cases array of a fixture. */
def cases(name: String): Seq[ujson.Value] = load(name)("cases").arr.toSeq
/** Decode a nullable JSON value into an Option. */
def opt[A](v: ujson.Value)(f: ujson.Value => A): Option[A] =
if v.isNull then None else Some(f(v))