Skip to content

Commit

Permalink
Switch to Hedgehog
Browse files Browse the repository at this point in the history
Since all NuGet Packages are checked in, I didn't want to run paket install,
as this would produce a large diff.

It should be done though in the next commit via:

    $ .paket/paket.bootstrapper.exe
    $ .paket/paket.exe install
    $ ./build.sh
  • Loading branch information
moodmosaic committed Jul 13, 2017
1 parent a3efa88 commit 368b23f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 58 deletions.
131 changes: 75 additions & 56 deletions PollingConsumer.Tests/PollingConsumerProperties.fs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
module Ploeh.Samples.PollingConsumerProperties module Ploeh.Samples.PollingConsumerProperties


open System open System
open FsCheck open Hedgehog
open FsCheck.Xunit open Xunit
open Swensen.Unquote open Swensen.Unquote
open PollingConsumer open PollingConsumer


Expand All @@ -30,21 +30,39 @@ module Gen =
// integers to stay within a reasonable range. The addition of 1 prevents // integers to stay within a reasonable range. The addition of 1 prevents
// the generation of zero TimeSpans. // the generation of zero TimeSpans.
let moderateTimeSpan = let moderateTimeSpan =
Arb.generate |> Gen.map (abs >> ((+) 1) >> int64 >> TimeSpan.FromTicks) Range.constantBounded ()
|> Gen.int
|> Gen.map (abs >> ((+) 1) >> int64 >> TimeSpan.FromTicks)


let cycleDuration = gen { let cycleDuration = gen {
let! (pd, hd) = Gen.two moderateTimeSpan let! (pd, hd) = Gen.tuple moderateTimeSpan
return { PollDuration = PollDuration pd return { PollDuration = PollDuration pd
HandleDuration = HandleDuration hd } } HandleDuration = HandleDuration hd } }


[<Property(QuietOnSuccess = true)>] let responses =
let ``transitionFromNoMessage returns correct result when it has time to idle`` let timeSpan =
statistics Range.constantBounded ()
responses = |> Gen.int
Gen.moderateTimeSpan |> Gen.map (int64 >> TimeSpan.FromTicks)
|> Gen.two
|> Arb.fromGen gen {
|> Prop.forAll <| fun (idleDuration, margin) -> let! currentTime = Gen.dateTime |> Gen.map DateTimeOffset
let! poll = timeSpan |> Gen.map PollDuration |> Gen.zip (Gen.constant <| Some ())
let! handle = timeSpan |> Gen.map HandleDuration
let! idle = timeSpan |> Gen.map IdleDuration

return { CurrentTime = currentTime
Poll = poll
Handle = handle
Idle = idle } }

[<Fact>]
let ``transitionFromNoMessage returns correct result when it has time to idle`` () =
Property.check <| property {
let! statistics = Gen.list (Range.linear 0 100) Gen.cycleDuration
let! responses = Gen.responses
let! idleDuration = Gen.moderateTimeSpan
let! margin = Gen.moderateTimeSpan
let stopBefore = responses.CurrentTime + idleDuration + margin let stopBefore = responses.CurrentTime + idleDuration + margin


let actual = let actual =
Expand All @@ -53,32 +71,32 @@ let ``transitionFromNoMessage returns correct result when it has time to idle``
|> createInterpreter responses |> createInterpreter responses


let expected = ReadyState statistics let expected = ReadyState statistics
expected =! actual return expected = actual

}
[<Property(QuietOnSuccess = true)>]
let ``transitionFromNoMessage returns correct result when it has no time to idle`` [<Fact>]
statistics let ``transitionFromNoMessage returns correct result when it has no time to idle`` () =
responses = Property.check <| property {
Gen.moderateTimeSpan let! statistics = Gen.list (Range.linear 0 100) Gen.cycleDuration
|> Gen.two let! responses = Gen.responses
|> Arb.fromGen let! idleDuration = Gen.moderateTimeSpan
|> Prop.forAll <| fun (idleDuration, x) -> let! x = Gen.moderateTimeSpan
let stopBefore = responses.CurrentTime + idleDuration - x let stopBefore = responses.CurrentTime + idleDuration - x

let actual = let actual =
transitionFromNoMessage transitionFromNoMessage
(IdleDuration idleDuration) stopBefore (statistics, ()) (IdleDuration idleDuration) stopBefore (statistics, ())
|> createInterpreter responses |> createInterpreter responses
StoppedState statistics =! actual return StoppedState statistics = actual

}
[<Property(QuietOnSuccess = true)>]
let ``transitionFromReady returns correct result when it has no time to cycle`` [<Fact>]
responses = let ``transitionFromReady returns correct result when it has no time to cycle`` () =
fun x y -> x, y Property.check <| property {
<!> Gen.listOf Gen.cycleDuration let! statistics = Gen.list (Range.linear 0 100) Gen.cycleDuration
<*> Gen.two Gen.moderateTimeSpan let! responses = Gen.responses
|> Arb.fromGen let! estimatedDuration = Gen.moderateTimeSpan
|> Prop.forAll <| fun (statistics, (estimatedDuration, x)) -> let! x = Gen.moderateTimeSpan
let expectedDuration = let expectedDuration =
statistics statistics
|> List.map PollingConsumer.toTotalCycleTimeSpan |> List.map PollingConsumer.toTotalCycleTimeSpan
Expand All @@ -89,16 +107,16 @@ let ``transitionFromReady returns correct result when it has no time to cycle``
transitionFromReady estimatedDuration stopBefore statistics transitionFromReady estimatedDuration stopBefore statistics
|> createInterpreter responses |> createInterpreter responses


StoppedState statistics =! actual return StoppedState statistics = actual
}


