如何为第二个参数someBoundedInt编写一个生成器,它将在为minmaxBound生成的值之间随机生成一个Int?
val boundedIntProperty = forAll {
(minmaxBound: (Int,Int), someBoundedInt: Int) =>
minmaxBound._1 <= someBoundedInt && someBoundedInt <= minmaxBound._2
}
答案 0 :(得分:6)
您可以将呼叫嵌套到forAll
,如下所示:
val boundedIntProperty = forAll { (minBound: Int, maxBound: Int) =>
forAll( Gen.choose(minBound, maxBound) ) { someBoundedInt =>
...
}
}
请注意,上面minBound
有时可能会大于maxBound
,这会使Gen.choose
失败(不会产生值)。所以你可能也希望以更智能的方式生成你的界限。