Packages

sealed abstract class Gen[+T] extends Serializable

A generator produces values for Props

This module provides:

  1. Definitions for non-arbitrary generators,
  2. Factories to construct generators,
  3. Methods to modify a generator, and
  4. Various combinators for producing generators of values for more complex data types.

Explicit generators aren't required to write Props:

Prop.forAll { (n: Int) =>
  n == n
}

The Prop above is defined with parameters only and without an explicit generator, because generators are implicitly provided by Arbitrary for various data types.

However, it's not uncommon to need to write explicit custom generators:

val genInt: Gen[Int] = Gen.choose(1,10)
Prop.forAll(genInt) { (n: Int) =>
  n == n
}

This is a simple definition of a generator for booleans:

val genBool: Gen[Boolean] = Gen.oneOf(true,false)

The above definition isn't necessary, though. The same boolean generator is defined in Arbitrary as an implicit declaration for automatically parameterizing Props. Instead, use use a generator that is defined in Arbitrary with the polymorphic method Arbitrary.arbitrary and an explicit type parameter:

val genBool: Gen[Boolean] = Arbitrary.arbitrary[Boolean]

Alternatively, this is a boolean generator, but one that always produces true:

val genBool = Gen.const(true)

This is a generator of booleans that is true at a 2-to-1 ratio:

val genBool = Gen.frequency(2 -> true, 1 -> false)

This is a boolean generator that will produce true 75% of the time:

val genBool = Gen.prob(0.75)

For more information on designing custom generators and the motivations for doing so, see chapter 6, Generators in Detail, of the book ScalaCheck: The Definitive Guide (2013) by Rickard Nilsson published by Artima Press.

This is an example of a custom generator for integers:

val genSmallInt: Gen[Int] = Gen.choose(-100,100)

This can be used to generate different collections of zero or more small integers:

val genListOfInts: Gen[List[Int]] = Gen.listOf(genSmallInt)

val genSeqOfInts: Gen[Seq[Int]] = Gen.someOf(-100 to 100)

val genVectorOfInts: Gen[Vector[Int]] = Gen.containerOf[Vector,Int](genSmallInt)

val genMap: Gen[Map[Int,Boolean]] = Gen.mapOf(Gen.zip(genSmallInt, genBool))

val genOptionalInt: Gen[Option[Int]] = Gen.option(genSmallInt)

Or collections of one or more small integers:

val genListOfInts: Gen[List[Int]] = Gen.nonEmptyListOf(genSmallInt)

val genSeqOfInts: Gen[Seq[Int]] = Gen.atLeastOne(-100 to 100)

val genVectorOfInts: Gen[Vector[Int]] = Gen.nonEmptyContainerOf[Vector,Int](genSmallInt)

val genMap: Gen[Map[Int,Boolean]] = Gen.nonEmptyMap(Gen.zip(genSmallInt, genBool))

val genOptionalInt: Gen[Option[Int]] = Gen.some(genSmallInt)

The class methods for Gen should be familiar with those in the Scala collections API:

  • map - Apply a function to generated values
  • flatMap - Apply a function that returns a generator
  • filter - Use values that satisfy a predicate

The Gen class also supports for-comprehensions to compose complex generators:

val genPerson = for {
  firstName <- Gen.oneOf("Alan", "Ada", "Alonzo")
  lastName <- Gen.oneOf("Lovelace", "Turing", "Church")
  age <- Gen.choose(0,100) if (age >= 18)
} yield Person(firstName, lastName, age)

Constructors and factories for generators:

  • const - Always generates a single value
  • oneOf - Generate a value from a list of values
  • atLeastOne - Generate a collection with at least one value from a list
  • someOf - Generate a collection with zero or more values from a list
  • choose - Generate numeric values in an (inclusive) range
  • frequency - Choose from multiple values with a weighted distribution

Combinators of generators:

Methods for working with Gen internals:

  • resize - Creates a resized version of a generator
  • parameterized - Generator with the parameters
  • size - Generate with the value of the default size parameter
  • sized - Build a generator using the default size parameter

Methods for probabilistic generators:

  • exponential - Generate numbers according to an exponential distribution
  • gaussian - Generates numbers according to a Gaussian distribution
  • geometric - Generates numbers according to a geometric distribution
  • poisson - Generates numbers according to a Poisson distribution
  • prob - Generates a boolean for the probability of true

Definitions for generating various, non-arbitrary, common values of strings and characters:

  • alphaChar - Generates an alpha character
  • alphaStr - Generates a string of alpha characters
  • numChar - Generates a numerical character
  • numStr - Generates a string of digits
  • alphaNumChar - Generates an alphanumerical character
  • alphaNumStr - Generates a string of alphanumerical characters
  • alphaLowerChar - Generates a lower-case alpha character
  • alphaLowerStr - Generates a string of lower-case alpha characters
  • alphaUpperChar - Generates an upper-case alpha character
  • alphaUpperStr - Generates a string of upper-case alpha characters
  • asciiChar - Generates an ASCII character
  • asciiStr - Generates a string of ASCII characters
  • identifier - Generates an identifier
  • uuid - Generates a UUID
  • hexChar - Generates a character of a hexadecimal digit
  • hexStr - Generates a string of hexadecimal digits

