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):
165 tmpl = env.from_string('''{{ obj|string }}''')
166 assert tmpl.render(obj=x) == unicode(x)
168 def test_title(self):
169 tmpl = env.from_string('''{{ "foo bar"|title }}''')
170 assert tmpl.render() == "Foo Bar"
172 def test_truncate(self):
173 tmpl = env.from_string(
174 '{{ data|truncate(15, true, ">>>") }}|'
175 '{{ data|truncate(15, false, ">>>") }}|'
176 '{{ smalldata|truncate(15) }}'
178 out = tmpl.render(data='foobar baz bar' * 1000,
179 smalldata='foobar baz bar')
180 assert out == 'foobar baz barf>>>|foobar baz >>>|foobar baz bar'
182 def test_upper(self):
183 tmpl = env.from_string('{{ "foo"|upper }}')
184 assert tmpl.render() == 'FOO'
186 def test_urlize(self):
187 tmpl = env.from_string('{{ "foo http://www.example.com/ bar"|urlize }}')
188 assert tmpl.render() == 'foo <a href="http://www.example.com/">'\
189 'http://www.example.com/</a> bar'
191 def test_wordcount(self):
192 tmpl = env.from_string('{{ "foo bar baz"|wordcount }}')
193 assert tmpl.render() == '3'
195 def test_block(self):
196 tmpl = env.from_string('{% filter lower|escape %}<HEHE>{% endfilter %}')
197 assert tmpl.render() == '<hehe>'
199 def test_chaining(self):
200 tmpl = env.from_string('''{{ ['<foo>', '<bar>']|first|upper|escape }}''')
201 assert tmpl.render() == '<FOO>'
204 tmpl = env.from_string('''{{ [1, 2, 3, 4, 5, 6]|sum }}''')
205 assert tmpl.render() == '21'
208 tmpl = env.from_string('''{{ -1|abs }}|{{ 1|abs }}''')
209 return tmpl.render() == '1|1'
211 def test_round_positive(self):
212 tmpl = env.from_string('{{ 2.7|round }}|{{ 2.1|round }}|'
213 "{{ 2.1234|round(3, 'floor') }}|"
214 "{{ 2.1|round(0, 'ceil') }}")
215 assert tmpl.render() == '3.0|2.0|2.123|3.0', tmpl.render()
217 def test_round_negative(self):
218 tmpl = env.from_string('{{ 21.3|round(-1)}}|'
219 "{{ 21.3|round(-1, 'ceil')}}|"
220 "{{ 21.3|round(-1, 'floor')}}")
221 assert tmpl.render() == '20.0|30.0|20.0',tmpl.render()
223 def test_xmlattr(self):
224 tmpl = env.from_string("{{ {'foo': 42, 'bar': 23, 'fish': none, "
225 "'spam': missing, 'blub:blub': '<?>'}|xmlattr }}")
226 out = tmpl.render().split()
228 assert 'foo="42"' in out
229 assert 'bar="23"' in out
230 assert 'blub:blub="<?>"' in out
232 def test_sort1(self):
233 tmpl = env.from_string('{{ [2, 3, 1]|sort }}|{{ [2, 3, 1]|sort(true) }}')
234 assert tmpl.render() == '[1, 2, 3]|[3, 2, 1]'
236 def test_sort2(self):
237 tmpl = env.from_string('{{ "".join(["c", "A", "b", "D"]|sort(false, true)) }}')
238 assert tmpl.render() == 'AbcD'
240 def test_groupby(self):
241 tmpl = env.from_string('''
242 {%- for grouper, list in [{'foo': 1, 'bar': 2},
243 {'foo': 2, 'bar': 3},
244 {'foo': 1, 'bar': 1},
245 {'foo': 3, 'bar': 4}]|groupby('foo') -%}
246 {{ grouper }}{% for x in list %}: {{ x.foo }}, {{ x.bar }}{% endfor %}|
248 assert tmpl.render().split('|') == [
255 def test_filtertag(self):
256 tmpl = env.from_string("{% filter upper|replace('FOO', 'foo') %}"
257 "foobar{% endfilter %}")
258 assert tmpl.render() == 'fooBAR'
260 def test_replace(self):
262 tmpl = env.from_string('{{ string|replace("o", 42) }}')
263 assert tmpl.render(string='<foo>') == '<f4242>'
264 env = Environment(autoescape=True)
265 tmpl = env.from_string('{{ string|replace("o", 42) }}')
266 assert tmpl.render(string='<foo>') == '<f4242>'
267 tmpl = env.from_string('{{ string|replace("<", 42) }}')
268 assert tmpl.render(string='<foo>') == '42foo>'
269 tmpl = env.from_string('{{ string|replace("o", ">x<") }}')
270 assert tmpl.render(string=Markup('foo')) == 'f>x<>x<'
272 def test_forceescape(self):
273 tmpl = env.from_string('{{ x|forceescape }}')
274 assert tmpl.render(x=Markup('<div />')) == u'<div />'
277 env = Environment(autoescape=True)
278 tmpl = env.from_string('{{ "<div>foo</div>"|safe }}')
279 assert tmpl.render() == '<div>foo</div>'
280 tmpl = env.from_string('{{ "<div>foo</div>" }}')
281 assert tmpl.render() == '<div>foo</div>'
283 def test_sort2(self):
284 tmpl = env.from_string('''{{ ['foo', 'Bar', 'blah']|sort }}''')
285 assert tmpl.render() == "['Bar', 'blah', 'foo']"
289 suite = unittest.TestSuite()
290 suite.addTest(unittest.makeSuite(FilterTestCase))