2

I'm just getting started with FsCheck. I need to generate some test data, here TestData represents the input to my test:

type Interval = { Start : DateTime; End : DateTime }
type Frob = { Interval : Interval; MustFrob : bool }

type TestData = { Intervals: Interval[]; Frob : Frob option }

My generation code looks like:

let generator = gen {
    let! startTime = Arb.Default.DateTime().Generator
    return! Gen.sized(fun size -> gen {
         let intervalDurations = Gen.listOfLength size myOwnSuperDurationGenerator
         let intervals = 
              // blabla using startTime and intervalDurations

         let! maybeFrob = Arb.Default.Option<Frob>().Generator
         let adjustedFrob = maybeFrob |> Option.map(fun f ->
              { f with Interval = // mess with the interval }
         return { Intervals = intervals; Frob = adjustedFrob }         
    }
}

So, this works, but I don't like the calls to Arb.Default.Blabla().Generator because I can't pass them the size parameter so my tests don't take advantage of shrinking there. For instance shrinking the Frob option to None, or choosing a simpler startTime. Is there a way to propagate that size parameter when generating single values like this? - also thanks for pointing out any egregious beginner mistakes, I'm really just trying to figure out how to use this API.

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
Asik
  • 21,506
  • 6
  • 72
  • 131
  • I don't think that there's an easy way to get a shrinker out of a complex generator, but you can build your own from existing shrinkers. This question seems reminiscent of this recent question on GitHub: https://github.com/fscheck/FsCheck/issues/298 If you can supply an [MCV](http://stackoverflow.com/help/mcve), I'll be happy to suggest concrete code, but ATM the OP code doesn't compile. – Mark Seemann Aug 16 '16 at 11:23

0 Answers0