diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/AddFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/AddFilter.java index 100f5a390..da4ad50da 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/AddFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/AddFilter.java @@ -74,7 +74,7 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar } if (args[0] == null) { - return base; + return null; } BigDecimal addend; diff --git a/src/test/java/com/hubspot/jinjava/lib/filter/AddFilterTest.java b/src/test/java/com/hubspot/jinjava/lib/filter/AddFilterTest.java new file mode 100644 index 000000000..69ccd9af2 --- /dev/null +++ b/src/test/java/com/hubspot/jinjava/lib/filter/AddFilterTest.java @@ -0,0 +1,32 @@ +package com.hubspot.jinjava.lib.filter; + +import com.google.common.collect.ImmutableMap; +import com.hubspot.jinjava.BaseInterpretingTest; +import org.junit.Before; +import org.junit.Test; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +public class AddFilterTest extends BaseInterpretingTest { + private Map bindings; + + @Before + public void setup() { + jinjava.getGlobalContext().registerClasses(AddFilter.class); + + bindings = ImmutableMap.of( + "num1", 12, + "num2", 48 + ); + } + + @Test + public void itAddsNumber() { + assertThat(jinjava.render("{{ 5|add(13) }}", bindings)).isEqualTo("18"); + assertThat(jinjava.render("{{ num1|add(4) }}", bindings)).isEqualTo("16"); + assertThat(jinjava.render("{{ 7|add(num2) }}", bindings)).isEqualTo("55"); + assertThat(jinjava.render("{{ num1|add(num2) }}", bindings)).isEqualTo("60"); + } +}