net.jqwik.testing.TestingSupport Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jqwik-testing Show documentation
Show all versions of jqwik-testing Show documentation
Jqwik Testing support module
package net.jqwik.testing;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
import org.apiguardian.api.*;
import org.assertj.core.api.*;
import net.jqwik.api.*;
import static org.apiguardian.api.API.Status.*;
import static org.assertj.core.api.Assertions.*;
@API(status = EXPERIMENTAL, since = "1.4.0")
public class TestingSupport {
private TestingSupport() {
}
public static void assertAllGenerated(RandomGenerator extends T> generator, Random random, Predicate checker) {
Optional extends Shrinkable extends T>> failure =
generator
.stream(random)
.limit(100)
.filter(shrinkable -> !checker.test(shrinkable.value()))
.findAny();
failure.ifPresent(shrinkable -> {
Assertions.fail(String.format("Value [%s] failed to fulfill condition.", shrinkable.value().toString()));
});
}
public static void assertAllGenerated(RandomGenerator extends T> generator, Random random, Consumer assertions) {
Predicate checker = value -> {
try {
assertions.accept(value);
return true;
} catch (Throwable any) {
return false;
}
};
assertAllGenerated(generator, random, checker);
}
public static void assertAtLeastOneGenerated(
RandomGenerator extends T> generator,
Random random,
Function checker,
String failureMessage
) {
Optional extends Shrinkable extends T>> success =
generator
.stream(random)
.limit(3000)
.filter(shrinkable -> checker.apply(shrinkable.value()))
.findAny();
if (!success.isPresent()) {
fail(failureMessage);
}
}
public static void assertAtLeastOneGenerated(
RandomGenerator extends T> generator,
Random random,
Function checker
) {
assertAtLeastOneGenerated(generator, random, checker, "Failed to generate at least one");
}
@SafeVarargs
public static void assertAtLeastOneGeneratedOf(
RandomGenerator extends T> generator,
Random random,
T... values
) {
for (T value : values) {
assertAtLeastOneGenerated(generator, random, value::equals, "Failed to generate " + value);
}
}
@SafeVarargs
public static void assertGeneratedExactly(RandomGenerator extends T> generator, Random random, T... expectedValues) {
List generated = generator
.stream(random)
.limit(expectedValues.length)
.map(Shrinkable::value)
.collect(Collectors.toList());
assertThat(generated).containsExactly(expectedValues);
}
public static Set> collectEdgeCaseShrinkables(EdgeCases edgeCases) {
Set> shrinkables = new HashSet<>();
for (Shrinkable edgeCase : edgeCases) {
shrinkables.add(edgeCase);
}
return shrinkables;
}
public static Set collectEdgeCaseValues(EdgeCases edgeCases) {
Set values = new HashSet<>();
for (Shrinkable edgeCase : edgeCases) {
values.add(edgeCase.value());
}
return values;
}
public static T generateFirst(Arbitrary arbitrary, Random random) {
RandomGenerator generator = arbitrary.generator(1, true);
return generator.next(random).value();
}
public static Map count(RandomGenerator generator, int tries, Random random) {
return generator
.stream(random)
.limit(tries)
.map(shrinkable -> shrinkable.value())
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
}
// TODO: Call from TestingSupportFacade
public static Shrinkable generateUntil(RandomGenerator generator, Random random, Function condition) {
long maxTries = 1000;
return generator
.stream(random)
.limit(maxTries)
.filter(shrinkable -> condition.apply(shrinkable.value()))
.findFirst()
.orElseThrow(() -> new JqwikException("Failed to generate value that fits condition after " + maxTries + " tries."));
}
}