Skip to content

Commit

Permalink
Add percentage functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Vinzenz Maennig <vinzenz.maennig@tngtech.com>
  • Loading branch information
VinzenzMaennig committed May 19, 2024
1 parent 43820e8 commit b04f680
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 @@ -118,6 +118,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 @@ -502,6 +503,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 b04f680

Please sign in to comment.