1 # -*- coding: utf-8 -*-
3 jinja2.testsuite.filters
4 ~~~~~~~~~~~~~~~~~~~~~~~~
6 Tests for the jinja filters.
8 :copyright: (c) 2010 by the Jinja Team.
9 :license: BSD, see LICENSE for more details.
12 from jinja2.testsuite import JinjaTestCase
14 from jinja2 import Markup, Environment
19 class FilterTestCase(JinjaTestCase):
21 def test_capitalize(self):
22 tmpl = env.from_string('{{ "foo bar"|capitalize }}')
23 assert tmpl.render() == 'Foo bar'
25 def test_center(self):
26 tmpl = env.from_string('{{ "foo"|center(9) }}')
27 assert tmpl.render() == ' foo '
29 def test_default(self):
30 tmpl = env.from_string(
31 "{{ missing|default('no') }}|{{ false|default('no') }}|"
32 "{{ false|default('no', true) }}|{{ given|default('no') }}"
34 assert tmpl.render(given='yes') == 'no|False|no|yes'
36 def test_dictsort(self):
37 tmpl = env.from_string(
39 '{{ foo|dictsort(true) }}|'
40 '{{ foo|dictsort(false, "value") }}'
42 out = tmpl.render(foo={"aa": 0, "b": 1, "c": 2, "AB": 3})
43 assert out == ("[('aa', 0), ('AB', 3), ('b', 1), ('c', 2)]|"
44 "[('AB', 3), ('aa', 0), ('b', 1), ('c', 2)]|"
45 "[('aa', 0), ('b', 1), ('c', 2), ('AB', 3)]")
48 tmpl = env.from_string("{{ foo|batch(3)|list }}|"
49 "{{ foo|batch(3, 'X')|list }}")
50 out = tmpl.render(foo=range(10))
51 assert out == ("[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]|"
52 "[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 'X', 'X']]")
55 tmpl = env.from_string('{{ foo|slice(3)|list }}|'
56 '{{ foo|slice(3, "X")|list }}')
57 out = tmpl.render(foo=range(10))
58 assert out == ("[[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]|"
59 "[[0, 1, 2, 3], [4, 5, 6, 'X'], [7, 8, 9, 'X']]")
61 def test_escape(self):
62 tmpl = env.from_string('''{{ '<">&'|escape }}''')
64 assert out == '<">&'
66 def test_striptags(self):
67 tmpl = env.from_string('''{{ foo|striptags }}''')
68 out = tmpl.render(foo=' <p>just a small \n <a href="#">'
69 'example</a> link</p>\n<p>to a webpage</p> '
70 '<!-- <p>and some commented stuff</p> -->')
71 assert out == 'just a small example link to a webpage'
73 def test_filesizeformat(self):
74 tmpl = env.from_string(
75 '{{ 100|filesizeformat }}|'
76 '{{ 1000|filesizeformat }}|'
77 '{{ 1000000|filesizeformat }}|'
78 '{{ 1000000000|filesizeformat }}|'
79 '{{ 1000000000000|filesizeformat }}|'
80 '{{ 100|filesizeformat(true) }}|'
81 '{{ 1000|filesizeformat(true) }}|'
82 '{{ 1000000|filesizeformat(true) }}|'
83 '{{ 1000000000|filesizeformat(true) }}|'
84 '{{ 1000000000000|filesizeformat(true) }}'
88 '100 Bytes|1.0 KB|1.0 MB|1.0 GB|1000.0 GB|'
89 '100 Bytes|1000 Bytes|976.6 KiB|953.7 MiB|931.3 GiB'
93 tmpl = env.from_string('{{ foo|first }}')
94 out = tmpl.render(foo=range(10))
98 tmpl = env.from_string('{{ "42"|float }}|'
99 '{{ "ajsghasjgd"|float }}|'
100 '{{ "32.32"|float }}')
102 assert out == '42.0|0.0|32.32'
104 def test_format(self):
105 tmpl = env.from_string('''{{ "%s|%s"|format("a", "b") }}''')
109 def test_indent(self):
110 tmpl = env.from_string('{{ foo|indent(2) }}|{{ foo|indent(2, true) }}')
111 text = '\n'.join([' '.join(['foo', 'bar'] * 2)] * 2)
112 out = tmpl.render(foo=text)
113 assert out == ('foo bar foo bar\n foo bar foo bar| '
114 'foo bar foo bar\n foo bar foo bar')
117 tmpl = env.from_string('{{ "42"|int }}|{{ "ajsghasjgd"|int }}|'
120 assert out == '42|0|32'
123 tmpl = env.from_string('{{ [1, 2, 3]|join("|") }}')
125 assert out == '1|2|3'
127 env2 = Environment(autoescape=True)
128 tmpl = env2.from_string('{{ ["<foo>", "<span>foo</span>"|safe]|join }}')
129 assert tmpl.render() == '<foo><span>foo</span>'
132 tmpl = env.from_string('''{{ foo|last }}''')
133 out = tmpl.render(foo=range(10))
136 def test_length(self):
137 tmpl = env.from_string('''{{ "hello world"|length }}''')
141 def test_lower(self):
142 tmpl = env.from_string('''{{ "FOO"|lower }}''')
146 def test_pprint(self):
147 from pprint import pformat
148 tmpl = env.from_string('''{{ data|pprint }}''')
150 assert tmpl.render(data=data) == pformat(data)
152 def test_random(self):
153 tmpl = env.from_string('''{{ seq|random }}''')
156 assert int(tmpl.render(seq=seq)) in seq
158 def test_reverse(self):
159 tmpl = env.from_string('{{ "foobar"|reverse|join }}|'
160 '{{ [1, 2, 3]|reverse|list }}')
161 assert tmpl.render() == 'raboof|[3, 2, 1]'
163 def test_string(self):
164 tmpl = env.from_string('''{{ range(10)|string }}''')
165 assert tmpl.render(foo=range(10)) == unicode(xrange(10))
167 def test_title(self):
168 tmpl = env.from_string('''{{ "foo bar"|title }}''')
169 assert tmpl.render() == "Foo Bar"
171 def test_truncate(self):
172 tmpl = env.from_string(
173 '{{ data|truncate(15, true, ">>>") }}|'
174 '{{ data|truncate(15, false, ">>>") }}|'
175 '{{ smalldata|truncate(15) }}'
177 out = tmpl.render(data='foobar baz bar' * 1000,
178 smalldata='foobar baz bar')
179 assert out == 'foobar baz barf>>>|foobar baz >>>|foobar baz bar'
181 def test_upper(self):
182 tmpl = env.from_string('{{ "foo"|upper }}')
183 assert tmpl.render() == 'FOO'
185 def test_urlize(self):
186 tmpl = env.from_string('{{ "foo http://www.example.com/ bar"|urlize }}')
187 assert tmpl.render() == 'foo <a href="http://www.example.com/">'\
188 'http://www.example.com/</a> bar'
190 def test_wordcount(self):
191 tmpl = env.from_string('{{ "foo bar baz"|wordcount }}')
192 assert tmpl.render() == '3'
194 def test_block(self):
195 tmpl = env.from_string('{% filter lower|escape %}<HEHE>{% endfilter %}')
196 assert tmpl.render() == '<hehe>'
198 def test_chaining(self):
199 tmpl = env.from_string('''{{ ['<foo>', '<bar>']|first|upper|escape }}''')
200 assert tmpl.render() == '<FOO>'
203 tmpl = env.from_string('''{{ [1, 2, 3, 4, 5, 6]|sum }}''')
204 assert tmpl.render() == '21'
207 tmpl = env.from_string('''{{ -1|abs }}|{{ 1|abs }}''')
208 return tmpl.render() == '1|1'
210 def test_round(self):
211 tmpl = env.from_string('{{ 2.7|round }}|{{ 2.1|round }}|'
212 "{{ 2.1234|round(2, 'floor') }}|"
213 "{{ 2.1|round(0, 'ceil') }}")
214 return tmpl.render() == '3.0|2.0|2.1|3.0'
216 def test_xmlattr(self):
217 tmpl = env.from_string("{{ {'foo': 42, 'bar': 23, 'fish': none, "
218 "'spam': missing, 'blub:blub': '<?>'}|xmlattr }}")
219 out = tmpl.render().split()
221 assert 'foo="42"' in out
222 assert 'bar="23"' in out
223 assert 'blub:blub="<?>"' in out
225 def test_sort1(self):
226 tmpl = env.from_string('{{ [2, 3, 1]|sort }}|{{ [2, 3, 1]|sort(true) }}')
227 assert tmpl.render() == '[1, 2, 3]|[3, 2, 1]'
229 def test_groupby(self):
230 tmpl = env.from_string('''
231 {%- for grouper, list in [{'foo': 1, 'bar': 2},
232 {'foo': 2, 'bar': 3},
233 {'foo': 1, 'bar': 1},
234 {'foo': 3, 'bar': 4}]|groupby('foo') -%}
235 {{ grouper }}{% for x in list %}: {{ x.foo }}, {{ x.bar }}{% endfor %}|
237 assert tmpl.render().split('|') == [
244 def test_filtertag(self):
245 tmpl = env.from_string("{% filter upper|replace('FOO', 'foo') %}"
246 "foobar{% endfilter %}")
247 assert tmpl.render() == 'fooBAR'
249 def test_replace(self):
251 tmpl = env.from_string('{{ string|replace("o", 42) }}')
252 assert tmpl.render(string='<foo>') == '<f4242>'
253 env = Environment(autoescape=True)
254 tmpl = env.from_string('{{ string|replace("o", 42) }}')
255 assert tmpl.render(string='<foo>') == '<f4242>'
256 tmpl = env.from_string('{{ string|replace("<", 42) }}')
257 assert tmpl.render(string='<foo>') == '42foo>'
258 tmpl = env.from_string('{{ string|replace("o", ">x<") }}')
259 assert tmpl.render(string=Markup('foo')) == 'f>x<>x<'
261 def test_forceescape(self):
262 tmpl = env.from_string('{{ x|forceescape }}')
263 assert tmpl.render(x=Markup('<div />')) == u'<div />'
266 env = Environment(autoescape=True)
267 tmpl = env.from_string('{{ "<div>foo</div>"|safe }}')
268 assert tmpl.render() == '<div>foo</div>'
269 tmpl = env.from_string('{{ "<div>foo</div>" }}')
270 assert tmpl.render() == '<div>foo</div>'
272 def test_sort2(self):
273 tmpl = env.from_string('''{{ ['foo', 'Bar', 'blah']|sort }}''')
274 assert tmpl.render() == "['Bar', 'blah', 'foo']"
278 suite = unittest.TestSuite()
279 suite.addTest(unittest.makeSuite(FilterTestCase))