当第二代的生成取决于第一个时,如何用2个参数编写测试?

Gui*_*rel 2 scalacheck

如何为第二个参数someBoundedInt编写一个生成器,它将在为minmaxBound生成的值之间随机生成一个Int?

val boundedIntProperty = forAll {
  (minmaxBound: (Int,Int), someBoundedInt: Int) => 
    minmaxBound._1 <= someBoundedInt && someBoundedInt <= minmaxBound._2

}
Run Code Online (Sandbox Code Playgroud)

Ric*_*son 6

您可以将调用嵌套为forAll:

val boundedIntProperty = forAll { (minBound: Int, maxBound: Int) =>
  forAll( Gen.choose(minBound, maxBound) ) { someBoundedInt =>
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,上面minBound可能比maxBound有时更大,这将导致Gen.choose失败(不产生值).因此,您可能希望以更智能的方式生成边界.