Rewrite two tests with Prop.classify instead

This commit is contained in:
Sven van Heugten 2026-01-16 21:17:37 +01:00
parent 79405b3841
commit f22f31ee6c
3 changed files with 29 additions and 24 deletions

View file

@ -144,5 +144,8 @@ type FakeHome() =
type FakeHome with
member this.Interact(interactions: Interaction seq) = interactions |> Seq.iter this.Interact
member this.LightsThatAreOn =
this.LightStates |> Seq.filter (snd >> _.IsOn) |> Seq.toList
member this.LightShouldHaveState light condition =
this.LightStates |> Seq.find (fst >> (=) light) |> snd |> condition

View file

@ -31,6 +31,17 @@ let genBiasedInteractionsExcept biasTowardsLight disqualifier =
let genBiasedInteractions biasTowardsLight =
genBiasedInteractionsExcept biasTowardsLight (fun _ -> false)
let genInteractions = genInteraction None |> Gen.listOf
let getPartOfDayAfterInteractions interactions =
interactions
|> Seq.choose (fun interaction ->
match interaction with
| Interaction.TimeChanged time -> Some time
| _ -> None)
|> Seq.last
|> getPartOfDay
let ensureStartsWithTimeChanged (genInteractions: Gen<Interaction list>) =
genInteractions
|> Gen.bind (fun interactions ->

View file

@ -25,33 +25,24 @@ type NightLightTests() =
fakeHome
[<Property(Arbitrary = [| typeof<ArbitraryLight> |])>]
let ``All lights should be either off, white or yellow during the day`` (light: Light) =
genBiasedInteractions light
|> ensurePartOfDayIs Day
|> ensureStartsWithTimeChanged
|> Arb.fromGen
|> Prop.forAll
[<Property>]
let ``All lights should either be off or have the right color`` () =
genInteractions |> ensureStartsWithTimeChanged |> Arb.fromGen |> Prop.forAll
<| fun interactions ->
let fakeHome = createFakeHomeWithNightLightAndInteract interactions
let partOfDay = getPartOfDayAfterInteractions interactions
fakeHome.LightShouldHaveState light (function
| Off -> true
| On(_, color) -> color = White || color = Yellow)
[<Property(Arbitrary = [| typeof<ArbitraryLight> |])>]
let ``All lights should be either off or red during the night`` (light: Light) =
genBiasedInteractions light
|> ensurePartOfDayIs Night
|> ensureStartsWithTimeChanged
|> Arb.fromGen
|> Prop.forAll
<| fun interactions ->
let fakeHome = createFakeHomeWithNightLightAndInteract interactions
fakeHome.LightShouldHaveState light (function
| Off -> true
| On(_, color) -> color = Red)
fakeHome.LightStates
|> Seq.forall (function
| _, Off -> true
| _, On(_, color) ->
match partOfDay with
| Day -> color = White || color = Yellow
| Night -> color = Red)
|> Prop.classify (partOfDay = Day) "day"
|> Prop.classify (partOfDay = Night) "night"
|> Prop.trivial (fakeHome.LightsThatAreOn.Length = 0)
|> Prop.collect $"{fakeHome.LightsThatAreOn.Length} light(s) on"
[<Property(Arbitrary = [| typeof<ArbitraryNonRemotelyControlledLight> |])>]
let ``All non-remotely controlled lights with power should be on`` (light: Light) =