Skip to content

Commit d68410e

Browse files
committed
Improve second-pass result checking by also directly checking the original file against the final expected result
1 parent 8b313d5 commit d68410e

7 files changed

Lines changed: 127 additions & 26 deletions

src/test/java/com/hubspot/jinjava/EagerTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,9 @@ public void itSetsMultipleVarsDeferredInChildSecondPass() {
551551
expectedTemplateInterpreter.assertExpectedOutput(
552552
"sets-multiple-vars-deferred-in-child.expected"
553553
);
554+
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
555+
"sets-multiple-vars-deferred-in-child.expected"
556+
);
554557
}
555558

556559
@Test
@@ -657,6 +660,8 @@ public void itEagerlyDefersMacro() {
657660

658661
@Test
659662
public void itEagerlyDefersMacroSecondPass() {
663+
localContext.put("foo", "I am foo");
664+
localContext.put("bar", "I am bar");
660665
localContext.put("deferred", true);
661666
expectedTemplateInterpreter.assertExpectedOutput("eagerly-defers-macro.expected");
662667
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
@@ -801,6 +806,9 @@ public void itHandlesDeferredImportVarsSecondPass() {
801806
expectedTemplateInterpreter.assertExpectedOutput(
802807
"handles-deferred-import-vars.expected"
803808
);
809+
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
810+
"handles-deferred-import-vars.expected"
811+
);
804812
}
805813

806814
@Test
@@ -822,6 +830,9 @@ public void itHandlesDeferredFromImportAsSecondPass() {
822830
expectedTemplateInterpreter.assertExpectedOutput(
823831
"handles-deferred-from-import-as.expected"
824832
);
833+
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
834+
"handles-deferred-from-import-as.expected"
835+
);
825836
}
826837

827838
@Test
@@ -889,6 +900,9 @@ public void itHandlesDeferredInNamespaceSecondPass() {
889900
expectedTemplateInterpreter.assertExpectedOutput(
890901
"handles-deferred-in-namespace.expected"
891902
);
903+
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
904+
"handles-deferred-in-namespace.expected"
905+
);
892906
}
893907

894908
@Test
@@ -918,6 +932,9 @@ public void itHandlesBlockSetInDeferredIfSecondPass() {
918932
expectedTemplateInterpreter.assertExpectedOutput(
919933
"handles-block-set-in-deferred-if.expected"
920934
);
935+
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
936+
"handles-block-set-in-deferred-if.expected"
937+
);
921938
}
922939

