We're only fooling ourselves if we think that it's ready *right* after construction anyway. After all, the initial state of the lights won't be updated when the state machine is constructed.
49 lines
1.6 KiB
FSharp
49 lines
1.6 KiB
FSharp
namespace NightLight.Core.Tests
|
|
|
|
open System
|
|
open FsCheck.Xunit
|
|
open FsCheck.FSharp
|
|
|
|
module InteractionsHelpers =
|
|
let getTimeAfter interactions =
|
|
interactions
|
|
|> Seq.choose (fun interaction ->
|
|
match interaction with
|
|
| TimeChanged time -> Some time
|
|
| _ -> None)
|
|
|> Seq.tryLast
|
|
|> function
|
|
| Some time -> time
|
|
| None -> failwith "Time wasn't changed"
|
|
|
|
let isDayAfter interactions =
|
|
let time = getTimeAfter interactions
|
|
|
|
time.TimeOfDay >= TimeSpan.FromHours 5.5
|
|
&& time.TimeOfDay < TimeSpan.FromHours 20.5
|
|
|
|
let isNightAfter = not << isDayAfter
|
|
|
|
[<Properties(Arbitrary = [| typeof<Arbitraries> |])>]
|
|
type NightLightTests() =
|
|
[<Property>]
|
|
let ``Brightness should always be under 255`` (interactions: Interaction list) =
|
|
let fakeHome = FakeHome()
|
|
fakeHome.Interact interactions
|
|
fakeHome.ForAllLightsThatAreOn(fun (_, brightness, _) -> brightness < 255uy)
|
|
|
|
[<Property>]
|
|
let ``Lights should be red during the night`` (interactions: Interaction list) =
|
|
let fakeHome = FakeHome()
|
|
fakeHome.Interact interactions
|
|
|
|
InteractionsHelpers.isNightAfter interactions
|
|
==> fakeHome.ForAllLightsThatAreOn(fun (_, _, color) -> color = Red)
|
|
|
|
[<Property>]
|
|
let ``Lights should be white or yellow during the day`` (interactions: Interaction list) =
|
|
let fakeHome = FakeHome()
|
|
fakeHome.Interact interactions
|
|
|
|
InteractionsHelpers.isDayAfter interactions
|
|
==> fakeHome.ForAllLightsThatAreOn(fun (_, _, color) -> color = White || color = Yellow)
|