From 8866053172df787ef3f2fc4401d32289953220e5 Mon Sep 17 00:00:00 2001 From: Sven van Heugten Date: Fri, 16 Jan 2026 21:49:38 +0100 Subject: [PATCH] Introduce ArbitraryInteractions --- .../InteractionListGenerators.fs | 4 ++ NightLight.Core.Tests/NightLightTests.fs | 62 +++++++++---------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/NightLight.Core.Tests/InteractionListGenerators.fs b/NightLight.Core.Tests/InteractionListGenerators.fs index aadeb95..c18e430 100644 --- a/NightLight.Core.Tests/InteractionListGenerators.fs +++ b/NightLight.Core.Tests/InteractionListGenerators.fs @@ -95,3 +95,7 @@ let ensureLastRemoteInteractionIs interactions else interactions @ [ RemoteInteraction desiredLastRemoteInteraction ]) + +type ArbitraryInteractions() = + static member Interactions() = + genInteractions |> ensureStartsWithTimeChanged |> Arb.fromGen diff --git a/NightLight.Core.Tests/NightLightTests.fs b/NightLight.Core.Tests/NightLightTests.fs index 5ad3ab6..c76ee9c 100644 --- a/NightLight.Core.Tests/NightLightTests.fs +++ b/NightLight.Core.Tests/NightLightTests.fs @@ -25,42 +25,38 @@ type NightLightTests() = fakeHome - [] - 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 + [ |])>] + 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" + + [ |])>] + 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 - [] - 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" [ |])>] let ``If the remote was never used, all remote controlled lights with power should be on`` (light: Light) =