[svn] Fix a few typos.
[jinja2.git] / tests / test_filters.py
1 # -*- coding: utf-8 -*-
2 """
3     unit test for the filters
4     ~~~~~~~~~~~~~~~~~~~~~~~~~
5
6     Missing tests:
7
8     -   wordcount
9     -   rst
10     -   markdown
11     -   textile
12
13     :copyright: 2007 by Armin Ronacher.
14     :license: BSD, see LICENSE for more details.
15 """
16
17 CAPITALIZE = '''{{ "foo bar"|capitalize }}'''
18 CAPTURE = '''{{ "foo"|capture('bar') }}|{{ bar }}'''
19 CENTER = '''{{ "foo"|center(9) }}'''
20 DEFAULT = '''{{ missing|default("no") }}|{{ false|default('no') }}|\
21 {{ false|default('no', true) }}|{{ given|default("no") }}'''
22 DICTSORT = '''{{ foo|dictsort }}|\
23 {{ foo|dictsort(true) }}|\
24 {{ foo|dictsort(false, 'value') }}'''
25 BATCH = '''{{ foo|batch(3) }}|{{ foo|batch(3, 'X') }}'''
26 SLICE = '''{{ foo|slice(3) }}|{{ foo|slice(3, 'X') }}'''
27 ESCAPE = '''{{ '<">&'|escape }}|{{ '<">&'|escape(true) }}'''
28 STRIPTAGS = '''{{ foo|striptags }}'''
29 FILESIZEFORMAT = '{{ 100|filesizeformat }}|\
30 {{ 1000|filesizeformat }}|\
31 {{ 1000000|filesizeformat }}|\
32 {{ 1000000000|filesizeformat }}|\
33 {{ 1000000000000|filesizeformat }}'
34 FIRST = '''{{ foo|first }}'''
35 FLOAT = '''{{ "42"|float }}|{{ "ajsghasjgd"|float }}|{{ "32.32"|float }}'''
36 FORMAT = '''{{ "%s|%s"|format("a", "b") }}'''
37 INDENT = '''{{ foo|indent(2) }}|{{ foo|indent(2, true) }}'''
38 INT = '''{{ "42"|int }}|{{ "ajsghasjgd"|int }}|{{ "32.32"|int }}'''
39 JOIN = '''{{ [1, 2, 3]|join("|") }}'''
40 LAST = '''{{ foo|last }}'''
41 LENGTH = '''{{ "hello world"|length }}'''
42 LOWER = '''{{ "FOO"|lower }}'''
43 PPRINT = '''{{ data|pprint }}'''
44 RANDOM = '''{{ seq|random }}'''
45 REVERSE = '''{{ "foobar"|reverse }}|{{ [1, 2, 3]|reverse }}'''
46 STRING = '''{{ range(10)|string }}'''
47 TITLE = '''{{ "foo bar"|title }}'''
48 TRIM = '''{{ "      foo       "|trim }}'''
49 TRUNCATE = '''{{ data|truncate(15, true, ">>>") }}|\
50 {{ data|truncate(15, false, ">>>") }}|\
51 {{ smalldata|truncate(15) }}'''
52 UPPER = '''{{ "foo"|upper }}'''
53 URLENCODE = '''{{ "f#b"|urlencode }}'''
54 URLIZE = '''{{ "foo http://www.example.com/ bar"|urlize }}'''
55 WORDCOUNT = '''{{ "foo bar baz"|wordcount }}'''
56 BLOCK = '''{% filter lower|escape %}<HEHE>{% endfilter %}'''
57 CHAINING = '''{{ ['<foo>', '<bar>']|first|upper|escape }}'''
58 SUM = '''{{ [1, 2, 3, 4, 5, 6]|sum }}'''
59 ABS = '''{{ -1|abs }}|{{ 1|abs }}'''
60 ROUND = '''{{ 2.7|round }}|{{ 2.1|round }}|\
61 {{ 2.1234|round(2, 'floor') }}|{{ 2.1|round(0, 'ceil') }}'''
62 XMLATTR = '''{{ {'foo': 42, 'bar': 23, 'fish': none,
63 'spam': missing, 'blub:blub': '<?>'}|xmlattr }}'''
64
65
66 def test_capitalize(env):
67     tmpl = env.from_string(CAPITALIZE)
68     assert tmpl.render() == 'Foo bar'
69
70
71 def test_capture(env):
72     tmpl = env.from_string(CAPTURE)
73     assert tmpl.render() == 'foo|foo'
74
75
76 def test_center(env):
77     tmpl = env.from_string(CENTER)
78     assert tmpl.render() == '   foo   '
79
80
81 def test_default(env):
82     tmpl = env.from_string(DEFAULT)
83     assert tmpl.render(given='yes') == 'no|False|no|yes'
84
85
86 def test_dictsort(env):
87     tmpl = env.from_string(DICTSORT)
88     out = tmpl.render(foo={"a": 0, "b": 1, "c": 2, "A": 3})
89     assert out == ("[('a', 0), ('A', 3), ('b', 1), ('c', 2)]|"
90                    "[('A', 3), ('a', 0), ('b', 1), ('c', 2)]|"
91                    "[('a', 0), ('b', 1), ('c', 2), ('A', 3)]")
92
93
94 def test_batch(env):
95     tmpl = env.from_string(BATCH)
96     out = tmpl.render(foo=range(10))
97     assert out == ("[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]|"
98                    "[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 'X', 'X']]")
99
100
101 def test_slice(env):
102     tmpl = env.from_string(SLICE)
103     out = tmpl.render(foo=range(10))
104     assert out == ("[[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]|"
105                    "[[0, 1, 2, 3], [4, 5, 6, 'X'], [7, 8, 9, 'X']]")
106
107
108 def test_escape(env):
109     tmpl = env.from_string(ESCAPE)
110     out = tmpl.render()
111     assert out == '&lt;"&gt;&amp;|&lt;&quot;&gt;&amp;'
112
113
114 def test_striptags(env):
115     tmpl = env.from_string(STRIPTAGS)
116     out = tmpl.render(foo='  <p>just a small   \n <a href="#">'
117                       'example</a> link</p>\n<p>to a webpage</p>')
118     assert out == 'just a small example link to a webpage'
119
120
121 def test_filesizeformat(env):
122     tmpl = env.from_string(FILESIZEFORMAT)
123     out = tmpl.render()
124     assert out == '100 Bytes|1000 Bytes|976.6 KB|953.7 MB|931.3 GB'
125
126
127 def test_first(env):
128     tmpl = env.from_string(FIRST)
129     out = tmpl.render(foo=range(10))
130     assert out == '0'
131
132
133 def test_float(env):
134     tmpl = env.from_string(FLOAT)
135     out = tmpl.render()
136     assert out == '42.0|0.0|32.32'
137
138
139 def test_format(env):
140     tmpl = env.from_string(FORMAT)
141     out = tmpl.render()
142     assert out == 'a|b'
143
144
145 def test_indent(env):
146     tmpl = env.from_string(INDENT)
147     text = '\n'.join([' '.join(['foo', 'bar'] * 2)] * 2)
148     out = tmpl.render(foo=text)
149     assert out == 'foo bar foo bar\n  foo bar foo bar|  ' \
150                   'foo bar foo bar\n  foo bar foo bar'
151
152
153 def test_int(env):
154     tmpl = env.from_string(INT)
155     out = tmpl.render()
156     assert out == '42|0|32'
157
158
159 def test_join(env):
160     tmpl = env.from_string(JOIN)
161     out = tmpl.render()
162     assert out == '1|2|3'
163
164
165 def test_last(env):
166     tmpl = env.from_string(LAST)
167     out = tmpl.render(foo=range(10))
168     assert out == '9'
169
170
171 def test_length(env):
172     tmpl = env.from_string(LENGTH)
173     out = tmpl.render()
174     assert out == '11'
175
176
177 def test_lower(env):
178     tmpl = env.from_string(LOWER)
179     out = tmpl.render()
180     assert out == 'foo'
181
182
183 def test_pprint(env):
184     from pprint import pformat
185     tmpl = env.from_string(PPRINT)
186     data = range(10000)
187     assert tmpl.render(data=data) == pformat(data)
188
189
190 def test_random(env):
191     tmpl = env.from_string(RANDOM)
192     seq = range(100)
193     for _ in range(10):
194         assert int(tmpl.render(seq=seq)) in seq
195
196
197 def test_reverse(env):
198     tmpl = env.from_string(REVERSE)
199     assert tmpl.render() == 'raboof|[3, 2, 1]'
200
201
202 def test_string(env):
203     tmpl = env.from_string(STRING)
204     assert tmpl.render(foo=range(10)) == str(range(10))
205
206
207 def test_title(env):
208     tmpl = env.from_string(TITLE)
209     assert tmpl.render() == "Foo Bar"
210
211
212 def test_truncate(env):
213     tmpl = env.from_string(TRUNCATE)
214     assert tmpl.render() == 'foo'
215
216
217 def test_truncate(env):
218     tmpl = env.from_string(TRUNCATE)
219     out = tmpl.render(data='foobar baz bar' * 1000,
220                       smalldata='foobar baz bar')
221     assert out == 'foobar baz barf>>>|foobar baz >>>|foobar baz bar'
222
223
224 def test_upper(env):
225     tmpl = env.from_string(UPPER)
226     assert tmpl.render() == 'FOO'
227
228
229 def test_urlencode(env):
230     tmpl = env.from_string(URLENCODE)
231     assert tmpl.render() == 'f%23b'
232
233
234 def test_urlize(env):
235     tmpl = env.from_string(URLIZE)
236     assert tmpl.render() == 'foo <a href="http://www.example.com/">'\
237                             'http://www.example.com/</a> bar'
238
239
240 def test_wordcount(env):
241     tmpl = env.from_string(WORDCOUNT)
242     assert tmpl.render() == '3'
243
244
245 def test_block(env):
246     tmpl = env.from_string(BLOCK)
247     assert tmpl.render() == '&lt;hehe&gt;'
248
249
250 def test_chaining(env):
251     tmpl = env.from_string(CHAINING)
252     assert tmpl.render() == '&lt;FOO&gt;'
253
254
255 def test_sum(env):
256     tmpl = env.from_string(SUM)
257     assert tmpl.render() == '21'
258
259
260 def test_abs(env):
261     tmpl = env.from_string(ABS)
262     return tmpl.render() == '1|1'
263
264
265 def test_round(env):
266     tmpl = env.from_string(ROUND)
267     return tmpl.render() == '3.0|2.0|2.1|3.0'
268
269
270 def test_xmlattr(env):
271     tmpl = env.from_string(XMLATTR)
272     out = tmpl.render().split()
273     assert len(out) == 3
274     assert 'foo="42"' in out
275     assert 'bar="23"' in out
276     assert 'blub:blub="&lt;?&gt;"' in out