Conditional Generators

Like conditional Properties, we can write conditional Generators using the suchThat method like so:

val smallEvenInteger = Gen.choose(0,200) suchThat (_ % 2 == 0)

Combine with for-yield

Generators can be combined with for-yield expressions like so:

val myGen = for {
  n <- Gen.choose(10,20)
  m <- Gen.choose(2*n, 500)
} yield (n,m)

Gen methods

choose

We can use choose to generate a numeric values in an inclusive range.

val ages = Gen.choose(18, 100)

listOf

generate a list of n

given Arbitrary[LazyList[Int]] =
      Arbitrary { Gen.listOf(Gen.choose(1, 100))
        .map { l => LazyList(l*) } }

oneOf

We can use oneOf to randomly pick some of its arguments.

val vowel = Gen.oneOf('A', 'E', 'I', 'O', 'U', 'Y')

atLeastOne

We can generate a collection with at least one element of the list of arguments.

someOf

We can also create a collection with zero or more of the arguments.