unit-testingscalatestscalacheckproperty-based-testing

Scalacheck, generator for lists between size 5 and 12


I can find many examples of setting maximum sizes for generators, but how do I generate lists between a min and max length?


Solution

  • A neat property about generators is they are composable, so you can simply compose a generator for the length of your list with a listOfN generator.

    for {
      numElems <- Gen.choose(5, 12)
      elems <- Gen.listOfN(numElems, elemGenerator)
    } yield elems