Implement shrinking in the arbitrary

This commit is contained in:
Sven van Heugten 2026-01-18 06:40:53 +01:00
parent 1265289866
commit da5f7ea4df

View file

@ -2,7 +2,6 @@ module NightLight.Core.Tests.InteractionListGenerators
open System open System
open NightLight.Core.Models open NightLight.Core.Models
open FsCheck
open FsCheck.FSharp open FsCheck.FSharp
let private genTimeChanged = let private genTimeChanged =
@ -20,15 +19,25 @@ let private genRemoteInteraction =
let private genInteraction = let private genInteraction =
Gen.oneof [ genTimeChanged; genHumanInteraction; genRemoteInteraction ] Gen.oneof [ genTimeChanged; genHumanInteraction; genRemoteInteraction ]
let private genInteractions = genInteraction |> Gen.listOf
let private ensureStartsWithTimeChanged (genInteractions: Gen<Interaction list>) =
genInteractions
|> Gen.bind (fun interactions ->
match interactions with
| Interaction.TimeChanged _ :: _ -> Gen.constant interactions
| _ -> genTimeChanged |> Gen.map (fun tc -> tc :: interactions))
type ArbitraryInteractions() = type ArbitraryInteractions() =
static member Interactions() = static member Interactions() =
genInteractions |> ensureStartsWithTimeChanged |> Arb.fromGen let gen =
genInteraction
|> Gen.listOf
|> Gen.bind (fun interactions ->
match interactions with
| Interaction.TimeChanged _ :: _ -> Gen.constant interactions
| _ -> genTimeChanged |> Gen.map (fun tc -> tc :: interactions))
let removeFromFrontAfterFirst (lst: 'a list) : seq<'a list> =
Seq.unfold
(fun current ->
match current with
| [] -> None
| [ _ ] -> None
| first :: _ :: rest ->
let next = first :: rest
Some(next, next))
lst
Arb.fromGenShrink (gen, removeFromFrontAfterFirst)