Top Description Methods
io.vavr.test

public Interface Arbitrary<T>

Annotations
@FunctionalInterface
Type Parameters
<T>
The type of the arbitrary object.
Imports
io.vavr.collection.List, .Stream, java.time.Duration, .LocalDateTime, java.time.temporal.ChronoUnit, java.util.Comparator, .Objects, java.util.function.Consumer, .Function, .Predicate

Represents an arbitrary object of type T.
Author
Daniel Dietrich

Method Summary

Modifier and TypeMethod and Description
public Gen<T>

Returns:

A generator for objects of type T.
apply
(int
A (not necessarily positive) size parameter which may be interpreted individually and is constant for all arbitrary objects regarding one property check.
size
)

Returns a generator for objects of type T.

public default Arbitrary<T>

Returns:

A new generator
distinct
()

Returns an Arbitrary based on this Arbitrary which produces unique values.

public default Arbitrary<T>

Returns:

A new generator
distinctBy
(Comparator<? super T>
A comparator
comparator
)

Returns an Arbitrary based on this Arbitrary which produces unique values based on the given comparator.

public default <
key type
U
>
Arbitrary<T>

Returns:

A new generator
distinctBy
(Function<? super T, ? extends U>
A function
keyExtractor
)

Returns an Arbitrary based on this Arbitrary which produces unique values based on the given function.

public default Arbitrary<T>

Returns:

A new generator
filter
(Predicate<? super T>
A predicate
predicate
)

Returns an Arbitrary based on this Arbitrary which produces values that fulfill the given predicate.

public default <
New type of arbitrary objects
U
>
Arbitrary<U>

Returns:

A new Arbitrary
flatMap
(Function<? super T, ? extends Arbitrary<? extends U>>
A function that maps arbitrary Ts to arbitrary Us given a mapper.
mapper
)

Maps arbitrary objects T to arbitrary object U.

public static Arbitrary<Integer>

Returns:

A new Arbitrary of Integer
integer
()

Generates arbitrary integer values.

public default Arbitrary<T>

Returns:

A new T arbitrary
intersperse
(Arbitrary<T>
another T arbitrary to accept values from.
other
)

Intersperses values from this arbitrary instance with those of another.

public static <
Component type of the List
T
>
Arbitrary<List<T>>

Returns:

a new Arbitrary of List<T>
list
(Arbitrary<T>
Arbitrary elements of type T
arbitraryT
)

Generates arbitrary lists based on a given element generator arbitraryT.

public static Arbitrary<LocalDateTime>

Returns:

A new Arbitrary of LocalDateTime
localDateTime
()

Generates arbitrary LocalDateTimes with LocalDateTime#now() as median and ChronoUnit#DAYS as chronological unit.

public static Arbitrary<LocalDateTime>

Returns:

A new Arbitrary of LocalDateTime
localDateTime
(ChronoUnit
Chronological unit of size
unit
)

Generates arbitrary LocalDateTimes with LocalDateTime#now() as median.

public static Arbitrary<LocalDateTime>

Returns:

A new Arbitrary of LocalDateTime
localDateTime
(LocalDateTime
Center of the LocalDateTime range
median
,
ChronoUnit
Chronological unit of size
unit
)

Generates arbitrary LocalDateTimes.

public default <
Type of the mapped object
U
>
Arbitrary<U>

Returns:

A new generator
map
(Function<? super T, ? extends U>
A function that maps an arbitrary T to an object of type U.
mapper
)

Maps arbitrary objects T to arbitrary object U.

public static <
Type of generator value
U
>
Arbitrary<U>

Returns:

A new generator
of
(U...
A fixed set of values
values
)

Generates an arbitrary value from a fixed set of values

public static <
Type of generator value
U
>
Arbitrary<U>

Returns:

A new generator
ofAll
(Gen<U>
A generator to produce arbitrary values
generator
)

Generates an arbitrary value from a given generator

public default Arbitrary<T>
peek(Consumer<? super T> action)

public static <
Component type of the Stream
T
>
Arbitrary<Stream<T>>

