test_runtime.py
This file
Path
tests/test_runtime.py
125 lines · 4.1 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.
Merge branch 'stable'David Lordbefore adoption6aeab5d
line 1why, one step back1import copyupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 2why, one step back2import itertoolsMerge branch 'stable'David Lordbefore adoption6aeab5d
line 3why, one step back3import pickleupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 4why, one step back4Merge branch 'stable'David Lordbefore adoption6aeab5d
lines 5–9why, one step back5import pytest67from jinja2 import ChainableUndefined8from jinja2 import DebugUndefined9from jinja2 import StrictUndefinedupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 10why, one step back10from jinja2 import TemplateMerge branch 'stable'David Lordbefore adoption6aeab5d
lines 11–12why, one step back11from jinja2 import TemplateRuntimeError12from jinja2 import Undefinedupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 13–67why, one step back13from jinja2.runtime import LoopContext1415TEST_IDX_TEMPLATE_STR_1 = (16 "[{% for i in lst|reverse %}(len={{ loop.length }},"17 " revindex={{ loop.revindex }}, index={{ loop.index }}, val={{ i }}){% endfor %}]"18)19TEST_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)242526def 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)313233def 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)383940def test_loopcontext0():41 in_lst = []42 lc = LoopContext(reversed(in_lst), None)43 assert lc.length == len(in_lst)444546def test_loopcontext1():47 in_lst = [10]48 lc = LoopContext(reversed(in_lst), None)49 assert lc.length == len(in_lst)505152def test_loopcontext2():53 in_lst = [10, 11]54 lc = LoopContext(reversed(in_lst), None)55 assert lc.length == len(in_lst)565758def 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. If64 # it was advanced early, the lists would appear empty.65 assert out == "1 [(1, 'a'), (1, 'b')]\n2 [(2, 'c')]\n3 [(3, 'd')]\n"6667Merge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8
line 68why, one step back68def test_mock_not_pass_arg_marker():update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 69–70why, one step back69 """If a callable class has a ``__getattr__`` that returns True-like70 values for arbitrary attrs, it should not be incorrectly identifiedMerge pull request #1389 from pallets/unify-decoratorsDavid Lordbefore adoption39846a8
line 71why, one step back71 as a ``pass_context`` function.update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 72–84why, one step back72 """7374 class Calc:75 def __getattr__(self, item):76 return object()7778 def __call__(self, *args, **kwargs):79 return len(args) + len(kwargs)8081 t = Template("{{ calc() }}")82 out = t.render(calc=Calc())83 # Would be "1" if context argument was passed.84 assert out == "0"Merge branch 'stable'David Lordbefore adoption6aeab5d
lines 85–125why, one step back858687_undefined_types = (Undefined, ChainableUndefined, DebugUndefined, StrictUndefined)888990@pytest.mark.parametrize("undefined_type", _undefined_types)91def test_undefined_copy(undefined_type):92 undef = undefined_type("a hint", ["foo"], "a name", TemplateRuntimeError)93 copied = copy.copy(undef)9495 assert copied is not undef96 assert copied._undefined_hint is undef._undefined_hint97 assert copied._undefined_obj is undef._undefined_obj98 assert copied._undefined_name is undef._undefined_name99 assert copied._undefined_exception is undef._undefined_exception100101102@pytest.mark.parametrize("undefined_type", _undefined_types)103def test_undefined_deepcopy(undefined_type):104 undef = undefined_type("a hint", ["foo"], "a name", TemplateRuntimeError)105 copied = copy.deepcopy(undef)106107 assert copied._undefined_hint is undef._undefined_hint108 assert copied._undefined_obj is not undef._undefined_obj109 assert copied._undefined_obj == undef._undefined_obj110 assert copied._undefined_name is undef._undefined_name111 assert copied._undefined_exception is undef._undefined_exception112113114@pytest.mark.parametrize("undefined_type", _undefined_types)115def test_undefined_pickle(undefined_type):116 undef = undefined_type("a hint", ["foo"], "a name", TemplateRuntimeError)117 copied = pickle.loads(pickle.dumps(undef))118119 assert copied._undefined_hint is not undef._undefined_hint120 assert copied._undefined_hint == undef._undefined_hint121 assert copied._undefined_obj is not undef._undefined_obj122 assert copied._undefined_obj == undef._undefined_obj123 assert copied._undefined_name is not undef._undefined_name124 assert copied._undefined_name == undef._undefined_name125 assert copied._undefined_exception is undef._undefined_exception