Package io.vavr.test
Interface Arbitrary<T>
- Type Parameters:
T
- The type of the arbitrary object.
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Represents an arbitrary object of type T.
-
Method Summary
Modifier and TypeMethodDescriptionapply
(int size) Returns a generator for objects of type T.distinct()
Returns an Arbitrary based on this Arbitrary which produces unique values.distinctBy
(Comparator<? super T> comparator) Returns an Arbitrary based on this Arbitrary which produces unique values based on the given comparator.distinctBy
(Function<? super T, ? extends U> keyExtractor) Returns an Arbitrary based on this Arbitrary which produces unique values based on the given function.Returns an Arbitrary based on this Arbitrary which produces values that fulfill the given predicate.default <U> Arbitrary
<U> Maps arbitrary objects T to arbitrary object U.integer()
Generates arbitrary integer values.intersperse
(Arbitrary<T> other) Intersperses values from this arbitrary instance with those of another.Generates arbitrary lists based on a given element generator arbitraryT.static Arbitrary
<LocalDateTime> Generates arbitraryLocalDateTime
s withLocalDateTime.now()
asmedian
andChronoUnit.DAYS
as chronological unit.static Arbitrary
<LocalDateTime> localDateTime
(LocalDateTime median, ChronoUnit unit) Generates arbitraryLocalDateTime
s.static Arbitrary
<LocalDateTime> localDateTime
(ChronoUnit unit) default <U> Arbitrary
<U> Maps arbitrary objects T to arbitrary object U.static <U> Arbitrary
<U> of
(U... values) Generates an arbitrary value from a fixed set of valuesstatic <U> Arbitrary
<U> Generates an arbitrary value from a given generatorGenerates arbitrary streams based on a given element generator arbitraryT.Generates arbitrary strings based on a given alphabet represented by gen.default <U> U
Transforms thisArbitrary
.
-
Method Details
-
apply
Returns a generator for objects of type T. UseGen.map(Function)
andGen.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
- A (not necessarily positive) size parameter which may be interpreted individually and is constant for all arbitrary objects regarding one property check.- Returns:
- A generator for objects of type T.
-
distinct
Returns an Arbitrary based on this Arbitrary which produces unique values.- Returns:
- A new generator
-
distinctBy
Returns an Arbitrary based on this Arbitrary which produces unique values based on the given comparator.- Parameters:
comparator
- A comparator- Returns:
- A new generator
-
distinctBy
Returns an Arbitrary based on this Arbitrary which produces unique values based on the given function.- Type Parameters:
U
- key type- Parameters:
keyExtractor
- A function- Returns:
- A new generator
-
filter
Returns an Arbitrary based on this Arbitrary which produces values that fulfill the given predicate.- Parameters:
predicate
- A predicate- Returns:
- A new generator
-
flatMap
Maps arbitrary objects T to arbitrary object U.- Type Parameters:
U
- New type of arbitrary objects- Parameters:
mapper
- A function that maps arbitrary Ts to arbitrary Us given a mapper.- Returns:
- A new Arbitrary
-
intersperse
Intersperses values from this arbitrary instance with those of another.- Parameters:
other
- another T arbitrary to accept values from.- Returns:
- A new T arbitrary
-
map
Maps arbitrary objects T to arbitrary object U.- Type Parameters:
U
- Type of the mapped object- Parameters:
mapper
- A function that maps an arbitrary T to an object of type U.- Returns:
- A new generator
-
peek
-
transform
Transforms thisArbitrary
.- Type Parameters:
U
- Type of transformation result- Parameters:
f
- A transformation- Returns:
- An instance of type
U
- Throws:
NullPointerException
- iff
is null
-
of
Generates an arbitrary value from a fixed set of values- Type Parameters:
U
- Type of generator value- Parameters:
values
- A fixed set of values- Returns:
- A new generator
-
ofAll
Generates an arbitrary value from a given generator- Type Parameters:
U
- Type of generator value- Parameters:
generator
- A generator to produce arbitrary values- Returns:
- A new generator
-
integer
Generates arbitrary integer values.- Returns:
- A new Arbitrary of Integer
-
localDateTime
Generates arbitraryLocalDateTime
s withLocalDateTime.now()
asmedian
andChronoUnit.DAYS
as chronological unit.- Returns:
- A new Arbitrary of LocalDateTime
- See Also:
-
localDateTime
- Parameters:
unit
- Chronological unit ofsize
- Returns:
- A new Arbitrary of LocalDateTime
- See Also:
-
localDateTime
Generates arbitraryLocalDateTime
s. All generated values are drawn from a range withmedian
as center andmedian +/- size
as included boundaries.unit
defines the chronological unit ofsize
.Example:
Arbitrary.localDateTime(LocalDateTime.now(), ChronoUnit.YEARS);
- Parameters:
median
- Center of the LocalDateTime rangeunit
- Chronological unit ofsize
- Returns:
- A new Arbitrary of LocalDateTime
-
string
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
- A character generator- Returns:
- a new Arbitrary of String
-
list
Generates arbitrary lists based on a given element generator arbitraryT.Example:
Arbitrary.list(Arbitrary.integer());
- Type Parameters:
T
- Component type of the List- Parameters:
arbitraryT
- Arbitrary elements of type T- Returns:
- a new Arbitrary of List<T>
-
stream
Generates arbitrary streams based on a given element generator arbitraryT.Example:
Arbitrary.stream(Arbitrary.integer());
- Type Parameters:
T
- Component type of the Stream- Parameters:
arbitraryT
- Arbitrary elements of type T- Returns:
- a new Arbitrary of Stream<T>
-