Skip to content

Commit

Permalink
Merge pull request #33
Browse files Browse the repository at this point in the history
Added optionalOf methods to AbstractValueProvider
  • Loading branch information
stefanhechtltng authored May 24, 2024
2 parents 6108682 + 1f46c39 commit 58839d3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.gradle
.idea
.vscode
*.log
**/build/
jgiven-reports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -1018,6 +1020,49 @@ public boolean booleanValue() {
return random.nextBoolean();
}

/**
* <p>
* Generates an {@link Optional} of {@code <T>} by means of {@code generator}.
* Example:
* <pre>
* static class MyBeanTestData {
* public static MyBean myBean(ValueProvider valueProvider) {
* // builds and returns your bean
* }
* }
*
* ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
* vp.optionalOf(MyBeanTestData::myBean);
* vp.optionalOf(provider -> provider.randomString(10));
* </pre>
* </p>
*
* @param <T> the type of value to generate
* @param generator a generator {@link Function} to generate {@code <T>} given an implementation of {@link AbstractValueProvider}.
* @return the generated {@link Optional}.
*/
public <T> Optional<T> optionalOf(Function<VP, T> generator) {
return booleanValue() ? Optional.of(generator.apply((VP) this)) : Optional.empty();
}

/**
* <p>
* Generates an {@link Optional} of {@code <T>} by means of {@code supplier}.
* Example:
* <pre>
* ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
* vp.optionalOf(vp::uuid);
* </pre>
* </p>
*
* @param <T> the type of value to generate
* @param supplier a supplier {@link Supplier} to generate {@code <T>}.
* @return the generated {@link Optional}.
*/
public <T> Optional<T> optionalOf(Supplier<T> supplier) {
return booleanValue() ? Optional.of(supplier.get()) : Optional.empty();
}

/**
* Generates an IPv4 or IPv6 address.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,40 @@ void booleanValue_should_create_both_values() {
assertThat(falseCreated).as("false created").isTrue();
}

@Test
void optionalOf_on_function_should_create_present_and_empty() {
ValueProvider random = withRandomValues();

boolean presentCreated = false;
boolean emptyCreated = false;
for (int i = 0; i < 1000 && !(presentCreated && emptyCreated); i++) {
if (random.optionalOf(vp -> vp.randomString(10)).isPresent()) {
presentCreated = true;
} else {
emptyCreated = true;
}
}
assertThat(presentCreated).as("present created").isTrue();
assertThat(emptyCreated).as("empty created").isTrue();
}

@Test
void optionalOf_on_supplier_should_create_present_and_empty() {
ValueProvider random = withRandomValues();

boolean presentCreated = false;
boolean emptyCreated = false;
for (int i = 0; i < 1000 && !(presentCreated && emptyCreated); i++) {
if (random.optionalOf(random::uuid).isPresent()) {
presentCreated = true;
} else {
emptyCreated = true;
}
}
assertThat(presentCreated).as("present created").isTrue();
assertThat(emptyCreated).as("empty created").isTrue();
}

@Test
void intNumber_should_create_number_between_min_and_max() {
for (int i = 0; i < 1000; i++) {
Expand Down

0 comments on commit 58839d3

Please sign in to comment.