net.corda.client.mock / Generator / <init>

<init>

Generator(generate: (Random) -> ErrorOr<A>)

This file defines a basic Generator library for composing random generators of objects.

An object of type Generator<A> captures a generator of As. Generators may be composed in several ways.

Generator.choice picks a generator from the specified list and runs that. Generator.frequency is similar to choice but the probability may be specified for each generator (it is normalised before picking). Generator.combine combines two generators of A and B with a function (A, B) -> C. Variants exist for other arities. Generator.bind sequences two generators using an arbitrary A->Generator function. Keep the usage of this function minimal as it may explode the stack, especially when using recursion.

There are other utilities as well, the type of which are usually descriptive.

Example: val birdNameGenerator = Generator.pickOne(listOf("raven", "pigeon")) val birdHeightGenerator = Generator.doubleRange(from = 10.0, to = 30.0) val birdGenerator = birdNameGenerator.combine(birdHeightGenerator) { name, height -> Bird(name, height) } val birdsGenerator = Generator.replicate(2, birdGenerator) val mammalsGenerator = Generator.sampleBernoulli(listOf(Mammal("fox"), Mammal("elephant"))) val animalsGenerator = Generator.frequency( 0.2 to birdsGenerator, 0.8 to mammalsGenerator ) val animals = animalsGenerator.generate(Random()).getOrThrow()

The above will generate a random list of animals.