Write the first test
This commit is contained in:
parent
fc069edf31
commit
109ebde64a
3 changed files with 44 additions and 0 deletions
22
NightLight.Core.Tests/Arbitraries.fs
Normal file
22
NightLight.Core.Tests/Arbitraries.fs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
namespace NightLight.Core.Tests
|
||||
|
||||
open System
|
||||
open FsCheck.FSharp
|
||||
open NightLight.Core.Models
|
||||
|
||||
type Arbitraries =
|
||||
static member Interaction() =
|
||||
let genTimeChangedInteraction =
|
||||
gen {
|
||||
let! time = ArbMap.defaults |> ArbMap.generate<DateTime>
|
||||
return Interaction.TimeChanged time
|
||||
}
|
||||
|
||||
let genHumanInteraction =
|
||||
gen {
|
||||
let! light = Gen.elements lights
|
||||
let! humanInteraction = Gen.elements [ LightTurnedOn light; LightTurnedOff light ]
|
||||
return Interaction.HumanInteraction humanInteraction
|
||||
}
|
||||
|
||||
Gen.oneof [ genTimeChangedInteraction; genHumanInteraction ] |> Arb.fromGen
|
||||
|
|
@ -9,6 +9,8 @@
|
|||
<ItemGroup>
|
||||
<Compile Include="NightLightStateMachine.fs" />
|
||||
<Compile Include="FakeHome.fs" />
|
||||
<Compile Include="Arbitraries.fs" />
|
||||
<Compile Include="NightLightTests.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
20
NightLight.Core.Tests/NightLightTests.fs
Normal file
20
NightLight.Core.Tests/NightLightTests.fs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
module NightLight.Core.Tests.NightLightTests
|
||||
|
||||
open FsCheck.Xunit
|
||||
|
||||
let private assertIsOk (result: Result<unit, 'a>) : unit =
|
||||
match result with
|
||||
| Ok() -> ()
|
||||
| Error error -> failwith $"Expected Ok, got Error {error}"
|
||||
|
||||
[<Property(Arbitrary = [| typeof<Arbitraries> |])>]
|
||||
let ``Brightness should always be under 255`` (fakeHome: FakeHome) (interactions: Interaction list) =
|
||||
interactions
|
||||
|> Seq.iter (fun interaction -> fakeHome.Interact interaction |> assertIsOk)
|
||||
|
||||
fakeHome.LightStates
|
||||
|> Seq.choose (fun (_, state) ->
|
||||
match state with
|
||||
| On brightness -> Some brightness
|
||||
| Off -> None)
|
||||
|> Seq.forall (fun brightness -> brightness < 255uy)
|
||||
Loading…
Add table
Add a link
Reference in a new issue