net.jqwik.engine.facades.ArbitrariesFacadeImpl Maven / Gradle / Ivy
package net.jqwik.engine.facades;
import java.util.*;
import java.util.function.*;
import net.jqwik.api.*;
import net.jqwik.api.arbitraries.*;
import net.jqwik.api.domains.*;
import net.jqwik.api.providers.*;
import net.jqwik.api.stateful.*;
import net.jqwik.engine.properties.*;
import net.jqwik.engine.properties.arbitraries.*;
import net.jqwik.engine.properties.arbitraries.exhaustive.*;
import net.jqwik.engine.properties.arbitraries.randomized.*;
import net.jqwik.engine.properties.stateful.*;
/**
* Is loaded through reflection in api module
*/
public class ArbitrariesFacadeImpl extends Arbitraries.ArbitrariesFacade {
@Override
public RandomGenerator randomChoose(List values) {
return RandomGenerators.choose(values);
}
@Override
public Optional> exhaustiveChoose(List values) {
return ExhaustiveGenerators.choose(values);
}
@Override
public RandomGenerator randomChoose(char[] values) {
return RandomGenerators.choose(values);
}
@Override
public Optional> exhaustiveChoose(char[] values) {
return ExhaustiveGenerators.choose(values);
}
@Override
public Optional> exhaustiveCreate(Supplier supplier) {
return ExhaustiveGenerators.create(supplier);
}
@SuppressWarnings("unchecked")
@Override
public RandomGenerator randomChoose(Class enumClass) {
return RandomGenerators.choose(enumClass);
}
@SuppressWarnings("unchecked")
@Override
public Optional> exhaustiveChoose(Class enumClass) {
return ExhaustiveGenerators.choose(enumClass);
}
@Override
public Arbitrary oneOf(List> all) {
return new OneOfArbitrary<>(all);
}
@Override
public RandomGenerator randomFrequency(List> frequencies) {
return RandomGenerators.frequency(frequencies);
}
@Override
public RandomGenerator randomSamples(T[] samples) {
return RandomGenerators.samples(samples);
}
@Override
public RandomGenerator> randomShuffle(List values) {
return RandomGenerators.shuffle(values);
}
@Override
public Optional>> exhaustiveShuffle(List values) {
return ExhaustiveGenerators.shuffle(values);
}
@Override
public ActionSequenceArbitrary sequences(Arbitrary> actionArbitrary) {
return new DefaultActionSequenceArbitrary<>(actionArbitrary);
}
@Override
public Arbitrary frequencyOf(List>> frequencies) {
return new FrequencyOfArbitrary<>(frequencies);
}
@Override
public IntegerArbitrary integers() {
return new DefaultIntegerArbitrary();
}
@Override
public LongArbitrary longs() {
return new DefaultLongArbitrary();
}
@Override
public BigIntegerArbitrary bigIntegers() {
return new DefaultBigIntegerArbitrary();
}
@Override
public FloatArbitrary floats() {
return new DefaultFloatArbitrary();
}
@Override
public BigDecimalArbitrary bigDecimals() {
return new DefaultBigDecimalArbitrary();
}
@Override
public DoubleArbitrary doubles() {
return new DefaultDoubleArbitrary();
}
@Override
public ByteArbitrary bytes() {
return new DefaultByteArbitrary();
}
@Override
public ShortArbitrary shorts() {
return new DefaultShortArbitrary();
}
@Override
public StringArbitrary strings() {
return new DefaultStringArbitrary();
}
@Override
public CharacterArbitrary chars() {
return new DefaultCharacterArbitrary();
}
@Override
public Arbitrary lazy(Supplier> arbitrarySupplier) {
return new LazyArbitrary<>(arbitrarySupplier);
}
@Override
public Arbitrary defaultFor(Class type, Class>[] typeParameters) {
TypeUsage[] genericTypeParameters =
Arrays.stream(typeParameters)
.map(TypeUsage::of)
.toArray(TypeUsage[]::new);
return defaultFor(TypeUsage.of(type, genericTypeParameters));
}
@Override
public Arbitrary defaultFor(TypeUsage typeUsage) {
// Lazy evaluation is necessary since defaults only exist in context of a domain
// and domains are only present during execution of a property
return Arbitraries.lazy(() -> {
Set> arbitraries = allDefaultsFor(typeUsage);
if (arbitraries.isEmpty()) {
throw new CannotFindArbitraryException(typeUsage);
}
List> arbitrariesList = new ArrayList<>();
//noinspection unchecked
arbitraries.forEach(arbitrary -> arbitrariesList.add((Arbitrary) arbitrary));
return Arbitraries.oneOf(arbitrariesList);
});
}
@Override
public TypeArbitrary forType(Class targetType) {
return new DefaultTypeArbitrary<>(targetType).useDefaults();
}
private static Set> allDefaultsFor(TypeUsage typeUsage) {
DomainContext domainContext = DomainContextFacadeImpl.currentContext.get();
RegisteredArbitraryResolver defaultArbitraryResolver =
new RegisteredArbitraryResolver(domainContext.getArbitraryProviders());
ArbitraryProvider.SubtypeProvider subtypeProvider = ArbitrariesFacadeImpl::allDefaultsFor;
return defaultArbitraryResolver.resolve(typeUsage, subtypeProvider);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy