kotest-property / io.kotest.property.arbitrary / Arb

Arb

interface Arb<A> : Gen<A>

An Arb (for arbitrary) is a provider of property test data in two categories: edgecases and samples.

Edge cases are values that are a common source of bugs. For example, a function using ints is more likely to fail for common edge cases like zero, minus 1, positive 1, Int.MAX_VALUE and Int.MIN_VALUE rather than random values like 965489. Therefore, edge cases can be included in sequences generated by an arbitrary.

Not all arbitraries will utilize edge cases. For example, if you define an integer generator using a subset of the number space - say from 100 to 250,000 - then no edge cases are provided.

Samples are chosen randomly from the sample space and are used to give a greater breadth to the test cases. For example, in the case of a function using integers, these random values could be from across the entire integer number line, or could be limited to a subset of ints such as natural numbers or even numbers.

In order to use an Arb inside a property test, one must invoke the take method, passing in the number of iterations required and optionally a ShrinkingMode.

Functions

edgecases

Edgecase values for this type A.

abstract fun edgecases(): List<A>

generate

open fun generate(rs: RandomSource): Sequence<Sample<A>>

minIterations

The minimum iteration count required for this Gen to be invoked. Requesting a property test with fewer than this cound will result in an exception.

open fun minIterations(): Int

samples

abstract fun samples(rs: RandomSource): Sequence<Sample<A>>

Extension Functions

distinct

Returns a new Arb which ensures only unique values are generated by keeping track of previously generated values.

fun <A> Arb<A>.distinct(): Arb<A>

filter

Returns a new Arb which takes its elements from the receiver and filters them using the supplied predicate. This gen will continue to request elements from the underlying gen until one satisfies the predicate.

fun <A> Arb<A>.filter(predicate: (A) -> Boolean): Arb<A>

filterIsInstance

Create a new Arb by keeping only instances of B generated by this gen. This is useful if you have a type hierarchy and only want to retain a particular subtype.

fun <A, B : A> Arb<A>.filterIsInstance(): Arb<B>

filterNot

fun <A> Arb<A>.filterNot(f: (A) -> Boolean): Arb<A>

flatMap

Returns a new Arb which takes its elements from the receiver and flat maps them using the supplied function.

fun <A, B> Arb<A>.flatMap(f: (A) -> Arb<B>): Arb<B>

map

Returns a new Arb which takes its elements from the receiver and maps them using the supplied function.

fun <A, B> Arb<A>.map(f: (A) -> B): Arb<B>

merge

Returns a new Gen which will merge the values from this gen and the values of the supplied gen together, taking one from each in turn.

fun <A, B : A> Gen<A>.merge(other: Gen<B>): Gen<A>

next

fun <A> Arb<A>.next(rs: RandomSource = RandomSource.Default): A

Draws a random value from this generator

fun <A> Arb<A>.next(predicate: (A) -> Boolean = { true }, random: RandomSource?): A

single

Returns a single value generated from this arb ignoring edgecases.

fun <A> Arb<A>.single(rs: RandomSource = RandomSource.Default): A

take

Returns a sequence of size count from values generated from this arb.

fun <A> Arb<A>.take(count: Int, rs: RandomSource = RandomSource.Default): Sequence<A>

Companion Object Extension Functions

bigInt

fun Arb.Companion.bigInt(range: IntRange): Arb<BigInteger>

bind

fun <A, T> Arb.Companion.bind(arbA: Arb<A>, createFn: (A) -> T): Arb<T>
fun <A, B, T> Arb.Companion.bind(arbA: Arb<A>, arbB: Arb<B>, createFn: (A, B) -> T): Arb<T>
fun <A, B, C, T> Arb.Companion.bind(arbA: Arb<A>, arbB: Arb<B>, arbC: Arb<C>, createFn: (A, B, C) -> T): Arb<T>
fun <A, B, C, D, T> Arb.Companion.bind(arbA: Arb<A>, arbB: Arb<B>, arbC: Arb<C>, arbD: Arb<D>, createFn: (A, B, C, D) -> T): Arb<T>
fun <A, B, C, D, E, T> Arb.Companion.bind(arbA: Arb<A>, arbB: Arb<B>, arbC: Arb<C>, arbD: Arb<D>, arbE: Arb<E>, createFn: (A, B, C, D, E) -> T): Arb<T>
fun <A, B, C, D, E, F, T> Arb.Companion.bind(arbA: Arb<A>, arbB: Arb<B>, arbC: Arb<C>, arbD: Arb<D>, arbE: Arb<E>, arbF: Arb<F>, createFn: (A, B, C, D, E, F) -> T): Arb<T>
fun <A, B, C, D, E, F, G, T> Arb.Companion.bind(arbA: Arb<A>, arbB: Arb<B>, arbC: Arb<C>, arbD: Arb<D>, arbE: Arb<E>, arbF: Arb<F>, arbG: Arb<G>, createFn: (A, B, C, D, E, F, G) -> T): Arb<T>

