1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
import itertools
from jinja2 import Template
from jinja2.runtime import LoopContext
TEST_IDX_TEMPLATE_STR_1 = (
"[{% for i in lst|reverse %}"
+ "(len={{ loop.length }}, revindex={{ loop.revindex }}, index={{ loop.index }}, val={{ i }})"
+ "{% endfor %}]"
)
TEST_IDX0_TEMPLATE_STR_1 = (
"[{% for i in lst|reverse %}"
+ "(len={{ loop.length }}, revindex0={{ loop.revindex0 }}, index0={{ loop.index0 }}, val={{ i }})"
+ "{% endfor %}]"
)
def test_loop_idx():
t = Template(TEST_IDX_TEMPLATE_STR_1)
lst = [10]
excepted_render = "[(len=1, revindex=1, index=1, val=10)]"
assert excepted_render == t.render(lst=lst)
def test_loop_idx0():
t = Template(TEST_IDX0_TEMPLATE_STR_1)
lst = [10]
excepted_render = "[(len=1, revindex0=0, index0=0, val=10)]"
assert excepted_render == t.render(lst=lst)
def test_loopcontext0():
in_lst = []
l = LoopContext(reversed(in_lst), None)
assert l.length == len(in_lst)
def test_loopcontext1():
in_lst = [10]
l = LoopContext(reversed(in_lst), None)
assert l.length == len(in_lst)
def test_loopcontext2():
in_lst = [10, 11]
l = LoopContext(reversed(in_lst), None)
assert l.length == len(in_lst)
def test_iterator_not_advanced_early():
t = Template("{% for _, g in gs %}{{ loop.index }} {{ g|list }}\n{% endfor %}")
out = t.render(gs=itertools.groupby(
[(1, "a"), (1, "b"), (2, "c"), (3, "d")], lambda x: x[0]
))
# groupby groups depend on the current position of the iterator. If
# it was advanced early, the lists would appear empty.
assert out == "1 [(1, 'a'), (1, 'b')]\n2 [(2, 'c')]\n3 [(3, 'd')]\n"
|