Returns:

a new Arbitrary of Stream<T>
stream
(Arbitrary<T>
Arbitrary elements of type T
arbitraryT
)

Generates arbitrary streams based on a given element generator arbitraryT.

public static Arbitrary<String>

Returns:

a new Arbitrary of String
string
(Gen<Character>
A character generator
gen
)

Generates arbitrary strings based on a given alphabet represented by gen.

public default <
Type of transformation result
U
>
U

Returns:

An instance of type U
transform
(Function<? super Arbitrary<T>, ? extends U>
A transformation
f
)

Transforms this Arbitrary.

Method Detail

applyback to summary
public Gen<T> apply(int size)

Returns a generator for objects of type T. Use Gen#map(Function) and Gen#flatMap(Function) to combine object generators.

Example:


// represents arbitrary binary trees of a certain depth n
final class ArbitraryTree implements Arbitrary<BinaryTree<Integer>> {
    @Override
    public Gen<BinaryTree<Integer>> apply(int n) {
        return Gen.choose(-1000, 1000).flatMap(value -> {
                 if (n == 0) {
                     return Gen.of(BinaryTree.leaf(value));
                 } else {
                     return Gen.frequency(
                             Tuple.of(1, Gen.of(BinaryTree.leaf(value))),
                             Tuple.of(4, Gen.of(BinaryTree.branch(apply(n / 2).get(), value, apply(n / 2).get())))
                     );
                 }
        });
    }
}

// tree generator with a size hint of 10
final Gen<BinaryTree<Integer>> treeGen = new ArbitraryTree().apply(10);

// stream sum of tree node values to console for 100 arbitrary trees
Stream.of(() -> treeGen.apply(RNG.get())).map(Tree::sum).take(100).stdout();

Parameters
size:int

A (not necessarily positive) size parameter which may be interpreted individually and is constant for all arbitrary objects regarding one property check.

Returns:Gen<T>

A generator for objects of type T.

distinctback to summary
public default Arbitrary<T> distinct()

Returns an Arbitrary based on this Arbitrary which produces unique values.

Returns:Arbitrary<T>

A new generator

distinctByback to summary
public default Arbitrary<T> distinctBy(Comparator<? super T> comparator)

Returns an Arbitrary based on this Arbitrary which produces unique values based on the given comparator.

Parameters
comparator:Comparator<? super T>

A comparator

Returns:Arbitrary<T>

A new generator

distinctByback to summary
public default <U> Arbitrary<T> distinctBy(Function<? super T, ? extends U> keyExtractor)

Returns an Arbitrary based on this Arbitrary which produces unique values based on the given function.

Parameters
<U>
key type
keyExtractor:Function<? super T, ? extends U>

A function

Returns:Arbitrary<T>

A new generator

filterback to summary
public default Arbitrary<T> filter(Predicate<? super T> predicate)

Returns an Arbitrary based on this Arbitrary which produces values that fulfill the given predicate.

Parameters
predicate:Predicate<? super T>

A predicate

Returns:Arbitrary<T>

A new generator

flatMapback to summary
public default <U> Arbitrary<U> flatMap(Function<? super T, ? extends Arbitrary<? extends U>> mapper)

Maps arbitrary objects T to arbitrary object U.

Parameters
<U>
New type of arbitrary objects
mapper:Function<? super T, ? extends Arbitrary<? extends U>>

A function that maps arbitrary Ts to arbitrary Us given a mapper.

Returns:Arbitrary<U>

A new Arbitrary

integerback to summary
public static Arbitrary<Integer> integer()

Generates arbitrary integer values.

Returns:Arbitrary<Integer>

A new Arbitrary of Integer

intersperseback to summary
public default Arbitrary<T> intersperse(Arbitrary<T> other)

Intersperses values from this arbitrary instance with those of another.

Parameters
other:Arbitrary<T>

another T arbitrary to accept values from.

Returns:Arbitrary<T>

A new T arbitrary

listback to summary
public static <T> Arbitrary<List<T>> list(Arbitrary<T> arbitraryT)

