Introduce ArbitraryInteractions

This commit is contained in:
Sven van Heugten 2026-01-16 21:49:38 +01:00
parent d278b5d8ea
commit 8866053172
2 changed files with 33 additions and 33 deletions

View file

@ -25,42 +25,38 @@ type NightLightTests() =
fakeHome
[<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
[<Property(Arbitrary = [| typeof<ArbitraryInteractions> |])>]
let ``All lights should either be off or have the right color`` (interactions: Interaction list) =
let fakeHome = createFakeHomeWithNightLightAndInteract interactions
let partOfDay = getPartOfDayAfterInteractions interactions
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<ArbitraryInteractions> |])>]
let ``All non-remotely controlled lights that have power should be on`` (interactions: Interaction list) =
let fakeHome = createFakeHomeWithNightLightAndInteract interactions
let nonRemotelyControlledLightsWithPower =
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"
|> Seq.filter (fun (light, _) ->
light.ControlledWithRemote = NonRemote
&& doesLightHavePowerAfterInteractions light interactions)
|> Seq.toList
[<Property>]
let ``All non-remotely controlled lights that have power should be on`` () =
genInteractions |> ensureStartsWithTimeChanged |> Arb.fromGen |> Prop.forAll
<| fun interactions ->
let fakeHome = createFakeHomeWithNightLightAndInteract interactions
let nonRemotelyControlledLightsWithPower =
fakeHome.LightStates
|> Seq.filter (fun (light, _) ->
light.ControlledWithRemote = NonRemote
&& doesLightHavePowerAfterInteractions light interactions)
|> Seq.toList
nonRemotelyControlledLightsWithPower
|> Seq.forall (snd >> _.IsOn)
|> Prop.trivial (nonRemotelyControlledLightsWithPower.Length = 0)
|> Prop.collect $"{nonRemotelyControlledLightsWithPower.Length} non-remotely controlled light(s) with power"
nonRemotelyControlledLightsWithPower
|> Seq.forall (snd >> _.IsOn)
|> Prop.trivial (nonRemotelyControlledLightsWithPower.Length = 0)
|> Prop.collect $"{nonRemotelyControlledLightsWithPower.Length} non-remotely controlled light(s) with power"
[<Property(Arbitrary = [| typeof<ArbitraryRemotelyControlledLight> |])>]
let ``If the remote was never used, all remote controlled lights with power should be on`` (light: Light) =