scalacheck案例类随机数据生成器

scalacheck案例类随机数据生成器

本文介绍了scalacheck案例类随机数据生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试使用Scalacheck生成随机数据.我有一个带有许多属性的案例类层次结构.到目前为止,我发现填充案例类的唯一方法是这样的:

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
  )

对于具有10到20个属性的案例类,这非常繁琐.我想知道是否有某种方式可以使其自动化...

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

推荐答案

我确信有人会提出一个使用变形.但是,有一些辅助方法可以从可变性的函数生成Gen [T]实例,这些方法可以与case类伴侣对象的apply方法一起使用

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)

这篇关于scalacheck案例类随机数据生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:01