Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change recheck API to accept recheck data as string #385

Merged
merged 4 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Rename `Property.failOnFalse` to `Property.falseToFailure` ([#384][384], [@TysonMN][TysonMN])
- Add `BindReturn` to the `property` CE ([#364][364], [@TysonMN][TysonMN])
- A breaking change. Previously, returning a `bool` from a `property` CE (after using `let!`) caused the CE to have return type `Property<unit>`. Now this results in a return type of `Property<bool>`. The previous behavior can now be expressed by piping the `Property<bool>` instance into `Property.falseToFailure`.
- Change recheck API to accept recheck data encoded as `string` ([#385][385], [@TysonMN][TysonMN])

## Version 0.11.1 (2021-11-19)

Expand Down Expand Up @@ -182,6 +183,8 @@
[porges]:
/~https://github.com/porges

[385]:
/~https://github.com/hedgehogqa/fsharp-hedgehog/pull/385
[384]:
/~https://github.com/hedgehogqa/fsharp-hedgehog/pull/384
[382]:
Expand Down
32 changes: 16 additions & 16 deletions src/Hedgehog/Linq/Property.fs
Original file line number Diff line number Diff line change
Expand Up @@ -97,40 +97,40 @@ type PropertyExtensions private () =
property |> Property.falseToFailure |> Property.checkWith config

[<Extension>]
static member Recheck (property : Property, size : Size, seed : Seed) : unit =
static member Recheck (property : Property, recheckData: string) : unit =
let (Property property) = property
Property.recheck size seed property
Property.recheck recheckData property

[<Extension>]
static member Recheck (property : Property, size : Size, seed : Seed, config : Hedgehog.PropertyConfig) : unit =
static member Recheck (property : Property, recheckData: string, config : Hedgehog.PropertyConfig) : unit =
let (Property property) = property
Property.recheckWith size seed config property
Property.recheckWith recheckData config property

[<Extension>]
static member Recheck (property : Property<bool>, size : Size, seed : Seed) : unit =
property |> Property.falseToFailure |> Property.recheck size seed
static member Recheck (property : Property<bool>, recheckData: string) : unit =
property |> Property.falseToFailure |> Property.recheck recheckData

[<Extension>]
static member Recheck (property : Property<bool>, size : Size, seed : Seed, config : Hedgehog.PropertyConfig) : unit =
property |> Property.falseToFailure |> Property.recheckWith size seed config
static member Recheck (property : Property<bool>, recheckData: string, config : Hedgehog.PropertyConfig) : unit =
property |> Property.falseToFailure |> Property.recheckWith recheckData config

[<Extension>]
static member ReportRecheck (property : Property, size : Size, seed : Seed) : Report =
static member ReportRecheck (property : Property, recheckData: string) : Report =
let (Property property) = property
Property.reportRecheck size seed property
Property.reportRecheck recheckData property

[<Extension>]
static member ReportRecheck (property : Property, size : Size, seed : Seed, config : Hedgehog.PropertyConfig) : Report =
static member ReportRecheck (property : Property, recheckData: string, config : Hedgehog.PropertyConfig) : Report =
let (Property property) = property
Property.reportRecheckWith size seed config property
Property.reportRecheckWith recheckData config property

[<Extension>]
static member ReportRecheck (property : Property<bool>, size : Size, seed : Seed) : Report =
property |> Property.falseToFailure |> Property.reportRecheck size seed
static member ReportRecheck (property : Property<bool>, recheckData: string) : Report =
property |> Property.falseToFailure |> Property.reportRecheck recheckData

[<Extension>]
static member ReportRecheck (property : Property<bool>, size : Size, seed : Seed, config : Hedgehog.PropertyConfig) : Report =
property |> Property.falseToFailure |> Property.reportRecheckWith size seed config
static member ReportRecheck (property : Property<bool>, recheckData: string, config : Hedgehog.PropertyConfig) : Report =
property |> Property.falseToFailure |> Property.reportRecheckWith recheckData config

[<Extension>]
static member Render (property : Property) : string =
Expand Down
61 changes: 30 additions & 31 deletions src/Hedgehog/Property.fs
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,18 @@ module Property =
//

let private shrinkInput
(args : PropertyArgs)
(recheckType: RecheckType)
(data : RecheckData)
(shrinkLimit : int<shrinks> Option) =
let rec loop
(nshrinks : int<shrinks>)
(Node ((journal, _), xs) : Tree<Journal * Outcome<'a>>) =
let failed =
Failed {
Size = args.Size
Seed = args.Seed
RecheckData = data
Shrinks = nshrinks
Journal = journal
RecheckType = args.RecheckType
RecheckType = recheckType
}
match shrinkLimit, Seq.tryFind (Tree.outcome >> snd >> Outcome.isFailure) xs with
| Some shrinkLimit', _ when nshrinks >= shrinkLimit' -> failed
Expand All @@ -168,7 +168,7 @@ module Property =
else
size + 1

let rec loop args tests discards =
let rec loop data tests discards =
if tests = config.TestLimit then
{ Tests = tests
Discards = discards
Expand All @@ -178,25 +178,25 @@ module Property =
Discards = discards
Status = GaveUp }
else
let seed1, seed2 = Seed.split args.Seed
let result = Random.run seed1 args.Size random
let nextArgs = {
args with
let seed1, seed2 = Seed.split data.Seed
let result = Random.run seed1 data.Size random
let nextData = {
data with
Seed = seed2
Size = nextSize args.Size
Size = nextSize data.Size
}

match snd (Tree.outcome result) with
| Failure ->
{ Tests = tests + 1<tests>
Discards = discards
Status = shrinkInput args config.ShrinkLimit result }
Status = shrinkInput args.RecheckType data config.ShrinkLimit result }
| Success () ->
loop nextArgs (tests + 1<tests>) discards
loop nextData (tests + 1<tests>) discards
| Discard ->
loop nextArgs tests (discards + 1<discards>)
loop nextData tests (discards + 1<discards>)

loop args 0<tests> 0<discards>
loop args.RecheckData 0<tests> 0<discards>

let reportWith (config : PropertyConfig) (p : Property<unit>) : Report =
p |> reportWith' PropertyArgs.init config
Expand All @@ -222,35 +222,34 @@ module Property =
let checkBoolWith (config : PropertyConfig) (g : Property<bool>) : unit =
g |> falseToFailure |> checkWith config

let reportRecheckWith (size : Size) (seed : Seed) (config : PropertyConfig) (p : Property<unit>) : Report =
let reportRecheckWith (recheckData: string) (config : PropertyConfig) (p : Property<unit>) : Report =
let args = {
PropertyArgs.init with
RecheckType = RecheckType.None
Seed = seed
Size = size
RecheckData = recheckData |> RecheckData.deserialize
}
p |> reportWith' args config

let reportRecheck (size : Size) (seed : Seed) (p : Property<unit>) : Report =
p |> reportRecheckWith size seed PropertyConfig.defaultConfig
let reportRecheck (recheckData: string) (p : Property<unit>) : Report =
p |> reportRecheckWith recheckData PropertyConfig.defaultConfig

let reportRecheckBoolWith (size : Size) (seed : Seed) (config : PropertyConfig) (p : Property<bool>) : Report =
p |> falseToFailure |> reportRecheckWith size seed config
let reportRecheckBoolWith (recheckData: string) (config : PropertyConfig) (p : Property<bool>) : Report =
p |> falseToFailure |> reportRecheckWith recheckData config

let reportRecheckBool (size : Size) (seed : Seed) (p : Property<bool>) : Report =
p |> falseToFailure |> reportRecheck size seed
let reportRecheckBool (recheckData: string) (p : Property<bool>) : Report =
p |> falseToFailure |> reportRecheck recheckData

let recheckWith (size : Size) (seed : Seed) (config : PropertyConfig) (p : Property<unit>) : unit =
p |> reportRecheckWith size seed config |> Report.tryRaise
let recheckWith (recheckData: string) (config : PropertyConfig) (p : Property<unit>) : unit =
p |> reportRecheckWith recheckData config |> Report.tryRaise

let recheck (size : Size) (seed : Seed) (p : Property<unit>) : unit =
p |> reportRecheck size seed |> Report.tryRaise
let recheck (recheckData: string) (p : Property<unit>) : unit =
p |> reportRecheck recheckData |> Report.tryRaise

let recheckBoolWith (size : Size) (seed : Seed) (config : PropertyConfig) (g : Property<bool>) : unit =
g |> falseToFailure |> recheckWith size seed config
let recheckBoolWith (recheckData: string) (config : PropertyConfig) (g : Property<bool>) : unit =
g |> falseToFailure |> recheckWith recheckData config

let recheckBool (size : Size) (seed : Seed) (g : Property<bool>) : unit =
g |> falseToFailure |> recheck size seed
let recheckBool (recheckData: string) (g : Property<bool>) : unit =
g |> falseToFailure |> recheck recheckData

let renderWith (n : PropertyConfig) (p : Property<unit>) : string =
p |> reportWith n |> Report.render
Expand Down
11 changes: 6 additions & 5 deletions src/Hedgehog/PropertyArgs.fs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
namespace Hedgehog

[<Struct>]
type PropertyArgs = private {
type PropertyArgs = internal {
RecheckType : RecheckType
Size : Size
Seed : Seed
RecheckData : RecheckData
}

module PropertyArgs =

let init = {
RecheckType = RecheckType.FSharp
Size = 0
Seed = Seed.random ()
RecheckData = {
Size = 0
Seed = Seed.random ()
}
}
47 changes: 37 additions & 10 deletions src/Hedgehog/Report.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ type RecheckType =
| None
| CSharp
| FSharp

type FailureData = {


[<Struct>]
type RecheckData = internal {
Size : Size
Seed : Seed
}

type FailureData = {
RecheckData : RecheckData
Shrinks : int<shrinks>
Journal : Journal
RecheckType : RecheckType
Expand All @@ -29,6 +35,31 @@ type Report = {
Status : Status
}


module internal RecheckData =
open System

let private separator = "_"

let serialize data =
[ string data.Size
string data.Seed.Value
string data.Seed.Gamma ]
|> String.concat separator

let deserialize (s: string) =
try
let parts = s.Split([|separator|], StringSplitOptions.None)
let size = parts.[0] |> Int32.Parse
let seed =
{ Value = parts.[1] |> UInt64.Parse
Gamma = parts.[2] |> UInt64.Parse }
{ Size = size
Seed = seed }
with e ->
raise (ArgumentException("Failed to deserialize RecheckData", e))


module Report =

open System
Expand Down Expand Up @@ -92,17 +123,13 @@ module Report =

| RecheckType.FSharp ->
appendLinef sb "This failure can be reproduced by running:"
appendLinef sb "> Property.recheck %d ({ Value = %A; Gamma = %A }) <property>"
failure.Size
failure.Seed.Value
failure.Seed.Gamma
appendLinef sb "> Property.recheck \"%s\" <property>"
(RecheckData.serialize failure.RecheckData)

| RecheckType.CSharp ->
appendLinef sb "This failure can be reproduced by running:"
appendLinef sb "> property.Recheck(%d, new Seed { Value = %A; Gamma = %A })"
failure.Size
failure.Seed.Value
failure.Seed.Gamma
appendLinef sb "> property.Recheck(\"%s\")"
(RecheckData.serialize failure.RecheckData)

sb.ToString().Trim() // Exclude extra newline.

Expand Down
1 change: 1 addition & 0 deletions tests/Hedgehog.Tests/Hedgehog.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<Compile Include="SeedTests.fs" />
<Compile Include="ShrinkTests.fs" />
<Compile Include="MinimalTests.fs" />
<Compile Include="ReportTests.fs" />
<Compile Include="PropertyTests.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions tests/Hedgehog.Tests/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let allTests = testList "All tests" [
SeedTests.seedTests
ShrinkTests.shrinkTests
MinimalTests.minimalTests
ReportTests.reportTests
PropertyTests.propertyTests
]

Expand Down
22 changes: 22 additions & 0 deletions tests/Hedgehog.Tests/ReportTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Hedgehog.Tests.ReportTests

open Hedgehog

open TestDsl


let reportTests = testList "Report tests" [
testCase "Roundtrip RecheckData serialization" <| fun () ->
property {
let! size = Range.linear 0 1000 |> Gen.int32
let expected = {
Size = size
Seed = Seed.random () }
let actual =
expected
|> RecheckData.serialize
|> RecheckData.deserialize
actual =! expected
}
|> Property.check
]