Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 4 randomly populated collections #17

Merged
merged 10 commits into from
May 19, 2022
2 changes: 2 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
dependencies {
testImplementation dependency.junit_jupiter_api
testCompileOnly dependency.lombok
testAnnotationProcessor dependency.lombok

testRuntimeOnly dependency.junit_jupiter_engine
}
Expand Down
134 changes: 133 additions & 1 deletion core/src/main/java/com/tngtech/valueprovider/ValueProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.google.common.collect.Iterables;

Expand All @@ -34,6 +35,7 @@
import static java.time.temporal.ChronoUnit.DAYS;
import static java.time.temporal.ChronoUnit.SECONDS;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;

@SuppressWarnings("WeakerAccess")
public class ValueProvider {
Expand Down Expand Up @@ -829,6 +831,136 @@ public final <T> T oneOfExcluding(Iterable<T> elements, T... elementsToExclude)
return oneOf(allElements);
}

/**
* Generates a {@link List} of &lt;T&gt; (by means of {@code generator}) and includes {@code furtherContainedElements} in the {@link List}.
* <p>
* Example:
* <pre>
* static class MyBeanTestData {
* public static MyBean myBean(ValueProvider valueProvider) {
* // builds and returns your bean
* }
* }
*
* ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
* vp.listOfContaining(MyBeanTestData::myBean, myBean(), myBean(), myBean()); // -> List[myBean_1, myBean_2, myBean_random, myBean_3, myBean_random]
* </pre>
* </p>
*
* @param generator a generator {@link Function} to generate T given a {@link ValueProvider}.
* @param firstContainedElement first element that should be contained in the generated list.
* @param furtherContainedElements further elements that should be contained in the generated list.
*
* @return the generated {@link List}.
*
* @see #listOf(Function)
*/
@SafeVarargs
public final <T, V extends ValueProvider> List<T> listOfContaining(Function<V, T> generator, T firstContainedElement, T... furtherContainedElements) {
return listOfContaining(generator, asList(firstContainedElement, furtherContainedElements));
}

/**
* see {@link #listOfContaining(Function, Object, Object[])}
*/
public final <T, V extends ValueProvider> List<T> listOfContaining(Function<V, T> generator, Collection<T> containedElements) {
int maxNumberOfRandomElements = maxNumberOfRandomElements(containedElements);

List<T> generatedElements = new ArrayList<>();
for (T containedValue : containedElements) {
generatedElements.addAll(listOf(generator, intNumber(0, maxNumberOfRandomElements)));
generatedElements.add(containedValue);
}
generatedElements.addAll(listOf(generator, intNumber(0, maxNumberOfRandomElements)));
return generatedElements;
}

private <T> int maxNumberOfRandomElements(Collection<T> containedElements) {
if (containedElements.size() == 0) {
return 5;
}
if (containedElements.size() == 1) {
return 2;
}
return 1;
}

/**
* Generates a {@link List} of &lt;T&gt; (by means of {@code generator}). Ensures that the {@link List} contains at least one element.
* <p>
* Example:
* <pre>
* static class MyBeanTestData {
* public static MyBean myBean(ValueProvider valueProvider) {
* // builds and returns your bean
* }
* }
*
* ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
* vp.nonEmptyListOf(MyBeanTestData::myBean); // -> List[myBean_generated_1, myBean_generated_2]
* </pre>
* </p>
*
* @param generator a generator {@link Function} to generate T given a {@link ValueProvider}.
*
* @return the generated {@link List}.
*/
public <T, V extends ValueProvider> List<T> nonEmptyListOf(Function<V, T> generator) {
return listOf(generator, intNumber(1, 5));
}

/**
* Generates a {@link List} of &lt;T&gt; (by means of {@code generator}). Might return the empty list.
* <p>
* Example:
* <pre>
* static class MyBeanTestData {
* public static MyBean myBean(ValueProvider valueProvider) {
* // builds and returns your bean
* }
* }
*
* ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
* vp.listOf(MyBeanTestData::myBean); // -> List[myBean_generated_1, myBean_generated_2]
* vp.listOf(MyBeanTestData::myBean); // -> List[]
* </pre>
* </p>
*
* @param generator a generator {@link Function} to generate T given a {@link ValueProvider}.
*
* @return the generated {@link List}.
*/
public <T, V extends ValueProvider> List<T> listOf(Function<V, T> generator) {
return listOf(generator, intNumber(0, 5));
}

/**
* Generates a {@link List} of &lt;T&gt; (by means of {@code generator}). Containing exactly {@code numberOfElements} elements.
* <p>
* Example:
* <pre>
* static class MyBeanTestData {
* public static MyBean myBean(ValueProvider valueProvider) {
* // builds and returns your bean
* }
* }
*
* ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
* vp.listOf(MyBeanTestData::myBean, 4); // -> List[myBean_generated_1, myBean_generated_2, myBean_generated_3, myBean_generated_4]
* </pre>
* </p>
*
* @param generator a generator {@link Function} to generate T given a {@link ValueProvider}.
*
* @return the generated {@link List}.
*/
public <T, V extends ValueProvider> List<T> listOf(Function<V, T> generator, int numberOfElements) {
//noinspection unchecked
return IntStream.range(0, numberOfElements)
.mapToObj(i -> generator.apply((V) this))
.collect(toList());
}

/**
* Randomly draws true or false.
*
Expand Down Expand Up @@ -907,7 +1039,7 @@ private String createIPv6Block() {
}

private List<String> truncateLeadingZerosFromBlocks(List<String> blocks) {
return blocks.stream().map(this::truncateLeadingZerosFromBlock).collect(Collectors.toList());
return blocks.stream().map(this::truncateLeadingZerosFromBlock).collect(toList());
}

private String truncateLeadingZerosFromBlock(String block) {
Expand Down
Loading