923940
@Test
@@ -1001,6 +1018,9 @@ public void itHandlesDoubleImportModification() {
10011018
@Test
10021019
public void itHandlesDoubleImportModificationSecondPass() {
10031020
interpreter.getContext().put("deferred", false);
1021+
expectedTemplateInterpreter.assertExpectedOutput(
1022+
"handles-double-import-modification.expected"
1023+
);
10041024
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
10051025
"handles-double-import-modification.expected"
10061026
);
@@ -1016,6 +1036,9 @@ public void itHandlesSameNameImportVar() {
10161036
@Test
10171037
public void itHandlesSameNameImportVarSecondPass() {
10181038
interpreter.getContext().put("deferred", "resolved");
1039+
expectedTemplateInterpreter.assertExpectedOutput(
1040+
"handles-same-name-import-var.expected"
1041+
);
10191042
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
10201043
"handles-same-name-import-var.expected"
10211044
);
@@ -1069,6 +1092,9 @@ public void itFullyDefersFilteredMacroSecondPass() {
10691092
expectedTemplateInterpreter.assertExpectedOutput(
10701093
"fully-defers-filtered-macro.expected"
10711094
);
1095+
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
1096+
"fully-defers-filtered-macro.expected"
1097+
);
10721098
}
10731099

10741100
@Test
@@ -1103,6 +1129,7 @@ public void itReconstructsMapNode() {
11031129
@Test
11041130
public void itReconstructsMapNodeSecondPass() {
11051131
interpreter.getContext().put("deferred", "resolved");
1132+
expectedTemplateInterpreter.assertExpectedOutput("reconstructs-map-node.expected");
11061133
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
11071134
"reconstructs-map-node.expected"
11081135
);
@@ -1123,6 +1150,9 @@ public void itDefersCallTagWithDeferredArgument() {
11231150
@Test
11241151
public void itDefersCallTagWithDeferredArgumentSecondPass() {
11251152
interpreter.getContext().put("deferred", "resolved");
1153+
expectedTemplateInterpreter.assertExpectedOutput(
1154+
"defers-call-tag-with-deferred-argument.expected"
1155+
);
11261156
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
11271157
"defers-call-tag-with-deferred-argument.expected"
11281158
);
@@ -1145,6 +1175,9 @@ public void itHandlesHigherScopeReferenceModification() {
11451175
@Test
11461176
public void itHandlesHigherScopeReferenceModificationSecondPass() {
11471177
interpreter.getContext().put("deferred", "b");
1178+
expectedTemplateInterpreter.assertExpectedOutput(
1179+
"handles-higher-scope-reference-modification.expected"
1180+
);
11481181
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
11491182
"handles-higher-scope-reference-modification.expected"
11501183
);
@@ -1181,6 +1214,9 @@ public void itDoesNotOverrideImportModificationInFor() {
11811214
@Test
11821215
public void itDoesNotOverrideImportModificationInForSecondPass() {
11831216
interpreter.getContext().put("deferred", "resolved");
1217+
expectedTemplateInterpreter.assertExpectedOutput(
1218+
"does-not-override-import-modification-in-for.expected"
1219+
);
11841220
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
11851221
"does-not-override-import-modification-in-for.expected"
11861222
);
@@ -1196,6 +1232,9 @@ public void itHandlesDeferredForLoopVarFromMacro() {
11961232
@Test
11971233
public void itHandlesDeferredForLoopVarFromMacroSecondPass() {
11981234
interpreter.getContext().put("deferred", "resolved");
1235+
expectedTemplateInterpreter.assertExpectedOutput(
1236+
"handles-deferred-for-loop-var-from-macro.expected"
1237+
);
11991238
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
12001239
"handles-deferred-for-loop-var-from-macro.expected"
12011240
);
@@ -1225,6 +1264,9 @@ public void itReconstructsNamespaceForSetTagsUsingPeriod() {
12251264
@Test
12261265
public void itReconstructsNamespaceForSetTagsUsingPeriodSecondPass() {
12271266
interpreter.getContext().put("deferred", "resolved");
1267+
expectedTemplateInterpreter.assertExpectedOutput(
1268+
"reconstructs-namespace-for-set-tags-using-period.expected"
1269+
);
12281270
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
12291271
"reconstructs-namespace-for-set-tags-using-period.expected"
12301272
);
@@ -1241,6 +1283,9 @@ public void itUsesUniqueMacroNames() {
12411283
public void itUsesUniqueMacroNamesSecondPass() {
12421284
interpreter.getContext().put("deferred", "resolved");
12431285
expectedTemplateInterpreter.assertExpectedOutput("uses-unique-macro-names.expected");
1286+
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
1287+
"uses-unique-macro-names.expected"
1288+
);
12441289
}
12451290

12461291
@Test
@@ -1298,6 +1343,9 @@ public void itKeepsMacroModificationsInScopeSecondPass() {
12981343
expectedTemplateInterpreter.assertExpectedOutput(
12991344
"keeps-macro-modifications-in-scope.expected"
13001345
);
1346+
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
1347+
"keeps-macro-modifications-in-scope.expected"
1348+
);
13011349
}
13021350

13031351
@Test
@@ -1310,6 +1358,9 @@ public void itDoesNotReconstructVariableInWrongScope() {
13101358
@Test
13111359
public void itDoesNotReconstructVariableInWrongScopeSecondPass() {
13121360
interpreter.getContext().put("deferred", true);
1361+
expectedTemplateInterpreter.assertExpectedOutput(
1362+
"does-not-reconstruct-variable-in-wrong-scope.expected"
1363+
);
13131364
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
13141365
"does-not-reconstruct-variable-in-wrong-scope.expected"
13151366
);
@@ -1356,6 +1407,9 @@ public void itRreconstructsValueUsedInDeferredImportedMacroSecondPass() {
13561407
expectedTemplateInterpreter.assertExpectedOutput(
13571408
"reconstructs-value-used-in-deferred-imported-macro.expected"
13581409
);
1410+
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
1411+
"reconstructs-value-used-in-deferred-imported-macro.expected"
1412+
);
13591413
}
13601414

13611415
@Test
@@ -1371,6 +1425,9 @@ public void itAllowsDeferredLazyReferenceToGetOverriddenSecondPass() {
13711425
expectedTemplateInterpreter.assertExpectedOutput(
13721426
"allows-deferred-lazy-reference-to-get-overridden.expected"
13731427
);
1428+
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
1429+
"allows-deferred-lazy-reference-to-get-overridden.expected"
1430+
);
13741431
}
13751432

13761433
@Test
@@ -1411,6 +1468,9 @@ public void itReconstructsNestedValueInStringRepresentationSecondPass() {
14111468
expectedTemplateInterpreter.assertExpectedOutput(
14121469
"reconstructs-nested-value-in-string-representation.expected"
14131470
);
1471+
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
1472+
"reconstructs-nested-value-in-string-representation.expected"
1473+
);
14141474
}
14151475

14161476
@Test
@@ -1425,6 +1485,9 @@ public void itDefersLoopSettingMetaContextVar() {
14251485
public void itDefersLoopSettingMetaContextVarSecondPass() {
14261486
interpreter.getContext().put("deferred", "resolved");
14271487
interpreter.getContext().getMetaContextVariables().add("content");
1488+
expectedTemplateInterpreter.assertExpectedOutput(
1489+
"defers-loop-setting-meta-context-var.expected"
1490+
);
14281491
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
14291492
"defers-loop-setting-meta-context-var.expected"
14301493
);

src/test/java/com/hubspot/jinjava/ExpectedTemplateInterpreter.java

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,34 +47,65 @@ public String assertExpectedOutputNonIdempotent(String name) {
4747
}
4848

4949
public String assertExpectedNonEagerOutput(String name) {
50-
JinjavaInterpreter preserveInterpreter = new JinjavaInterpreter(
51-
jinjava,
52-
jinjava.getGlobalContextCopy(),
53-
JinjavaConfig
54-
.newBuilder()
55-
.withExecutionMode(DefaultExecutionMode.instance())
56-
.withNestedInterpretationEnabled(true)
57-
.withLegacyOverrides(
58-
LegacyOverrides.newBuilder().withUsePyishObjectMapper(true).build()
59-
)
60-
.withMaxMacroRecursionDepth(20)
61-
.withEnableRecursiveMacroCalls(true)
62-
.build()
63-
);
50+
String output;
6451
try {
52+
JinjavaInterpreter preserveInterpreter = new JinjavaInterpreter(
53+
jinjava,
54+
jinjava.getGlobalContextCopy(),
55+
JinjavaConfig
56+
.newBuilder()
57+
.withExecutionMode(DefaultExecutionMode.instance())
58+
.withNestedInterpretationEnabled(true)
59+
.withLegacyOverrides(
60+
LegacyOverrides.newBuilder().withUsePyishObjectMapper(true).build()
61+
)
62+
.withMaxMacroRecursionDepth(20)
63+
.withEnableRecursiveMacroCalls(true)
64+
.build()
65+
);
6566
JinjavaInterpreter.pushCurrent(preserveInterpreter);
6667

6768
preserveInterpreter.getContext().putAll(interpreter.getContext());
6869
String template = getFixtureTemplate(name);
69-
String output = JinjavaInterpreter.getCurrent().render(template);
70+
output = JinjavaInterpreter.getCurrent().render(template);
7071
assertThat(JinjavaInterpreter.getCurrent().getContext().getDeferredNodes())
7172
.as("Ensure no deferred nodes were created")
7273
.isEmpty();
7374
assertThat(output.trim()).isEqualTo(expected(name).trim());
74-
return output;
7575
} finally {
7676
JinjavaInterpreter.popCurrent();
7777
}
78+
if (name.contains(".expected")) {
79+
String originalName = name.replace(".expected", "");
80+
try {
81+
JinjavaInterpreter preserveInterpreter = new JinjavaInterpreter(
82+
jinjava,
83+
jinjava.getGlobalContextCopy(),
84+
JinjavaConfig
85+
.newBuilder()
86+
.withExecutionMode(DefaultExecutionMode.instance())
87+
.withNestedInterpretationEnabled(true)
88+
.withLegacyOverrides(
89+
LegacyOverrides.newBuilder().withUsePyishObjectMapper(true).build()
90+
)
91+
.withMaxMacroRecursionDepth(20)
92+
.withEnableRecursiveMacroCalls(true)
93+
.build()
94+
);
95+
JinjavaInterpreter.pushCurrent(preserveInterpreter);
96+
97+
preserveInterpreter.getContext().putAll(interpreter.getContext());
98+
String template = getFixtureTemplate(originalName);
99+
output = JinjavaInterpreter.getCurrent().render(template);
100+
assertThat(JinjavaInterpreter.getCurrent().getContext().getDeferredNodes())
101+
.as("Ensure no deferred nodes were created")
102+
.isEmpty();
103+
assertThat(output.trim()).isEqualTo(expected(name).trim());
104+
} finally {
105+
JinjavaInterpreter.popCurrent();
106+
}
107+
}
108+
return output;
78109
}
79110

80111
public String getFixtureTemplate(String name) {

src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerExtendsTagTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public void itDefersBlockInExtendsChildSecondPass() {
5454
expectedTemplateInterpreter.assertExpectedOutput(
5555
"defers-block-in-extends-child.expected"
5656
);
57+
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
58+
"defers-block-in-extends-child.expected"
59+
);
5760
}
5861

5962
@Test
@@ -67,6 +70,9 @@ public void itDefersSuperBlockWithDeferredSecondPass() {
6770
expectedTemplateInterpreter.assertExpectedOutput(
6871
"defers-super-block-with-deferred.expected"
6972
);
73+
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
74+
"defers-super-block-with-deferred.expected"
75+
);
7076
}
7177

7278
@Test
@@ -83,6 +89,9 @@ public void itReconstructsDeferredOutsideBlockSecondPass() {
8389
expectedTemplateInterpreter.assertExpectedOutput(
8490
"reconstructs-deferred-outside-block.expected"
8591
);
92+
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
93+
"reconstructs-deferred-outside-block.expected"
94+
);
8695
}
8796

8897
@Test
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{% set foo = ['1','2','3'] %}
2+
{% set one = '1' %}
3+
{% set two = '2' %}
4+
{% set three = '3' %}
25
{%- for item in deferred %}
36
{% cycle foo %}
4-
{% cycle foo[0],foo[1],foo[2] %}
7+
{% cycle one,two,three %}
58
{%- endfor -%}

src/test/resources/eager/reconstructs-map-node.expected.expected.jinja

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
First key is foo.
2-
31
foo ff
42
bar bb
53
['resolved', 'resolved']
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
{% if deferred %}
22
{% set foo = [fn:map_entry('foo', 'ff'), fn:map_entry('bar', 'bb')] %}
33
{% endif %}
4-
First key is {{ foo[0].key }}.
5-
{% set my_list = [] %}{% for __ignored__ in [0] %}{% do my_list.append(deferred) %}
6-
foo ff{% do my_list.append(deferred) %}
7-
bar bb{% endfor %}
4+
{% set my_list = [] %}{% for key, val in foo %}{% do my_list.append(deferred) %}
5+
{{ key ~ ' ' ~ val }}{% endfor %}
86
{{ my_list }}
97

108
{% set my_list = [] %}{% for __ignored__ in [0] %}{% do my_list.append(deferred) %}
119
foo{% do my_list.append(deferred) %}
1210
bar{% endfor %}
13-
{{ my_list }}
11+
{{ my_list }}

src/test/resources/eager/reconstructs-map-node.jinja

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
{% if deferred %}
33
{% set foo = {'foo': 'ff', 'bar': 'bb'}.items() %}
44
{% endif %}
5-
First key is {{ foo[0].key }}.
6-
{% for key, val in {'foo': 'ff', 'bar': 'bb'}.items() -%}
5+
{% for key, val in foo -%}
76
{% do my_list.append(deferred) %}
87
{{ key ~ ' ' ~ val }}
98
{%- endfor %}

0 commit comments

Comments
 (0)