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

net.jqwik.engine.properties.arbitraries.randomized.IgnoreExceptionGenerator Maven / Gradle / Ivy

There is a newer version: 1.9.1
Show newest version
package net.jqwik.engine.properties.arbitraries.randomized;

import java.util.*;
import java.util.function.*;

import net.jqwik.api.*;
import net.jqwik.engine.properties.*;
import net.jqwik.engine.properties.shrinking.*;

public class IgnoreExceptionGenerator implements RandomGenerator {

	private final RandomGenerator base;
	private final Class exceptionType;

	public IgnoreExceptionGenerator(RandomGenerator base, Class exceptionType) {
		this.base = base;
		this.exceptionType = exceptionType;
	}

	@Override
	public Shrinkable next(final Random random) {
		return new IgnoreExceptionShrinkable<>(nextUntilAccepted(random, base::next), exceptionType);
	}

	private Shrinkable nextUntilAccepted(Random random, Function> fetchShrinkable) {
		return MaxTriesLoop.loop(
			() -> true,
			next -> {
				try {
					next = fetchShrinkable.apply(random);
					// Enforce value generation for possible exception raising
					next.value();
					return Tuple.of(true, next);
				} catch (Throwable throwable) {
					if (exceptionType.isAssignableFrom(throwable.getClass())) {
						return Tuple.of(false, next);
					}
					throw throwable;
				}
			},
			(maxMisses) -> {
				String message = String.format("%s missed more than %s times.", toString(), maxMisses);
				return new TooManyFilterMissesException(message);
			},
			10000
		);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy