@API(status=STABLE,
since="1.0")
public class Arbitraries
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
Arbitraries.ArbitrariesFacade |
Modifier and Type | Method and Description |
---|---|
static BigDecimalArbitrary |
bigDecimals()
Create an arbitrary that generates values of type BigDecimal.
|
static BigIntegerArbitrary |
bigIntegers()
Create an arbitrary that generates values of type BigInteger.
|
static ByteArbitrary |
bytes()
Create an arbitrary that generates values of type Byte.
|
static CharacterArbitrary |
chars()
Create an arbitrary that generates values of type Character.
|
static <T> Arbitrary<T> |
create(java.util.function.Supplier<T> supplier)
Create an arbitrary that will use a supplier to generate a value.
|
static <T> Arbitrary<T> |
defaultFor(java.lang.Class<T> type,
java.lang.Class<?>... typeParameters)
Find a registered arbitrary that will be used to generate values of type T.
|
static <T> Arbitrary<T> |
defaultFor(TypeUsage typeUsage)
Find a registered arbitrary that will be used to generate values of type T.
|
static <T> Arbitrary<T> |
defaultFor(TypeUsage typeUsage,
java.util.function.Function<TypeUsage,Arbitrary<java.lang.Object>> noDefaultResolver)
Find a registered arbitrary that will be used to generate values of type T.
|
static DoubleArbitrary |
doubles()
Create an arbitrary that generates values of type Double.
|
static <K,V> Arbitrary<java.util.Map.Entry<K,V>> |
entries(Arbitrary<K> keysArbitrary,
Arbitrary<V> valuesArbitrary)
Create an arbitrary to create instances of Map.Entry.
|
static FloatArbitrary |
floats()
Create an arbitrary that generates values of type Float.
|
static <T> TypeArbitrary<T> |
forType(java.lang.Class<T> targetType)
Create an arbitrary for type
T that will by default use the type's
public constructors and public factory methods. |
static <T> Arbitrary<T> |
frequency(java.util.List<Tuple.Tuple2<java.lang.Integer,T>> frequencies)
Create an arbitrary that will randomly choose between all given values of the same type T.
|
static <T> Arbitrary<T> |
frequency(Tuple.Tuple2<java.lang.Integer,T>... frequencies)
Create an arbitrary that will randomly choose between all given values of the same type T.
|
static <T> Arbitrary<T> |
frequencyOf(java.util.List<Tuple.Tuple2<java.lang.Integer,Arbitrary<T>>> frequencies)
Create an arbitrary that will randomly choose between all given arbitraries of the same type T.
|
static <T> Arbitrary<T> |
frequencyOf(Tuple.Tuple2<java.lang.Integer,Arbitrary<? extends T>>... frequencies)
Create an arbitrary that will randomly choose between all given arbitraries of the same type T.
|
static <T> Arbitrary<T> |
fromGenerator(RandomGenerator<T> generator)
Create an arbitrary of type T from a corresponding generator of type T.
|
static <T> Arbitrary<T> |
fromGeneratorWithSize(java.util.function.IntFunction<RandomGenerator<T>> generatorSupplier)
Create an arbitrary of type T by supplying a corresponding generator of type T.
|
static IntegerArbitrary |
integers()
Create an arbitrary that generates values of type Integer.
|
static <T> Arbitrary<T> |
just(T value)
Create an arbitrary that will always generate the same value.
|
static <T> Arbitrary<T> |
lazy(java.util.function.Supplier<Arbitrary<T>> arbitrarySupplier)
Create an arbitrary that will evaluate arbitrarySupplier as soon as it is used for generating values.
|
static <T> Arbitrary<T> |
lazyOf(java.util.function.Supplier<Arbitrary<? extends T>> first,
java.util.function.Supplier<Arbitrary<? extends T>>... rest)
Create an arbitrary by lazy supplying one of several arbitraries.
|
static LongArbitrary |
longs()
Create an arbitrary that generates values of type Long.
|
static <K,V> MapArbitrary<K,V> |
maps(Arbitrary<K> keysArbitrary,
Arbitrary<V> valuesArbitrary)
Create an arbitrary to create instances of Map.
|
static Arbitrary<java.lang.Void> |
nothing()
Create an arbitrary that never creates anything.
|
static Arbitrary<java.lang.Character> |
of(char[] values)
Create an arbitrary of character values.
|
static <T extends java.lang.Enum<T>> |
of(java.lang.Class<T> enumClass)
Create an arbitrary for enum values of type T.
|
static <T> Arbitrary<T> |
of(java.util.Collection<T> values)
Create an arbitrary that will randomly choose from a given collection of values.
|
static <T> Arbitrary<T> |
of(T... values)
Create an arbitrary that will randomly choose from a given array of values.
|
static <T> Arbitrary<T> |
ofSuppliers(java.util.Collection<java.util.function.Supplier<T>> valueSuppliers)
Create an arbitrary that will randomly choose from a given collection of value suppliers
and then get the value from the supplier.
|
static <T> Arbitrary<T> |
ofSuppliers(java.util.function.Supplier<T>... valueSuppliers)
Create an arbitrary that will randomly choose from a given array of value suppliers
and then get the value from the supplier.
|
static <T> Arbitrary<T> |
oneOf(Arbitrary<? extends T> first,
Arbitrary<? extends T>... rest)
Create an arbitrary that will randomly choose between all given arbitraries of the same type T.
|
static <T> Arbitrary<T> |
oneOf(java.util.Collection<Arbitrary<? extends T>> choices)
Create an arbitrary that will randomly choose between all given arbitraries of the same type T.
|
static Arbitrary<java.util.Random> |
randoms()
Create an arbitrary for Random objects.
|
static <T> Arbitrary<T> |
randomValue(java.util.function.Function<java.util.Random,T> generator)
Create an arbitrary that will generate values of type T using a generator function.
|
static <T> Arbitrary<T> |
recursive(java.util.function.Supplier<Arbitrary<T>> base,
java.util.function.Function<Arbitrary<T>,Arbitrary<T>> recur,
int depth)
Create an arbitrary by deterministic recursion.
|
static <T> Arbitrary<T> |
recursive(java.util.function.Supplier<Arbitrary<T>> base,
java.util.function.Function<Arbitrary<T>,Arbitrary<T>> recur,
int minDepth,
int maxDepth)
Create an arbitrary by deterministic recursion.
|
static <M> ActionSequenceArbitrary<M> |
sequences(Arbitrary<? extends Action<M>> actionArbitrary)
Create an arbitrary to create a sequence of actions.
|
static ShortArbitrary |
shorts()
Create an arbitrary that generates values of type Short.
|
static <T> Arbitrary<java.util.List<T>> |
shuffle(java.util.List<T> values)
Create an arbitrary that will always generate a list which is a
permutation of the values handed to it.
|
static <T> Arbitrary<java.util.List<T>> |
shuffle(T... values)
Create an arbitrary that will always generate a list which is a
permutation of the values handed to it.
|
static StringArbitrary |
strings()
Create an arbitrary that generates values of type String.
|
static <T> SetArbitrary<T> |
subsetOf(java.util.Collection<T> values)
Create a new arbitrary of element type
Set<T> using the handed in values as elements of the set. |
static <T> SetArbitrary<T> |
subsetOf(T... values)
Create a new arbitrary of element type
Set<T> using the handed in values as elements of the set. |
static <T> TraverseArbitrary<T> |
traverse(java.lang.Class<T> targetType,
TraverseArbitrary.Traverser traverser)
Create an arbitrary for type
T that will try to traverse a type
- and all types it is based on - given a TraverseArbitrary.Traverser strategy. |
public static <T> Arbitrary<T> fromGenerator(RandomGenerator<T> generator)
T
- The type of values to generategenerator
- The generator to be used for generating the values@API(status=EXPERIMENTAL, since="1.8.0") public static <T> Arbitrary<T> fromGeneratorWithSize(java.util.function.IntFunction<RandomGenerator<T>> generatorSupplier)
T
- The type of values to generategeneratorSupplier
- A function to supply a generator instance given the "size" of a generation attemptpublic static <T> Arbitrary<T> randomValue(java.util.function.Function<java.util.Random,T> generator)
T
- The type of values to generategenerator
- The generator function to be used for generating the valuespublic static Arbitrary<java.util.Random> randoms()
@SafeVarargs public static <T> Arbitrary<T> of(T... values)
Use this method only for immutable arrays of immutable values. Changing a value will change subsequently generated values as well. For mutable values use ofSuppliers(Supplier[]) instead. Modifying the array may cause erratic behavior, things may halt and catch fire.
T
- The type of values to generatevalues
- The array of values to choose from@API(status=MAINTAINED, since="1.3.1") public static <T> Arbitrary<T> of(java.util.Collection<T> values)
Use this method only for immutable collections of immutable values. Changing a value will change subsequently generated values as well. For mutable values use ofSuppliers(Collection) instead. Modifying the collection may cause erratic behavior, kittens may die.
T
- The type of values to generatevalues
- The collection of values to choose from@API(status=MAINTAINED, since="1.3.0") @SafeVarargs public static <T> Arbitrary<T> ofSuppliers(java.util.function.Supplier<T>... valueSuppliers)
Use this method instead of of(Object[]) for mutable objects to make sure that changing a generated object will not influence other generated objects.
T
- The type of values to generatevalueSuppliers
- The array of values to choose from@API(status=MAINTAINED, since="1.3.1") public static <T> Arbitrary<T> ofSuppliers(java.util.Collection<java.util.function.Supplier<T>> valueSuppliers)
Use this method instead of of(Collection) for mutable objects to make sure that changing a generated object will not influence other generated objects.
T
- The type of values to generatevalueSuppliers
- The collection of values to choose frompublic static Arbitrary<java.lang.Character> of(char[] values)
values
- The array of characters to choose from.public static <T extends java.lang.Enum<T>> Arbitrary<T> of(java.lang.Class<T> enumClass)
T
- The type of values to generateenumClass
- The enum class.@SafeVarargs public static <T> Arbitrary<T> oneOf(Arbitrary<? extends T> first, Arbitrary<? extends T>... rest)
T
- The type of values to generatefirst
- The first arbitrary to choose formrest
- An array of arbitraries to choose frompublic static <T> Arbitrary<T> oneOf(java.util.Collection<Arbitrary<? extends T>> choices)
T
- The type of values to generatechoices
- A collection of arbitraries to choose from@SafeVarargs public static <T> Arbitrary<T> frequency(Tuple.Tuple2<java.lang.Integer,T>... frequencies)
T
- The type of values to generatefrequencies
- An array of tuples of which the first parameter gives the weight and the second the value.public static <T> Arbitrary<T> frequency(java.util.List<Tuple.Tuple2<java.lang.Integer,T>> frequencies)
T
- The type of values to generatefrequencies
- A list of tuples of which the first parameter gives the weight and the second the value.@SafeVarargs public static <T> Arbitrary<T> frequencyOf(Tuple.Tuple2<java.lang.Integer,Arbitrary<? extends T>>... frequencies)
T
- The type of values to generatefrequencies
- An array of tuples of which the first parameter gives the weight and the second the arbitrary.public static <T> Arbitrary<T> frequencyOf(java.util.List<Tuple.Tuple2<java.lang.Integer,Arbitrary<T>>> frequencies)
T
- The type of values to generatefrequencies
- A list of tuples of which the first parameter gives the weight and the second the arbitrary.public static IntegerArbitrary integers()
public static LongArbitrary longs()
public static BigIntegerArbitrary bigIntegers()
public static FloatArbitrary floats()
public static BigDecimalArbitrary bigDecimals()
public static DoubleArbitrary doubles()
public static ByteArbitrary bytes()
public static ShortArbitrary shorts()
public static StringArbitrary strings()
public static CharacterArbitrary chars()
@API(status=MAINTAINED, since="1.3.2") public static <T> Arbitrary<T> just(T value)
T
- The type of the valuevalue
- The value to "generate".@API(status=MAINTAINED, since="1.1.1") public static <T> Arbitrary<T> create(java.util.function.Supplier<T> supplier)
Mind that within a supplier
you should never use other arbitraries
or do anything non-deterministic.
For exhaustive shrinking all generated values are supposed to have identical behaviour, i.e. that means that only one value is generated per combination.
T
- The type of values to generatesupplier
- The supplier use to generate a value@SafeVarargs public static <T> Arbitrary<java.util.List<T>> shuffle(T... values)
T
- The type of values to generatevalues
- The values to permutepublic static <T> Arbitrary<java.util.List<T>> shuffle(java.util.List<T> values)
T
- The type of values to generatevalues
- The values to permutepublic static <T> Arbitrary<T> defaultFor(java.lang.Class<T> type, java.lang.Class<?>... typeParameters)
T
- The type of values to generatetype
- The type of the value to find an arbitrary fortypeParameters
- The type parameters if type is a generic typeCannotFindArbitraryException
- if there is no registered arbitrary provider to serve this type@API(status=MAINTAINED, since="1.1") public static <T> Arbitrary<T> defaultFor(TypeUsage typeUsage)
The returned arbitrary is lazy, i.e. it be evaluated at generation time to allow domain contexts to be used.
T
- The type of values to generatetypeUsage
- The type of the value to find an arbitrary for@API(status=EXPERIMENTAL, since="1.6.1") public static <T> Arbitrary<T> defaultFor(TypeUsage typeUsage, java.util.function.Function<TypeUsage,Arbitrary<java.lang.Object>> noDefaultResolver)
The returned arbitrary is lazy, i.e. it be evaluated at generation time to allow domain contexts to be used. Those
T
- The type of values to generatetypeUsage
- The type of the value to find an arbitrary fornoDefaultResolver
- Alternative resolution when no default arbitrary can be found at generation time@API(status=MAINTAINED, since="1.2.0") public static <T> TypeArbitrary<T> forType(java.lang.Class<T> targetType)
T
that will by default use the type's
public constructors and public factory methods.T
- The type of values to generatetargetType
- The class of the type to create an arbitrary forTypeArbitrary
@API(status=EXPERIMENTAL, since="1.6.1") public static <T> TraverseArbitrary<T> traverse(java.lang.Class<T> targetType, TraverseArbitrary.Traverser traverser)
T
that will try to traverse a type
- and all types it is based on - given a TraverseArbitrary.Traverser strategy.
By default recursion is disable, i.e. that parameters of creator functions can be resolved by the traverser, have a default arbitrary or generation fails at runtime. Use TraverseArbitrary.enableRecursion() to switch on recursive traversal.
One usage of this traversing mechanism is forType(Class)
which uses #traverse(Class, Traverser)
under the hood.
T
- The type of values to generatetargetType
- The class of the type to create an arbitrary fortraverser
- The traversing strategy specificationTraverseArbitrary
public static <T> Arbitrary<T> lazy(java.util.function.Supplier<Arbitrary<T>> arbitrarySupplier)
This is useful (and necessary) when arbitrary providing functions use other arbitrary providing functions in a recursive way. Without the use of lazy() this would result in a stack overflow. Most of the time, however, using lazyOf(Supplier, Supplier[]) is the better choice because it has significantly better shrinking behaviour.
T
- The type of values to generatearbitrarySupplier
- The supplier function being used to generate an arbitraryrecursive(Supplier, Function, int)
,
lazyOf(Supplier, Supplier[])
public static <T> Arbitrary<T> recursive(java.util.function.Supplier<Arbitrary<T>> base, java.util.function.Function<Arbitrary<T>,Arbitrary<T>> recur, int depth)
Mind that the arbitrary will be created by invoking recursion at arbitrary creation time. Using lazyOf(Supplier, Supplier[]) or lazy(Supplier) instead will recur at value generation time.
T
- The type of values to generatebase
- The supplier returning the recursion's base caserecur
- The function to extend the base casedepth
- The number of times to invoke recursionlazy(Supplier)
@API(status=MAINTAINED, since="1.6.4") public static <T> Arbitrary<T> recursive(java.util.function.Supplier<Arbitrary<T>> base, java.util.function.Function<Arbitrary<T>,Arbitrary<T>> recur, int minDepth, int maxDepth)
Mind that the arbitrary will be created by invoking recursion at arbitrary creation time. Using lazyOf(Supplier, Supplier[]) or lazy(Supplier) instead will recur at value generation time.
T
- The type of values to generatebase
- The supplier returning the recursion's base caserecur
- The function to extend the base caseminDepth
- The minimum number of times to invoke recursionmaxDepth
- The maximum number of times to invoke recursionlazy(Supplier)
@SafeVarargs @API(status=MAINTAINED, since="1.3.4") public static <T> Arbitrary<T> lazyOf(java.util.function.Supplier<Arbitrary<? extends T>> first, java.util.function.Supplier<Arbitrary<? extends T>>... rest)
One alternative is to use lazy(Supplier) combined with
oneOf(Arbitrary, Arbitrary[])
or frequencyOf(Tuple.Tuple2[]).
But lazyOf()
has considerably better shrinking behaviour with recursion.
Caveat: Never use this construct if suppliers make use of variable state like method parameters or changing instance members. In those cases use lazy(Supplier) instead.
T
- The type of values to generatefirst
- The first supplier to choose fromrest
- The rest of suppliers to choose fromlazy(Supplier)
,
recursive(Supplier, Function, int)
@API(status=MAINTAINED, since="1.0") public static <M> ActionSequenceArbitrary<M> sequences(Arbitrary<? extends Action<M>> actionArbitrary)
M
- The type of actions to generateactionArbitrary
- The arbitrary to generate individual actions.@API(status=MAINTAINED, since="1.1.6") public static <K,V> MapArbitrary<K,V> maps(Arbitrary<K> keysArbitrary, Arbitrary<V> valuesArbitrary)
K
- type of keysV
- type of valueskeysArbitrary
- The arbitrary to generate the keysvaluesArbitrary
- The arbitrary to generate the values@API(status=MAINTAINED, since="1.2.0") public static <K,V> Arbitrary<java.util.Map.Entry<K,V>> entries(Arbitrary<K> keysArbitrary, Arbitrary<V> valuesArbitrary)
K
- type of keysV
- type of valueskeysArbitrary
- The arbitrary to generate the keysvaluesArbitrary
- The arbitrary to generate the values@API(status=MAINTAINED, since="1.3.0") public static Arbitrary<java.lang.Void> nothing()
@API(status=MAINTAINED, since="1.6.4") public static <T> SetArbitrary<T> subsetOf(java.util.Collection<T> values)
Set<T>
using the handed in values as elements of the set.@SafeVarargs @API(status=MAINTAINED, since="1.6.4") public static <T> SetArbitrary<T> subsetOf(T... values)
Set<T>
using the handed in values as elements of the set.