[<Property(QuietOnSuccess = true)>] [<Fact>]
let ``transitionFromReady returns correct result when polling no message`` let ``transitionFromReady returns correct result when polling no message`` () =
responses = Property.check <| property {
fun x y -> x, y let! statistics = Gen.list (Range.linear 0 100) Gen.cycleDuration
<!> Gen.listOf Gen.cycleDuration let! responses = Gen.responses
<*> Gen.two Gen.moderateTimeSpan let! estimatedDuration = Gen.moderateTimeSpan
|> Arb.fromGen let! margin = Gen.moderateTimeSpan
|> Prop.forAll <| fun (statistics, (estimatedDuration, margin)) ->
let responses = { responses with Poll = (None, snd responses.Poll) } let responses = { responses with Poll = (None, snd responses.Poll) }
let expectedDuration = let expectedDuration =
statistics statistics
Expand All @@ -111,16 +129,16 @@ let ``transitionFromReady returns correct result when polling no message``
|> createInterpreter responses |> createInterpreter responses


let expected = NoMessageState (statistics, snd responses.Poll) let expected = NoMessageState (statistics, snd responses.Poll)
expected =! actual return expected = actual

}
[<Property(QuietOnSuccess = true)>]
let ``transitionFromReady returns correct result when polling a message`` [<Fact>]
responses = let ``transitionFromReady returns correct result when polling a message`` () =
fun x y -> x, y Property.check <| property {
<!> Gen.listOf Gen.cycleDuration let! statistics = Gen.list (Range.linear 0 100) Gen.cycleDuration
<*> Gen.two Gen.moderateTimeSpan let! responses = Gen.responses
|> Arb.fromGen let! estimatedDuration = Gen.moderateTimeSpan
|> Prop.forAll <| fun (statistics, (estimatedDuration, margin)) -> let! margin = Gen.moderateTimeSpan
let responses = { responses with Poll = (Some (), snd responses.Poll) } let responses = { responses with Poll = (Some (), snd responses.Poll) }
let expectedDuration = let expectedDuration =
statistics statistics
Expand All @@ -133,4 +151,5 @@ let ``transitionFromReady returns correct result when polling a message``
|> createInterpreter responses |> createInterpreter responses


let expected = ReceivedMessageState (statistics, snd responses.Poll, ()) let expected = ReceivedMessageState (statistics, snd responses.Poll, ())
expected =! actual return expected = actual
}
3 changes: 2 additions & 1 deletion PollingConsumer.Tests/paket.references
Original file line number Original file line Diff line number Diff line change
@@ -1,2 +1,3 @@
FsCheck.Xunit Hedgehog
xunit.core
Unquote Unquote
3 changes: 2 additions & 1 deletion paket.dependencies
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,8 @@
source https://www.nuget.org/api/v2/ source https://www.nuget.org/api/v2/


nuget FAKE nuget FAKE
nuget FsCheck.Xunit nuget Hedgehog
nuget FSharp.Core nuget FSharp.Core
nuget Unquote nuget Unquote
nuget xunit.core
nuget xunit.runner.console nuget xunit.runner.console

0 comments on commit 368b23f

Please sign in to comment.