From 7eb6f732b2537cd683e830eebf1c53f084c0a86a Mon Sep 17 00:00:00 2001 From: Manish Devgan Date: Sat, 18 Jan 2020 17:51:27 +0530 Subject: [PATCH] Changes DefaultFilter to extend AdvancedFilter --- .../jinjava/lib/filter/DefaultFilter.java | 12 ++++-- .../jinjava/lib/filter/DefaultFilterTest.java | 42 +++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 src/test/java/com/hubspot/jinjava/lib/filter/DefaultFilterTest.java diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java index a41877b84..e01749596 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java @@ -15,6 +15,9 @@ **********************************************************************/ package com.hubspot.jinjava.lib.filter; +import java.util.Map; +import java.util.Objects; + import org.apache.commons.lang3.BooleanUtils; import com.hubspot.jinjava.doc.annotations.JinjavaDoc; @@ -22,6 +25,7 @@ import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.interpret.TemplateSyntaxException; +import com.hubspot.jinjava.objects.PyWrapper; import com.hubspot.jinjava.util.ObjectTruthValue; @JinjavaDoc( @@ -39,10 +43,10 @@ desc = "If you want to use default with variables that evaluate to false you have to set the second parameter to true", code = "{{ ''|default('the string was empty', true) }}") }) -public class DefaultFilter implements Filter { +public class DefaultFilter implements AdvancedFilter { @Override - public Object filter(Object object, JinjavaInterpreter interpreter, String... args) { + public Object filter(Object object, JinjavaInterpreter interpreter, Object[] args, Map kwargs) { boolean truthy = false; if (args.length < 1) { @@ -50,7 +54,7 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar } if (args.length > 1) { - truthy = BooleanUtils.toBoolean(args[1]); + truthy = BooleanUtils.toBoolean(Objects.toString(args[1])); } if (truthy) { @@ -61,7 +65,7 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar return object; } - return args[0]; + return args[0] instanceof PyWrapper ? args[0] : Objects.toString(args[0]); } @Override diff --git a/src/test/java/com/hubspot/jinjava/lib/filter/DefaultFilterTest.java b/src/test/java/com/hubspot/jinjava/lib/filter/DefaultFilterTest.java new file mode 100644 index 000000000..bb9f0f3de --- /dev/null +++ b/src/test/java/com/hubspot/jinjava/lib/filter/DefaultFilterTest.java @@ -0,0 +1,42 @@ +package com.hubspot.jinjava.lib.filter; + +import java.util.HashMap; + +import com.hubspot.jinjava.Jinjava; +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Created by manishdevgan on 25/06/19. + */ + +public class DefaultFilterTest { + Jinjava jinjava; + + @Before + public void setup() { + jinjava = new Jinjava(); + jinjava.getGlobalContext().registerClasses(DefaultFilter.class); + } + + @Test + public void itSetsDefaultStringValues() { + assertThat(jinjava.render("{% set d=d | default(\"some random value\") %}{{ d }}", new HashMap<>())).isEqualTo("some random value"); System.out.println(jinjava.render("{% set d=d | default(\"some random value\") %}{{ d }}", new HashMap<>())); + } + + @Test + public void itSetsDefaultObjectValue() { + assertThat(jinjava.render("{% set d=d | default({\"key\": \"value\"}) %}Value = {{ d.key }}", new HashMap<>())).isEqualTo("Value = value"); System.out.println(jinjava.render("{% set d=d | default({\"key\": \"value\"}) %}Value = {{ d.key }}", new HashMap<>())); + } + + @Test + public void itChecksForType() { + assertThat(jinjava.render("{% set d=d | default({\"key\": \"value\"}) %}Type = {{ type(d.key) }}", new HashMap<>())) + .isEqualTo("Type = str"); + assertThat(jinjava.render("{% set d=d | default(\"some random value\") %}{{ type(d) }}", new HashMap<>())) + .isEqualTo("str"); + System.out.println(jinjava.render("{% set d=d | default(\"some random value\") %}{{ type(d) }}", new HashMap<>())); + } +} \ No newline at end of file