scalacheck case class random data generator

为君一笑 提交于 2019-11-30 11:43:33

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)

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

Another approach would be DanielaSfregola/random-data-generator, A library to generate random data for test purposes, using ScalaCheck and scalacheck-shapeless.

For Scala 2.11.8 or more, it can generate for you the initialization sequence for a case class with many field.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!