Skip to content
Myr

test_nativetypes.py

This file

Path

tests/test_nativetypes.py

162 lines · 4.2 KiB · Python

Last changed by

Merge pull request #1511 from mkrizek/issue-1510David Lordbefore adoption7d72eb7

History of this file

Take it

1import math23import pytest45from jinja2.exceptions import UndefinedError6from jinja2.nativetypes import NativeEnvironment7from jinja2.nativetypes import NativeTemplate8from jinja2.runtime import Undefined91011@pytest.fixture12def env():13    return NativeEnvironment()141516def test_is_defined_native_return(env):17    t = env.from_string("{{ missing is defined }}")18    assert not t.render()192021def test_undefined_native_return(env):22    t = env.from_string("{{ missing }}")23    assert isinstance(t.render(), Undefined)242526def test_adding_undefined_native_return(env):27    t = env.from_string("{{ 3 + missing }}")2829    with pytest.raises(UndefinedError):30        t.render()313233def test_cast_int(env):34    t = env.from_string("{{ value|int }}")35    result = t.render(value="3")36    assert isinstance(result, int)37    assert result == 3383940def test_list_add(env):41    t = env.from_string("{{ a + b }}")42    result = t.render(a=["a", "b"], b=["c", "d"])43    assert isinstance(result, list)44    assert result == ["a", "b", "c", "d"]454647def test_multi_expression_add(env):48    t = env.from_string("{{ a }} + {{ b }}")49    result = t.render(a=["a", "b"], b=["c", "d"])50    assert not isinstance(result, list)51    assert result == "['a', 'b'] + ['c', 'd']"525354def test_loops(env):55    t = env.from_string("{% for x in value %}{{ x }}{% endfor %}")56    result = t.render(value=["a", "b", "c", "d"])57    assert isinstance(result, str)58    assert result == "abcd"596061def test_loops_with_ints(env):62    t = env.from_string("{% for x in value %}{{ x }}{% endfor %}")63    result = t.render(value=[1, 2, 3, 4])64    assert isinstance(result, int)65    assert result == 1234666768def test_loop_look_alike(env):69    t = env.from_string("{% for x in value %}{{ x }}{% endfor %}")70    result = t.render(value=[1])71    assert isinstance(result, int)72    assert result == 1737475@pytest.mark.parametrize(76    ("source", "expect"),77    (78        ("{{ value }}", True),79        ("{{ value }}", False),80        ("{{ 1 == 1 }}", True),81        ("{{ 2 + 2 == 5 }}", False),82        ("{{ None is none }}", True),83        ("{{ '' == None }}", False),84    ),85)86def test_booleans(env, source, expect):87    t = env.from_string(source)88    result = t.render(value=expect)89    assert isinstance(result, bool)90    assert result is expect919293def test_variable_dunder(env):94    t = env.from_string("{{ x.__class__ }}")95    result = t.render(x=True)96    assert isinstance(result, type)979899def test_constant_dunder(env):100    t = env.from_string("{{ true.__class__ }}")101    result = t.render()102    assert isinstance(result, type)103104105def test_constant_dunder_to_string(env):106    t = env.from_string("{{ true.__class__|string }}")107    result = t.render()108    assert not isinstance(result, type)109    assert result in {"<type 'bool'>", "<class 'bool'>"}110111112def test_string_literal_var(env):113    t = env.from_string("[{{ 'all' }}]")114    result = t.render()115    assert isinstance(result, str)116    assert result == "[all]"117118119def test_string_top_level(env):120    t = env.from_string("'Jinja'")121    result = t.render()122    assert result == "Jinja"123124125def test_tuple_of_variable_strings(env):126    t = env.from_string("'{{ a }}', 'data', '{{ b }}', b'{{ c }}'")127    result = t.render(a=1, b=2, c="bytes")128    assert isinstance(result, tuple)129    assert result == ("1", "data", "2", b"bytes")130131132def test_concat_strings_with_quotes(env):133    t = env.from_string("--host='{{ host }}' --user \"{{ user }}\"")134    result = t.render(host="localhost", user="Jinja")135    assert result == "--host='localhost' --user \"Jinja\""136137138def test_no_intermediate_eval(env):139    t = env.from_string("0.000{{ a }}")140    result = t.render(a=7)141    assert isinstance(result, float)142    # If intermediate eval happened, 0.000 would render 0.0, then 7143    # would be appended, resulting in 0.07.144    assert math.isclose(result, 0.0007)145146147def test_spontaneous_env():148    t = NativeTemplate("{{ true }}")149    assert isinstance(t.environment, NativeEnvironment)150151152def test_leading_spaces(env):153    t = env.from_string(" {{ True }}")154    result = t.render()155    assert result == " True"156157158def test_macro(env):159    t = env.from_string("{%- macro x() -%}{{- [1,2] -}}{%- endmacro -%}{{- x()[1] -}}")160    result = t.render()161    assert result == 2162    assert isinstance(result, int)