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

net.jqwik.engine.properties.arbitraries.GenericEdgeCasesConfiguration Maven / Gradle / Ivy

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

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

import net.jqwik.api.*;

public class GenericEdgeCasesConfiguration implements EdgeCases.Config {
	private boolean none;
	private final List> filters = new ArrayList<>();
	private final List additionalEdgeCases = new ArrayList<>();

	@Override
	public EdgeCases.Config none() {
		none = true;
		return this;
	}

	@Override
	public EdgeCases.Config filter(Predicate filter) {
		filters.add(filter);
		return this;
	}

	@SafeVarargs
	@Override
	public final EdgeCases.Config add(T... edgeCases) {
		for (T edgeCase : edgeCases) {
			checkEdgeCaseIsValid(edgeCase);
			additionalEdgeCases.add(edgeCase);
		}
		return this;
	}

	// Override in subclasses if there is anything to check
	protected void checkEdgeCaseIsValid(T edgeCase) {
	}

	@SafeVarargs
	@Override
	public final EdgeCases.Config includeOnly(T... includedValues) {
		List values = Arrays.asList(includedValues);
		return filter(values::contains);
	}

	public EdgeCases configure(Consumer> configurator, Function> edgeCasesCreator, int maxEdgeCases) {
		configurator.accept(this);

		EdgeCases configuredEdgeCases;
		if (none) {
			configuredEdgeCases = EdgeCases.none();
		} else if (filters.isEmpty()) {
			configuredEdgeCases = edgeCasesCreator.apply(maxEdgeCases);
		} else {
			configuredEdgeCases = edgeCasesCreator.apply(Integer.MAX_VALUE);
		}

		List>> suppliers = configuredEdgeCases.suppliers();
		for (Predicate filter : new ArrayList<>(filters)) {
			suppliers = suppliers.stream().filter(s -> filter.test(s.get().value())).collect(Collectors.toList());
		}
		for (T additionalEdgeCase : additionalEdgeCases) {
			suppliers.add(() -> createShrinkable(additionalEdgeCase));
		}
		return EdgeCasesSupport.fromSuppliers(suppliers);
	}

	protected Shrinkable createShrinkable(T additionalEdgeCase) {
		return Shrinkable.unshrinkable(additionalEdgeCase);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy