Package io.vavr.test
Interface Gen<T>
- Type Parameters:
T
- type of generated objects
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Generators are the building blocks for providing arbitrary objects.
To ease the creation of Arbitraries, Gen is a FunctionalInterface which extends Function<Random, T>
.
Gen objects are obtained via one of the methods choose
, fail
, frequency
, of
and
oneOf
.
Given Gen objects may be transformed using one of the methods filter
, map
and flatMap
.
A simple way to obtain an Arbitrary of a Gen is to call arbitrary(). This will ignore the size hint of Arbitrary.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionFunctional interface of this generator.Converts this Gen to an Arbitrarychoose
(char... characters) Chooses a char from all chars in the arraychoose
(char min, char max) Chooses a char between min and max, bounds inclusive and chars distributed according to the underlying random number generator.choose
(double min, double max) Chooses a double between min and max, bounds inclusive and numbers distributed according to the distribution of the underlying random number generator.choose
(int min, int max) Chooses an int between min and max, bounds inclusive and numbers distributed according to the distribution of the underlying random number generator.choose
(long min, long max) Chooses a long between min and max, bounds inclusive and numbers distributed according to the distribution of the underlying random number generator.Chooses an enum value from all the enum constants defined in the enumerated type.static <T> Gen
<T> Chooses a value from all values in the iterablestatic <T> Gen
<T> choose
(T... values) Chooses a value from all values in the array.static <T> Gen
<T> fail()
A failing generator which throws a RuntimeException("failed").static <T> Gen
<T> A failing generator which throws a RuntimeException.Returns a generator based on this generator which produces values that fulfill the given predicate.default <U> Gen
<U> Maps generated Ts to Us.static <T> Gen
<T> Chooses one of the given generators according to their frequency.static <T> Gen
<T> Chooses one of the given generators according to their frequency.intersperse
(Gen<T> other) Intersperse values from this generator instance with those of another.default <U> Gen
<U> Maps generated Ts to Us.static <T> Gen
<T> of
(T t) A generator which constantly returns t.static <T> Gen
<T> static <T> Gen
<T> Randomly chooses one of the given generators.static <T> Gen
<T> Randomly chooses one of the given generators.default <U> U
Transforms thisGen
.
-
Method Details
-
apply
Functional interface of this generator.- Parameters:
random
- a random number generator- Returns:
- A generated value of type T.
-
of
A generator which constantly returns t.- Type Parameters:
T
- Type of t.- Parameters:
t
- A value.- Returns:
- A new T generator
-
of
-
choose
Chooses an int between min and max, bounds inclusive and numbers distributed according to the distribution of the underlying random number generator.Note: min and max are internally swapped if min > max.
- Parameters:
min
- lower boundmax
- upper bound- Returns:
- A new int generator
-
choose
Chooses a long between min and max, bounds inclusive and numbers distributed according to the distribution of the underlying random number generator.Note: min and max are internally swapped if min > max.
- Parameters:
min
- lower boundmax
- upper bound- Returns:
- A new long generator
-
choose
Chooses a double between min and max, bounds inclusive and numbers distributed according to the distribution of the underlying random number generator.Note: min and max are internally swapped if min > max.
- Parameters:
min
- lower boundmax
- upper bound- Returns:
- A new double generator
- Throws:
IllegalArgumentException
- if min or max is infinite, min or max is not a number (NaN)
-
choose
Chooses a char between min and max, bounds inclusive and chars distributed according to the underlying random number generator.Note: min and max are internally swapped if min > max.
- Parameters:
min
- lower boundmax
- upper bound- Returns:
- A new char generator
-
choose
Chooses a char from all chars in the array- Parameters:
characters
- array with the characters to choose from- Returns:
- A new array generator
-
choose
Chooses an enum value from all the enum constants defined in the enumerated type.- Type Parameters:
T
- type of enum constants- Parameters:
clazz
- Enum class- Returns:
- A new enum generator
-
choose
Chooses a value from all values in the array.- Type Parameters:
T
- value type- Parameters:
values
- array with the values to choose from- Returns:
- A new array generator
-
choose
Chooses a value from all values in the iterable- Type Parameters:
T
- value type- Parameters:
values
- iterable with the values to choose from.- Returns:
- A new iterable generator
-
fail
A failing generator which throws a RuntimeException("failed").- Type Parameters:
T
- Type of values theoretically generated.- Returns:
- A new generator which always fails with the message "failed"
-
fail
A failing generator which throws a RuntimeException.- Type Parameters:
T
- Type of values theoretically generated.- Parameters:
message
- Message thrown.- Returns:
- A new generator which always fails with the given message
-
frequency
Chooses one of the given generators according to their frequency. Only generators with positive frequencies are used in returned generator.- Type Parameters:
T
- Type to be generated- Parameters:
generators
- A non-empty array of Tuples (frequency, generator)- Returns:
- A new T generator
- Throws:
NullPointerException
- if generators is nullIllegalArgumentException
- if generators doesn't contain any generator with positive frequency
-
frequency
Chooses one of the given generators according to their frequency. Only generators with positive frequencies ares used in returned generator.- Type Parameters:
T
- Type to be generated- Parameters:
generators
- A non-empty traversable of Tuples (frequency, generator)- Returns:
- A new T generator
- Throws:
NullPointerException
- if generators is nullIllegalArgumentException
- if generators doesn't contain any generator with positive frequency
-
intersperse
Intersperse values from this generator instance with those of another.- Parameters:
other
- another T generator to accept values from.- Returns:
- A new T generator
-
oneOf
Randomly chooses one of the given generators.- Type Parameters:
T
- Type to be generated- Parameters:
generators
- A non-empty array of generators- Returns:
- A new T generator
- Throws:
NullPointerException
- if generators is nullIllegalArgumentException
- if generators is empty
-
oneOf
Randomly chooses one of the given generators.- Type Parameters:
T
- Type to be generated- Parameters:
generators
- A non-empty Iterable of generators- Returns:
- A new T generator
- Throws:
NullPointerException
- if generators is nullIllegalArgumentException
- if generators is empty
-
arbitrary
Converts this Gen to an Arbitrary- Returns:
- An arbitrary which returns this generator regardless of the provided size hint n
-
filter
Returns a generator based on this generator which produces values that fulfill the given predicate.- Parameters:
predicate
- A predicate- Returns:
- A new generator
-
flatMap
Maps generated Ts to Us.- Type Parameters:
U
- Type of generated objects of the new generator- Parameters:
mapper
- A function that maps a generated T to a new generator which generates objects of type U.- Returns:
- A new generator
-
map
Maps generated Ts to Us.- Type Parameters:
U
- Type of the mapped object- Parameters:
mapper
- A function that maps a generated T to an object of type U.- Returns:
- A new generator
-
peek
-
transform
Transforms thisGen
.- Type Parameters:
U
- Type of transformation result- Parameters:
f
- A transformation- Returns:
- An instance of type
U
- Throws:
NullPointerException
- iff
is null
-