test_nativetypes.py
This file
Path
tests/test_nativetypes.py
148 lines · 3.9 KiB · Python
Last changed by
native only evals at end of renderDavid Lordbefore adoptionf75cb42
History of this fileTake it
1import pytest23from jinja2._compat import text_type4from jinja2.exceptions import UndefinedError5from jinja2.nativetypes import NativeEnvironment6from jinja2.nativetypes import NativeTemplate7from jinja2.runtime import Undefined8910@pytest.fixture11def env():12 return NativeEnvironment()131415def test_is_defined_native_return(env):16 t = env.from_string("{{ missing is defined }}")17 assert not t.render()181920def test_undefined_native_return(env):21 t = env.from_string("{{ missing }}")22 assert isinstance(t.render(), Undefined)232425def test_adding_undefined_native_return(env):26 t = env.from_string("{{ 3 + missing }}")2728 with pytest.raises(UndefinedError):29 t.render()303132def test_cast_int(env):33 t = env.from_string("{{ value|int }}")34 result = t.render(value="3")35 assert isinstance(result, int)36 assert result == 3373839def test_list_add(env):40 t = env.from_string("{{ a + b }}")41 result = t.render(a=["a", "b"], b=["c", "d"])42 assert isinstance(result, list)43 assert result == ["a", "b", "c", "d"]444546def test_multi_expression_add(env):47 t = env.from_string("{{ a }} + {{ b }}")48 result = t.render(a=["a", "b"], b=["c", "d"])49 assert not isinstance(result, list)50 assert result == "['a', 'b'] + ['c', 'd']"515253def test_loops(env):54 t = env.from_string("{% for x in value %}{{ x }}{% endfor %}")55 result = t.render(value=["a", "b", "c", "d"])56 assert isinstance(result, text_type)57 assert result == "abcd"585960def test_loops_with_ints(env):61 t = env.from_string("{% for x in value %}{{ x }}{% endfor %}")62 result = t.render(value=[1, 2, 3, 4])63 assert isinstance(result, int)64 assert result == 1234656667def test_loop_look_alike(env):68 t = env.from_string("{% for x in value %}{{ x }}{% endfor %}")69 result = t.render(value=[1])70 assert isinstance(result, int)71 assert result == 1727374@pytest.mark.parametrize(75 ("source", "expect"),76 (77 ("{{ value }}", True),78 ("{{ value }}", False),79 ("{{ 1 == 1 }}", True),80 ("{{ 2 + 2 == 5 }}", False),81 ("{{ None is none }}", True),82 ("{{ '' == None }}", False),83 ),84)85def test_booleans(env, source, expect):86 t = env.from_string(source)87 result = t.render(value=expect)88 assert isinstance(result, bool)89 assert result is expect909192def test_variable_dunder(env):93 t = env.from_string("{{ x.__class__ }}")94 result = t.render(x=True)95 assert isinstance(result, type)969798def test_constant_dunder(env):99 t = env.from_string("{{ true.__class__ }}")100 result = t.render()101 assert isinstance(result, type)102103104def test_constant_dunder_to_string(env):105 t = env.from_string("{{ true.__class__|string }}")106 result = t.render()107 assert not isinstance(result, type)108 assert result in {"<type 'bool'>", "<class 'bool'>"}109110111def test_string_literal_var(env):112 t = env.from_string("[{{ 'all' }}]")113 result = t.render()114 assert isinstance(result, text_type)115 assert result == "[all]"116117118def test_string_top_level(env):119 t = env.from_string("'Jinja'")120 result = t.render()121 assert result == "Jinja"122123124def test_tuple_of_variable_strings(env):125 t = env.from_string("'{{ a }}', 'data', '{{ b }}', b'{{ c }}'")126 result = t.render(a=1, b=2, c="bytes")127 assert isinstance(result, tuple)128 assert result == ("1", "data", "2", b"bytes")129130131def test_concat_strings_with_quotes(env):132 t = env.from_string("--host='{{ host }}' --user \"{{ user }}\"")133 result = t.render(host="localhost", user="Jinja")134 assert result == "--host='localhost' --user \"Jinja\""135136137def test_no_intermediate_eval(env):138 t = env.from_string("0.000{{ a }}")139 result = t.render(a=7)140 assert isinstance(result, float)141 # If intermediate eval happened, 0.000 would render 0.0, then 7142 # would be appended, resulting in 0.07.143 assert result < 0.007 # TODO use math.isclose in Python 3144145146def test_spontaneous_env():147 t = NativeTemplate("{{ true }}")148 assert isinstance(t.environment, NativeEnvironment)