Skip to content

Commit

Permalink
Fixed #1946 - maths helper now makes a last-ditch attempt to parse th…
Browse files Browse the repository at this point in the history
…e toString() result from an unknown value type into a number, only returning 0 if this fails. This means that renderable dates formatted as epoch will be parseable.
  • Loading branch information
Tom Akehurst committed Sep 15, 2022
1 parent f339447 commit d370fed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Thomas Akehurst
* Copyright (C) 2021-2022 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -75,11 +75,11 @@ private static BigDecimal coerceToBigDecimal(Object value) {
return BigDecimal.valueOf((float) value);
}

if (value instanceof CharSequence) {
try {
return new BigDecimal(value.toString());
} catch (NumberFormatException e) {
return new BigDecimal(0);
}

return new BigDecimal(0);
}

private static Object reduceToPrimitiveNumber(BigDecimal value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Thomas Akehurst
* Copyright (C) 2021-2022 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.is;

import java.util.Date;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -101,4 +102,12 @@ public void dividesTwoIntegers() throws Exception {
public void modsTwoIntegers() throws Exception {
assertThat(renderHelperValue(helper, 11, "%", 3), is(2));
}

@Test
void coercesEpochFormattedRenderableDateParameterCorrectly() throws Exception {
Date date = new Date(1663258226792L);
assertThat(
renderHelperValue(helper, new RenderableDate(date, "epoch", null), "+", 0),
is(1663258226792L));
}
}

0 comments on commit d370fed

Please sign in to comment.