Generates arbitrary lists based on a given element generator arbitraryT.

Example:


Arbitrary.list(Arbitrary.integer());

Parameters
<T>
Component type of the List
arbitraryT:Arbitrary<T>

Arbitrary elements of type T

Returns:Arbitrary<List<T>>

a new Arbitrary of List<T>

localDateTimeback to summary
public static Arbitrary<LocalDateTime> localDateTime()

Generates arbitrary LocalDateTimes with LocalDateTime#now() as median and ChronoUnit#DAYS as chronological unit.

Returns:Arbitrary<LocalDateTime>

A new Arbitrary of LocalDateTime

See Also
localDateTime(LocalDateTime, ChronoUnit)
localDateTimeback to summary
public static Arbitrary<LocalDateTime> localDateTime(ChronoUnit unit)

Generates arbitrary LocalDateTimes with LocalDateTime#now() as median.

Parameters
unit:ChronoUnit

Chronological unit of size

Returns:Arbitrary<LocalDateTime>

A new Arbitrary of LocalDateTime

See Also
localDateTime(LocalDateTime, ChronoUnit)
localDateTimeback to summary
public static Arbitrary<LocalDateTime> localDateTime(LocalDateTime median, ChronoUnit unit)

Generates arbitrary LocalDateTimes. All generated values are drawn from a range with median as center and median +/- size as included boundaries. unit defines the chronological unit of size.

Example:


Arbitrary.localDateTime(LocalDateTime.now(), ChronoUnit.YEARS);

Parameters
median:LocalDateTime

Center of the LocalDateTime range

unit:ChronoUnit

Chronological unit of size

Returns:Arbitrary<LocalDateTime>

A new Arbitrary of LocalDateTime

mapback to summary
public default <U> Arbitrary<U> map(Function<? super T, ? extends U> mapper)

Maps arbitrary objects T to arbitrary object U.

Parameters
<U>
Type of the mapped object
mapper:Function<? super T, ? extends U>

A function that maps an arbitrary T to an object of type U.

Returns:Arbitrary<U>

A new generator

ofback to summary
public static <U> Arbitrary<U> of(U... values)

Generates an arbitrary value from a fixed set of values

Parameters
<U>
Type of generator value
values:U[]

A fixed set of values

Returns:Arbitrary<U>

A new generator

Annotations
@SafeVarargs
@SuppressWarnings:varargs
ofAllback to summary
public static <U> Arbitrary<U> ofAll(Gen<U> generator)

Generates an arbitrary value from a given generator

Parameters
<U>
Type of generator value
generator:Gen<U>

A generator to produce arbitrary values

Returns:Arbitrary<U>

A new generator

peekback to summary
public default Arbitrary<T> peek(Consumer<? super T> action)
streamback to summary
public static <T> Arbitrary<Stream<T>> stream(Arbitrary<T> arbitraryT)

Generates arbitrary streams based on a given element generator arbitraryT.

Example:


Arbitrary.stream(Arbitrary.integer());

Parameters
<T>
Component type of the Stream
arbitraryT:Arbitrary<T>

Arbitrary elements of type T

Returns:Arbitrary<Stream<T>>

a new Arbitrary of Stream<T>

stringback to summary
public static Arbitrary<String> string(Gen<Character> gen)

Generates arbitrary strings based on a given alphabet represented by gen.

Example:


Arbitrary.string(
    Gen.frequency(
        Tuple.of(1, Gen.choose('A', 'Z')),
        Tuple.of(1, Gen.choose('a', 'z')),
        Tuple.of(1, Gen.choose('0', '9'))));

Parameters
gen:Gen<Character>

A character generator

Returns:Arbitrary<String>

a new Arbitrary of String

transformback to summary
public default <U> U transform(Function<? super Arbitrary<T>, ? extends U> f)

Transforms this Arbitrary.

Parameters
<U>
Type of transformation result
f:Function<? super Arbitrary<T>, ? extends U>

A transformation

Returns:U

An instance of type U

Exceptions
NullPointerException:
if f is null