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

net.jqwik.engine.providers.ArrayArbitraryProvider Maven / Gradle / Ivy

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

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

import net.jqwik.api.*;
import net.jqwik.api.providers.*;
import net.jqwik.engine.properties.arbitraries.*;

public class ArrayArbitraryProvider implements ArbitraryProvider {
	@Override
	public boolean canProvideFor(TypeUsage targetType) {
		return targetType.isArray();
	}

	@Override
	public Set> provideFor(TypeUsage targetType, SubtypeProvider subtypeProvider) {
		TypeUsage componentType = targetType.getComponentType().orElse(TypeUsage.forType(Object.class));
		return subtypeProvider.apply(componentType)
			.stream()
			.map(elementArbitrary -> {
				if (!componentType.isTypeVariable()) {
					Class arrayClass = targetType.getRawType();
					return elementArbitrary.array(arrayClass);
				} else {
					Class componentClass = upperboundsSupertype(componentType);
					return DefaultArrayArbitrary.forComponentType(elementArbitrary, componentClass);
				}
			})
			.collect(Collectors.toSet());
	}

	private Class upperboundsSupertype(TypeUsage componentType) {
		List upperBounds = componentType.getUpperBounds();
		if (upperBounds.size() != 1) {
			String message = String.format("jqwik cannot handle more than one upper bound in this case: <%s>", componentType);
			throw new JqwikException(message);
		}
		return upperBounds.get(0).getRawType();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy