blob: 5c0288d0ac4b06a13328919846f721b7016f9b37 [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 Ronacherab45b842007-03-18 20:47:50 +010038
39
40def test_simple(env):
41 tmpl = env.from_string(SIMPLE)
42 assert tmpl.render(seq=range(10)) == '0123456789'
43
44
45def test_else(env):
46 tmpl = env.from_string(ELSE)
47 assert tmpl.render() == '...'
48
49
50def test_empty_blocks(env):
51 tmpl = env.from_string(EMPTYBLOCKS)
52 assert tmpl.render() == '<>'
53
54
55def test_context_vars(env):
56 tmpl = env.from_string(CONTEXTVARS)
57 one, two, _ = tmpl.render(seq=[0, 1]).split('###')
58 (one_index, one_index0, one_revindex, one_revindex0, one_first,
Armin Ronacher10f3ba22008-04-18 11:30:37 +020059 one_last, one_length) = one.split('|')
Armin Ronacherab45b842007-03-18 20:47:50 +010060 (two_index, two_index0, two_revindex, two_revindex0, two_first,
Armin Ronacher10f3ba22008-04-18 11:30:37 +020061 two_last, two_length) = two.split('|')
Armin Ronacherab45b842007-03-18 20:47:50 +010062
63 assert int(one_index) == 1 and int(two_index) == 2
64 assert int(one_index0) == 0 and int(two_index0) == 1
65 assert int(one_revindex) == 2 and int(two_revindex) == 1
66 assert int(one_revindex0) == 1 and int(two_revindex0) == 0
67 assert one_first == 'True' and two_first == 'False'
68 assert one_last == 'False' and two_last == 'True'
Armin Ronacherab45b842007-03-18 20:47:50 +010069 assert one_length == two_length == '2'
70
71
72def test_cycling(env):
73 tmpl = env.from_string(CYCLING)
74 output = tmpl.render(seq=range(4), through=('<1>', '<2>'))
75 assert output == '<1><2>' * 4
76
77
78def test_scope(env):
79 tmpl = env.from_string(SCOPE)
80 output = tmpl.render(seq=range(10))
81 assert not output
Armin Ronacher6dba4d62007-05-21 23:41:36 +020082
83
84def test_varlen(env):
85 def inner():
86 for item in range(5):
87 yield item
88 tmpl = env.from_string(VARLEN)
89 output = tmpl.render(iter=inner())
90 assert output == '01234'
Armin Ronacher9bcd4112007-05-29 14:17:24 +020091
92
93def test_noniter(env):
94 tmpl = env.from_string(NONITER)
Armin Ronacher10f3ba22008-04-18 11:30:37 +020095 raises(TypeError, tmpl.render)
Armin Ronacher66a93442008-05-11 23:42:19 +020096
97
98def test_recursive(env):
99 tmpl = env.from_string(RECURSIVE)
100 assert tmpl.render(seq=[
101 dict(a=1, b=[dict(a=1), dict(a=2)]),
102 dict(a=2, b=[dict(a=1), dict(a=2)]),
103 dict(a=3, b=[dict(a='a')])
Armin Ronacherb455c312008-05-11 23:43:07 +0200104 ]) == '[1<[1][2]>][2<[1][2]>][3<[a]>]'
Armin Ronacher24db4512008-05-19 09:28:17 +0200105
106
107def test_looploop(env):
108 tmpl = env.from_string(LOOPLOOP)
109 assert tmpl.render(table=['ab', 'cd']) == '[1|1][1|2][2|1][2|2]'
Armin Ronacher105f0dc2008-05-23 16:12:47 +0200110
111
112def test_loop_errors(env):
113 tmpl = env.from_string(LOOPERROR1)
114 raises(UndefinedError, tmpl.render)
115 tmpl = env.from_string(LOOPERROR2)
116 assert tmpl.render() == ''