Gen.sequence忽略给定Traversable的大小

时间:2019-06-29 18:40:53

标签: scala scalatest scalacheck

Gen.sequence似乎忽略了给定Traversable的大小。这是设计使然吗?我在Scala 2.13中使用1.14.0版。跟随发电机

Gen.sequence[List[Int], Int](List.fill(3)(Gen.const(5)))

有时会生成大小为1的列表。我想念什么?

样本测试

import org.scalacheck.Gen
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{Assertions, FlatSpec, Inside, Inspectors, Matchers, OptionValues}
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks

class RuleSpec extends FlatSpec with Matchers with Assertions with OptionValues with Inside with Inspectors with ScalaFutures with ScalaCheckPropertyChecks {

  val filterGen = Gen.listOfN(3, Gen.const(5))
  val inputGen = Gen.pick(10, 5 to 15).map(_.toList).filter(_.nonEmpty)

  "A HasAny rule with partially matching filter" should "validate input" in {
    forAll(filterGen, inputGen) { case (filter, input) =>
      val result = HasAny(filter: _*).valid(input)
      println(s"$result: $filter   ${input}\n")
      result should be(true)
    }
  }
}

1 个答案:

答案 0 :(得分:2)

这可能是由于Test Case Minimisation

  

ScalaCheck的一个有趣的功能是,如果它找到一个参数   伪造属性,它会在尝试最小化该参数之前   据报道。当您使用   Prop.propertyProp.forAll方法来创建属性,但是如果   您使用Prop.forAllNoShrink

因此尝试像这样使用Prop.forAllNoShrink

Prop.forAllNoShrink(filterGen, inputGen) { case (filter, input) => ...

如果使用ScalaTest的forAll,则可以通过创建以下特征来尝试Add support for not shrinking values in GeneratorDrivenPropertyChecks #584中的建议

trait NoShrink {
  implicit def noShrink[A]: Shrink[A] = Shrink(_ => Stream.empty)
}

像这样混入规格

class RuleSpec extends FlatSpec ... with ScalaCheckPropertyChecks with NoShrink {
  ...
    forAll(filterGen, inputGen) { case (filter, input) => ...