kotest-property / io.kotest.property / Arb

Arb

abstract class 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.

Constructors

<init>

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

Arb()

Functions

edgecases

Edgecase values for this type A.

abstract fun edgecases(): List<A>

values

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

Extension Functions

checkAll

suspend fun <A> Gen<A>.checkAll(property: suspend PropertyContext.(A) -> Unit): PropertyContext
suspend fun <A> Gen<A>.checkAll(iterations: Int, property: suspend PropertyContext.(A) -> Unit): PropertyContext
suspend fun <A> Gen<A>.checkAll(config: PropTestConfig, property: suspend PropertyContext.(A) -> Unit): PropertyContext
suspend fun <A> Gen<A>.checkAll(iterations: Int, config: PropTestConfig, property: suspend PropertyContext.(A) -> Unit): PropertyContext

chunked

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

fun <A> Arb<A>.chunked(size: IntRange): Arb<List<A>>

Returns an Arb whose of values are a list of values generated by the current arb. The size of each list is determined randomly by the specified minSize and maxSize.

fun <A> Arb<A>.chunked(minSize: Int, maxSize: Int): Arb<List<A>>

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>

distinctBy

Returns a new Arb which ensures only distinct keys returned by the given selector function are generated by keeping track of previously generated values.

fun <A, B> Arb<A>.distinctBy(selector: (A) -> B): 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 maps them using the supplied function.

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

forAll

suspend fun <A> Gen<A>.forAll(property: PropertyContext.(A) -> Boolean): PropertyContext
suspend fun <A> Gen<A>.forAll(iterations: Int, property: PropertyContext.(A) -> Boolean): PropertyContext
suspend fun <A> Gen<A>.forAll(config: PropTestConfig, property: PropertyContext.(A) -> Boolean): PropertyContext
suspend fun <A> Gen<A>.forAll(iterations: Int, config: PropTestConfig, property: PropertyContext.(A) -> Boolean): PropertyContext

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 Arb which will merge the values from this Arb and the values of the supplied gen together, taking one from each in turn.

fun <A, B : A> Arb<A>.merge(other: Gen<B>): Arb<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 }, rs: 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. Edgecases will be ignored.

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

Companion Object Extension Functions

arabic

fun Arb.Companion.arabic(): Arb<Codepoint>

armenian

fun Arb.Companion.armenian(): Arb<Codepoint>

ascii

fun Arb.Companion.ascii(): Arb<Codepoint>

bigInt

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

bind

fun <A, B, T : Any> Arb.Companion.bind(genA: Gen<A>, genB: Gen<B>, bindFn: (A, B) -> T): Arb<T>
fun <A, B, C, T : Any> Arb.Companion.bind(genA: Gen<A>, genB: Gen<B>, genC: Gen<C>, bindFn: (A, B, C) -> T): Arb<T>
fun <A, B, C, D, T : Any> Arb.Companion.bind(genA: Gen<A>, genB: Gen<B>, genC: Gen<C>, genD: Gen<D>, bindFn: (A, B, C, D) -> T): Arb<T>
fun <A, B, C, D, E, T : Any> Arb.Companion.bind(genA: Arb<A>, genB: Arb<B>, genC: Arb<C>, genD: Arb<D>, genE: Arb<E>, bindFn: (A, B, C, D, E) -> T): Arb<T>
fun <A, B, C, D, E, F, T : Any> Arb.Companion.bind(genA: Gen<A>, genB: Gen<B>, genC: Gen<C>, genD: Gen<D>, genE: Gen<E>, genF: Gen<F>, bindFn: (A, B, C, D, E, F) -> T): Arb<T>
fun <A, B, C, D, E, F, G, T : Any> Arb.Companion.bind(genA: Gen<A>, genB: Gen<B>, genC: Gen<C>, genD: Gen<D>, genE: Gen<E>, genF: Gen<F>, genG: Gen<G>, bindFn: (A, B, C, D, E, F, G) -> T): Arb<T>
fun <T : Any> Arb.Companion.bind(): 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 gens to generate the next element. The input must be non-empty. The input gens must be infinite.

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>

codepoints

fun Arb.Companion.codepoints(): Arb<Codepoint>

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(edgeCases: List<A>, fn: (RandomSource) -> A): Arb<A>fun <A> Arb.Companion.create(fn: (RandomSource) -> A): Arb<A>

cyrillic

fun Arb.Companion.cyrillic(): Arb<Codepoint>

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>

egyptianHieroglyphs

fun Arb.Companion.egyptianHieroglyphs(): Arb<Codepoint>

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>
fun <T> Arb.Companion.element(vararg collection: T): Arb<T>

email

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

enum

Returns an Arb that produces values randomly from the constants of the supplied enum type. Eg, val arb: Arb = Arb.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>

georgian

fun Arb.Companion.georgian(): Arb<Codepoint>

greekCoptic

fun Arb.Companion.greekCoptic(): Arb<Codepoint>

hebrew

fun Arb.Companion.hebrew(): Arb<Codepoint>

hiragana

fun Arb.Companion.hiragana(): Arb<Codepoint>

instant

Arberates a stream of random Instant

fun Arb.Companion.instant(range: InstantRange): Arb<Instant>
fun Arb.Companion.instant(minValue: Instant = Instant.MIN, maxValue: Instant = Instant.MAX): Arb<Instant>

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>

katakana

fun Arb.Companion.katakana(): Arb<Codepoint>

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.MAX_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.MAX_VALUE, to: Float = Float.MAX_VALUE): Arb<Float>

pair

Returns an Arb that produces Pairs of K,V using the supplied arbs for K and V.

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>

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 within 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. By default the arb uses a ascii codepoint generator, but this can be substituted with any codepoint generator. There are many available, such as katakana and so on.

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

stringPattern

Generate strings that match the given pattern.

fun Arb.Companion.stringPattern(pattern: String): Arb<String>

subsequence

Generates a random subsequence of the input list, including the empty list. The returned list has the same order as the input 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>