spock-genesis
spock-genesis copied to clipboard
Add flatMap() on Generators
Sometimes I want to control a generator based on the value generated from another. A simple case is where some value should be one of several subclasses. For example flatMap
on generators would allow an integer to be generated, and one of the subclasses generated based on the value of the integer.
Something like:
class Data {}
class A extends Data {}
class B extends Data {}
class C extends Data {}
class DataContainer { Data data }
Gen.type(DataContainer,
data: Gen.integer(0..2).flatMap{ Gen.type([A,B,C][it]) }
)
Interesting, I have been swamped with work and home stuff recently so I apologize for not getting to this sooner. I will have some time this weekend to think about an implementation unless you have already thought about how you might accomplish this.