diff --git a/NightLight.Core.Tests/FakeHome.fs b/NightLight.Core.Tests/FakeHome.fs index e7c0112..c7bffd9 100644 --- a/NightLight.Core.Tests/FakeHome.fs +++ b/NightLight.Core.Tests/FakeHome.fs @@ -44,6 +44,8 @@ type FakeLight(light: Light) = color <- newColor type FakeHome(now: DateTime) = + let mutable time = now + let mutable nightLightStateMachine = NightLightStateMachine now let assertIsOkAndGet result = @@ -93,6 +95,8 @@ type FakeHome(now: DateTime) = commands |> Seq.iter processCommand nightLightStateMachine <- newState + member _.Time = time + member _.LightStates = friendlyNameToFakeLight.Values |> Seq.map _.LightWithState member _.Interact(interaction: Interaction) = @@ -109,4 +113,23 @@ type FakeHome(now: DateTime) = |> ReceivedZigbeeEvent |> sendEvent | HumanInteraction(LightTurnedOff light) -> friendlyNameToFakeLight[light.FriendlyName].TurnOff() - | TimeChanged time -> time |> Event.TimeChanged |> sendEvent + | TimeChanged newTime -> + time <- newTime + newTime |> Event.TimeChanged |> sendEvent + +type FakeHome with + member this.Interact(interactions: Interaction seq) = interactions |> Seq.iter this.Interact + + member this.ForAllLightsThatAreOn condition = + this.LightStates + |> Seq.choose (fun (light, state) -> + match state with + | On(brightness, color) -> Some(light, brightness, color) + | Off -> None) + |> Seq.forall condition + + member this.IsDay() = + this.Time.TimeOfDay >= TimeSpan.FromHours 5.5 + && this.Time.TimeOfDay < TimeSpan.FromHours 20.5 + + member this.IsNight() = not (this.IsDay()) diff --git a/NightLight.Core.Tests/NightLightTests.fs b/NightLight.Core.Tests/NightLightTests.fs index b9a361e..e74a2a0 100644 --- a/NightLight.Core.Tests/NightLightTests.fs +++ b/NightLight.Core.Tests/NightLightTests.fs @@ -1,16 +1,29 @@ namespace NightLight.Core.Tests +open System open FsCheck.Xunit +open FsCheck.FSharp [ |])>] type NightLightTests() = [] - let ``Brightness should always be under 255`` (fakeHome: FakeHome) (interactions: Interaction list) = - interactions |> Seq.iter (fun interaction -> fakeHome.Interact interaction) + let ``Brightness should always be under 255`` (now: DateTime) (interactions: Interaction list) = + let fakeHome = FakeHome now + fakeHome.Interact interactions + fakeHome.ForAllLightsThatAreOn(fun (_, brightness, _) -> brightness < 255uy) - fakeHome.LightStates - |> Seq.choose (fun (_, state) -> - match state with - | On(brightness, _) -> Some brightness - | Off -> None) - |> Seq.forall (fun brightness -> brightness < 255uy) + [] + let ``Lights should be red during the night`` (now: DateTime) (interactions: Interaction list) = + let fakeHome = FakeHome now + fakeHome.Interact interactions + + fakeHome.IsNight() + ==> fakeHome.ForAllLightsThatAreOn(fun (_, _, color) -> color = Red) + + [] + let ``Lights should be white or yellow during the day`` (now: DateTime) (interactions: Interaction list) = + let fakeHome = FakeHome now + fakeHome.Interact interactions + + fakeHome.IsDay() + ==> fakeHome.ForAllLightsThatAreOn(fun (_, _, color) -> color = White || color = Yellow)