net.jqwik.engine.facades.ShrinkingSupportFacadeImpl Maven / Gradle / Ivy
package net.jqwik.engine.facades;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
import net.jqwik.api.*;
import net.jqwik.api.facades.*;
import net.jqwik.api.lifecycle.*;
import net.jqwik.engine.properties.*;
import net.jqwik.engine.properties.shrinking.*;
public class ShrinkingSupportFacadeImpl extends ShrinkingSupportFacade {
private final TestingSupportFacadeImpl testingSupportFacade = new TestingSupportFacadeImpl();
@Override
public T falsifyThenShrink(Arbitrary extends T> arbitrary, Random random, Falsifier falsifier) {
RandomGenerator extends T> generator = arbitrary.generator(10, true);
return falsifyThenShrink(generator, random, falsifier);
}
@Override
@SuppressWarnings("unchecked")
public T falsifyThenShrink(
RandomGenerator extends T> generator,
Random random,
Falsifier falsifier
) {
Throwable[] originalError = new Throwable[1];
Shrinkable falsifiedShrinkable =
(Shrinkable) testingSupportFacade.generateUntil(generator, random, value -> {
TryExecutionResult result = falsifier.execute(value);
if (result.isFalsified()) {
originalError[0] = result.throwable().orElse(null);
}
return result.isFalsified();
});
// System.out.println("### " + falsifiedShrinkable.value());
return shrink(falsifiedShrinkable, falsifier, originalError[0]);
}
@Override
@SuppressWarnings("unchecked")
public T shrink(
Shrinkable falsifiedShrinkable,
Falsifier falsifier,
Throwable originalError
) {
ShrunkFalsifiedSample sample = shrinkToSample(falsifiedShrinkable, falsifier, originalError);
return (T) sample.parameters().get(0);
}
@Override
public ShrunkFalsifiedSample shrinkToSample(
Shrinkable falsifiedShrinkable,
Falsifier falsifier,
Throwable originalError
) {
FalsifiedSample sample = toFalsifiedSample(falsifiedShrinkable, originalError);
Consumer parametersReporter = ignore -> {};
PropertyShrinker shrinker = new PropertyShrinker(sample, ShrinkingMode.FULL, 10, parametersReporter, null);
return shrinker.shrink(toParamFalsifier(falsifier));
}
@SuppressWarnings("unchecked")
private static Falsifier> toParamFalsifier(Falsifier tFalsifier) {
return params -> {
T t = (T) params.get(0);
return tFalsifier.execute(t);
};
}
@SuppressWarnings("unchecked")
private static FalsifiedSample toFalsifiedSample(Shrinkable falsifiedShrinkable, Throwable originalError) {
List> shrinkables = Collections.singletonList((Shrinkable
© 2015 - 2024 Weber Informatics LLC | Privacy Policy