Rewrite another test to use a more generic generator

This commit is contained in:
Sven van Heugten 2026-01-16 21:46:14 +01:00
parent cbc948c005
commit d278b5d8ea
2 changed files with 26 additions and 19 deletions

View file

@ -42,6 +42,16 @@ let getPartOfDayAfterInteractions interactions =
|> Seq.last
|> getPartOfDay
let doesLightHavePowerAfterInteractions light interactions =
interactions
|> Seq.choose (fun interaction ->
match interaction with
| HumanInteraction(LightPoweredOff l) when l = light -> Some false
| HumanInteraction(LightPoweredOn l) when l = light -> Some true
| _ -> None)
|> Seq.tryLast
|> Option.defaultValue false
let ensureStartsWithTimeChanged (genInteractions: Gen<Interaction list>) =
genInteractions
|> Gen.bind (fun interactions ->
@ -52,17 +62,7 @@ let ensureStartsWithTimeChanged (genInteractions: Gen<Interaction list>) =
let ensureLightHasPower (light: Light) (genInteractions: Gen<Interaction list>) =
genInteractions
|> Gen.map (fun interactions ->
let lightHasPower =
interactions
|> Seq.choose (fun interaction ->
match interaction with
| HumanInteraction(LightPoweredOff l) when l = light -> Some false
| HumanInteraction(LightPoweredOn l) when l = light -> Some true
| _ -> None)
|> Seq.tryLast
|> Option.defaultValue false
if lightHasPower then
if doesLightHavePowerAfterInteractions light interactions then
interactions
else
interactions @ [ HumanInteraction(LightPoweredOn light) ])

View file

@ -44,16 +44,23 @@ type NightLightTests() =
|> 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) =
genBiasedInteractions light
|> ensureLightHasPower light
|> ensureStartsWithTimeChanged
|> Arb.fromGen
|> Prop.forAll
[<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
fakeHome.LightShouldHaveState light _.IsOn
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"
[<Property(Arbitrary = [| typeof<ArbitraryRemotelyControlledLight> |])>]
let ``If the remote was never used, all remote controlled lights with power should be on`` (light: Light) =