Skip to content

Commit 8ba78b9

Browse files
committed
Update some tests
1 parent 1059bda commit 8ba78b9

17 files changed

Lines changed: 203 additions & 125 deletions

3.0-changes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
- Migrated JinjavaConfig to immutable
22
- Removed whitespaceRequiredWithinTokens LegacyOverride and converted to BuiltinFeatures
3-
-
3+
- Nested interpretation disabled by defaut

src/main/java/com/hubspot/jinjava/objects/DummyObject.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.hubspot.jinjava.objects;
22

33
import com.google.common.collect.ImmutableList;
4+
import com.google.common.collect.ImmutableSet;
45
import java.util.Collection;
56
import java.util.Map;
67
import java.util.Set;
78

8-
public class DummyObject implements Map<String, Object>, PyWrapper {
9+
public class DummyObject implements Map<Object, Object>, PyWrapper {
910

1011
@Override
1112
public int size() {
@@ -33,7 +34,7 @@ public Object get(Object key) {
3334
}
3435

3536
@Override
36-
public Object put(String key, Object value) {
37+
public Object put(Object key, Object value) {
3738
return new DummyObject();
3839
}
3940

@@ -43,14 +44,14 @@ public Object remove(Object key) {
4344
}
4445

4546
@Override
46-
public void putAll(Map<? extends String, ?> m) {}
47+
public void putAll(Map<? extends Object, ?> m) {}
4748

4849
@Override
4950
public void clear() {}
5051

5152
@Override
52-
public Set<String> keySet() {
53-
return null;
53+
public Set<Object> keySet() {
54+
return ImmutableSet.of(new DummyObject());
5455
}
5556

5657
@Override
@@ -59,7 +60,7 @@ public Collection<Object> values() {
5960
}
6061

6162
@Override
62-
public Set<Entry<String, Object>> entrySet() {
63-
return null;
63+
public Set<Entry<Object, Object>> entrySet() {
64+
return ImmutableSet.of(Map.entry(new DummyObject(), new DummyObject()));
6465
}
6566
}

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@ public abstract class BaseJinjavaTest {
88

99
@Before
1010
public void baseSetup() {
11-
jinjava =
12-
new Jinjava(
13-
JinjavaConfig
14-
.newBuilder()
15-
.withLegacyOverrides(
16-
LegacyOverrides
17-
.newBuilder()
18-
.withUsePyishObjectMapper(true)
19-
.withKeepNullableLoopValues(true)
20-
.build()
21-
)
22-
.build()
23-
);
11+
jinjava = new Jinjava(JinjavaConfig.newBuilder().build());
2412
}
2513
}

src/test/java/com/hubspot/jinjava/el/ExtendedSyntaxBuilderTest.java

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.google.common.io.Resources;
88
import com.hubspot.jinjava.Jinjava;
99
import com.hubspot.jinjava.JinjavaConfig;
10+
import com.hubspot.jinjava.LegacyOverrides;
1011
import com.hubspot.jinjava.interpret.Context;
1112
import com.hubspot.jinjava.interpret.IndexOutOfRangeException;
1213
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
@@ -188,22 +189,37 @@ public void literalTuple() {
188189

189190
@Test
190191
public void mapLiteral() {
191-
context.put("foo", "bar");
192-
assertThat((Map<String, Object>) val("{}")).isEmpty();
193-
Map<String, Object> map = (Map<String, Object>) val(
194-
"{foo: foo, \"foo2\": foo, foo3: 123, foo4: 'string', foo5: {}, foo6: [1, 2]}"
195-
);
196-
assertThat(map)
197-
.contains(
198-
entry("foo", "bar"),
199-
entry("foo2", "bar"),
200-
entry("foo3", 123L),
201-
entry("foo4", "string"),
202-
entry("foo6", Arrays.asList(1L, 2L))
192+
interpreter =
193+
new Jinjava(
194+
JinjavaConfig
195+
.newBuilder()
196+
.withMaxOutputSize(MAX_STRING_LENGTH)
197+
.withLegacyOverrides(
198+
LegacyOverrides.newBuilder().withEvaluateMapKeys(false).build()
199+
)
200+
.build()
201+
)
202+
.newInterpreter();
203+
try (var a = JinjavaInterpreter.closeablePushCurrent(interpreter).get()) {
204+
context.put("foo", "bar");
205+
assertThat((Map<String, Object>) val("{}")).isEmpty();
206+
Map<String, Object> map = (Map<String, Object>) val(
207+
"{foo: foo, \"foo2\": foo, foo3: 123, foo4: 'string', foo5: {}, foo6: [1, 2]}"
203208
);
204-
205-
assertThat((Map<String, Object>) val("{\"address\":\"123 Main - Boston, MA 02111\"}"))
206-
.contains(entry("address", "123 Main - Boston, MA 02111"));
209+
assertThat(map)
210+
.contains(
211+
entry("foo", "bar"),
212+
entry("foo2", "bar"),
213+
entry("foo3", 123L),
214+
entry("foo4", "string"),
215+
entry("foo6", Arrays.asList(1L, 2L))
216+
);
217+
218+
assertThat(
219+
(Map<String, Object>) val("{\"address\":\"123 Main - Boston, MA 02111\"}")
220+
)
221+
.contains(entry("address", "123 Main - Boston, MA 02111"));
222+
}
207223
}
208224

209225
@Test

src/test/java/com/hubspot/jinjava/lib/filter/PrettyPrintFilterTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public void ppObject() {
7171
"{%% raw %%}(MyClass: {\n" +
7272
" &quot;foo&quot; : &quot;%s&quot;,\n" +
7373
" &quot;bar&quot; : %d,\n" +
74-
" &quot;nestedClass&quot; : {\n" +
75-
" &quot;fooField&quot; : &quot;%s&quot;,\n" +
76-
" &quot;barField&quot; : %d\n" +
74+
" &quot;nested_class&quot; : {\n" +
75+
" &quot;foo_field&quot; : &quot;%s&quot;,\n" +
76+
" &quot;bar_field&quot; : %d\n" +
7777
" }\n" +
7878
"}){%% endraw %%}",
7979
myClass.getFoo(),

src/test/java/com/hubspot/jinjava/lib/tag/MacroTagTest.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -282,38 +282,41 @@ public void itEnforcesMacroRecursionWithMaxDepth() throws IOException {
282282
.build()
283283
)
284284
.newInterpreter();
285-
JinjavaInterpreter.pushCurrent(interpreter);
286-
287-
try {
285+
try (var a = JinjavaInterpreter.closeablePushCurrent(interpreter).get()) {
288286
String template = fixtureText("ending-recursion");
289287
String out = interpreter.render(template);
290288
assertThat(interpreter.getErrorsCopy().get(0).getMessage())
291289
.contains("Max recursion limit of 2 reached for macro 'hello'");
292290
assertThat(out).contains("Hello Hello");
293-
} finally {
294-
JinjavaInterpreter.popCurrent();
295291
}
296292
}
297293

298294
@Test
299295
public void itPreventsRecursionForMacroWithVar() {
300-
String jinja =
301-
"{%- macro func(var) %}" +
302-
"{%- for f in var %}" +
303-
"{{ f.val }}" +
304-
"{%- endfor %}" +
305-
"{%- endmacro %}" +
306-
"{%- set var = {" +
307-
" 'f' : {" +
308-
" 'val': '{{ self }}'," +
309-
" }" +
310-
"} %}" +
311-
"{% set self='{{var}}' %}" +
312-
"{{ func(var) }}" +
313-
"";
314-
Node node = new TreeParser(interpreter, jinja).buildTree();
315-
assertThat(interpreter.render(node))
316-
.isEqualTo("{'f': {'val': '{'f': {'val': '{{ self }}'} }'} }");
296+
interpreter =
297+
new Jinjava(
298+
JinjavaConfig.newBuilder().withNestedInterpretationEnabled(true).build()
299+
)
300+
.newInterpreter();
301+
try (var a = JinjavaInterpreter.closeablePushCurrent(interpreter).get()) {
302+
String jinja =
303+
"{%- macro func(var) %}" +
304+
"{%- for f in var %}" +
305+
"{{ f.val }}" +
306+
"{%- endfor %}" +
307+
"{%- endmacro %}" +
308+
"{%- set var = {" +
309+
" 'f' : {" +
310+
" 'val': '{{ self }}'," +
311+
" }" +
312+
"} %}" +
313+
"{% set self='{{var}}' %}" +
314+
"{{ func(var) }}" +
315+
"";
316+
Node node = new TreeParser(interpreter, jinja).buildTree();
317+
assertThat(interpreter.render(node))
318+
.isEqualTo("{'f': {'val': '{'f': {'val': '{{ self }}'} }'} }");
319+
}
317320
}
318321

319322
@Test

src/test/java/com/hubspot/jinjava/lib/tag/ValidationModeTest.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.common.collect.ImmutableList;
66
import com.hubspot.jinjava.Jinjava;
77
import com.hubspot.jinjava.JinjavaConfig;
8+
import com.hubspot.jinjava.LegacyOverrides;
89
import com.hubspot.jinjava.interpret.Context;
910
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
1011
import com.hubspot.jinjava.lib.filter.Filter;
@@ -17,7 +18,6 @@
1718
import java.util.LinkedHashMap;
1819
import java.util.List;
1920
import java.util.Map;
20-
import org.junit.After;
2121
import org.junit.Before;
2222
import org.junit.Test;
2323
import org.junit.runner.RunWith;
@@ -81,15 +81,12 @@ public void setup() {
8181
new JinjavaInterpreter(
8282
jinjava,
8383
context,
84-
JinjavaConfig.newBuilder().withValidationMode(true).build()
84+
JinjavaConfig
85+
.newBuilder()
86+
.withLegacyOverrides(LegacyOverrides.NONE)
87+
.withValidationMode(true)
88+
.build()
8589
);
86-
87-
JinjavaInterpreter.pushCurrent(interpreter);
88-
}
89-
90-
@After
91-
public void tearDown() {
92-
JinjavaInterpreter.popCurrent();
9390
}
9491

9592
@Test
@@ -278,17 +275,19 @@ public void itDoesNotExecuteMacrosInValidatedBlocks() {
278275
interpreter.getContext()
279276
);
280277
interpreter.getContext().addGlobalMacro(macro);
281-
282278
String template =
283279
"{{ hello() }}" + "{% if false %} " + " {{ hello() }}" + "{% endif %}";
284280

285-
assertThat(interpreter.getErrors()).isEmpty();
286-
assertThat(interpreter.render(template).trim()).isEqualTo("hello");
287-
assertThat(macro.getInvocationCount()).isEqualTo(1);
288-
289-
assertThat(validatingInterpreter.render(template).trim()).isEqualTo("hello");
290-
assertThat(macro.getInvocationCount()).isEqualTo(3);
291-
assertThat(validatingInterpreter.getErrors()).isEmpty();
281+
try (var a = JinjavaInterpreter.closeablePushCurrent(interpreter).get()) {
282+
assertThat(interpreter.getErrors()).isEmpty();
283+
assertThat(interpreter.render(template).trim()).isEqualTo("hello");
284+
assertThat(macro.getInvocationCount()).isEqualTo(1);
285+
}
286+
try (var a = JinjavaInterpreter.closeablePushCurrent(validatingInterpreter).get()) {
287+
assertThat(validatingInterpreter.render(template).trim()).isEqualTo("hello");
288+
assertThat(macro.getInvocationCount()).isEqualTo(3);
289+
assertThat(validatingInterpreter.getErrors()).isEmpty();
290+
}
292291
}
293292

294293
@Test
@@ -333,13 +332,13 @@ public void itDoesNotExecuteFiltersInValidatedBlocks() {
333332
assertThat(result).isEqualTo("10");
334333
assertThat(validationFilter.getExecutionCount()).isEqualTo(1);
335334

336-
JinjavaInterpreter.pushCurrent(validatingInterpreter);
337-
result = validatingInterpreter.render(template).trim();
335+
try (var a = JinjavaInterpreter.closeablePushCurrent(validatingInterpreter).get()) {
336+
result = validatingInterpreter.render(template).trim();
338337

339-
assertThat(validatingInterpreter.getErrors().size()).isEqualTo(1);
340-
assertThat(validatingInterpreter.getErrors().get(0).getMessage()).contains("hey(");
341-
assertThat(result).isEqualTo("10");
342-
assertThat(validationFilter.getExecutionCount()).isEqualTo(2);
343-
JinjavaInterpreter.popCurrent();
338+
assertThat(validatingInterpreter.getErrors().size()).isEqualTo(1);
339+
assertThat(validatingInterpreter.getErrors().get(0).getMessage()).contains("hey(");
340+
assertThat(result).isEqualTo("10");
341+
assertThat(validationFilter.getExecutionCount()).isEqualTo(2);
342+
}
344343
}
345344
}

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@ public class EagerExtendsTagTest extends ExtendsTagTest {
2222

2323
@Before
2424
public void eagerSetup() {
25+
eagerSetup(false);
26+
}
27+
28+
void eagerSetup(boolean nestedInterpretation) {
29+
JinjavaInterpreter.popCurrent();
2530
interpreter =
2631
new JinjavaInterpreter(
2732
jinjava,
2833
context,
2934
JinjavaConfig
3035
.newBuilder()
36+
.withNestedInterpretationEnabled(nestedInterpretation)
3137
.withExecutionMode(EagerExecutionMode.instance())
3238
.withLegacyOverrides(
3339
LegacyOverrides.newBuilder().withUsePyishObjectMapper(true).build()
@@ -74,15 +80,28 @@ public void itDefersSuperBlockWithDeferred() {
7480
@Test
7581
public void itDefersSuperBlockWithDeferredSecondPass() {
7682
context.put("deferred", "Resolved now");
77-
expectedTemplateInterpreter.assertExpectedOutput(
78-
"defers-super-block-with-deferred.expected"
79-
);
80-
context.remove(RelativePathResolver.CURRENT_PATH_CONTEXT_KEY);
8183
expectedTemplateInterpreter.assertExpectedNonEagerOutput(
8284
"defers-super-block-with-deferred.expected"
8385
);
8486
}
8587

88+
@Test
89+
public void itDefersSuperBlockWithDeferredNestedInterp() {
90+
eagerSetup(true);
91+
expectedTemplateInterpreter.assertExpectedOutputNonIdempotent(
92+
"defers-super-block-with-deferred-nested-interp"
93+
);
94+
}
95+
96+
@Test
97+
public void itDefersSuperBlockWithDeferredNestedInterpSecondPass() {
98+
eagerSetup(true);
99+
context.put("deferred", "Resolved now");
100+
expectedTemplateInterpreter.assertExpectedOutput(
101+
"defers-super-block-with-deferred-nested-interp.expected"
102+
);
103+
}
104+
86105
@Test
87106
public void itReconstructsDeferredOutsideBlock() {
88107
expectedTemplateInterpreter.assertExpectedOutputNonIdempotent(

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,10 +570,11 @@ public void itHandlesVarFromImportedMacro() {
570570
"a" +
571571
"{% endset %}" +
572572
"{{ __macro_adjust_108896029_temp_variable_0__ }}\n" +
573-
"{% for __ignored__ in [0] %}" +
573+
"{% set __macro_adjust_108896029_temp_variable_1__ %}" +
574574
"{% do var.append('b' ~ deferred) %}" +
575575
"b" +
576-
"{% endfor %}\n" +
576+
"{% endset %}" +
577+
"{{ __macro_adjust_108896029_temp_variable_1__ }}\n" +
577578
"c{{ var }}"
578579
);
579580
context.put("deferred", "resolved");

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5-
import static org.mockito.Mockito.*;
5+
import static org.mockito.Mockito.any;
6+
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.when;
68

79
import com.hubspot.jinjava.BaseInterpretingTest;
810
import com.hubspot.jinjava.JinjavaConfig;
@@ -136,7 +138,7 @@ public void itPutsOnContextInChildContext() {
136138
public void itModifiesContextInChildContext() {
137139
context.put("foo", new ArrayList<>());
138140
assertThat(interpreter.render("{{ modify_context('foo', 'bar') }}{{ foo }}"))
139-
.isEqualTo("[bar]");
141+
.isEqualTo("['bar']");
140142
}
141143

142144
@Test
@@ -145,7 +147,7 @@ public void itDoesntModifyContextWhenResultIsDeferred() {
145147
assertThat(
146148
interpreter.render("{{ modify_context('foo', 'bar') ~ deferred }}{{ foo }}")
147149
)
148-
.isEqualTo("{{ null ~ deferred }}[bar]");
150+
.isEqualTo("{{ null ~ deferred }}['bar']");
149151
}
150152

151153
public static void addToContext(String key, Object value) {

0 commit comments

Comments
 (0)