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

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

The 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 net.jqwik.api.facades.*;
import net.jqwik.api.lifecycle.*;

import org.jspecify.annotations.*;

import static org.apiguardian.api.API.Status.*;
import static org.assertj.core.api.Assertions.*;

@API(status = EXPERIMENTAL, since = "1.4.0")
public class ShrinkingSupport {

	private ShrinkingSupport() {
	}

	public static  T falsifyThenShrink(Arbitrary arbitrary, Random random) {
		return falsifyThenShrink(arbitrary, random, ignore -> TryExecutionResult.falsified(null));
	}

	public static  T falsifyThenShrink(Arbitrary arbitrary, Random random, Falsifier falsifier) {
		return ShrinkingSupportFacade.implementation.falsifyThenShrink(arbitrary, random, falsifier);
	}

	public static  T falsifyThenShrink(RandomGenerator generator, Random random, Falsifier falsifier) {
		return ShrinkingSupportFacade.implementation.falsifyThenShrink(generator, random, falsifier);
	}

	public static  T shrink(
			Shrinkable falsifiedShrinkable,
			Falsifier falsifier,
			Throwable originalError
	) {
		return ShrinkingSupportFacade.implementation.shrink(falsifiedShrinkable, falsifier, originalError);
	}

	public static  ShrunkFalsifiedSample shrinkToSample(
			Shrinkable falsifiedShrinkable,
			Falsifier falsifier,
			Throwable originalError
	) {
		return ShrinkingSupportFacade.implementation.shrinkToSample(falsifiedShrinkable, falsifier, originalError);
	}

	public static  void assertWhileShrinking(Shrinkable shrinkable, Predicate condition) {
		while(true) {
			List> collect = shrinkable.shrink().collect(Collectors.toList());
			assertThat(collect).allMatch(s -> condition.test(s.value()));
			if (collect.isEmpty()) {
				break;
			}
			shrinkable = collect.iterator().next();
		}
	}

	public static  void assertAllValuesAreShrunkTo(Arbitrary arbitrary, Random random, T expectedShrunkValue) {
		T value = falsifyThenShrink(arbitrary, random);
		Assertions.assertThat(value).isEqualTo(expectedShrunkValue);
	}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy