diff --git a/src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java b/src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java index e4d2c5f0e..ec8dd4069 100644 --- a/src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java @@ -9,6 +9,7 @@ import com.hubspot.jinjava.interpret.Context; import com.hubspot.jinjava.interpret.DeferredValue; import com.hubspot.jinjava.interpret.JinjavaInterpreter; +import com.hubspot.jinjava.interpret.RenderResult; import com.hubspot.jinjava.lib.filter.Filter; import com.hubspot.jinjava.lib.tag.ImportTag; import com.hubspot.jinjava.lib.tag.ImportTagTest; @@ -22,12 +23,12 @@ import com.hubspot.jinjava.loader.RelativePathResolver; import com.hubspot.jinjava.loader.ResourceLocator; import com.hubspot.jinjava.mode.EagerExecutionMode; -import com.hubspot.jinjava.objects.collections.PyMap; import com.hubspot.jinjava.tree.parse.DefaultTokenScannerSymbols; import com.hubspot.jinjava.tree.parse.TagToken; import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Optional; @@ -165,58 +166,20 @@ public void itHandlesMultiLayerAliased() { @Test @SuppressWarnings("unchecked") public void itHandlesMultiLayerAliasedAndDeferred() { + setupResourceLocator(); String child2Alias = "double_child"; - JinjavaInterpreter child = getChildInterpreter(interpreter, CONTEXT_VAR); - JinjavaInterpreter child2 = getChildInterpreter(child, child2Alias); - - child2.render("{% set foo = 'foo val' %}"); - child.render("{% set bar = 'bar val' %}"); - child2.render("{% set foo_d = deferred %}"); - - getAliasedStrategy(child2Alias, child).integrateChild(child2); - getAliasedStrategy(CONTEXT_VAR, interpreter).integrateChild(child); + RenderResult result = jinjava.renderForResult( + "{% import 'layer-one.jinja' as context_var %}", + new HashMap<>() + ); - assertThat(interpreter.getContext().get(CONTEXT_VAR)).isInstanceOf(PyMap.class); - assertThat( - ((Map) interpreter.getContext().get(CONTEXT_VAR)).get(child2Alias) - ) - .isInstanceOf(DeferredValue.class); + assertThat(result.getContext().get(CONTEXT_VAR)).isInstanceOf(DeferredValue.class); assertThat( ( - ( - (Map) ( - (DeferredValue) ( - (Map) (interpreter.getContext().get(CONTEXT_VAR)) - ).get(child2Alias) - ).getOriginalValue() - ).get("foo") - ) - ) - .isEqualTo("foo val"); - - assertThat( - (((Map) interpreter.getContext().get(CONTEXT_VAR)).get("bar")) - ) - .isEqualTo("bar val"); - } - - @Test - @SuppressWarnings("unchecked") - public void itHandlesMultiLayerAliasedAndNullDeferred() { - String child2Alias = "double_child"; - JinjavaInterpreter child = getChildInterpreter(interpreter, CONTEXT_VAR); - JinjavaInterpreter child2 = getChildInterpreter(child, child2Alias); - - child2.render("{% set foo = 'foo val' %}"); - child.render("{% set bar = 'bar val' %}"); - child2.render("{% set foo_d = deferred %}"); - - getAliasedStrategy(child2Alias, child).integrateChild(child2); - getAliasedStrategy(CONTEXT_VAR, interpreter).integrateChild(child); - - assertThat(interpreter.getContext().get(CONTEXT_VAR)).isInstanceOf(PyMap.class); - assertThat( - ((Map) interpreter.getContext().get(CONTEXT_VAR)).get(child2Alias) + (Map) ( + (DeferredValue) result.getContext().get(CONTEXT_VAR) + ).getOriginalValue() + ).get(child2Alias) ) .isInstanceOf(DeferredValue.class); assertThat( @@ -224,7 +187,11 @@ public void itHandlesMultiLayerAliasedAndNullDeferred() { ( (Map) ( (DeferredValue) ( - (Map) interpreter.getContext().get(CONTEXT_VAR) + ( + (Map) ( + (DeferredValue) result.getContext().get(CONTEXT_VAR) + ).getOriginalValue() + ) ).get(child2Alias) ).getOriginalValue() ).get("foo") @@ -233,7 +200,11 @@ public void itHandlesMultiLayerAliasedAndNullDeferred() { .isEqualTo("foo val"); assertThat( - (((Map) interpreter.getContext().get(CONTEXT_VAR)).get("bar")) + ( + (Map) ( + (DeferredValue) result.getContext().get(CONTEXT_VAR) + ).getOriginalValue() + ).get("bar") ) .isEqualTo("bar val"); } diff --git a/src/test/resources/tags/eager/importtag/layer-one.jinja b/src/test/resources/tags/eager/importtag/layer-one.jinja new file mode 100644 index 000000000..163bd0c3d --- /dev/null +++ b/src/test/resources/tags/eager/importtag/layer-one.jinja @@ -0,0 +1,2 @@ +{% set bar = 'bar val' %} +{% import 'layer-two.jinja' as double_child %} \ No newline at end of file diff --git a/src/test/resources/tags/eager/importtag/layer-two.jinja b/src/test/resources/tags/eager/importtag/layer-two.jinja new file mode 100644 index 000000000..6c9be8f6f --- /dev/null +++ b/src/test/resources/tags/eager/importtag/layer-two.jinja @@ -0,0 +1,2 @@ +{% set foo = 'foo val' %} +{% set foo_d = deferred %}