From f22f31ee6cfc92885d08f9b425188b788c3f8115 Mon Sep 17 00:00:00 2001 From: Sven van Heugten Date: Fri, 16 Jan 2026 21:17:37 +0100 Subject: [PATCH] Rewrite two tests with Prop.classify instead --- NightLight.Core.Tests/FakeHome.fs | 3 ++ .../InteractionListGenerators.fs | 11 ++++++ NightLight.Core.Tests/NightLightTests.fs | 39 +++++++------------ 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/NightLight.Core.Tests/FakeHome.fs b/NightLight.Core.Tests/FakeHome.fs index c8ede0a..84e781a 100644 --- a/NightLight.Core.Tests/FakeHome.fs +++ b/NightLight.Core.Tests/FakeHome.fs @@ -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 diff --git a/NightLight.Core.Tests/InteractionListGenerators.fs b/NightLight.Core.Tests/InteractionListGenerators.fs index 286bfd2..8202a63 100644 --- a/NightLight.Core.Tests/InteractionListGenerators.fs +++ b/NightLight.Core.Tests/InteractionListGenerators.fs @@ -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) = genInteractions |> Gen.bind (fun interactions -> diff --git a/NightLight.Core.Tests/NightLightTests.fs b/NightLight.Core.Tests/NightLightTests.fs index 6109f17..c55ec08 100644 --- a/NightLight.Core.Tests/NightLightTests.fs +++ b/NightLight.Core.Tests/NightLightTests.fs @@ -25,33 +25,24 @@ type NightLightTests() = fakeHome - [ |])>] - let ``All lights should be either off, white or yellow during the day`` (light: Light) = - genBiasedInteractions light - |> ensurePartOfDayIs Day - |> ensureStartsWithTimeChanged - |> Arb.fromGen - |> Prop.forAll + [] + 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) - - [ |])>] - 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" [ |])>] let ``All non-remotely controlled lights with power should be on`` (light: Light) =