-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
test_jinja.py
41 lines (36 loc) · 1.2 KB
/
test_jinja.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""Verify behavior of Jinja2's `tojson` template filter"""
import jinja2
import pytest
@pytest.mark.parametrize('obj, expected', [
(True, 'true'),
(1, '1'),
(3.14, '3.14'),
('hi', '"hi"'),
(
'<div style="something">content</div>',
'"\\u003cdiv style=\\"something\\"\\u003econtent\\u003c/div\\u003e"'
),
('Mus\u00e9e d\'Orsay', '"Mus\\u00e9e d\\u0027Orsay"'),
([1, 2, 3], '[1, 2, 3]'),
([1, 'hi', False], '[1, "hi", false]'),
([[0, 0], [1, 1]], '[[0, 0], [1, 1]]'),
([(0, 0), (1, 1)], '[[0, 0], [1, 1]]'),
({'hi': 'there'}, '{"hi": "there"}'),
({'hi': {'there': 1, 'whats': 'up'}}, '{"hi": {"there": 1, "whats": "up"}}'),
])
def test_jinja2_tojson(obj, expected):
res = jinja2.Template('{{ obj|tojson }}').render(obj=obj)
assert res == expected
@pytest.mark.parametrize('obj, expected', [
(
{'hi': 'there', 'what': 'isup', },
'{\n "hi": "there",\n "what": "isup"\n}'
),
(
[(0, 0), (1, 1)],
'[\n [\n 0,\n 0\n ],\n [\n 1,\n 1\n ]\n]',
),
])
def test_jinja2_tojson_indented(obj, expected):
res = jinja2.Template('{{ obj|tojson(2) }}').render(obj=obj)
assert res == expected