Rewrite another test to use a more generic generator
This commit is contained in:
parent
cbc948c005
commit
d278b5d8ea
2 changed files with 26 additions and 19 deletions
|
|
@ -42,6 +42,16 @@ let getPartOfDayAfterInteractions interactions =
|
||||||
|> Seq.last
|
|> Seq.last
|
||||||
|> getPartOfDay
|
|> 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>) =
|
let ensureStartsWithTimeChanged (genInteractions: Gen<Interaction list>) =
|
||||||
genInteractions
|
genInteractions
|
||||||
|> Gen.bind (fun interactions ->
|
|> Gen.bind (fun interactions ->
|
||||||
|
|
@ -52,17 +62,7 @@ let ensureStartsWithTimeChanged (genInteractions: Gen<Interaction list>) =
|
||||||
let ensureLightHasPower (light: Light) (genInteractions: Gen<Interaction list>) =
|
let ensureLightHasPower (light: Light) (genInteractions: Gen<Interaction list>) =
|
||||||
genInteractions
|
genInteractions
|
||||||
|> Gen.map (fun interactions ->
|
|> Gen.map (fun interactions ->
|
||||||
let lightHasPower =
|
if doesLightHavePowerAfterInteractions light interactions then
|
||||||
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
|
|
||||||
interactions
|
interactions
|
||||||
else
|
else
|
||||||
interactions @ [ HumanInteraction(LightPoweredOn light) ])
|
interactions @ [ HumanInteraction(LightPoweredOn light) ])
|
||||||
|
|
|
||||||
|
|
@ -44,16 +44,23 @@ type NightLightTests() =
|
||||||
|> Prop.trivial (fakeHome.LightsThatAreOn.Length = 0)
|
|> Prop.trivial (fakeHome.LightsThatAreOn.Length = 0)
|
||||||
|> Prop.collect $"{fakeHome.LightsThatAreOn.Length} light(s) on"
|
|> Prop.collect $"{fakeHome.LightsThatAreOn.Length} light(s) on"
|
||||||
|
|
||||||
[<Property(Arbitrary = [| typeof<ArbitraryNonRemotelyControlledLight> |])>]
|
[<Property>]
|
||||||
let ``All non-remotely controlled lights with power should be on`` (light: Light) =
|
let ``All non-remotely controlled lights that have power should be on`` () =
|
||||||
genBiasedInteractions light
|
genInteractions |> ensureStartsWithTimeChanged |> Arb.fromGen |> Prop.forAll
|
||||||
|> ensureLightHasPower light
|
|
||||||
|> ensureStartsWithTimeChanged
|
|
||||||
|> Arb.fromGen
|
|
||||||
|> Prop.forAll
|
|
||||||
<| fun interactions ->
|
<| fun interactions ->
|
||||||
let fakeHome = createFakeHomeWithNightLightAndInteract 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> |])>]
|
[<Property(Arbitrary = [| typeof<ArbitraryRemotelyControlledLight> |])>]
|
||||||
let ``If the remote was never used, all remote controlled lights with power should be on`` (light: Light) =
|
let ``If the remote was never used, all remote controlled lights with power should be on`` (light: Light) =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue