| 1 |
import copy |
| 2 |
import itertools |
| 3 |
import pickle |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from jinja2 import ChainableUndefined |
| 8 |
from jinja2 import DebugUndefined |
| 9 |
from jinja2 import StrictUndefined |
| 10 |
from jinja2 import Template |
| 11 |
from jinja2 import TemplateRuntimeError |
| 12 |
from jinja2 import Undefined |
| 13 |
from jinja2.runtime import LoopContext |
| 14 |
|
| 15 |
TEST_IDX_TEMPLATE_STR_1 = ( |
| 16 |
"[{% for i in lst|reverse %}(len={{ loop.length }}," |
| 17 |
" revindex={{ loop.revindex }}, index={{ loop.index }}, val={{ i }}){% endfor %}]" |
| 18 |
) |
| 19 |
TEST_IDX0_TEMPLATE_STR_1 = ( |
| 20 |
"[{% for i in lst|reverse %}(len={{ loop.length }}," |
| 21 |
" revindex0={{ loop.revindex0 }}, index0={{ loop.index0 }}, val={{ i }})" |
| 22 |
"{% endfor %}]" |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
def test_loop_idx(): |
| 27 |
t = Template(TEST_IDX_TEMPLATE_STR_1) |
| 28 |
lst = [10] |
| 29 |
excepted_render = "[(len=1, revindex=1, index=1, val=10)]" |
| 30 |
assert excepted_render == t.render(lst=lst) |
| 31 |
|
| 32 |
|
| 33 |
def test_loop_idx0(): |
| 34 |
t = Template(TEST_IDX0_TEMPLATE_STR_1) |
| 35 |
lst = [10] |
| 36 |
excepted_render = "[(len=1, revindex0=0, index0=0, val=10)]" |
| 37 |
assert excepted_render == t.render(lst=lst) |
| 38 |
|
| 39 |
|
| 40 |
def test_loopcontext0(): |
| 41 |
in_lst = [] |
| 42 |
lc = LoopContext(reversed(in_lst), None) |
| 43 |
assert lc.length == len(in_lst) |
| 44 |
|
| 45 |
|
| 46 |
def test_loopcontext1(): |
| 47 |
in_lst = [10] |
| 48 |
lc = LoopContext(reversed(in_lst), None) |
| 49 |
assert lc.length == len(in_lst) |
| 50 |
|
| 51 |
|
| 52 |
def test_loopcontext2(): |
| 53 |
in_lst = [10, 11] |
| 54 |
lc = LoopContext(reversed(in_lst), None) |
| 55 |
assert lc.length == len(in_lst) |
| 56 |
|
| 57 |
|
| 58 |
def test_iterator_not_advanced_early(): |
| 59 |
t = Template("{% for _, g in gs %}{{ loop.index }} {{ g|list }}\n{% endfor %}") |
| 60 |
out = t.render( |
| 61 |
gs=itertools.groupby([(1, "a"), (1, "b"), (2, "c"), (3, "d")], lambda x: x[0]) |
| 62 |
) |
| 63 |
# groupby groups depend on the current position of the iterator. If |
| 64 |
# it was advanced early, the lists would appear empty. |
| 65 |
assert out == "1 [(1, 'a'), (1, 'b')]\n2 [(2, 'c')]\n3 [(3, 'd')]\n" |
| 66 |
|
| 67 |
|
| 68 |
def test_mock_not_pass_arg_marker(): |
| 69 |
"""If a callable class has a ``__getattr__`` that returns True-like |
| 70 |
values for arbitrary attrs, it should not be incorrectly identified |
| 71 |
as a ``pass_context`` function. |
| 72 |
""" |
| 73 |
|
| 74 |
class Calc: |
| 75 |
def __getattr__(self, item): |
| 76 |
return object() |
| 77 |
|
| 78 |
def __call__(self, *args, **kwargs): |
| 79 |
return len(args) + len(kwargs) |
| 80 |
|
| 81 |
t = Template("{{ calc() }}") |
| 82 |
out = t.render(calc=Calc()) |
| 83 |
# Would be "1" if context argument was passed. |
| 84 |
assert out == "0" |
| 85 |
|
| 86 |
|
| 87 |
_undefined_types = (Undefined, ChainableUndefined, DebugUndefined, StrictUndefined) |
| 88 |
|
| 89 |
|
| 90 |
@pytest.mark.parametrize("undefined_type", _undefined_types) |
| 91 |
def test_undefined_copy(undefined_type): |
| 92 |
undef = undefined_type("a hint", ["foo"], "a name", TemplateRuntimeError) |
| 93 |
copied = copy.copy(undef) |
| 94 |
|
| 95 |
assert copied is not undef |
| 96 |
assert copied._undefined_hint is undef._undefined_hint |
| 97 |
assert copied._undefined_obj is undef._undefined_obj |
| 98 |
assert copied._undefined_name is undef._undefined_name |
| 99 |
assert copied._undefined_exception is undef._undefined_exception |
| 100 |
|
| 101 |
|
| 102 |
@pytest.mark.parametrize("undefined_type", _undefined_types) |
| 103 |
def test_undefined_deepcopy(undefined_type): |
| 104 |
undef = undefined_type("a hint", ["foo"], "a name", TemplateRuntimeError) |
| 105 |
copied = copy.deepcopy(undef) |
| 106 |
|
| 107 |
assert copied._undefined_hint is undef._undefined_hint |
| 108 |
assert copied._undefined_obj is not undef._undefined_obj |
| 109 |
assert copied._undefined_obj == undef._undefined_obj |
| 110 |
assert copied._undefined_name is undef._undefined_name |
| 111 |
assert copied._undefined_exception is undef._undefined_exception |
| 112 |
|
| 113 |
|
| 114 |
@pytest.mark.parametrize("undefined_type", _undefined_types) |
| 115 |
def test_undefined_pickle(undefined_type): |
| 116 |
undef = undefined_type("a hint", ["foo"], "a name", TemplateRuntimeError) |
| 117 |
copied = pickle.loads(pickle.dumps(undef)) |
| 118 |
|
| 119 |
assert copied._undefined_hint is not undef._undefined_hint |
| 120 |
assert copied._undefined_hint == undef._undefined_hint |
| 121 |
assert copied._undefined_obj is not undef._undefined_obj |
| 122 |
assert copied._undefined_obj == undef._undefined_obj |
| 123 |
assert copied._undefined_name is not undef._undefined_name |
| 124 |
assert copied._undefined_name == undef._undefined_name |
| 125 |
assert copied._undefined_exception is undef._undefined_exception |