blob: 7469f93efebf45636563d31ca4d6ddbdb2015f16 [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
10
Armin Ronacherab45b842007-03-18 20:47:50 +010011
12SIMPLE = '''{% for item in seq %}{{ item }}{% endfor %}'''
13ELSE = '''{% for item in seq %}XXX{% else %}...{% endfor %}'''
14EMPTYBLOCKS = '''<{% for item in seq %}{% else %}{% endfor %}>'''
15CONTEXTVARS = '''{% for item in seq %}\
16{{ loop.index }}|{{ loop.index0 }}|{{ loop.revindex }}|{{
17 loop.revindex0 }}|{{ loop.first }}|{{ loop.last }}|{{
Armin Ronacher10f3ba22008-04-18 11:30:37 +020018 loop.length }}###{% endfor %}'''
19CYCLING = '''{% for item in seq %}{{ loop.cycle('<1>', '<2>') }}{% endfor %}\
20{% for item in seq %}{{ loop.cycle(*through) }}{% endfor %}'''
Armin Ronacherab45b842007-03-18 20:47:50 +010021SCOPE = '''{% for item in seq %}{% endfor %}{{ item }}'''
Armin Ronacher6dba4d62007-05-21 23:41:36 +020022VARLEN = '''{% for item in iter %}{{ item }}{% endfor %}'''
Armin Ronacher9bcd4112007-05-29 14:17:24 +020023NONITER = '''{% for item in none %}...{% endfor %}'''
Armin Ronacher66a93442008-05-11 23:42:19 +020024RECURSIVE = '''{% for item in seq recursive -%}
Armin Ronacherb455c312008-05-11 23:43:07 +020025 [{{ item.a }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}]
Armin Ronacher66a93442008-05-11 23:42:19 +020026{%- endfor %}'''
Armin Ronacher24db4512008-05-19 09:28:17 +020027LOOPLOOP = '''{% for row in table %}
28 {%- set rowloop = loop -%}
29 {% for cell in row -%}
30 [{{ rowloop.index }}|{{ loop.index }}]
31 {%- endfor %}
32{%- endfor %}'''
Armin Ronacherab45b842007-03-18 20:47:50 +010033
34
35def test_simple(env):
36 tmpl = env.from_string(SIMPLE)
37 assert tmpl.render(seq=range(10)) == '0123456789'
38
39
40def test_else(env):
41 tmpl = env.from_string(ELSE)
42 assert tmpl.render() == '...'
43
44
45def test_empty_blocks(env):
46 tmpl = env.from_string(EMPTYBLOCKS)
47 assert tmpl.render() == '<>'
48
49
50def test_context_vars(env):
51 tmpl = env.from_string(CONTEXTVARS)
52 one, two, _ = tmpl.render(seq=[0, 1]).split('###')
53 (one_index, one_index0, one_revindex, one_revindex0, one_first,
Armin Ronacher10f3ba22008-04-18 11:30:37 +020054 one_last, one_length) = one.split('|')
Armin Ronacherab45b842007-03-18 20:47:50 +010055 (two_index, two_index0, two_revindex, two_revindex0, two_first,
Armin Ronacher10f3ba22008-04-18 11:30:37 +020056 two_last, two_length) = two.split('|')
Armin Ronacherab45b842007-03-18 20:47:50 +010057
58 assert int(one_index) == 1 and int(two_index) == 2
59 assert int(one_index0) == 0 and int(two_index0) == 1
60 assert int(one_revindex) == 2 and int(two_revindex) == 1
61 assert int(one_revindex0) == 1 and int(two_revindex0) == 0
62 assert one_first == 'True' and two_first == 'False'
63 assert one_last == 'False' and two_last == 'True'
Armin Ronacherab45b842007-03-18 20:47:50 +010064 assert one_length == two_length == '2'
65
66
67def test_cycling(env):
68 tmpl = env.from_string(CYCLING)
69 output = tmpl.render(seq=range(4), through=('<1>', '<2>'))
70 assert output == '<1><2>' * 4
71
72
73def test_scope(env):
74 tmpl = env.from_string(SCOPE)
75 output = tmpl.render(seq=range(10))
76 assert not output
Armin Ronacher6dba4d62007-05-21 23:41:36 +020077
78
79def test_varlen(env):
80 def inner():
81 for item in range(5):
82 yield item
83 tmpl = env.from_string(VARLEN)
84 output = tmpl.render(iter=inner())
85 assert output == '01234'
Armin Ronacher9bcd4112007-05-29 14:17:24 +020086
87
88def test_noniter(env):
89 tmpl = env.from_string(NONITER)
Armin Ronacher10f3ba22008-04-18 11:30:37 +020090 raises(TypeError, tmpl.render)
Armin Ronacher66a93442008-05-11 23:42:19 +020091
92
93def test_recursive(env):
94 tmpl = env.from_string(RECURSIVE)
95 assert tmpl.render(seq=[
96 dict(a=1, b=[dict(a=1), dict(a=2)]),
97 dict(a=2, b=[dict(a=1), dict(a=2)]),
98 dict(a=3, b=[dict(a='a')])
Armin Ronacherb455c312008-05-11 23:43:07 +020099 ]) == '[1<[1][2]>][2<[1][2]>][3<[a]>]'
Armin Ronacher24db4512008-05-19 09:28:17 +0200100
101
102def test_looploop(env):
103 tmpl = env.from_string(LOOPLOOP)
104 assert tmpl.render(table=['ab', 'cd']) == '[1|1][1|2][2|1][2|2]'