To write your own generator for a type T, you just create an instance of
When writing a custom arbitrary we can use the
arbitrary builder which accepts a lambda that must return the type we are generating for.
The parameter to this lambda is a
RandomSource parameter which contains the seed and the
Random instance. We should typically
use the provided
RandomSource if we need access to a
kotlin.Random instance, as this instance will have been seeded by the framework to allow for repeatable tests.
For example, here is a custom arb that generates a random int between 3 and 6 using the
We can also use this random if we are composing other arbs when building ours.
For example, here is an
Arbitrary that supports a custom class called
Person, delegating to a String arbitrary and an Int arbitrary.
Although in the real world this type of arb would use bind, it demonstrates the principle of composing.
When writing a custom exhaustive we can use the
exhaustive() extension function on a List. Nothing more to it than that really!