89 lines
3.2 KiB
FSharp
89 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) }
|
|
|
|
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) ]
|