All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.jqwik.testing.TestingSupport Maven / Gradle / Ivy

There is a newer version: 1.9.1
Show newest version
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 generator, Random random, Predicate checker) {
		Optional> 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 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 generator,
			Random random,
			Function checker,
			String failureMessage
	) {
		Optional> success =
				generator
						.stream(random)
						.limit(3000)
						.filter(shrinkable -> checker.apply(shrinkable.value()))
						.findAny();
		if (!success.isPresent()) {
			fail(failureMessage);
		}
	}

	public static  void assertAtLeastOneGenerated(
			RandomGenerator generator,
			Random random,
			Function checker
	) {
		assertAtLeastOneGenerated(generator, random, checker, "Failed to generate at least one");
	}

	@SafeVarargs
	public static  void assertAtLeastOneGeneratedOf(
		RandomGenerator generator,
		Random random,
		T... values
	) {
		for (T value : values) {
			assertAtLeastOneGenerated(generator, random, value::equals, "Failed to generate " + value);
		}
	}

	@SafeVarargs
	public static  void assertGeneratedExactly(RandomGenerator 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."));
	}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy