diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/BreakTag.java b/src/main/java/com/hubspot/jinjava/lib/tag/BreakTag.java index aaf6a6cba..3c05f260b 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/BreakTag.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/BreakTag.java @@ -2,6 +2,7 @@ import com.hubspot.jinjava.doc.annotations.JinjavaDoc; import com.hubspot.jinjava.doc.annotations.JinjavaTextMateSnippet; +import com.hubspot.jinjava.interpret.DeferredValueException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.interpret.NotInLoopException; import com.hubspot.jinjava.tree.TagNode; @@ -31,6 +32,9 @@ public String getName() { public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) { Object loop = interpreter.getContext().get(ForTag.LOOP); if (loop instanceof ForLoop) { + if (interpreter.getContext().isDeferredExecutionMode()) { + throw new DeferredValueException("Deferred break"); + } ForLoop forLoop = (ForLoop) loop; forLoop.doBreak(); } else { diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/ContinueTag.java b/src/main/java/com/hubspot/jinjava/lib/tag/ContinueTag.java index d6250843a..2671ed38b 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/ContinueTag.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/ContinueTag.java @@ -2,6 +2,7 @@ import com.hubspot.jinjava.doc.annotations.JinjavaDoc; import com.hubspot.jinjava.doc.annotations.JinjavaTextMateSnippet; +import com.hubspot.jinjava.interpret.DeferredValueException; import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.interpret.NotInLoopException; import com.hubspot.jinjava.tree.TagNode; @@ -29,6 +30,9 @@ public String getName() { public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) { Object loop = interpreter.getContext().get(ForTag.LOOP); if (loop instanceof ForLoop) { + if (interpreter.getContext().isDeferredExecutionMode()) { + throw new DeferredValueException("Deferred continue"); + } ForLoop forLoop = (ForLoop) loop; forLoop.doContinue(); } else { diff --git a/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerTagFactory.java b/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerTagFactory.java index b019e68cf..ba3788404 100644 --- a/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerTagFactory.java +++ b/src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerTagFactory.java @@ -5,7 +5,9 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.hubspot.jinjava.lib.tag.BlockTag; +import com.hubspot.jinjava.lib.tag.BreakTag; import com.hubspot.jinjava.lib.tag.CallTag; +import com.hubspot.jinjava.lib.tag.ContinueTag; import com.hubspot.jinjava.lib.tag.CycleTag; import com.hubspot.jinjava.lib.tag.DoTag; import com.hubspot.jinjava.lib.tag.ElseIfTag; @@ -54,6 +56,8 @@ public class EagerTagFactory { .add(ElseTag.class) .add(RawTag.class) .add(ExtendsTag.class) // TODO support reconstructing extends tags + .add(BreakTag.class) // TODO support eager break tag + .add(ContinueTag.class) // TODO support eager continue tag .build(); @SuppressWarnings("unchecked") diff --git a/src/test/java/com/hubspot/jinjava/EagerTest.java b/src/test/java/com/hubspot/jinjava/EagerTest.java index 2f39838a8..3dce7fcd0 100644 --- a/src/test/java/com/hubspot/jinjava/EagerTest.java +++ b/src/test/java/com/hubspot/jinjava/EagerTest.java @@ -1644,4 +1644,24 @@ public void itWrapsMacroThatWouldChangeCurrentPathInChildScope() { "wraps-macro-that-would-change-current-path-in-child-scope/test" ); } + + @Test + public void itHandlesDeferredBreakInForLoop() { + String input = expectedTemplateInterpreter.getFixtureTemplate( + "handles-deferred-break-in-for-loop/test" + ); + interpreter.render(input); + // We don't support this yet + assertThat(interpreter.getContext().getDeferredNodes()).isNotEmpty(); + } + + @Test + public void itHandlesDeferredContinueInForLoop() { + String input = expectedTemplateInterpreter.getFixtureTemplate( + "handles-deferred-continue-in-for-loop/test" + ); + interpreter.render(input); + // We don't support this yet + assertThat(interpreter.getContext().getDeferredNodes()).isNotEmpty(); + } } diff --git a/src/test/resources/eager/handles-deferred-break-in-for-loop/test.jinja b/src/test/resources/eager/handles-deferred-break-in-for-loop/test.jinja new file mode 100644 index 000000000..6cdda8f80 --- /dev/null +++ b/src/test/resources/eager/handles-deferred-break-in-for-loop/test.jinja @@ -0,0 +1,8 @@ +Start loop +{% for i in range(5) %} + {% if deferred %} + {% break %} + {% endif %} + i is: {{ i }} +{% endfor %} +End loop \ No newline at end of file diff --git a/src/test/resources/eager/handles-deferred-continue-in-for-loop/test.jinja b/src/test/resources/eager/handles-deferred-continue-in-for-loop/test.jinja new file mode 100644 index 000000000..3025b150e --- /dev/null +++ b/src/test/resources/eager/handles-deferred-continue-in-for-loop/test.jinja @@ -0,0 +1,8 @@ +Start loop +{% for i in range(5) %} + {% if deferred %} + {% continue %} + {% endif %} + i is: {{ i }} +{% endfor %} +End loop \ No newline at end of file