Skip to content

Commit 001c950

Browse files
author
Tom Akehurst
committed
randomDecimal helper now accepts integer and string bounds in addition to decimal
1 parent 237250b commit 001c950

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/main/java/com/github/tomakehurst/wiremock/extension/responsetemplating/helpers/HelperUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
public class HelperUtils {
44

55
public static Integer coerceToInt(Object value) {
6+
if (value == null) {
7+
return null;
8+
}
9+
610
if (Number.class.isAssignableFrom(value.getClass())) {
711
return ((Number) value).intValue();
812
}
@@ -13,4 +17,20 @@ public static Integer coerceToInt(Object value) {
1317

1418
return null;
1519
}
20+
21+
public static Double coerceToDouble(Object value) {
22+
if (value == null) {
23+
return null;
24+
}
25+
26+
if (Number.class.isAssignableFrom(value.getClass())) {
27+
return ((Number) value).doubleValue();
28+
}
29+
30+
if (CharSequence.class.isAssignableFrom(value.getClass())) {
31+
return Double.parseDouble(value.toString());
32+
}
33+
34+
return null;
35+
}
1636
}

src/main/java/com/github/tomakehurst/wiremock/extension/responsetemplating/helpers/RandomDecimalHelper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
import java.math.BigDecimal;
2222
import java.util.concurrent.ThreadLocalRandom;
2323

24+
import static com.github.tomakehurst.wiremock.extension.responsetemplating.helpers.HelperUtils.coerceToDouble;
2425
import static java.math.BigDecimal.ROUND_HALF_UP;
2526

2627
public class RandomDecimalHelper extends HandlebarsHelper<Void> {
2728

2829
@Override
2930
public Object apply(Void context, Options options) throws IOException {
30-
double lowerBound = options.hash("lower", Double.MIN_VALUE);
31-
double upperBound = options.hash("upper", Double.MAX_VALUE);
31+
double lowerBound = coerceToDouble(options.hash("lower", Double.MIN_VALUE));
32+
double upperBound = coerceToDouble(options.hash("upper", Double.MAX_VALUE));
3233

3334
return ThreadLocalRandom.current().nextDouble(lowerBound, upperBound);
3435
}

src/test/java/com/github/tomakehurst/wiremock/extension/responsetemplating/ResponseTemplateTransformerTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,8 +918,13 @@ public void generatesARandomInt() {
918918
public void generatesARandomDecimal() {
919919
assertThat(transform("{{randomDecimal}}"), matchesPattern("[\\-0-9\\.E]+"));
920920
assertThat(transformToDouble("{{randomDecimal lower=-10.1 upper=-0.9}}"), allOf(greaterThanOrEqualTo(-10.1), lessThanOrEqualTo(-0.9)));
921+
assertThat(transformToDouble("{{randomDecimal lower='-10.1' upper='-0.9'}}"), allOf(greaterThanOrEqualTo(-10.1), lessThanOrEqualTo(-0.9)));
921922
assertThat(transformToDouble("{{randomDecimal upper=12.5}}"), lessThanOrEqualTo(12.5));
922923
assertThat(transformToDouble("{{randomDecimal lower=-24.01}}"), greaterThanOrEqualTo(-24.01));
924+
assertThat(transformToDouble("{{randomDecimal lower=-1 upper=1}}"), Matchers.allOf(
925+
greaterThanOrEqualTo(-1.0),
926+
lessThanOrEqualTo(1.0)
927+
));
923928
}
924929

925930
@Test

0 commit comments

Comments
 (0)