|
1 | 1 | package com.hubspot.jinjava.lib.filter; |
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 5 | +import static org.mockito.Mockito.mock; |
| 6 | +import static org.mockito.Mockito.when; |
4 | 7 |
|
5 | 8 | import com.google.common.collect.ImmutableMap; |
6 | 9 | import com.google.common.io.Resources; |
7 | 10 | import com.hubspot.jinjava.BaseInterpretingTest; |
| 11 | +import com.hubspot.jinjava.interpret.Context; |
| 12 | +import com.hubspot.jinjava.interpret.DeferredValueException; |
| 13 | +import com.hubspot.jinjava.interpret.JinjavaInterpreter; |
| 14 | +import com.hubspot.jinjava.lib.tag.eager.DeferredToken; |
| 15 | +import com.hubspot.jinjava.tree.parse.ExpressionToken; |
8 | 16 | import java.io.IOException; |
9 | 17 | import java.nio.charset.StandardCharsets; |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.concurrent.atomic.AtomicInteger; |
10 | 20 | import org.junit.Before; |
11 | 21 | import org.junit.Test; |
12 | 22 |
|
@@ -54,6 +64,59 @@ public void itDoesntChopWordsWhenSpecified() { |
54 | 64 | ); |
55 | 65 | } |
56 | 66 |
|
| 67 | + @Test |
| 68 | + public void itExecutesJinjavaInsideTag() { |
| 69 | + assertThat( |
| 70 | + filter.filter("{% for i in [1, 2, 3] %}<div>{{i}}</div>{% endfor %}", interpreter) |
| 71 | + ) |
| 72 | + .isEqualTo("<div>\n 1\n</div>\n<div>\n 2\n</div>\n<div>\n 3\n</div>"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void itExecutesJinjavaInsideTagAndTruncates() { |
| 77 | + assertThat( |
| 78 | + filter.filter( |
| 79 | + "{% for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] %}<div>{{i}}</div>{% endfor %}", |
| 80 | + interpreter, |
| 81 | + "10" |
| 82 | + ) |
| 83 | + ) |
| 84 | + .isEqualTo( |
| 85 | + "<div>\n 1\n</div>\n<div>\n 2\n</div>\n<div>\n 3\n</div>\n<div>\n 4\n</div>\n<div>\n 5\n</div>\n" + |
| 86 | + "<div>\n 6\n</div>\n<div>\n 7\n</div>\n<div>\n 8\n</div>\n<div>\n 9\n</div>\n<div>\n ...\n</div>" |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void itIsolatesJinjavaScopeWhenExecutingCodeInsideTag() { |
| 92 | + filter.filter("{% set test = 'hello' %}", interpreter); |
| 93 | + assertThat(interpreter.getContext().get("test")).isNull(); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void itThrowsDeferredValueExceptionWhenDeferredTokensAreLeft() { |
| 98 | + AtomicInteger counter = new AtomicInteger(); |
| 99 | + JinjavaInterpreter mockedInterpreter = mock(JinjavaInterpreter.class); |
| 100 | + Context mockedContext = mock(Context.class); |
| 101 | + when(mockedInterpreter.getContext()).thenReturn(mockedContext); |
| 102 | + when(mockedContext.getDeferredTokens()) |
| 103 | + .thenAnswer(i -> |
| 104 | + counter.getAndIncrement() == 0 |
| 105 | + ? Collections.emptySet() |
| 106 | + : Collections.singleton( |
| 107 | + DeferredToken |
| 108 | + .builderFromImage( |
| 109 | + "{{ deferred && other }}", |
| 110 | + ExpressionToken.class, |
| 111 | + interpreter |
| 112 | + ) |
| 113 | + .build() |
| 114 | + ) |
| 115 | + ); |
| 116 | + assertThatThrownBy(() -> filter.filter("{{ deferred && other }}", mockedInterpreter)) |
| 117 | + .isInstanceOf(DeferredValueException.class); |
| 118 | + } |
| 119 | + |
57 | 120 | @Test |
58 | 121 | public void itTakesKwargs() { |
59 | 122 | String result = (String) filter.filter( |
|
0 commit comments