diff --git a/src/main/java/com/hubspot/jinjava/util/ObjectTruthValue.java b/src/main/java/com/hubspot/jinjava/util/ObjectTruthValue.java index e5996b71d..e4a08387b 100644 --- a/src/main/java/com/hubspot/jinjava/util/ObjectTruthValue.java +++ b/src/main/java/com/hubspot/jinjava/util/ObjectTruthValue.java @@ -39,7 +39,7 @@ public static boolean evaluate(Object object) { } if (object instanceof Number) { - return ((Number) object).intValue() != 0; + return ((Number) object).doubleValue() != 0; } if (object instanceof String) { diff --git a/src/test/java/com/hubspot/jinjava/util/ObjectTruthValueTest.java b/src/test/java/com/hubspot/jinjava/util/ObjectTruthValueTest.java index 00748fc4f..bcb4c7750 100644 --- a/src/test/java/com/hubspot/jinjava/util/ObjectTruthValueTest.java +++ b/src/test/java/com/hubspot/jinjava/util/ObjectTruthValueTest.java @@ -2,7 +2,6 @@ import static org.assertj.core.api.Assertions.assertThat; -import org.junit.Before; import org.junit.Test; public class ObjectTruthValueTest { @@ -15,6 +14,36 @@ public void itEvaluatesObjectsWithObjectTruthValue() { .isFalse(); } + @Test + public void itEvaluatesIntegers() { + checkNumberTruthiness(1, 0); + } + + @Test + public void itEvaluatesDoubles() { + checkNumberTruthiness(0.5, 0.0); + } + + @Test + public void itEvaluatesLongs() { + checkNumberTruthiness(1L, 0L); + } + + @Test + public void itEvaluatesShorts() { + checkNumberTruthiness((short) 1, (short) 0); + } + + @Test + public void itEvaluatesFloats() { + checkNumberTruthiness(0.5f, 0.0f); + } + + private void checkNumberTruthiness(Object a, Object b) { + assertThat(ObjectTruthValue.evaluate(a)).isTrue(); + assertThat(ObjectTruthValue.evaluate(b)).isFalse(); + } + private class TestObject implements HasObjectTruthValue { private boolean objectTruthValue = false;