|
| 1 | +"""Verify behavior of Jinja2's `tojson` template filter""" |
| 2 | + |
| 3 | +import jinja2 |
| 4 | +import pytest |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.parametrize('obj, expected', [ |
| 8 | + (True, 'true'), |
| 9 | + (1, '1'), |
| 10 | + (3.14, '3.14'), |
| 11 | + ('hi', '"hi"'), |
| 12 | + ( |
| 13 | + '<div style="something">content</div>', |
| 14 | + '"\\u003cdiv style=\\"something\\"\\u003econtent\\u003c/div\\u003e"' |
| 15 | + ), |
| 16 | + ('Mus\u00e9e d\'Orsay', '"Mus\\u00e9e d\\u0027Orsay"'), |
| 17 | + ([1, 2, 3], '[1, 2, 3]'), |
| 18 | + ([1, 'hi', False], '[1, "hi", false]'), |
| 19 | + ([[0, 0], [1, 1]], '[[0, 0], [1, 1]]'), |
| 20 | + ([(0, 0), (1, 1)], '[[0, 0], [1, 1]]'), |
| 21 | + ({'hi': 'there'}, '{"hi": "there"}'), |
| 22 | +]) |
| 23 | +def test_jinja2_tojson(obj, expected): |
| 24 | + res = jinja2.Template('{{ obj|tojson }}').render(obj=obj) |
| 25 | + assert res == expected |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.parametrize('obj, expected', [ |
| 29 | + ( |
| 30 | + {'hi': 'there', 'what': 'isup', }, |
| 31 | + '{\n "hi": "there",\n "what": "isup"\n}' |
| 32 | + ), |
| 33 | + ( |
| 34 | + [(0, 0), (1, 1)], |
| 35 | + '[\n [\n 0,\n 0\n ],\n [\n 1,\n 1\n ]\n]', |
| 36 | + ), |
| 37 | +]) |
| 38 | +def test_jinja2_tojson_indented(obj, expected): |
| 39 | + res = jinja2.Template('{{ obj|tojson(2) }}').render(obj=obj) |
| 40 | + assert res == expected |
0 commit comments