bool

fun Arb.Companion.bool(): Arb<Boolean>

byte

The edge cases are [Byte.MIN_VALUE, Byte.MAX_VALUE, 0]

fun Arb.Companion.byte(): Arb<Byte>

char

Returns a stream of randomly-chosen Chars. Custom characters can be generated by providing CharRanges. Distribution will be even across the ranges of Chars. For example: Gen.char('A'..'C', 'D'..'E') Ths will choose A, B, C, D, and E each 20% of the time.

fun Arb.Companion.char(range: CharRange, vararg ranges: CharRange): Arb<Char>

Returns a stream of randomly-chosen Chars. Custom characters can be generated by providing a list of CharRanges. Distribution will be even across the ranges of Chars. For example: Gen.char(listOf('A'..'C', 'D'..'E') Ths will choose A, B, C, D, and E each 20% of the time.

fun Arb.Companion.char(ranges: List<CharRange> = CharSets.BASIC_LATIN): Arb<Char>

choice

Randomly selects one of the given generators to generate the next element. The input must be non-empty.

fun <A> Arb.Companion.choice(vararg gens: Gen<A>): Arb<A>

choose

Returns a stream of values based on weights:

fun <A : Any> Arb.Companion.choose(a: Pair<Int, A>, b: Pair<Int, A>, vararg cs: Pair<Int, A>): Arb<A>

constant

fun <A> Arb.Companion.constant(a: A): Arb<A>

create

Returns an Arb where each value is generated from the given function.

fun <A> Arb.Companion.create(fn: (RandomSource) -> A): Arb<A>

date

fun Arb.Companion.date(minYear: Int = 1970, maxYear: Int = 2030): <ERROR CLASS>
fun Arb.Companion.date(yearRange: IntRange): <ERROR CLASS>

datetime

fun Arb.Companion.datetime(minYear: Int = 1970, maxYear: Int = 2030): <ERROR CLASS>
fun Arb.Companion.datetime(yearRange: IntRange): <ERROR CLASS>

default

fun <A> Arb.Companion.default(): Arb<A>

double

Returns an Arb where each value is a randomly chosen Double.

fun Arb.Companion.double(): Arb<Double>

element

Returns an Arb whose values are chosen randomly from those in the supplied collection. May not cover all items. If you want an exhaustive selection from the list, see Exhaustive.collection

fun <T> Arb.Companion.element(collection: Collection<T>): Arb<T>

email

fun Arb.Companion.email(usernameSize: IntRange = 3..10, domainSize: IntRange = 3..10): Arb<String>

enum

fun <T : Enum<T>> Arb.Companion.enum(): Arb<T>

factors

fun Arb.Companion.factors(k: Int): Arb<Int>

file

Returns an Arb where each value is a randomly created File object. The file objects do not necessarily exist on disk.

fun Arb.Companion.file(): Arb<File>

Returns an Arb where each value is a randomly chosen File object from given directory. If the Directory does not exist, an empty sequence will be returned instead.

fun Arb.Companion.file(directoryName: String, recursive: Boolean = false): Arb<File>

float

Returns an Arb where each value is a Float.

fun Arb.Companion.float(): Arb<Float>

forClass

fun <A> Arb.Companion.forClass(kClass: KClass<*>): Arb<A>

int

fun Arb.Companion.int(min: Int, max: Int): Arb<Int>

Returns an Arb where each value is a randomly chosen Int in the given range. The edgecases are: [Int.MIN_VALUE, Int.MAX_VALUE, 0, 1, -1]

fun Arb.Companion.int(range: IntRange = Int.MIN_VALUE..Int.MAX_VALUE): Arb<Int>

lines

fun Arb.Companion.lines(file: File, charset: Charset = Charsets.UTF_8): Arb<String>

list

Returns an Arb whose of values are a list of values generated by the given generator. The size of each list is determined randomly by the specified range.

fun <A> Arb.Companion.list(gen: Gen<A>, range: IntRange = 0..100): Arb<List<A>>

localDate

Arberates a stream of random LocalDates

fun Arb.Companion.localDate(minYear: Int = 1970, maxYear: Int = 2030): Arb<LocalDate>

localDateTime

Arberates a stream of random LocalDateTimes

fun Arb.Companion.localDateTime(minYear: Int = 1970, maxYear: Int = 2030): Arb<LocalDateTime>

localTime

Arberates a stream of random LocalTimes

fun Arb.Companion.localTime(): Arb<LocalTime>

long

fun Arb.Companion.long(min: Long = Long.MIN_VALUE, max: Long = Long.MAX_VALUE): Arb<Long>
fun Arb.Companion.long(range: LongRange = Long.MIN_VALUE..Long.MAX_VALUE): Arb<Long>

map

Returns an Arb where each generated value is a map, with the entries of the map drawn from the given pair generating arb. The size of each generated map is a random value between the specified min and max bounds.

fun <K, V> Arb.Companion.map(arb: Arb<Pair<K, V>>, minSize: Int = 1, maxSize: Int = 100): Arb<Map<K, V>>

Returns an Arb where each generated value is a map, with the entries of the map drawn by combining values from the key gen and value gen. The size of each generated map is a random value between the specified min and max bounds.

fun <K, V> Arb.Companion.map(keyArb: Arb<K>, valueArb: Arb<V>, minSize: Int = 1, maxSize: Int = 100): Arb<Map<K, V>>

multiples

Returns an Arb where each generated value is a multiple of k between 0 to the specified max.

fun Arb.Companion.multiples(k: Int, max: Int): Arb<Int>

nats

Returns an Arb where each value is a randomly chosen natural integer. The edge cases are: Int.MAX_VALUE

fun Arb.Companion.nats(max: Int = Int.MAX_VALUE): Arb<Int>

negativeDoubles

fun Arb.Companion.negativeDoubles(): Arb<Double>

negativeInts

Returns an Arb where each value is a randomly chosen negative integer. The edge cases are: Int.MIN_VALUE

fun Arb.Companion.negativeInts(min: Int = Int.MIN_VALUE): Arb<Int>

numericDoubles

Returns an Arb which is the same as double but does not include +INFINITY, -INFINITY or NaN.

fun Arb.Companion.numericDoubles(from: Double = Double.MIN_VALUE, to: Double = Double.MAX_VALUE): Arb<Double>

numericFloats

Returns an Arb which is the same as float but does not include +INFINITY, -INFINITY or NaN.

fun Arb.Companion.numericFloats(from: Float = Float.MIN_VALUE, to: Float = Float.MAX_VALUE): Arb<Float>

pair

fun <K, V> Arb.Companion.pair(k: Arb<K>, v: Arb<V>): Arb<Pair<K, V>>

period

Arberates a random Periods.

fun Arb.Companion.period(maxYear: Int = 10): Arb<Period>

positiveDoubles

fun Arb.Companion.positiveDoubles(): Arb<Double>

positiveInts

Returns an Arb where each value is a randomly chosen positive integer. The edge cases are: Int.MAX_VALUE

fun Arb.Companion.positiveInts(max: Int = Int.MAX_VALUE): Arb<Int>

regex

fun Arb.Companion.regex(regex: String): Arb<String!>
fun Arb.Companion.regex(regex: Regex): Arb<String!>

set

Returns an Arb whose of values are a set of values generated by the given element generator. The size of each set is determined randomly by the specified range.

fun <A> Arb.Companion.set(gen: Gen<A>, range: IntRange = 0..100): Arb<Set<A>>

short

The edge cases are [Short.MIN_VALUE, Short.MAX_VALUE, 0]

fun Arb.Companion.short(): Arb<Short>

shuffle

Generates random permutations of a list.

fun <A> Arb.Companion.shuffle(list: List<A>): Arb<List<A>>

string

Returns an Arb where each random value is a String of length between minSize and maxSize.

fun Arb.Companion.string(minSize: Int = 0, maxSize: Int = 100): Arb<String>fun Arb.Companion.string(range: IntRange): Arb<String>

subsequence

Generates a random subsequence, including the empty list.

fun <A> Arb.Companion.subsequence(list: List<A>): Arb<List<A>>

triple

Returns a Arb where each value is a Triple generated by a value from each of three supplied generators.

fun <A, B, C> Arb.Companion.triple(arbA: Arb<A>, arbB: Arb<B>, arbC: Arb<C>): Arb<Triple<A, B, C>>

ushort

The edge cases are [Short.MIN_VALUE, Short.MAX_VALUE, 0]

fun Arb.Companion.ushort(): Arb<UShort>

uuid

fun Arb.Companion.uuid(uuidVersion: UUIDVersion = UUIDVersion.V4, allowNilValue: Boolean = true): Arb<UUID>

Inheritors

BasicArb

interface BasicArb<A> : Arb<A>