Skip to content

Commit

Permalink
Merge pull request #38
Browse files Browse the repository at this point in the history
Add percentage functionality
  • Loading branch information
stefanhechtltng authored May 24, 2024
2 parents 72aafaa + b04f680 commit 2f73e69
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,22 @@ public BigDecimal bigDecimalNumberWithScale(BigDecimal min, BigDecimal max, int
return result.setScale(scale, RoundingMode.HALF_EVEN);
}

/**
* Generates a {@link BigDecimal} in [{@code 0} ; {@code 1}],
* <p>
* Example:
* <pre>
* ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
* vp.bigDecimalPercentage();
* </pre>
* </p>
*
* @return the generated percentage.
*/
public BigDecimal bigDecimalPercentage() {
return bigDecimalNumber(BigDecimal.ZERO, BigDecimal.ONE);
}

/**
* Returns the reference {@link LocalDateTime} as passed in the {@link ValueProviderInitialization}.
* <p><b>Note:</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ private static Collection<MethodInvocation> allMethodInvocations() {
invoke("url", address),
invoke("bigDecimalNumber", min, max),
invoke("bigIntegerNumber", BigInteger.valueOf(min), BigInteger.valueOf(max)),
invoke("bigDecimalPercentage"),
invoke("intNumber", min, max),
invoke("longNumber", min, max),
invoke("fixedLocalDate"),
Expand Down Expand Up @@ -508,6 +509,16 @@ void bigDecimalNumberWithScale_should_return_numbers_within_specified_range_as_l
.isBetween(BigDecimal.valueOf(min - offsetForScale), BigDecimal.valueOf(max + offsetForScale));
}

@Test
void bigDecimalPercentage_should_return_numbers_within_0_and_1() {
ValueProvider random = withRandomValues();
for (int i = 0; i < 1000; i++) {
BigDecimal percentage = random.bigDecimalPercentage();
assertThat(percentage).isGreaterThanOrEqualTo(BigDecimal.ZERO);
assertThat(percentage).isLessThanOrEqualTo(BigDecimal.ONE);
}
}

@TestFactory
List<DynamicTest> numericString_should_allow_restricting_min_and_max_in_addition_to_length() {
return newArrayList(
Expand Down

0 comments on commit 2f73e69

Please sign in to comment.