ScalaCheck 嵌套生成

ScalaCheck nested Gen

在 ScalaCheck 中,mapped/flatMapped Gen 似乎会在任何内部 gen 过滤掉值时立即失败。例如。 (使用 ScalaTest)

class ScalaCheckGen extends FreeSpec with GeneratorDrivenPropertyChecks {
  "Fails" in {
    forAll(Gen.listOfN(100, arbitrary[Int].suchThat(_ % 2 == 0))){
      _ => assert(true)}

    //Gave up after 0 successful property evaluations. 51 evaluations were discarded.
  }

  "Passes" in {
    forAll(Gen.listOfN(100, arbitrary[Int].map(_ * 2))){
      _ => assert(true)}
  }
}

假设上面的简单解决方法不起作用,因为 suchThat 条件很复杂。如何在仍然使用 suchThat 的情况下重写它以使其工作?

suchThat替换为retryUntil,但要小心无限循环。