Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/tests/test_jinja.py
1593 views
1
"""Verify behavior of Jinja2's `tojson` template filter"""
2
3
import jinja2
4
import pytest
5
6
7
@pytest.mark.parametrize(
8
"obj, expected",
9
[
10
(True, "true"),
11
(1, "1"),
12
(3.14, "3.14"),
13
("hi", '"hi"'),
14
(
15
'<div style="something">content</div>',
16
'"\\u003cdiv style=\\"something\\"\\u003econtent\\u003c/div\\u003e"',
17
),
18
("Mus\u00e9e d'Orsay", '"Mus\\u00e9e d\\u0027Orsay"'),
19
([1, 2, 3], "[1, 2, 3]"),
20
([1, "hi", False], '[1, "hi", false]'),
21
([[0, 0], [1, 1]], "[[0, 0], [1, 1]]"),
22
([(0, 0), (1, 1)], "[[0, 0], [1, 1]]"),
23
({"hi": "there"}, '{"hi": "there"}'),
24
({"hi": {"there": 1, "what": "up"}}, '{"hi": {"there": 1, "what": "up"}}'),
25
],
26
)
27
def test_jinja2_tojson(obj, expected):
28
res = jinja2.Template("{{ obj|tojson }}").render(obj=obj)
29
assert res == expected
30
31
32
@pytest.mark.parametrize(
33
"obj, expected",
34
[
35
(
36
{
37
"hi": "there",
38
"what": "isup",
39
},
40
'{\n "hi": "there",\n "what": "isup"\n}',
41
),
42
(
43
[(0, 0), (1, 1)],
44
"[\n [\n 0,\n 0\n ],\n [\n 1,\n 1\n ]\n]",
45
),
46
],
47
)
48
def test_jinja2_tojson_indented(obj, expected):
49
res = jinja2.Template("{{ obj|tojson(2) }}").render(obj=obj)
50
assert res == expected
51
52