Skip to content

Commit

Permalink
Extract logic in test of Gen.apply
Browse files Browse the repository at this point in the history
  • Loading branch information
TysonMN committed Dec 30, 2021
1 parent a710dc6 commit 9a11e9f
Showing 1 changed file with 45 additions and 41 deletions.
86 changes: 45 additions & 41 deletions tests/Hedgehog.Tests/GenTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,50 @@ open Hedgehog
open Hedgehog.Gen.Operators
open TestDsl


let private testGenPairViaApply gPair =
// In addition to asserting that Gen.apply is applicative, this test
// also asserts that the integral shrink tree is the one containing
// duplicates that existed before PR
// https://github.com/hedgehogqa/fsharp-hedgehog/pull/239
// The duplicate-free shrink trees that result from the code in that PR
// do not work well with the applicative behavior of Gen.apply because
// some values would shrink more if using the monadic version of
// Gen.apply, which should never happen.
let actual =
seq {
while true do
let t = gPair |> Gen.sampleTree 0 1 |> Seq.head
if Tree.outcome t = (2, 1) then
yield t
} |> Seq.head

let expected =
Node ((2, 1), [
Node ((0, 1), [
Node ((0, 0), [])
])
Node ((1, 1), [
Node ((0, 1), [
Node ((0, 0), [])
])
Node ((1, 0), [
Node ((0, 0), [])
])
])
Node ((2, 0), [
Node ((0, 0), [])
Node ((1, 0), [
Node ((0, 0), [])
])
])
])

(actual |> Tree.map (sprintf "%A") |> Tree.render)
=! (expected |> Tree.map (sprintf "%A") |> Tree.render)
Expect.isTrue <| Tree.equals actual expected


let genTests = testList "Gen tests" [
yield! testCases "dateTime creates DateTime instances"
[ 8; 16; 32; 64; 128; 256; 512 ] <| fun count->
Expand Down Expand Up @@ -138,50 +182,10 @@ let genTests = testList "Gen tests" [
|> Property.check

testCase "apply is applicative" <| fun () ->
// In addition to asserting that Gen.apply is applicative, this test
// also asserts that the integral shrink tree is the one containing
// duplicates that existed before PR
// https://github.com/hedgehogqa/fsharp-hedgehog/pull/239
// The duplicate-free shrink trees that result from the code in that PR
// do not work well with the applicative behavior of Gen.apply because
// some values would shrink more if using the monadic version of
// Gen.apply, which should never happen.
let gPair =
Gen.constant (fun a b -> a, b)
|> Gen.apply (Range.constant 0 2 |> Gen.int32)
|> Gen.apply (Range.constant 0 1 |> Gen.int32)

let actual =
seq {
while true do
let t = gPair |> Gen.sampleTree 0 1 |> Seq.head
if Tree.outcome t = (2, 1) then
yield t
} |> Seq.head

let expected =
Node ((2, 1), [
Node ((0, 1), [
Node ((0, 0), [])
])
Node ((1, 1), [
Node ((0, 1), [
Node ((0, 0), [])
])
Node ((1, 0), [
Node ((0, 0), [])
])
])
Node ((2, 0), [
Node ((0, 0), [])
Node ((1, 0), [
Node ((0, 0), [])
])
])
])

(actual |> Tree.map (sprintf "%A") |> Tree.render)
=! (expected |> Tree.map (sprintf "%A") |> Tree.render)
Expect.isTrue <| Tree.equals actual expected
testGenPairViaApply gPair

]

0 comments on commit 9a11e9f

Please sign in to comment.