From fda88a339c4dfb411b4cb190daf919ef7ad7a819 Mon Sep 17 00:00:00 2001 From: Tyson Williams Date: Thu, 30 Dec 2021 09:58:17 -0600 Subject: [PATCH] Add MergeSources to property CE --- CHANGELOG.md | 4 +++- src/Hedgehog/Property.fs | 3 +++ tests/Hedgehog.Tests/PropertyTests.fs | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 583c43f4..2c8bb0f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ - Add `Tree.apply`. Change `Gen.apply` from monadic to applicative. Revert runtime optimization of `Gen.integral`. ([#398][398], [@TysonMN][TysonMN]) - Change `ListGen.traverse` from monadic to applicative. ([#399][399], [@TysonMN][TysonMN]) - Fix bug in the `BindReturn` method of the `property` CE where the generated value is not added to the Journal. ([#401][401], [@TysonMN][TysonMN]) -- Add `BindReturn` to the `gen` CE. This essentially changes the last call to `let!` to use `Gen.map` instead of `Gen.bind`. Add `MergeSources` to the `gen` CE. This change enables the `and!` syntax. +- Add `BindReturn` to the `gen` CE. This essentially changes the last call to `let!` to use `Gen.map` instead of `Gen.bind`. Add `MergeSources` to the `gen` and `property` CEs. This change enables the `and!` syntax. ([#400][400], [@TysonMN][TysonMN]) ## Version 0.12.0 (2021-12-12) @@ -194,6 +194,8 @@ [401]: https://github.com/hedgehogqa/fsharp-hedgehog/pull/401 +[400]: + https://github.com/hedgehogqa/fsharp-hedgehog/pull/400 [399]: https://github.com/hedgehogqa/fsharp-hedgehog/pull/399 [398]: diff --git a/src/Hedgehog/Property.fs b/src/Hedgehog/Property.fs index 2b7afe18..08473357 100644 --- a/src/Hedgehog/Property.fs +++ b/src/Hedgehog/Property.fs @@ -330,6 +330,9 @@ module PropertyBuilder = |> Property.ofGen |> Property.map f + member __.MergeSources(ga, gb) = + Gen.zip ga gb + member __.ReturnFrom(m : Property<'a>) : Property<'a> = m diff --git a/tests/Hedgehog.Tests/PropertyTests.fs b/tests/Hedgehog.Tests/PropertyTests.fs index 63e4cb86..95241400 100644 --- a/tests/Hedgehog.Tests/PropertyTests.fs +++ b/tests/Hedgehog.Tests/PropertyTests.fs @@ -110,4 +110,25 @@ let propertyTests = testList "Property tests" [ actual =! "false" + testCase "and! syntax is applicative" <| fun () -> + // Based on https://well-typed.com/blog/2019/05/integrated-shrinking/#:~:text=For%20example%2C%20consider%20the%20property%20that + let actual = + property { + let! x = Range.constant 0 1_000_000_000 |> Gen.int32 + and! y = Range.constant 0 1_000_000_000 |> Gen.int32 + return x <= y |> Expect.isTrue + } + |> Property.report + |> Report.render + |> (fun x -> x.Split ([|Environment.NewLine|], StringSplitOptions.None)) + |> Array.item 1 + + let actual = + // normalize printing of a pair between .NET and Fable/JS + actual.Replace("(", "") + .Replace(" ", "") + .Replace(")", "") + + actual =! "1,0" + ]