3

I have a case class with 250 fields:

case class Data(field1:String, field2:Int, .. )

Is there a way to use scala check to generate values for its parameter

Irrelevant questions I've looked at:
How to generate case objects for every field in a Scala case class using macro?
Dynamically generate case class in Scala
Create an Arbitrary instance for a case class that holds a `Numeric` in ScalaCheck?

Community
  • 1
  • 1
Adrian
  • 5,603
  • 8
  • 53
  • 85
  • You could use the `Generic` approach from the shapeless library to transform an `HList` to a case class instance. There is an example here: https://github.com/milessabin/shapeless/blob/master/examples/src/main/scala/shapeless/examples/csv.scala. – devkat Jul 29 '16 at 15:03

1 Answers1

5

Use scalacheck-shapeless:

import org.scalacheck.Shapeless._
import org.scalacheck.Arbitrary._

implicitly[Arbitrary[Data]]

The above snippet does the following:

  • Summon a shapeless.Generic[Data] via implicit macro to transform you case class into a shapeless.HList
  • Recursively obtain all Arbitrary[String], Arbitrary[Int] and so on for every field
  • Assemble all that into a new Arbitrary[Data] instance
OlivierBlanvillain
  • 7,701
  • 4
  • 32
  • 51