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.shrinking.*;

import org.jspecify.annotations.*;

import static net.jqwik.engine.support.JqwikExceptionSupport.*;

public class IgnoreExceptionGenerator implements RandomGenerator {

	private final RandomGenerator base;
	private final Class[] exceptionTypes;
	private final int maxThrows;

	public IgnoreExceptionGenerator(RandomGenerator base, Class[] exceptionTypes, int maxThrows) {
		this.base = base;
		this.exceptionTypes = exceptionTypes;
		this.maxThrows = maxThrows;
	}

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

	private Shrinkable nextUntilAccepted(Random random, Function> fetchShrinkable) {
		for (int i = 0; i < maxThrows; i++) {
			try {
				Shrinkable next = fetchShrinkable.apply(random);
				// Enforce value generation for possible exception raising
				next.value();
				return next;
			} catch (Throwable throwable) {
				if (isInstanceOfAny(throwable, exceptionTypes)) {
					continue;
				}
				throw throwable;
			}
		}
		String message = String.format("%s missed more than %s times.", this, maxThrows);
		throw new TooManyFilterMissesException(message);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy