diff --git a/src/main/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTest.java b/src/main/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTest.java index 648b8d274..fa81ffe49 100644 --- a/src/main/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTest.java +++ b/src/main/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTest.java @@ -1,13 +1,15 @@ package com.hubspot.jinjava.lib.exptest; -import java.util.Objects; - import com.hubspot.jinjava.doc.annotations.JinjavaDoc; import com.hubspot.jinjava.doc.annotations.JinjavaParam; import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; +import com.hubspot.jinjava.el.TruthyTypeConverter; import com.hubspot.jinjava.interpret.InterpretException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import de.odysseus.el.misc.BooleanOperations; +import de.odysseus.el.misc.TypeConverter; + @JinjavaDoc( value = "Check if an object has the same value as another object", params = { @@ -24,6 +26,8 @@ }) public class IsEqualToExpTest implements ExpTest { + private static final TypeConverter TYPE_CONVERTER = new TruthyTypeConverter(); + @Override public String getName() { return "equalto"; @@ -35,7 +39,7 @@ public boolean evaluate(Object var, JinjavaInterpreter interpreter, Object... ar throw new InterpretException(getName() + " test requires 1 argument"); } - return Objects.equals(var, args[0]); + return BooleanOperations.eq(TYPE_CONVERTER, var, args[0]); } } diff --git a/src/test/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTestTest.java b/src/test/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTestTest.java new file mode 100644 index 000000000..a6bd08ef7 --- /dev/null +++ b/src/test/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTestTest.java @@ -0,0 +1,48 @@ +package com.hubspot.jinjava.lib.exptest; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.HashMap; + +import org.junit.Before; +import org.junit.Test; + +import com.hubspot.jinjava.Jinjava; + +public class IsEqualToExpTestTest { + + private static final String EQUAL_TEMPLATE = "{{ %s is equalto %s }}"; + + private Jinjava jinjava; + + @Before + public void setup() { + jinjava = new Jinjava(); + } + + @Test + public void itEquatesNumbers() { + assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "4", "4"), new HashMap<>())).isEqualTo("true"); + assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "4", "5"), new HashMap<>())).isEqualTo("false"); + } + + @Test + public void itEquatesStrings() { + assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "\"jinjava\"", "\"jinjava\""), new HashMap<>())).isEqualTo("true"); + assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "\"jinjava\"", "\"not jinjava\""), new HashMap<>())).isEqualTo("false"); + } + + @Test + public void itEquatesBooleans() { + assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "true", "true"), new HashMap<>())).isEqualTo("true"); + assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "true", "false"), new HashMap<>())).isEqualTo("false"); + } + + @Test + public void itEquatesDifferentTypes() { + assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "4", "\"4\""), new HashMap<>())).isEqualTo("true"); + assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "4", "\"5\""), new HashMap<>())).isEqualTo("false"); + assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "'c'", "\"c\""), new HashMap<>())).isEqualTo("true"); + assertThat(jinjava.render(String.format(EQUAL_TEMPLATE, "'c'", "\"b\""), new HashMap<>())).isEqualTo("false"); + } +}