Skip to content

Commit

Permalink
Fix expected results, test non-idempotently
Browse files Browse the repository at this point in the history
  • Loading branch information
jasmith-hs committed May 6, 2024
1 parent 76ed803 commit 016e299
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/test/java/com/hubspot/jinjava/EagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1544,7 +1544,7 @@ public void itReconstructsAliasedMacroSecondPass() {
@Test
public void itReconstructsBlockPathWhenDeferred() {
interpreter.getContext().getCurrentPathStack().push("Child path", 0, 0);
expectedTemplateInterpreter.assertExpectedOutput(
expectedTemplateInterpreter.assertExpectedOutputNonIdempotent(
"reconstructs-block-path-when-deferred/test"
);
}
Expand All @@ -1560,7 +1560,7 @@ public void itReconstructsBlockPathWhenDeferredSecondPass() {
@Test
public void itReconstructsBlockPathWhenDeferredNested() {
interpreter.getContext().getCurrentPathStack().push("Child path", 0, 0);
expectedTemplateInterpreter.assertExpectedOutput(
expectedTemplateInterpreter.assertExpectedOutputNonIdempotent(
"reconstructs-block-path-when-deferred-nested/test"
);
}
Expand Down
29 changes: 17 additions & 12 deletions src/test/java/com/hubspot/jinjava/ExpectedTemplateInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.JinjavaInterpreter.InterpreterScopeClosable;
import com.hubspot.jinjava.mode.DefaultExecutionMode;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -66,13 +67,15 @@ public String assertExpectedNonEagerOutput(String name) {
);
JinjavaInterpreter.pushCurrent(preserveInterpreter);

preserveInterpreter.getContext().putAll(interpreter.getContext());
String template = getFixtureTemplate(name);
output = JinjavaInterpreter.getCurrent().render(template);
assertThat(JinjavaInterpreter.getCurrent().getContext().getDeferredNodes())
.as("Ensure no deferred nodes were created")
.isEmpty();
assertThat(output.trim()).isEqualTo(expected(name).trim());
try (InterpreterScopeClosable ignored = preserveInterpreter.enterScope()) {
preserveInterpreter.getContext().putAll(interpreter.getContext());
String template = getFixtureTemplate(name);
output = JinjavaInterpreter.getCurrent().render(template);
assertThat(JinjavaInterpreter.getCurrent().getContext().getDeferredNodes())
.as("Ensure no deferred nodes were created")
.isEmpty();
assertThat(output.trim()).isEqualTo(expected(name).trim());
}
} finally {
JinjavaInterpreter.popCurrent();
}
Expand All @@ -97,11 +100,13 @@ public String assertExpectedNonEagerOutput(String name) {

preserveInterpreter.getContext().putAll(interpreter.getContext());
String template = getFixtureTemplate(originalName);
output = JinjavaInterpreter.getCurrent().render(template);
assertThat(JinjavaInterpreter.getCurrent().getContext().getDeferredNodes())
.as("Ensure no deferred nodes were created")
.isEmpty();
assertThat(output.trim()).isEqualTo(expected(name).trim());
try (InterpreterScopeClosable ignored = preserveInterpreter.enterScope()) {
output = JinjavaInterpreter.getCurrent().render(template);
assertThat(JinjavaInterpreter.getCurrent().getContext().getDeferredNodes())
.as("Ensure no deferred nodes were created")
.isEmpty();
assertThat(output.trim()).isEqualTo(expected(name).trim());
}
} finally {
JinjavaInterpreter.popCurrent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.hubspot.jinjava.interpret.DeferredValue;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.lib.tag.ExtendsTagTest;
import com.hubspot.jinjava.loader.RelativePathResolver;
import com.hubspot.jinjava.mode.EagerExecutionMode;
import java.io.IOException;
import org.junit.After;
Expand Down Expand Up @@ -46,7 +47,9 @@ public void teardown() {

@Test
public void itDefersBlockInExtendsChild() {
expectedTemplateInterpreter.assertExpectedOutput("defers-block-in-extends-child");
expectedTemplateInterpreter.assertExpectedOutputNonIdempotent(
"defers-block-in-extends-child"
);
}

@Test
Expand All @@ -55,14 +58,17 @@ public void itDefersBlockInExtendsChildSecondPass() {
expectedTemplateInterpreter.assertExpectedOutput(
"defers-block-in-extends-child.expected"
);
context.remove(RelativePathResolver.CURRENT_PATH_CONTEXT_KEY);
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
"defers-block-in-extends-child.expected"
);
}

@Test
public void itDefersSuperBlockWithDeferred() {
expectedTemplateInterpreter.assertExpectedOutput("defers-super-block-with-deferred");
expectedTemplateInterpreter.assertExpectedOutputNonIdempotent(
"defers-super-block-with-deferred"
);
}

@Test
Expand All @@ -71,6 +77,7 @@ public void itDefersSuperBlockWithDeferredSecondPass() {
expectedTemplateInterpreter.assertExpectedOutput(
"defers-super-block-with-deferred.expected"
);
context.remove(RelativePathResolver.CURRENT_PATH_CONTEXT_KEY);
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
"defers-super-block-with-deferred.expected"
);
Expand All @@ -90,6 +97,7 @@ public void itReconstructsDeferredOutsideBlockSecondPass() {
expectedTemplateInterpreter.assertExpectedOutput(
"reconstructs-deferred-outside-block.expected"
);
context.remove(RelativePathResolver.CURRENT_PATH_CONTEXT_KEY);
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
"reconstructs-deferred-outside-block.expected"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ Parent's current path is: {{ '{{' + prefix + '_path }}' }}
{% set current_path = temp_current_path_586961858 %}
-----Post-Block-----
Parent's current path is: {{ '{{' + prefix + '_path }}' }}
{% set current_path = '../../eager/reconstructs-block-path-when-deferred/base.jinja' %}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<html>
{% set current_path = '../eager/extendstag/base.html' %}<html>
<body>
<div class="sidebar">
<h3>Table Of Contents</h3>
{{ deferred }}
{% set temp_current_path_743889914 = current_path %}{% set current_path = '' %}<h3>Table Of Contents</h3>
{{ deferred }}{% set current_path = temp_current_path_743889914 %}
</div>
</body>
</html>

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<html>
{% set current_path = '../eager/extendstag/base-deferred.html' %}<html>
<body>
<div class="sidebar">
<h3>Table Of Contents</h3>
{% set temp_current_path_743889914 = current_path %}{% set current_path = '' %}<h3>Table Of Contents</h3>

<p>this is a {{ deferred }}.</p>

{% set current_path = temp_current_path_743889914 %}
</div>
</body>
</html>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
{% set foo = 'yes' %}
{% else %}
{% set foo = 'no' %}
{% endif %}{% enddo %}{# End Label: ignored_output_from_extends #}<html>
{% endif %}{% enddo %}{# End Label: ignored_output_from_extends #}{% set current_path = '../eager/extendstag/base.html' %}<html>
<body>
<div class="sidebar">
<h3>Table Of Contents</h3>
<p>{{ foo }}.</p>
{% set temp_current_path_743889914 = current_path %}{% set current_path = '' %}<h3>Table Of Contents</h3>
<p>{{ foo }}.</p>{% set current_path = temp_current_path_743889914 %}
</div>
</body>
</html>
</html>

0 comments on commit 016e299

Please sign in to comment.