test_nativetypes.py
This file
Path
tests/test_nativetypes.py
189 lines · 4.9 KiB · Python
Last changed by
Merge branch 'stable'David Lordbefore adoption6aeab5d
History of this fileTake it
The walk stopped after 200 commits: lines older than c580dd0 point at it.
update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 1–15why, one step back1import math23import pytest45from jinja2.exceptions import UndefinedError6from jinja2.nativetypes import NativeEnvironment7from jinja2.nativetypes import NativeTemplate8from jinja2.runtime import Undefined91011@pytest.fixture12def env():13 return NativeEnvironment()1415Additional test coverage for async rendering of native type templates (#1807)James Addisonbefore adoptionf8323cf
lines 16–20why, one step back16@pytest.fixture17def async_native_env():18 return NativeEnvironment(enable_async=True)1920update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 21–127why, one step back21def test_is_defined_native_return(env):22 t = env.from_string("{{ missing is defined }}")23 assert not t.render()242526def test_undefined_native_return(env):27 t = env.from_string("{{ missing }}")28 assert isinstance(t.render(), Undefined)293031def test_adding_undefined_native_return(env):32 t = env.from_string("{{ 3 + missing }}")3334 with pytest.raises(UndefinedError):35 t.render()363738def test_cast_int(env):39 t = env.from_string("{{ value|int }}")40 result = t.render(value="3")41 assert isinstance(result, int)42 assert result == 3434445def test_list_add(env):46 t = env.from_string("{{ a + b }}")47 result = t.render(a=["a", "b"], b=["c", "d"])48 assert isinstance(result, list)49 assert result == ["a", "b", "c", "d"]505152def test_multi_expression_add(env):53 t = env.from_string("{{ a }} + {{ b }}")54 result = t.render(a=["a", "b"], b=["c", "d"])55 assert not isinstance(result, list)56 assert result == "['a', 'b'] + ['c', 'd']"575859def test_loops(env):60 t = env.from_string("{% for x in value %}{{ x }}{% endfor %}")61 result = t.render(value=["a", "b", "c", "d"])62 assert isinstance(result, str)63 assert result == "abcd"646566def test_loops_with_ints(env):67 t = env.from_string("{% for x in value %}{{ x }}{% endfor %}")68 result = t.render(value=[1, 2, 3, 4])69 assert isinstance(result, int)70 assert result == 1234717273def test_loop_look_alike(env):74 t = env.from_string("{% for x in value %}{{ x }}{% endfor %}")75 result = t.render(value=[1])76 assert isinstance(result, int)77 assert result == 1787980@pytest.mark.parametrize(81 ("source", "expect"),82 (83 ("{{ value }}", True),84 ("{{ value }}", False),85 ("{{ 1 == 1 }}", True),86 ("{{ 2 + 2 == 5 }}", False),87 ("{{ None is none }}", True),88 ("{{ '' == None }}", False),89 ),90)91def test_booleans(env, source, expect):92 t = env.from_string(source)93 result = t.render(value=expect)94 assert isinstance(result, bool)95 assert result is expect969798def test_variable_dunder(env):99 t = env.from_string("{{ x.__class__ }}")100 result = t.render(x=True)101 assert isinstance(result, type)102103104def test_constant_dunder(env):105 t = env.from_string("{{ true.__class__ }}")106 result = t.render()107 assert isinstance(result, type)108109110def test_constant_dunder_to_string(env):111 t = env.from_string("{{ true.__class__|string }}")112 result = t.render()113 assert not isinstance(result, type)114 assert result in {"<type 'bool'>", "<class 'bool'>"}115116117def test_string_literal_var(env):118 t = env.from_string("[{{ 'all' }}]")119 result = t.render()120 assert isinstance(result, str)121 assert result == "[all]"122123124def test_string_top_level(env):125 t = env.from_string("'Jinja'")126 result = t.render()127 assert result == "Jinja"Additional test coverage for async rendering of native type templates (#1807)James Addisonbefore adoptionf8323cf
lines 128–139why, one step back128129130def test_string_concatenation(async_native_env, run_async_fn):131 async def async_render():132 t = async_native_env.from_string(133 "{%- macro x(y) -%}{{ y }}{%- endmacro -%}{{- x('not') }} {{ x('bad') -}}"134 )135 result = await t.render_async()136 assert isinstance(result, str)137 assert result == "not bad"138139 run_async_fn(async_render)update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 140–166why, one step back140141142def test_tuple_of_variable_strings(env):143 t = env.from_string("'{{ a }}', 'data', '{{ b }}', b'{{ c }}'")144 result = t.render(a=1, b=2, c="bytes")145 assert isinstance(result, tuple)146 assert result == ("1", "data", "2", b"bytes")147148149def test_concat_strings_with_quotes(env):150 t = env.from_string("--host='{{ host }}' --user \"{{ user }}\"")151 result = t.render(host="localhost", user="Jinja")152 assert result == "--host='localhost' --user \"Jinja\""153154155def test_no_intermediate_eval(env):156 t = env.from_string("0.000{{ a }}")157 result = t.render(a=7)158 assert isinstance(result, float)159 # If intermediate eval happened, 0.000 would render 0.0, then 7160 # would be appended, resulting in 0.07.161 assert math.isclose(result, 0.0007)162163164def test_spontaneous_env():165 t = NativeTemplate("{{ true }}")166 assert isinstance(t.environment, NativeEnvironment)Merge branch '3.0.x'David Lordbefore adoptiona42b291
lines 167–172why, one step back167168169def test_leading_spaces(env):170 t = env.from_string(" {{ True }}")171 result = t.render()172 assert result == " True"Merge pull request #1511 from mkrizek/issue-1510David Lordbefore adoption7d72eb7
lines 173–179why, one step back173174175def test_macro(env):176 t = env.from_string("{%- macro x() -%}{{- [1,2] -}}{%- endmacro -%}{{- x()[1] -}}")177 result = t.render()178 assert result == 2179 assert isinstance(result, int)Merge branch 'stable'David Lordbefore adoption6aeab5d
lines 180–189why, one step back180181182def test_block(env):183 t = env.from_string(184 "{% block b %}{% for i in range(1) %}{{ loop.index }}{% endfor %}"185 "{% endblock %}{{ self.b() }}"186 )187 result = t.render()188 assert result == 11189 assert isinstance(result, int)