blob: 67fb39e5809a4d62b67e00cba149a645d8445b91 [file] [log] [blame]
Armin Ronacherab45b842007-03-18 20:47:50 +01001# -*- coding: utf-8 -*-
2"""
3 unit test for loop functions
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5
6 :copyright: 2007 by Armin Ronacher.
7 :license: BSD, see LICENSE for more details.
8"""
Armin Ronacher10f3ba22008-04-18 11:30:37 +02009from py.test import raises
Armin Ronacher105f0dc2008-05-23 16:12:47 +020010from jinja2.exceptions import UndefinedError
Armin Ronacher10f3ba22008-04-18 11:30:37 +020011
Armin Ronacherab45b842007-03-18 20:47:50 +010012
13SIMPLE = '''{% for item in seq %}{{ item }}{% endfor %}'''
14ELSE = '''{% for item in seq %}XXX{% else %}...{% endfor %}'''
15EMPTYBLOCKS = '''<{% for item in seq %}{% else %}{% endfor %}>'''
16CONTEXTVARS = '''{% for item in seq %}\
17{{ loop.index }}|{{ loop.index0 }}|{{ loop.revindex }}|{{
18 loop.revindex0 }}|{{ loop.first }}|{{ loop.last }}|{{
Armin Ronacher10f3ba22008-04-18 11:30:37 +020019 loop.length }}###{% endfor %}'''
20CYCLING = '''{% for item in seq %}{{ loop.cycle('<1>', '<2>') }}{% endfor %}\
21{% for item in seq %}{{ loop.cycle(*through) }}{% endfor %}'''
Armin Ronacherab45b842007-03-18 20:47:50 +010022SCOPE = '''{% for item in seq %}{% endfor %}{{ item }}'''
Armin Ronacher6dba4d62007-05-21 23:41:36 +020023VARLEN = '''{% for item in iter %}{{ item }}{% endfor %}'''
Armin Ronacher9bcd4112007-05-29 14:17:24 +020024NONITER = '''{% for item in none %}...{% endfor %}'''
Armin Ronacher66a93442008-05-11 23:42:19 +020025RECURSIVE = '''{% for item in seq recursive -%}
Armin Ronacherb455c312008-05-11 23:43:07 +020026 [{{ item.a }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}]
Armin Ronacher66a93442008-05-11 23:42:19 +020027{%- endfor %}'''
Armin Ronacher24db4512008-05-19 09:28:17 +020028LOOPLOOP = '''{% for row in table %}
29 {%- set rowloop = loop -%}
30 {% for cell in row -%}
31 [{{ rowloop.index }}|{{ loop.index }}]
32 {%- endfor %}
33{%- endfor %}'''
Armin Ronacher105f0dc2008-05-23 16:12:47 +020034LOOPERROR1 = '''\
35{% for item in [1] if loop.index == 0 %}...{% endfor %}'''
36LOOPERROR2 = '''\
37{% for item in [] %}...{% else %}{{ loop }}{% endfor %}'''
Armin Ronacher6df604e2008-05-23 22:18:38 +020038LOOPFILTER = '''\
39{% for item in range(10) if item is even %}[{{ item }}]{% endfor %}'''
40EXTENDEDLOOPFILTER = '''\
41{% for item in range(10) if item is even %}[{{ loop.index
42}}:{{ item }}]{% endfor %}'''
Armin Ronacherab45b842007-03-18 20:47:50 +010043
44
45def test_simple(env):
46 tmpl = env.from_string(SIMPLE)
47 assert tmpl.render(seq=range(10)) == '0123456789'
48
49
50def test_else(env):
51 tmpl = env.from_string(ELSE)
52 assert tmpl.render() == '...'
53
54
55def test_empty_blocks(env):
56 tmpl = env.from_string(EMPTYBLOCKS)
57 assert tmpl.render() == '<>'
58
59
60def test_context_vars(env):
61 tmpl = env.from_string(CONTEXTVARS)
62 one, two, _ = tmpl.render(seq=[0, 1]).split('###')
63 (one_index, one_index0, one_revindex, one_revindex0, one_first,
Armin Ronacher10f3ba22008-04-18 11:30:37 +020064 one_last, one_length) = one.split('|')
Armin Ronacherab45b842007-03-18 20:47:50 +010065 (two_index, two_index0, two_revindex, two_revindex0, two_first,
Armin Ronacher10f3ba22008-04-18 11:30:37 +020066 two_last, two_length) = two.split('|')
Armin Ronacherab45b842007-03-18 20:47:50 +010067
68 assert int(one_index) == 1 and int(two_index) == 2
69 assert int(one_index0) == 0 and int(two_index0) == 1
70 assert int(one_revindex) == 2 and int(two_revindex) == 1
71 assert int(one_revindex0) == 1 and int(two_revindex0) == 0
72 assert one_first == 'True' and two_first == 'False'
73 assert one_last == 'False' and two_last == 'True'
Armin Ronacherab45b842007-03-18 20:47:50 +010074 assert one_length == two_length == '2'
75
76
77def test_cycling(env):
78 tmpl = env.from_string(CYCLING)
79 output = tmpl.render(seq=range(4), through=('<1>', '<2>'))
80 assert output == '<1><2>' * 4
81
82
83def test_scope(env):
84 tmpl = env.from_string(SCOPE)
85 output = tmpl.render(seq=range(10))
86 assert not output
Armin Ronacher6dba4d62007-05-21 23:41:36 +020087
88
89def test_varlen(env):
90 def inner():
91 for item in range(5):
92 yield item
93 tmpl = env.from_string(VARLEN)
94 output = tmpl.render(iter=inner())
95 assert output == '01234'
Armin Ronacher9bcd4112007-05-29 14:17:24 +020096
97
98def test_noniter(env):
99 tmpl = env.from_string(NONITER)
Armin Ronacher10f3ba22008-04-18 11:30:37 +0200100 raises(TypeError, tmpl.render)
Armin Ronacher66a93442008-05-11 23:42:19 +0200101
102
103def test_recursive(env):
104 tmpl = env.from_string(RECURSIVE)
105 assert tmpl.render(seq=[
106 dict(a=1, b=[dict(a=1), dict(a=2)]),
107 dict(a=2, b=[dict(a=1), dict(a=2)]),
108 dict(a=3, b=[dict(a='a')])
Armin Ronacherb455c312008-05-11 23:43:07 +0200109 ]) == '[1<[1][2]>][2<[1][2]>][3<[a]>]'
Armin Ronacher24db4512008-05-19 09:28:17 +0200110
111
112def test_looploop(env):
113 tmpl = env.from_string(LOOPLOOP)
114 assert tmpl.render(table=['ab', 'cd']) == '[1|1][1|2][2|1][2|2]'
Armin Ronacher105f0dc2008-05-23 16:12:47 +0200115
116
117def test_loop_errors(env):
118 tmpl = env.from_string(LOOPERROR1)
119 raises(UndefinedError, tmpl.render)
120 tmpl = env.from_string(LOOPERROR2)
121 assert tmpl.render() == ''
Armin Ronacher6df604e2008-05-23 22:18:38 +0200122
123
124def test_loop_filter(env):
125 tmpl = env.from_string(LOOPFILTER)
126 assert tmpl.render() == '[0][2][4][6][8]'
127 tmpl = env.from_string(EXTENDEDLOOPFILTER)
128 assert tmpl.render() == '[1:0][2:2][3:4][4:6][5:8]'