Definitions for generating arbitrary values of commonly used types in Scala are defined elsewhere, see Arbitrary.

There are a couple of factory methods that are for advanced uses of generators:

  • delay - Generate a value of an expression by-name
  • lzy - Lazily generate a value of an expression
  • fail - Fail to generate any values of a type
  • recursive - A fixed point generator
  • resultOf - Generate values with a function or class
  • zip - Generate tuples
Self Type
Gen[T]
Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Gen
  2. Serializable
  3. AnyRef
  4. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Type Members

  1. case class RetryUntilException(n: Int) extends RuntimeException with Product with Serializable
  2. final class WithFilter extends AnyRef

    A class supporting filtered operations.

Value Members

  1. def !=[U](g: Gen[U]): Prop
  2. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  3. def !==[U](g: Gen[U]): Prop
  4. final def ##: Int
    Definition Classes
    AnyRef → Any
  5. def :|(l: Symbol): Gen[T]

    Put a label on the generator to make test reports clearer

  6. def :|(l: String): Gen[T]

    Put a label on the generator to make test reports clearer

  7. def ==[U](g: Gen[U]): Prop

    Returns a new property that holds if and only if both this and the given generator generates the same result, or both generators generate no result.

  8. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  9. def apply(p: Parameters, seed: Seed): Option[T]

    Evaluate this generator with the given parameters

  10. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  11. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native()
  12. def doPureApply(p: Parameters, seed: Seed, retries: Int = 100): R[T]
  13. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  14. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  15. def filter(p: (T) => Boolean): Gen[T]

    Create a new generator that uses this generator to produce a value that fulfills the given condition.

    Create a new generator that uses this generator to produce a value that fulfills the given condition. If the condition is not fulfilled, the generator fails (returns None). Also, make sure that the provided test property is side-effect free, e.g. it should not use external vars.

  16. def filterNot(p: (T) => Boolean): Gen[T]

    Create a new generator that uses this generator to produce a value that doesn't fulfill the given condition.

    Create a new generator that uses this generator to produce a value that doesn't fulfill the given condition. If the condition is fulfilled, the generator fails (returns None). Also, make sure that the provided test property is side-effect free, e.g. it should not use external vars.

  17. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
  18. def flatMap[U](f: (T) => Gen[U]): Gen[U]

    Create a new generator by flat-mapping the result of this generator

  19. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  20. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  21. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  22. def label(l: String): Gen[T]

    Put a label on the generator to make test reports clearer

  23. def map[U](f: (T) => U): Gen[U]

    Create a new generator by mapping the result of this generator

  24. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  25. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  26. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  27. def pureApply(p: Parameters, seed: Seed, retries: Int = 100): T

    Evaluate this generator with the given parameters.

    Evaluate this generator with the given parameters.

    The generator will attempt to generate a valid T value. If a valid value is not produced it may retry several times, determined by the retries parameter (which defaults to 100).

    If all the retries fail it will throw a Gen.RetrievalError exception.

  28. def retryUntil(p: (T) => Boolean): Gen[T]

    Create a generator that calls this generator repeatedly until the given condition is fulfilled.

    Create a generator that calls this generator repeatedly until the given condition is fulfilled. The generated value is then returned. Make sure that the provided test property is side-effect free (it should not use external vars).

    If the generator fails more than 10000 times, a RetryUntilException will be thrown. You can call retryUntil with a second parameter to change this number.

  29. def retryUntil(p: (T) => Boolean, maxTries: Int): Gen[T]

    Create a generator that calls this generator repeatedly until the given condition is fulfilled.

    Create a generator that calls this generator repeatedly until the given condition is fulfilled. The generated value is then returned. Make sure that the provided test property is side-effect free (it should not use external vars).

    If the generator fails more than maxTries, a RetryUntilException will be thrown.

  30. def sample: Option[T]
  31. def suchThat(f: (T) => Boolean): Gen[T]

    Create a new generator that uses this generator to produce a value that fulfills the given condition.

    Create a new generator that uses this generator to produce a value that fulfills the given condition. If the condition is not fulfilled, the generator fails (returns None). Also, make sure that the provided test property is side-effect free, e.g. it should not use external vars. This method is identical to [Gen.filter].

  32. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  33. def toString(): String
    Definition Classes
    AnyRef → Any
  34. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  35. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  36. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  37. def withFilter(p: (T) => Boolean): WithFilter

    Creates a non-strict filtered version of this generator.

  38. def withPerturb(f: (Seed) => Seed): Gen[T]

    Perform some RNG perturbation before generating

  39. def |:(l: Symbol): Gen[T]

    Put a label on the generator to make test reports clearer

  40. def |:(l: String): Gen[T]

    Put a label on the generator to make test reports clearer

Inherited from Serializable

Inherited from AnyRef

Inherited from Any

Ungrouped