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

Add duration functionality #39

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,29 @@ public LocalTime localTime() {
return LocalTime.ofSecondOfDay(longNumber(0, secondsPerDay - 1));
}

/**
* Generates a {@link Duration} in [0 ; {@code max}].
*
* @param max the upper bound of the generated {@link Duration}.
* @return the generated {@link Duration}.
*/
public Duration duration(Duration max) {
return Duration.ofMillis(longNumber(0, max.toMillis()));
}

/**
* Generates a {@link Duration} in [{@code min} ; {@code max}].
*
* @param min the lower bound of the generated {@link Duration}.
* @param max the upper bound of the generated {@link Duration}.
* @return the generated {@link Duration}.
* @throws IllegalArgumentException if {@code min} > {@code max}.
*/
public Duration duration(Duration min, Duration max) {
checkArgument(min.compareTo(max) <= 0, "min %s must be less than or equal max %s", min, max);
return Duration.ofMillis(longNumber(min.toMillis(), max.toMillis()));
}

/**
* Draws one element from the elements of an Enum class.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -21,6 +22,7 @@
import java.util.stream.Stream;

import lombok.Data;

import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
Expand Down Expand Up @@ -95,6 +97,8 @@ private static Collection<MethodInvocation> allMethodInvocations() {
int min = random.intNumber(100, 1000);
int max = random.intNumber(1001, 2000);
int numLines = random.intNumber(2, 10);
Duration minDuration = Duration.ofHours(1);
Duration maxDuration = Duration.ofDays(1);

return newArrayList(
invoke("booleanValue"),
Expand Down Expand Up @@ -123,6 +127,8 @@ private static Collection<MethodInvocation> allMethodInvocations() {
invoke("fixedLocalDate"),
invoke("localDateBetweenYears", min, max),
invoke("localTime"),
invoke("duration", maxDuration),
invoke("duration", minDuration, maxDuration),
invoke("fixedLocalDateTime"),
invoke("fixedDecoratedStrings", numLines),
invoke("fixedDecoratedStrings", numLines, random.randomString(stringLength))
Expand Down Expand Up @@ -538,6 +544,21 @@ void numericString_should_reject_too_small_length_wrt_min() {
assertThat(thrown.getMessage()).contains("" + tooSmallLength, "" + min);
}

@Test
void duration_should_be_smaller_than_max_duration() {
ValueProvider random = withRandomValues();
assertThat(random.duration(Duration.ofDays(1)))
.isLessThanOrEqualTo(Duration.ofDays(1));
}

@Test
void duration_should_be_bigger_than_min_and_smaller_than_max_duration() {
ValueProvider random = withRandomValues();
assertThat(random.duration(Duration.ofMinutes(1), Duration.ofDays(1)))
.isGreaterThanOrEqualTo(Duration.ofMinutes(1))
.isLessThanOrEqualTo(Duration.ofDays(1));
}

@Test
void oneOf_should_return_single_value() {
ValueProvider random = withRandomValues();
Expand Down
Loading