1 # -*- coding: utf-8 -*-
3 unit test for the filters
4 ~~~~~~~~~~~~~~~~~~~~~~~~~
6 :copyright: 2008 by Armin Ronacher.
7 :license: BSD, see LICENSE for more details.
9 from jinja2 import Markup, Environment
12 CAPITALIZE = '''{{ "foo bar"|capitalize }}'''
13 CENTER = '''{{ "foo"|center(9) }}'''
14 DEFAULT = '''{{ missing|default("no") }}|{{ false|default('no') }}|\
15 {{ false|default('no', true) }}|{{ given|default("no") }}'''
16 DICTSORT = '''{{ foo|dictsort }}|\
17 {{ foo|dictsort(true) }}|\
18 {{ foo|dictsort(false, 'value') }}'''
19 BATCH = '''{{ foo|batch(3)|list }}|{{ foo|batch(3, 'X')|list }}'''
20 SLICE = '''{{ foo|slice(3)|list }}|{{ foo|slice(3, 'X')|list }}'''
21 ESCAPE = '''{{ '<">&'|escape }}'''
22 STRIPTAGS = '''{{ foo|striptags }}'''
23 FILESIZEFORMAT = '{{ 100|filesizeformat }}|\
24 {{ 1000|filesizeformat }}|\
25 {{ 1000000|filesizeformat }}|\
26 {{ 1000000000|filesizeformat }}|\
27 {{ 1000000000000|filesizeformat }}|\
28 {{ 100|filesizeformat(true) }}|\
29 {{ 1000|filesizeformat(true) }}|\
30 {{ 1000000|filesizeformat(true) }}|\
31 {{ 1000000000|filesizeformat(true) }}|\
32 {{ 1000000000000|filesizeformat(true) }}'
33 FIRST = '''{{ foo|first }}'''
34 FLOAT = '''{{ "42"|float }}|{{ "ajsghasjgd"|float }}|{{ "32.32"|float }}'''
35 FORMAT = '''{{ "%s|%s"|format("a", "b") }}'''
36 INDENT = '''{{ foo|indent(2) }}|{{ foo|indent(2, true) }}'''
37 INT = '''{{ "42"|int }}|{{ "ajsghasjgd"|int }}|{{ "32.32"|int }}'''
38 JOIN = '''{{ [1, 2, 3]|join("|") }}'''
39 LAST = '''{{ foo|last }}'''
40 LENGTH = '''{{ "hello world"|length }}'''
41 LOWER = '''{{ "FOO"|lower }}'''
42 PPRINT = '''{{ data|pprint }}'''
43 RANDOM = '''{{ seq|random }}'''
44 REVERSE = '''{{ "foobar"|reverse|join }}|{{ [1, 2, 3]|reverse|list }}'''
45 STRING = '''{{ range(10)|string }}'''
46 TITLE = '''{{ "foo bar"|title }}'''
47 TRIM = '''{{ " foo "|trim }}'''
48 TRUNCATE = '''{{ data|truncate(15, true, ">>>") }}|\
49 {{ data|truncate(15, false, ">>>") }}|\
50 {{ smalldata|truncate(15) }}'''
51 UPPER = '''{{ "foo"|upper }}'''
52 URLIZE = '''{{ "foo http://www.example.com/ bar"|urlize }}'''
53 WORDCOUNT = '''{{ "foo bar baz"|wordcount }}'''
54 BLOCK = '''{% filter lower|escape %}<HEHE>{% endfilter %}'''
55 CHAINING = '''{{ ['<foo>', '<bar>']|first|upper|escape }}'''
56 SUM = '''{{ [1, 2, 3, 4, 5, 6]|sum }}'''
57 ABS = '''{{ -1|abs }}|{{ 1|abs }}'''
58 ROUND = '''{{ 2.7|round }}|{{ 2.1|round }}|\
59 {{ 2.1234|round(2, 'floor') }}|{{ 2.1|round(0, 'ceil') }}'''
60 XMLATTR = '''{{ {'foo': 42, 'bar': 23, 'fish': none,
61 'spam': missing, 'blub:blub': '<?>'}|xmlattr }}'''
62 SORT = '''{{ [2, 3, 1]|sort }}|{{ [2, 3, 1]|sort(true) }}'''
63 GROUPBY = '''{% for grouper, list in [{'foo': 1, 'bar': 2},
66 {'foo': 3, 'bar': 4}]|groupby('foo') -%}
67 {{ grouper }}: {{ list|join(', ') }}
69 FILTERTAG = '''{% filter upper|replace('FOO', 'foo') %}foobar{% endfilter %}'''
70 SORT = '''{{ ['foo', 'Bar', 'blah']|sort }}'''
73 def test_capitalize(env):
74 tmpl = env.from_string(CAPITALIZE)
75 assert tmpl.render() == 'Foo bar'
79 tmpl = env.from_string(CENTER)
80 assert tmpl.render() == ' foo '
83 def test_default(env):
84 tmpl = env.from_string(DEFAULT)
85 assert tmpl.render(given='yes') == 'no|False|no|yes'
88 def test_dictsort(env):
89 tmpl = env.from_string(DICTSORT)
90 out = tmpl.render(foo={"a": 0, "b": 1, "c": 2, "A": 3})
91 assert out == ("[('a', 0), ('A', 3), ('b', 1), ('c', 2)]|"
92 "[('A', 3), ('a', 0), ('b', 1), ('c', 2)]|"
93 "[('a', 0), ('b', 1), ('c', 2), ('A', 3)]")
97 tmpl = env.from_string(BATCH)
98 out = tmpl.render(foo=range(10))
99 assert out == ("[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]|"
100 "[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 'X', 'X']]")
104 tmpl = env.from_string(SLICE)
105 out = tmpl.render(foo=range(10))
106 assert out == ("[[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]|"
107 "[[0, 1, 2, 3], [4, 5, 6, 'X'], [7, 8, 9, 'X']]")
110 def test_escape(env):
111 tmpl = env.from_string(ESCAPE)
113 assert out == '<">&'
116 def test_striptags(env):
117 tmpl = env.from_string(STRIPTAGS)
118 out = tmpl.render(foo=' <p>just a small \n <a href="#">'
119 'example</a> link</p>\n<p>to a webpage</p> '
120 '<!-- <p>and some commented stuff</p> -->')
121 assert out == 'just a small example link to a webpage'
124 def test_filesizeformat(env):
125 tmpl = env.from_string(FILESIZEFORMAT)
128 '100 Bytes|1.0 KB|1.0 MB|1.0 GB|1000.0 GB|'
129 '100 Bytes|1000 Bytes|976.6 KiB|953.7 MiB|931.3 GiB'
134 tmpl = env.from_string(FIRST)
135 out = tmpl.render(foo=range(10))
140 tmpl = env.from_string(FLOAT)
142 assert out == '42.0|0.0|32.32'
145 def test_format(env):
146 tmpl = env.from_string(FORMAT)
151 def test_indent(env):
152 tmpl = env.from_string(INDENT)
153 text = '\n'.join([' '.join(['foo', 'bar'] * 2)] * 2)
154 out = tmpl.render(foo=text)
155 assert out == 'foo bar foo bar\n foo bar foo bar| ' \
156 'foo bar foo bar\n foo bar foo bar'
160 tmpl = env.from_string(INT)
162 assert out == '42|0|32'
166 tmpl = env.from_string(JOIN)
168 assert out == '1|2|3'
170 env2 = Environment(autoescape=True)
171 tmpl = env2.from_string('{{ ["<foo>", "<span>foo</span>"|safe]|join }}')
172 assert tmpl.render() == '<foo><span>foo</span>'
176 tmpl = env.from_string(LAST)
177 out = tmpl.render(foo=range(10))
181 def test_length(env):
182 tmpl = env.from_string(LENGTH)
188 tmpl = env.from_string(LOWER)
193 def test_pprint(env):
194 from pprint import pformat
195 tmpl = env.from_string(PPRINT)
197 assert tmpl.render(data=data) == pformat(data)
200 def test_random(env):
201 tmpl = env.from_string(RANDOM)
204 assert int(tmpl.render(seq=seq)) in seq
207 def test_reverse(env):
208 tmpl = env.from_string(REVERSE)
209 assert tmpl.render() == 'raboof|[3, 2, 1]'
212 def test_string(env):
213 tmpl = env.from_string(STRING)
214 assert tmpl.render(foo=range(10)) == unicode(xrange(10))
218 tmpl = env.from_string(TITLE)
219 assert tmpl.render() == "Foo Bar"
222 def test_truncate(env):
223 tmpl = env.from_string(TRUNCATE)
224 assert tmpl.render() == 'foo'
227 def test_truncate(env):
228 tmpl = env.from_string(TRUNCATE)
229 out = tmpl.render(data='foobar baz bar' * 1000,
230 smalldata='foobar baz bar')
231 assert out == 'foobar baz barf>>>|foobar baz >>>|foobar baz bar'
235 tmpl = env.from_string(UPPER)
236 assert tmpl.render() == 'FOO'
239 def test_urlize(env):
240 tmpl = env.from_string(URLIZE)
241 assert tmpl.render() == 'foo <a href="http://www.example.com/">'\
242 'http://www.example.com/</a> bar'
245 def test_wordcount(env):
246 tmpl = env.from_string(WORDCOUNT)
247 assert tmpl.render() == '3'
251 tmpl = env.from_string(BLOCK)
252 assert tmpl.render() == '<hehe>'
255 def test_chaining(env):
256 tmpl = env.from_string(CHAINING)
257 assert tmpl.render() == '<FOO>'
261 tmpl = env.from_string(SUM)
262 assert tmpl.render() == '21'
266 tmpl = env.from_string(ABS)
267 return tmpl.render() == '1|1'
271 tmpl = env.from_string(ROUND)
272 return tmpl.render() == '3.0|2.0|2.1|3.0'
275 def test_xmlattr(env):
276 tmpl = env.from_string(XMLATTR)
277 out = tmpl.render().split()
279 assert 'foo="42"' in out
280 assert 'bar="23"' in out
281 assert 'blub:blub="<?>"' in out
285 tmpl = env.from_string(SORT)
286 assert tmpl.render() == '[1, 2, 3]|[3, 2, 1]'
289 def test_groupby(env):
290 tmpl = env.from_string(GROUPBY)
291 assert tmpl.render().splitlines() == [
292 "1: {'foo': 1, 'bar': 2}, {'foo': 1, 'bar': 1}",
293 "2: {'foo': 2, 'bar': 3}",
294 "3: {'foo': 3, 'bar': 4}"
298 def test_filtertag(env):
299 tmpl = env.from_string(FILTERTAG)
300 assert tmpl.render() == 'fooBAR'
305 tmpl = env.from_string('{{ string|replace("o", 42) }}')
306 assert tmpl.render(string='<foo>') == '<f4242>'
308 env = Environment(autoescape=True)
309 tmpl = env.from_string('{{ string|replace("o", 42) }}')
310 assert tmpl.render(string='<foo>') == '<f4242>'
311 tmpl = env.from_string('{{ string|replace("<", 42) }}')
312 assert tmpl.render(string='<foo>') == '42foo>'
313 tmpl = env.from_string('{{ string|replace("o", ">x<") }}')
314 assert tmpl.render(string=Markup('foo')) == 'f>x<>x<'
317 def test_forceescape(env):
318 tmpl = env.from_string('{{ x|forceescape }}')
319 assert tmpl.render(x=Markup('<div />')) == u'<div />'
323 env = Environment(autoescape=True)
324 tmpl = env.from_string('{{ "<div>foo</div>"|safe }}')
325 assert tmpl.render() == '<div>foo</div>'
326 tmpl = env.from_string('{{ "<div>foo</div>" }}')
327 assert tmpl.render() == '<div>foo</div>'
331 assert env.from_string(SORT).render() == "['Bar', 'blah', 'foo']"