Gen.sequence ignores size of given Traversable

Viewed 180

Gen.sequence seems to be ignoring size of given Traversable. Is that by design? I'm using version 1.14.0 with Scala 2.13. Following generator

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

sometimes generates List of size 1. What am I missing ?

Sample test

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 Answers

This might be due to Test Case Minimisation

One interesting feature of ScalaCheck is that if it finds an argument that falsifies a property, it tries to minimise that argument before it is reported. This is done automatically when you use the Prop.property and Prop.forAll methods to create properties, but not if you use Prop.forAllNoShrink.

Hence try using Prop.forAllNoShrink like so

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

If using ScalaTest's forAll then try the suggestion in Add support for not shrinking values in GeneratorDrivenPropertyChecks #584 by creating the following trait

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

and mix it in the spec like so

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