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

Throw a template error when attempting to divide by zero #754

Merged
merged 1 commit into from
Sep 27, 2021
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
78 changes: 44 additions & 34 deletions src/main/java/com/hubspot/jinjava/lib/filter/DivideFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,41 +74,51 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar
} else {
return object;
}
if (object instanceof Integer) {
return (Integer) object / num.intValue();
}
if (object instanceof Float) {
return (Float) object / num.floatValue();
}
if (object instanceof Long) {
return (Long) object / num.longValue();
}
if (object instanceof Short) {
return (Short) object / num.shortValue();
}
if (object instanceof Double) {
return (Double) object / num.doubleValue();
}
if (object instanceof BigDecimal) {
return ((BigDecimal) object).divide(BigDecimal.valueOf(num.doubleValue()));
}
if (object instanceof BigInteger) {
return ((BigInteger) object).divide(BigInteger.valueOf(num.longValue()));
}
if (object instanceof Byte) {
return (Byte) object / num.byteValue();
}
if (object instanceof String) {
try {
return Double.valueOf((String) object) / num.doubleValue();
} catch (NumberFormatException e) {
throw new InvalidInputException(
interpreter,
this,
InvalidReason.NUMBER_FORMAT,
object.toString()
);
try {
jmaroeder marked this conversation as resolved.
Show resolved Hide resolved
if (object instanceof Integer) {
return (Integer) object / num.intValue();
}
if (object instanceof Float) {
return (Float) object / num.floatValue();
}
if (object instanceof Long) {
return (Long) object / num.longValue();
}
if (object instanceof Short) {
return (Short) object / num.shortValue();
}
if (object instanceof Double) {
return (Double) object / num.doubleValue();
}
if (object instanceof BigDecimal) {
return ((BigDecimal) object).divide(BigDecimal.valueOf(num.doubleValue()));
}
if (object instanceof BigInteger) {
return ((BigInteger) object).divide(BigInteger.valueOf(num.longValue()));
}
if (object instanceof Byte) {
return (Byte) object / num.byteValue();
}
if (object instanceof String) {
try {
return Double.valueOf((String) object) / num.doubleValue();
} catch (NumberFormatException e) {
throw new InvalidInputException(
interpreter,
this,
InvalidReason.NUMBER_FORMAT,
object.toString()
);
}
}
} catch (ArithmeticException e) {
throw new InvalidInputException(
interpreter,
this,
InvalidReason.NON_ZERO_NUMBER,
0,
num.toString()
);
}
return object;
}
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/com/hubspot/jinjava/lib/filter/DivideFilterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.hubspot.jinjava.lib.filter;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import com.google.common.collect.ImmutableMap;
import com.hubspot.jinjava.BaseJinjavaTest;
import com.hubspot.jinjava.interpret.FatalTemplateErrorsException;
import org.junit.Test;

public class DivideFilterTest extends BaseJinjavaTest {

@Test
public void itDivides() {
assertThat(
jinjava.render(
"{{ numerator|divide(denominator) }}",
ImmutableMap.of("numerator", 10, "denominator", 2)
)
)
.isEqualTo("5");
assertThat(
jinjava.render(
"{{ numerator // denominator }}",
ImmutableMap.of("numerator", 10, "denominator", 2)
)
)
.isEqualTo("5");
assertThat(
jinjava.render(
"{{ numerator / denominator }}",
ImmutableMap.of("numerator", 10, "denominator", 2)
)
)
.isEqualTo("5.0");
}

@Test
public void itThrowsAnExceptionOnDivideByZero() {
assertThatExceptionOfType(FatalTemplateErrorsException.class)
.isThrownBy(
() -> {
jinjava.render(
"{{ numerator|divide(denominator) }}",
ImmutableMap.of("numerator", 10, "denominator", 0)
);
}
);
}
}