GeneratorsLike conditional Properties, we can write conditional Generators using the suchThat method like so:
val smallEvenInteger = Gen.choose(0,200) suchThat (_ % 2 == 0)
for-yieldGenerators 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 methodschooseWe can use choose to generate a numeric values in an inclusive range.
val ages = Gen.choose(18, 100)
listOfgenerate a list of n
given Arbitrary[LazyList[Int]] =
Arbitrary { Gen.listOf(Gen.choose(1, 100))
.map { l => LazyList(l*) } }
oneOfWe can use oneOf to randomly pick some of its arguments.
val vowel = Gen.oneOf('A', 'E', 'I', 'O', 'U', 'Y')
atLeastOneWe can generate a collection with at least one element of the list of arguments.
someOfWe can also create a collection with zero or more of the arguments.