Skip to content

Commit

Permalink
Added Gen.optionOf
Browse files Browse the repository at this point in the history
The implementation is essentially a copy of Arb.Default.Option(), which
now calls into Gen.optionOf. While there's a single test of
Arb.Default.Option(), that test doesn't really verify anything. I could
replace the Generator implementation with ` Gen.constant None` without
breaking any tests. This I describe only to defend that this commit,
which adds a new feature, adds no new tests. I can't, however, think of
an appropriate way to test Gen.optionOf in any meaningful way.
Apparently, the author of Arb.Default.Option() couldn't, as well.

The name is chosen to mirror Gen.listOf, Gen.arrayOf, and so on.
  • Loading branch information
ploeh authored and kurtschelfthout committed May 2, 2017
1 parent ff3bfe6 commit 0c3adbd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/FsCheck/Arbitrary.fs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ module Arb =
///Generate an option value that is 'None' 1/8 of the time.
static member Option() =
{ new Arbitrary<option<'a>>() with
override __.Generator = Gen.frequency [(1, gen { return None }); (7, Gen.map Some generate)]
override __.Generator = Gen.optionOf generate
override __.Shrinker o =
match o with
| Some x -> seq { yield None; for x' in shrink x -> Some x' }
Expand Down
6 changes: 5 additions & 1 deletion src/FsCheck/Gen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,11 @@ module Gen =
let! rows = chooseSqrtOfSize
let! cols = chooseSqrtOfSize
return! array2DOfDim (rows,cols) g }


///Generate an option value that is 'None' 1/8 of the time.
//[category: Creating generators from generators]
let optionOf g = frequency [(1, gen { return None }); (7, map Some g)]

///Always generate the same instance v. See also fresh.
//[category: Creating generators]
[<CompiledName("Constant")>]
Expand Down

0 comments on commit 0c3adbd

Please sign in to comment.