Files
functional-programming-snake/fsharp/tests/Fixtures.fs
T
2026-10-28 23:34:21 +01:00

22 lines
810 B
FSharp

/// Loads shared fixtures from ../../fixtures/<name>.json.
/// Typed decoders (point, direction, status, action, state, opt) arrive with the model (Task 13).
module Snake.Tests.Fixtures
open System.IO
open System.Text.Json
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)