feat(fsharp): m07 io and m08 stream with tests

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Lucas Winther
2026-10-29 00:52:44 +01:00
co-authored by Claude Opus 5.5
parent 9cbefeb2be
commit e27372f017
9 changed files with 463 additions and 0 deletions
+97
View File
@@ -0,0 +1,97 @@
module Snake.Tests.M07IO
open Expecto
open Snake
open Snake.View
open Snake.IO
let private rectAt (x: uint32) : Shape = Rect(int x, 0, 1, 1, "#000000")
let private r1 = rectAt 0u
let private t1 = Text(1, 2, "hi", "#ffffff", 16, "left")
// Monad laws, checked by interpreting both sides and comparing { Result; Ops }.
let private programs: (string * (unit -> IO<uint32>)) list =
[ "a pure value", (fun () -> IO.pure 4u)
"randomSeed", (fun () -> IO.randomSeed ())
"a draw followed by reading the seed", (fun () -> IO.draw r1 |> IO.bind (fun () -> IO.randomSeed ())) ]
let private f (n: uint32) : IO<uint32> = IO.draw (rectAt n) |> IO.bind (fun () -> IO.pure (n * 2u))
let private g (n: uint32) : IO<uint32> = IO.randomSeed () |> IO.map (fun s -> s + n)
[<Tests>]
let tests =
testList "M07 IO" [
yield testCase "trace of pure 3 has result 3 and no ops" (fun () ->
Expect.equal (IO.trace 0u (IO.pure 3)) { Result = 3; Ops = [] } "trace")
yield testCase "randomSeed is answered with the seed given to trace" (fun () ->
Expect.equal (IO.trace 9u (IO.randomSeed ())).Result 9u "result")
yield testCase "randomSeed records a RandomSeed op" (fun () ->
Expect.equal (IO.trace 9u (IO.randomSeed ())).Ops [ RandomSeed ] "ops")
yield testCase "effect op is an Effect whose continuation is pure" (fun () ->
match IO.effect RandomSeed with
| Effect(op, k) ->
Expect.equal op RandomSeed "op"
match k (Seed 5u) with
| Pure(Seed 5u) -> ()
| other -> failtest $"expected Pure (Seed 5u), got %A{other}"
| other -> failtest $"expected an Effect, got %A{other}")
yield testCase "draw records a Draw op and produces ()" (fun () ->
Expect.equal (IO.trace 0u (IO.draw r1)) { Result = (); Ops = [ Draw r1 ] } "trace")
yield testCase "drawScene draws the shapes in order" (fun () ->
Expect.equal (IO.trace 0u (IO.drawScene [ r1; t1 ])).Ops [ Draw r1; Draw t1 ] "ops")
yield testCase "drawScene of an empty scene has no ops" (fun () ->
Expect.equal (IO.trace 0u (IO.drawScene [])).Ops [] "ops")
yield testCase "sequence of nothing has no ops" (fun () ->
Expect.equal (IO.trace 0u (IO.sequence [])) { Result = (); Ops = [] } "trace")
yield testCase "sequence runs effects left to right" (fun () ->
Expect.equal
(IO.trace 0u (IO.sequence [ IO.randomSeed () |> IO.map ignore; IO.draw t1; IO.draw r1 ])).Ops
[ RandomSeed; Draw t1; Draw r1 ]
"ops")
yield testCase "map transforms the result without adding ops" (fun () ->
Expect.equal
(IO.trace 41u (IO.randomSeed () |> IO.map (fun s -> s + 1u)))
{ Result = 42u; Ops = [ RandomSeed ] }
"trace")
yield testCase "bind passes the result of the first IO to the second" (fun () ->
Expect.equal
(IO.trace 7u (IO.randomSeed () |> IO.bind (fun s -> IO.draw (rectAt s)))).Ops
[ RandomSeed; Draw(rectAt 7u) ]
"ops")
yield testCase "building IO.map throwingF io does not throw" (fun () ->
let boom (_: uint32) : uint32 = failwith "boom"
IO.randomSeed () |> IO.map boom |> ignore) // only builds a description
yield testCase "tracing IO.map throwingF io runs f and throws" (fun () ->
let boom (_: uint32) : uint32 = failwith "boom"
Expect.throwsC
(fun () -> IO.randomSeed () |> IO.map boom |> IO.trace 0u |> ignore)
(fun e -> Expect.equal e.Message "boom" "exception message"))
for name, program in programs do
yield testCase $"left identity holds for {name}" (fun () ->
let a = (IO.trace 5u (program ())).Result
Expect.equal (IO.trace 5u (IO.pure a |> IO.bind f)) (IO.trace 5u (f a)) "left identity")
yield testCase $"right identity holds for {name}" (fun () ->
Expect.equal (IO.trace 5u (program () |> IO.bind IO.pure)) (IO.trace 5u (program ())) "right identity")
yield testCase $"associativity holds for {name}" (fun () ->
Expect.equal
(IO.trace 5u (program () |> IO.bind f |> IO.bind g))
(IO.trace 5u (program () |> IO.bind (fun x -> f x |> IO.bind g)))
"associativity")
]
+106
View File
@@ -0,0 +1,106 @@
module Snake.Tests.M08Stream
open Expecto
open Snake
open Snake.Stream
[<Tests>]
let tests =
testList "M08 Stream" [
testCase "keyDowns emits keys of KeyDown events only" (fun () ->
Expect.equal (Stream.run [ KeyDown "a"; Time 5L; KeyUp "a"; KeyDown "b" ] Stream.keyDowns) [ "a"; "b" ] "keys")
testCase "keyUps emits keys of KeyUp events only" (fun () ->
Expect.equal
(Stream.run [ KeyDown "a"; Time 5L; KeyUp "a"; KeyDown "b"; KeyUp "b" ] Stream.keyUps)
[ "a"; "b" ]
"keys")
testCase "run of no events is empty" (fun () -> Expect.equal (Stream.run [] Stream.keyDowns) [] "outputs")
testCase "interval emits each multiple once as time passes" (fun () ->
Expect.equal (Stream.run [ Time 5L; Time 10L; Time 35L ] (Stream.interval 10L)) [ 10L; 20L; 30L ] "ticks")
testCase "interval emits several multiples after one big time jump" (fun () ->
Expect.equal (Stream.run [ Time 35L ] (Stream.interval 10L)) [ 10L; 20L; 30L ] "ticks")
testCase "interval ignores key events" (fun () ->
Expect.equal (Stream.run [ KeyDown "a"; KeyUp "a" ] (Stream.interval 10L)) [] "ticks")
testCase "map transforms every output" (fun () ->
Expect.equal
(Stream.keyDowns |> Stream.map (fun k -> k + "!") |> Stream.run [ KeyDown "a"; KeyDown "b" ])
[ "a!"; "b!" ]
"outputs")
testCase "filter keeps outputs satisfying the predicate" (fun () ->
Expect.equal
(Stream.keyDowns |> Stream.filter (fun k -> k <> "a") |> Stream.run [ KeyDown "a"; KeyDown "b" ])
[ "b" ]
"outputs")
testCase "choose transforms outputs and drops Nones" (fun () ->
Expect.equal
(Stream.keyDowns
|> Stream.choose (fun k -> if k = "a" then None else Some(k.ToUpperInvariant()))
|> Stream.run [ KeyDown "a"; KeyDown "b" ])
[ "B" ]
"outputs")
testCase "merge puts outputs of the first stream before the second at the same event" (fun () ->
Expect.equal
(Stream.merge (Stream.map (fun k -> "first " + k) Stream.keyDowns) (Stream.map (fun k -> "second " + k) Stream.keyDowns)
|> Stream.run [ KeyDown "x" ])
[ "first x"; "second x" ]
"outputs")
testCase "merge interleaves sources by event order" (fun () ->
Expect.equal
(Stream.merge (Stream.interval 10L) (Stream.map (fun _ -> -1L) Stream.keyDowns)
|> Stream.run [ Time 10L; KeyDown "a"; Time 20L ])
[ 10L; -1L; 20L ]
"outputs")
testCase "scan emits each new accumulator, not the initial value" (fun () ->
Expect.equal
(Stream.keyDowns
|> Stream.scan (fun n _ -> n + 1) 0
|> Stream.run [ KeyDown "a"; KeyUp "a"; KeyDown "b"; KeyDown "c" ])
[ 1; 2; 3 ]
"outputs")
testCase "scan passes the output to the folding function" (fun () ->
Expect.equal
(Stream.keyDowns |> Stream.scan (+) "" |> Stream.run [ KeyDown "a"; KeyDown "b" ])
[ "a"; "ab" ]
"outputs")
testCase "startWith emits the value once, before the outputs of the first step" (fun () ->
Expect.equal
(Stream.keyDowns |> Stream.startWith "init" |> Stream.run [ KeyDown "a"; KeyDown "b" ])
[ "init"; "a"; "b" ]
"outputs")
testCase "startWith emits at the first event even if that step has no outputs" (fun () ->
Expect.equal
(Stream.keyDowns |> Stream.startWith "init" |> Stream.run [ Time 1L; KeyDown "a" ])
[ "init"; "a" ]
"outputs")
testCase "startWith emits nothing when no event ever arrives" (fun () ->
Expect.equal (Stream.keyDowns |> Stream.startWith "init" |> Stream.run []) [] "outputs")
testCase "step feeds one event and returns its outputs and the next stream" (fun () ->
let out, next = Stream.step (Time 25L) (Stream.interval 10L)
Expect.equal out [ 10L; 20L ] "first step"
Expect.equal (fst (Stream.step (Time 30L) next)) [ 30L ] "next stream remembers the last time")
testCase "running the same stream value twice gives the same outputs" (fun () ->
let s =
Stream.merge (Stream.interval 10L) (Stream.map (fun _ -> 100L) Stream.keyDowns)
|> Stream.scan (+) 0L
let events = [ Time 25L; KeyDown "a"; Time 40L ]
Expect.equal (Stream.run events s) (Stream.run events s) "same outputs"
Expect.equal (Stream.run events s) [ 10L; 30L; 130L; 160L; 200L ] "outputs")
]
+2
View File
@@ -15,6 +15,8 @@
<Compile Include="M04Spawn.fs" />
<Compile Include="M05Game.fs" />
<Compile Include="M06View.fs" />
<Compile Include="M07IO.fs" />
<Compile Include="M08Stream.fs" />
<Compile Include="Purity.fs" />
<Compile Include="Main.fs" />
</ItemGroup>