Generalize the genInteractionListThatEndsAtTime concept
This commit is contained in:
parent
9e97d9a37b
commit
14cfaaeed5
3 changed files with 24 additions and 20 deletions
|
|
@ -8,18 +8,18 @@ let private isDay (time: DateTime) =
|
||||||
time.TimeOfDay >= TimeSpan.FromHours 5.5
|
time.TimeOfDay >= TimeSpan.FromHours 5.5
|
||||||
&& time.TimeOfDay < TimeSpan.FromHours 20.5
|
&& time.TimeOfDay < TimeSpan.FromHours 20.5
|
||||||
|
|
||||||
type ArbitraryInteractionsListThatEndsDuringTheDay =
|
type ArbitraryInteractionListThatEndsDuringTheDay =
|
||||||
static member InteractionsList() =
|
static member InteractionsList() =
|
||||||
ArbMap.defaults
|
ArbMap.defaults
|
||||||
|> ArbMap.generate<DateTime>
|
|> ArbMap.generate<DateTime>
|
||||||
|> Gen.filter isDay
|
|> Gen.filter isDay
|
||||||
|> Gen.bind genInteractionsListThatEndsAtTime
|
|> Gen.bind genInteractionListThatEndsAtTime
|
||||||
|> Arb.fromGen
|
|> Arb.fromGen
|
||||||
|
|
||||||
type ArbitraryInteractionsListThatEndsDuringTheNight =
|
type ArbitraryInteractionListThatEndsDuringTheNight =
|
||||||
static member InteractionsList() =
|
static member InteractionsList() =
|
||||||
ArbMap.defaults
|
ArbMap.defaults
|
||||||
|> ArbMap.generate<DateTime>
|
|> ArbMap.generate<DateTime>
|
||||||
|> Gen.filter (not << isDay)
|
|> Gen.filter (not << isDay)
|
||||||
|> Gen.bind genInteractionsListThatEndsAtTime
|
|> Gen.bind genInteractionListThatEndsAtTime
|
||||||
|> Arb.fromGen
|
|> Arb.fromGen
|
||||||
|
|
|
||||||
|
|
@ -17,25 +17,29 @@ let private genHumanInteraction =
|
||||||
let private genInteraction =
|
let private genInteraction =
|
||||||
Gen.oneof [ genTimeChangedInteraction; genHumanInteraction ]
|
Gen.oneof [ genTimeChangedInteraction; genHumanInteraction ]
|
||||||
|
|
||||||
let private genInteractionsListThatStartsWithTimeChange =
|
let private genInteractionsListThatStartsWithTimeChanged =
|
||||||
gen {
|
gen {
|
||||||
let! firstInteraction = genTimeChangedInteraction
|
let! firstInteraction = genTimeChangedInteraction
|
||||||
let! remainingInteractions = Gen.listOf genInteraction
|
let! remainingInteractions = Gen.listOf genInteraction
|
||||||
return firstInteraction :: remainingInteractions
|
return firstInteraction :: remainingInteractions
|
||||||
}
|
}
|
||||||
|
|
||||||
let private genInteractionsListWhere condition =
|
let private genInteractionListContaining containingInteraction afterFilter =
|
||||||
Gen.listOf (genInteraction |> Gen.filter condition)
|
gen {
|
||||||
|
let genNonTrivialList =
|
||||||
|
gen {
|
||||||
|
let! before = genInteractionsListThatStartsWithTimeChanged
|
||||||
|
let! after = Gen.listOf (genInteraction |> Gen.filter afterFilter)
|
||||||
|
return before @ containingInteraction :: after
|
||||||
|
}
|
||||||
|
|
||||||
let genInteractionsListThatEndsAtTime time =
|
return!
|
||||||
let genTrivialList = Gen.constant <| List.singleton (Interaction.TimeChanged time)
|
match containingInteraction with
|
||||||
|
| Interaction.TimeChanged _ ->
|
||||||
|
let genTrivialList = Gen.constant <| List.singleton containingInteraction
|
||||||
|
Gen.frequency [ 1, genTrivialList; 9, genNonTrivialList ]
|
||||||
|
| _ -> genNonTrivialList
|
||||||
|
}
|
||||||
|
|
||||||
let genNonTrivialList =
|
let genInteractionListThatEndsAtTime time =
|
||||||
gen {
|
genInteractionListContaining (Interaction.TimeChanged time) (not << _.IsTimeChanged)
|
||||||
let! before = genInteractionsListThatStartsWithTimeChange
|
|
||||||
let interactionThatSetsEndTime = Interaction.TimeChanged time
|
|
||||||
let! after = genInteractionsListWhere (not << _.IsTimeChanged)
|
|
||||||
return before @ interactionThatSetsEndTime :: after
|
|
||||||
}
|
|
||||||
|
|
||||||
Gen.frequency [ 1, genTrivialList; 9, genNonTrivialList ]
|
|
||||||
|
|
|
||||||
|
|
@ -21,12 +21,12 @@ type NightLightTests() =
|
||||||
|
|
||||||
fakeHome
|
fakeHome
|
||||||
|
|
||||||
[<Property(Arbitrary = [| typeof<ArbitraryInteractionsListThatEndsDuringTheDay> |])>]
|
[<Property(Arbitrary = [| typeof<ArbitraryInteractionListThatEndsDuringTheDay> |])>]
|
||||||
let ``Lights should be white or yellow during the day`` (interactions: Interaction list) =
|
let ``Lights should be white or yellow during the day`` (interactions: Interaction list) =
|
||||||
let fakeHome = createFakeHomeWithNightLightAndInteract interactions
|
let fakeHome = createFakeHomeWithNightLightAndInteract interactions
|
||||||
fakeHome.ForAllLightsThatAreOn(fun (_, _, color) -> color = White || color = Yellow)
|
fakeHome.ForAllLightsThatAreOn(fun (_, _, color) -> color = White || color = Yellow)
|
||||||
|
|
||||||
[<Property(Arbitrary = [| typeof<ArbitraryInteractionsListThatEndsDuringTheNight> |])>]
|
[<Property(Arbitrary = [| typeof<ArbitraryInteractionListThatEndsDuringTheNight> |])>]
|
||||||
let ``Lights should be red during the night`` (interactions: Interaction list) =
|
let ``Lights should be red during the night`` (interactions: Interaction list) =
|
||||||
let fakeHome = createFakeHomeWithNightLightAndInteract interactions
|
let fakeHome = createFakeHomeWithNightLightAndInteract interactions
|
||||||
fakeHome.ForAllLightsThatAreOn(fun (_, _, color) -> color = Red)
|
fakeHome.ForAllLightsThatAreOn(fun (_, _, color) -> color = Red)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue