Pangea Pangea - 1 month ago 4x
Scala Question

Defining a ScalaCheck generator in terms of another Gen

How do I define a generator in to produce random data for

of a given
. Following is the scaffolding code. I need help with an expression to replace the
. This expression should generate
, but the
should be generated using defined

def blockGen(b: Block): Gen[Block] = for {
id <- b.blockId //Use the same blockId as b
fields <- ??? //Get the type from each field and call genField
} yield Block(id, fields)


trait Data {}

trait Field extends Data {
val name: String
val value: String

case class StringField(name: String, value: String) extends Field
case class NumberField(name: String, value: String) extends Field
case class Block(blockId: Field, fields: Seq[Field]) extends Data


def fieldGen(fieldType: Field): Gen[Field] = {
for {
f <-
fieldType match {
case _: NumberField => numGen
case _: StringField => strGen
} yield f

val strGen: Gen[StringField] = for {
name <- Gen.identifier
value <- Gen.alphaStr
} yield StringField(name, value)

val numGen: Gen[NumberField] = for {
name <- Gen.identifier
value <- Gen.numStr
} yield NumberField(name, value)

Sample Block: myBlock

val cx = new StringField("blockId", "CX")
val seg = StringField("segmentation", "ABC")
val ver = NumberField("version", "1.0")

val myBlock = Block(cx, Seq(seg, ver))


I believe what you're looking for is Gen.sequence.

Also, id doesn't need to come from a generator, since all you're doing is using a particular value directly.

def blockGen(b: Block): Gen[Block] =
  for {
    fields <- Gen.sequence[Seq[Field], Field](
  } yield Block(b.blockId, fields)