Files
functional-programming-snake/fsharp/tests/Fixtures.fs
T
Lucas WintherandClaude Fable 5.2 edd57f0148 feat(course): audit fixes and twelve extensions (Modules 7b, 12, 13)
Audit pass:
- Frame rule counts frames since the last move, so every step lasts
  exactly movesEvery(score) frames, also right after a speed-up
- clampTime + MAX_GAP_MS: a hidden tab no longer replays the whole gap
- Pong Restart keeps held keys
- Fact corrections (mulberry32 output coverage, Haskell IO, uniform)
- New fixture cases from a mutation analysis

Extensions (all three tracks, shared fixtures, course text):
- Turn queue (Exercise 10.6), persisted high score (10.7),
  replay viewer (10.8), play() end-to-end helper (9.5)
- Optional State (4.7) and optional Module 7b, stack-safe IO
- Module 12, property-based testing with generators, shrinking,
  invariants and a model-based oracle
- Module 13, Conway's Life with a Click event
- JS type checking via jsconfig, JSDoc and types.d.ts
- Maintainer tools: scripts/mutate.mjs, scripts/typecheck-js.sh,
  Playwright end-to-end suite (e2e/, scripts/e2e.sh)

Docs: docs/contract.md is the canonical contract; specs and build log
under docs/superpowers.

Verified: JS 728, Scala 689, F# 691 unit tests; stubs and progression
M01-M13 and M07b; 176 end-to-end tests on Chromium and WebKit.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 12:05:29 +01:00

90 lines
3.2 KiB
FSharp

/// Loads shared fixtures from ../fixtures/<name>.json and decodes them into the course's types.
module Snake.Tests.Fixtures
open System.IO
open System.Text.Json
open Expecto
open Snake.Model
open Snake.View
let private dir =
Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "..", "fixtures"))
let load (name: string) : JsonElement =
use doc = JsonDocument.Parse(File.ReadAllText(Path.Combine(dir, name + ".json")))
doc.RootElement.Clone()
/// The cases array of a fixture.
let cases (name: string) : JsonElement list =
(load name).GetProperty("cases").EnumerateArray() |> List.ofSeq
/// Decode a nullable JSON value into an Option.
let opt (f: JsonElement -> 'a) (v: JsonElement) : 'a option =
if v.ValueKind = JsonValueKind.Null then None else Some(f v)
let prop (key: string) (v: JsonElement) : JsonElement = v.GetProperty(key)
let str (v: JsonElement) : string = v.GetString()
let num (v: JsonElement) : int = v.GetInt32()
let u32 (v: JsonElement) : uint32 = v.GetUInt32()
let bool (v: JsonElement) : bool = v.GetBoolean()
let list (f: JsonElement -> 'a) (v: JsonElement) : 'a list = v.EnumerateArray() |> Seq.map f |> List.ofSeq
/// The i-th argument of a case.
let arg (i: int) (c: JsonElement) : JsonElement = (prop "args" c)[i]
let expected (c: JsonElement) : JsonElement = prop "expected" c
let name (c: JsonElement) : string = str (prop "name" c)
let point (v: JsonElement) : Point = { X = num (prop "x" v); Y = num (prop "y" v) }
let direction (v: JsonElement) : Direction =
match str v with
| "Up" -> Up
| "Down" -> Down
| "Left" -> Left
| "Right" -> Right
| other -> failwith $"unknown direction: {other}"
let status (v: JsonElement) : Status =
match str v with
| "Playing" -> Playing
| "Paused" -> Paused
| "GameOver" -> GameOver
| "Won" -> Won
| other -> failwith $"unknown status: {other}"
let action (v: JsonElement) : Action =
match str (prop "tag" v) with
| "Tick" -> Tick
| "Turn" -> Turn(direction (prop "dir" v))
| "TogglePause" -> TogglePause
| "Restart" -> Restart
| "Frame" -> Frame
| other -> failwith $"unknown action tag: {other}"
/// JSON uses camelCase field names; the F# record uses PascalCase.
let gameState (v: JsonElement) : GameState =
{ Width = num (prop "width" v)
Height = num (prop "height" v)
Snake = list point (prop "snake" v)
Dir = direction (prop "dir" v)
NextDir = direction (prop "nextDir" v)
Apple = opt point (prop "apple" v)
Seed = u32 (prop "seed" v)
Score = num (prop "score" v)
Status = status (prop "status" v)
Frame = num (prop "frame" v)
Queued = opt direction (prop "queued" v) }
let shape (v: JsonElement) : Shape =
let n key = num (prop key v)
let s key = str (prop key v)
match s "tag" with
| "Rect" -> Rect(n "x", n "y", n "w", n "h", s "color")
| "Text" -> Text(n "x", n "y", s "text", s "color", n "size", s "align")
| other -> failwith $"unknown shape tag: {other}"
/// One test per case of a fixture, named "<label>: <case name>".
let fixtureTests (fixture: string) (label: string) (body: JsonElement -> unit) : Test list =
[ for c in cases fixture -> testCase $"{label}: {name c}" (fun () -> body c) ]