Files
functional-programming-snake/fsharp/tests/M08Stream.fs
T
2026-10-29 00:52:44 +01:00

107 lines
4.9 KiB
FSharp

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")
]