feat(fsharp): m01-m06 util, model, rules, random, spawn, game, view with Expecto tests
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
183821737e
commit
9cbefeb2be
@@ -1,9 +1,11 @@
|
||||
/// Loads shared fixtures from ../../fixtures/<name>.json.
|
||||
/// Typed decoders (point, direction, status, action, state, opt) arrive with the model (Task 13).
|
||||
/// 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"))
|
||||
@@ -19,3 +21,68 @@ let cases (name: string) : JsonElement list =
|
||||
/// 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) }
|
||||
|
||||
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) ]
|
||||
|
||||
Reference in New Issue
Block a user