net.jqwik.engine.properties.arbitraries.GenericEdgeCasesConfiguration Maven / Gradle / Ivy
The newest version!
package net.jqwik.engine.properties.arbitraries;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
import net.jqwik.api.*;
import org.jspecify.annotations.*;
public class GenericEdgeCasesConfiguration implements EdgeCases.Config {
private boolean none;
private final List> filters = new ArrayList<>();
private final List additionalEdgeCases = new ArrayList<>();
@Override
public EdgeCases.Config none() {
none = true;
return this;
}
@Override
public EdgeCases.Config filter(Predicate super T> filter) {
filters.add(filter);
return this;
}
@SafeVarargs
@Override
public final EdgeCases.Config add(T... edgeCases) {
for (T edgeCase : edgeCases) {
checkEdgeCaseIsValid(edgeCase);
additionalEdgeCases.add(edgeCase);
}
return this;
}
// Override in subclasses if there is anything to check
protected void checkEdgeCaseIsValid(T edgeCase) {
}
@SafeVarargs
@Override
public final EdgeCases.Config includeOnly(T... includedValues) {
List values = Arrays.asList(includedValues);
return filter(values::contains);
}
public EdgeCases configure(Consumer super EdgeCases.Config> configurator, Function super Integer, ? extends EdgeCases> edgeCasesCreator, int maxEdgeCases) {
configurator.accept(this);
EdgeCases configuredEdgeCases;
if (none) {
configuredEdgeCases = EdgeCases.none();
} else if (filters.isEmpty()) {
configuredEdgeCases = edgeCasesCreator.apply(maxEdgeCases);
} else {
configuredEdgeCases = edgeCasesCreator.apply(Integer.MAX_VALUE);
}
List>> suppliers = configuredEdgeCases.suppliers();
for (Predicate super T> filter : new ArrayList<>(filters)) {
suppliers = suppliers.stream().filter(s -> filter.test(s.get().value())).collect(Collectors.toList());
}
for (T additionalEdgeCase : additionalEdgeCases) {
suppliers.add(() -> createShrinkable(additionalEdgeCase));
}
return EdgeCasesSupport.fromSuppliers(suppliers);
}
protected Shrinkable createShrinkable(T additionalEdgeCase) {
return Shrinkable.unshrinkable(additionalEdgeCase);
}
}