spock-genesis icon indicating copy to clipboard operation
spock-genesis copied to clipboard

Add flatMap() on Generators

Open neilgall opened this issue 7 years ago • 1 comments

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]) }
)

neilgall avatar May 23 '17 10:05 neilgall

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.

Bijnagte avatar Jun 15 '17 12:06 Bijnagte