2

How can I establish conditions on test inputs when performing Property-based testing?

For example, the following code generates bools when I need ints:

Gen.map (fun v -> v > 0) 

Here's the function:

[<Property(QuietOnSuccess = true)>]
let ``number of cells in grid equals rowcount squared`` () =
    let values = Arb.generate<int> |> Gen.map (fun v -> v > 0) 
                                   |> Arb.fromGen

I need something like this so that I can get qualifying ints:

Gen.filter (fun v -> v > 0) 

However, I just don't see an option for this.

Any suggestions?

Guy Coder
  • 22,011
  • 6
  • 54
  • 113
Scott Nimrod
  • 10,451
  • 8
  • 46
  • 94
  • Dude, I'm starting to get the hang of FSharp now. I'm still not on your level though. I took a two week break from coding and now I'm back. – Scott Nimrod May 12 '16 at 14:19
  • Of interest: [FsCheck - generators, shrinkers and Arbitrary instances](https://fscheck.github.io/FsCheck/TestData.html#Test-data-generators-shrinkers-and-Arbitrary-instances) – Guy Coder May 12 '16 at 15:11
  • 1
    `Gen.where` or `Gen.suchThat`? – Mark Seemann May 12 '16 at 15:13
  • Of interest: [FsCheck - Gen](https://fscheck.github.io/FsCheck/reference/fscheck-gen.html) – Guy Coder May 12 '16 at 15:16
  • Of interest: [Use FsCheck to create random dummy data](https://fsharpforfunandprofit.com/posts/low-risk-ways-to-use-fsharp-at-work-3/#test-dummy) – Guy Coder May 12 '16 at 16:54
  • 1
    We've now [added `Gen.filter` to FsCheck](https://github.com/fscheck/FsCheck/pull/244). – Mark Seemann May 13 '16 at 12:54

1 Answers1

1

Try Gen.suchThat (fun v -> v > 0)

Mark Seemann
  • 209,566
  • 41
  • 390
  • 671
Ray
  • 2,766
  • 13
  • 20