Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scalacheck case class random data generator

I'm trying to generate random data with Scalacheck. I have a hierarchy of case classes with many properties. The only way I've found so far to populate the case classes is like this :

case class Data(a: String,
                b: String,
                c: String)

val genLigneDecompte: Gen[Data] = for {
  ag <- Gen.alphaStr
  bg <- Gen.alphaStr
  cg <- Gen.alphaStr
} yield Data(
    a = ag,
    b = bg,
    c = cg
  )

For a case class with 10-20 properties it's pretty tedious. I was wondering if there was a way to automate it somehow...

like image 432
KaC Avatar asked Oct 27 '15 14:10

KaC


Video Answer


2 Answers

I am sure somebody will come up with a solution that abstracts over arity using shapeless. But there are some helper methods to generate Gen[T] instances from functions of varying arity, which can be used with the apply method of the case class companion object

case class Data(a: String, b: String, c: String)

val dataArb = Arbitrary(Gen.resultOf(Data))
// equivalent to
// val f: (String, String, String) => Data = Data.apply
// val gen: Gen[Data] = Gen.resultOf(f)
// val arb: Arbitrary[Data] = Arbitrary(gen)
like image 160
Rüdiger Klaehn Avatar answered Oct 07 '22 19:10

Rüdiger Klaehn


There is a shapeless based Scalacheck library https://github.com/alexarchambault/scalacheck-shapeless what you might be looking for

like image 30
Vikas Pandya Avatar answered Oct 07 '22 19:10

Vikas Pandya