Closed monga closed 6 years ago
There are two methods on Gen that allow a value to be mapped in a random way.
flatMap
doesn't quite work in this scenario as you can't conveniently get a Gen of pairs.
Gen<Integer> g1 = integers().allPositive()
.flatMap(i -> Generate.range(i, Integer.MAX_VALUE));
But mutate
works nicely, when it maps it makes a source of randomness available to the mapping function.
Gen<Pair<Integer,Integer>> g2 = integers().allPositive()
.mutate((i,rand) -> Pair.of(i, Generate.range(i, Integer.MAX_VALUE).generate(rand)));
Thank you: I experimented with flatMap
, but not with mutate
(in fact in mind it was bound to "mutation" testing). It seems a clean solution.
I found myself in need of a dependent generator: I want two variables and the value generated for the second depends on the value generated for the first.
I can filter the pairs with an
assuming
but this reduces the number of examples and it may force a "Gave up after finding only ... examples" error.
My solution was to build my own Gen
Now I can write
Is there a more straightforward way to do this?