aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Jinja2/py3/tests/test_api.py
diff options
context:
space:
mode:
authorfloatdrop <floatdrop@yandex-team.ru>2022-02-10 16:47:15 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:47:15 +0300
commit4267de875ca703ff841f2e025723dadc78f3cc02 (patch)
tree9814fbd1c3effac9b8377c5d604b367b14e2db55 /contrib/python/Jinja2/py3/tests/test_api.py
parente63b84f1d39557d9e46ac380b1f388271894293c (diff)
downloadydb-4267de875ca703ff841f2e025723dadc78f3cc02.tar.gz
Restoring authorship annotation for <floatdrop@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/python/Jinja2/py3/tests/test_api.py')
-rw-r--r--contrib/python/Jinja2/py3/tests/test_api.py308
1 files changed, 154 insertions, 154 deletions
diff --git a/contrib/python/Jinja2/py3/tests/test_api.py b/contrib/python/Jinja2/py3/tests/test_api.py
index e36f54a5eda..4db3b4a96a6 100644
--- a/contrib/python/Jinja2/py3/tests/test_api.py
+++ b/contrib/python/Jinja2/py3/tests/test_api.py
@@ -1,8 +1,8 @@
import shutil
-import tempfile
+import tempfile
from pathlib import Path
-
-import pytest
+
+import pytest
from jinja2 import ChainableUndefined
from jinja2 import DebugUndefined
@@ -16,31 +16,31 @@ from jinja2 import Template
from jinja2 import TemplatesNotFound
from jinja2 import Undefined
from jinja2 import UndefinedError
-from jinja2.compiler import CodeGenerator
-from jinja2.runtime import Context
-from jinja2.utils import Cycler
+from jinja2.compiler import CodeGenerator
+from jinja2.runtime import Context
+from jinja2.utils import Cycler
from jinja2.utils import pass_context
from jinja2.utils import pass_environment
from jinja2.utils import pass_eval_context
-
-
+
+
class TestExtendedAPI:
- def test_item_and_attribute(self, env):
- from jinja2.sandbox import SandboxedEnvironment
-
- for env in Environment(), SandboxedEnvironment():
+ def test_item_and_attribute(self, env):
+ from jinja2.sandbox import SandboxedEnvironment
+
+ for env in Environment(), SandboxedEnvironment():
tmpl = env.from_string("{{ foo.items()|list }}")
assert tmpl.render(foo={"items": 42}) == "[('items', 42)]"
- tmpl = env.from_string('{{ foo|attr("items")()|list }}')
+ tmpl = env.from_string('{{ foo|attr("items")()|list }}')
assert tmpl.render(foo={"items": 42}) == "[('items', 42)]"
- tmpl = env.from_string('{{ foo["items"] }}')
+ tmpl = env.from_string('{{ foo["items"] }}')
assert tmpl.render(foo={"items": 42}) == "42"
-
+
def test_finalize(self):
e = Environment(finalize=lambda v: "" if v is None else v)
t = e.from_string("{% for item in seq %}|{{ item }}{% endfor %}")
assert t.render(seq=(None, 1, "foo")) == "||1|foo"
-
+
def test_finalize_constant_expression(self):
e = Environment(finalize=lambda v: "" if v is None else v)
t = e.from_string("<{{ none }}>")
@@ -81,34 +81,34 @@ class TestExtendedAPI:
t = e.from_string("{{ value }}")
assert t.render(value="hello") == "{{ 'hello' }}"
- def test_cycler(self, env):
- items = 1, 2, 3
- c = Cycler(*items)
- for item in items + items:
- assert c.current == item
- assert next(c) == item
- next(c)
- assert c.current == 2
- c.reset()
- assert c.current == 1
-
- def test_expressions(self, env):
- expr = env.compile_expression("foo")
- assert expr() is None
- assert expr(foo=42) == 42
- expr2 = env.compile_expression("foo", undefined_to_none=False)
- assert is_undefined(expr2())
-
- expr = env.compile_expression("42 + foo")
- assert expr(foo=42) == 84
-
- def test_template_passthrough(self, env):
+ def test_cycler(self, env):
+ items = 1, 2, 3
+ c = Cycler(*items)
+ for item in items + items:
+ assert c.current == item
+ assert next(c) == item
+ next(c)
+ assert c.current == 2
+ c.reset()
+ assert c.current == 1
+
+ def test_expressions(self, env):
+ expr = env.compile_expression("foo")
+ assert expr() is None
+ assert expr(foo=42) == 42
+ expr2 = env.compile_expression("foo", undefined_to_none=False)
+ assert is_undefined(expr2())
+
+ expr = env.compile_expression("42 + foo")
+ assert expr(foo=42) == 84
+
+ def test_template_passthrough(self, env):
t = Template("Content")
- assert env.get_template(t) is t
- assert env.select_template([t]) is t
- assert env.get_or_select_template([t]) is t
- assert env.get_or_select_template(t) is t
-
+ assert env.get_template(t) is t
+ assert env.select_template([t]) is t
+ assert env.get_or_select_template([t]) is t
+ assert env.get_or_select_template(t) is t
+
def test_get_template_undefined(self, env):
"""Passing Undefined to get/select_template raises an
UndefinedError or shows the undefined message in the list.
@@ -132,12 +132,12 @@ class TestExtendedAPI:
assert "'no_name_1' is undefined" in exc_message
assert "no_name_2" in exc_message
- def test_autoescape_autoselect(self, env):
- def select_autoescape(name):
+ def test_autoescape_autoselect(self, env):
+ def select_autoescape(name):
if name is None or "." not in name:
- return False
+ return False
return name.endswith(".html")
-
+
env = Environment(
autoescape=select_autoescape,
loader=DictLoader({"test.txt": "{{ foo }}", "test.html": "{{ foo }}"}),
@@ -148,7 +148,7 @@ class TestExtendedAPI:
assert t.render(foo="<foo>") == "&lt;foo&gt;"
t = env.from_string("{{ foo }}")
assert t.render(foo="<foo>") == "<foo>"
-
+
def test_sandbox_max_range(self, env):
from jinja2.sandbox import SandboxedEnvironment, MAX_RANGE
@@ -160,60 +160,60 @@ class TestExtendedAPI:
class TestMeta:
- def test_find_undeclared_variables(self, env):
+ def test_find_undeclared_variables(self, env):
ast = env.parse("{% set foo = 42 %}{{ bar + foo }}")
- x = meta.find_undeclared_variables(ast)
+ x = meta.find_undeclared_variables(ast)
assert x == {"bar"}
-
+
ast = env.parse(
"{% set foo = 42 %}{{ bar + foo }}"
"{% macro meh(x) %}{{ x }}{% endmacro %}"
"{% for item in seq %}{{ muh(item) + meh(seq) }}"
"{% endfor %}"
)
- x = meta.find_undeclared_variables(ast)
+ x = meta.find_undeclared_variables(ast)
assert x == {"bar", "seq", "muh"}
-
+
ast = env.parse("{% for x in range(5) %}{{ x }}{% endfor %}{{ foo }}")
x = meta.find_undeclared_variables(ast)
assert x == {"foo"}
- def test_find_refererenced_templates(self, env):
- ast = env.parse('{% extends "layout.html" %}{% include helper %}')
- i = meta.find_referenced_templates(ast)
+ def test_find_refererenced_templates(self, env):
+ ast = env.parse('{% extends "layout.html" %}{% include helper %}')
+ i = meta.find_referenced_templates(ast)
assert next(i) == "layout.html"
- assert next(i) is None
- assert list(i) == []
-
+ assert next(i) is None
+ assert list(i) == []
+
ast = env.parse(
'{% extends "layout.html" %}'
'{% from "test.html" import a, b as c %}'
'{% import "meh.html" as meh %}'
'{% include "muh.html" %}'
)
- i = meta.find_referenced_templates(ast)
+ i = meta.find_referenced_templates(ast)
assert list(i) == ["layout.html", "test.html", "meh.html", "muh.html"]
-
- def test_find_included_templates(self, env):
- ast = env.parse('{% include ["foo.html", "bar.html"] %}')
- i = meta.find_referenced_templates(ast)
+
+ def test_find_included_templates(self, env):
+ ast = env.parse('{% include ["foo.html", "bar.html"] %}')
+ i = meta.find_referenced_templates(ast)
assert list(i) == ["foo.html", "bar.html"]
-
- ast = env.parse('{% include ("foo.html", "bar.html") %}')
- i = meta.find_referenced_templates(ast)
+
+ ast = env.parse('{% include ("foo.html", "bar.html") %}')
+ i = meta.find_referenced_templates(ast)
assert list(i) == ["foo.html", "bar.html"]
-
- ast = env.parse('{% include ["foo.html", "bar.html", foo] %}')
- i = meta.find_referenced_templates(ast)
+
+ ast = env.parse('{% include ["foo.html", "bar.html", foo] %}')
+ i = meta.find_referenced_templates(ast)
assert list(i) == ["foo.html", "bar.html", None]
-
- ast = env.parse('{% include ("foo.html", "bar.html", foo) %}')
- i = meta.find_referenced_templates(ast)
+
+ ast = env.parse('{% include ("foo.html", "bar.html", foo) %}')
+ i = meta.find_referenced_templates(ast)
assert list(i) == ["foo.html", "bar.html", None]
-
-
+
+
class TestStreaming:
- def test_basic_streaming(self, env):
+ def test_basic_streaming(self, env):
t = env.from_string(
"<ul>{% for item in seq %}<li>{{ loop.index }} - {{ item }}</li>"
"{%- endfor %}</ul>"
@@ -221,51 +221,51 @@ class TestStreaming:
stream = t.stream(seq=list(range(3)))
assert next(stream) == "<ul>"
assert "".join(stream) == "<li>1 - 0</li><li>2 - 1</li><li>3 - 2</li></ul>"
-
- def test_buffered_streaming(self, env):
+
+ def test_buffered_streaming(self, env):
tmpl = env.from_string(
"<ul>{% for item in seq %}<li>{{ loop.index }} - {{ item }}</li>"
"{%- endfor %}</ul>"
)
stream = tmpl.stream(seq=list(range(3)))
- stream.enable_buffering(size=3)
+ stream.enable_buffering(size=3)
assert next(stream) == "<ul><li>1"
assert next(stream) == " - 0</li>"
-
- def test_streaming_behavior(self, env):
- tmpl = env.from_string("")
- stream = tmpl.stream()
- assert not stream.buffered
- stream.enable_buffering(20)
- assert stream.buffered
- stream.disable_buffering()
- assert not stream.buffered
-
- def test_dump_stream(self, env):
+
+ def test_streaming_behavior(self, env):
+ tmpl = env.from_string("")
+ stream = tmpl.stream()
+ assert not stream.buffered
+ stream.enable_buffering(20)
+ assert stream.buffered
+ stream.disable_buffering()
+ assert not stream.buffered
+
+ def test_dump_stream(self, env):
tmp = Path(tempfile.mkdtemp())
- try:
+ try:
tmpl = env.from_string("\u2713")
- stream = tmpl.stream()
+ stream = tmpl.stream()
stream.dump(str(tmp / "dump.txt"), "utf-8")
assert (tmp / "dump.txt").read_bytes() == b"\xe2\x9c\x93"
- finally:
- shutil.rmtree(tmp)
-
-
+ finally:
+ shutil.rmtree(tmp)
+
+
class TestUndefined:
- def test_stopiteration_is_undefined(self):
- def test():
- raise StopIteration()
+ def test_stopiteration_is_undefined(self):
+ def test():
+ raise StopIteration()
t = Template("A{{ test() }}B")
assert t.render(test=test) == "AB"
t = Template("A{{ test().missingattribute }}B")
- pytest.raises(UndefinedError, t.render, test=test)
-
- def test_undefined_and_special_attributes(self):
+ pytest.raises(UndefinedError, t.render, test=test)
+
+ def test_undefined_and_special_attributes(self):
with pytest.raises(AttributeError):
Undefined("Foo").__dict__
-
+
def test_undefined_attribute_error(self):
# Django's LazyObject turns the __class__ attribute into a
# property that resolves the wrapped function. If that wrapped
@@ -281,34 +281,34 @@ class TestUndefined:
with pytest.raises(UndefinedError):
getattr(u, "recursion", None)
- def test_logging_undefined(self):
- _messages = []
-
+ def test_logging_undefined(self):
+ _messages = []
+
class DebugLogger:
- def warning(self, msg, *args):
+ def warning(self, msg, *args):
_messages.append("W:" + msg % args)
-
- def error(self, msg, *args):
+
+ def error(self, msg, *args):
_messages.append("E:" + msg % args)
-
- logging_undefined = make_logging_undefined(DebugLogger())
- env = Environment(undefined=logging_undefined)
+
+ logging_undefined = make_logging_undefined(DebugLogger())
+ env = Environment(undefined=logging_undefined)
assert env.from_string("{{ missing }}").render() == ""
pytest.raises(UndefinedError, env.from_string("{{ missing.attribute }}").render)
assert env.from_string("{{ missing|list }}").render() == "[]"
assert env.from_string("{{ missing is not defined }}").render() == "True"
assert env.from_string("{{ foo.missing }}").render(foo=42) == ""
assert env.from_string("{{ not missing }}").render() == "True"
- assert _messages == [
+ assert _messages == [
"W:Template variable warning: 'missing' is undefined",
- "E:Template variable error: 'missing' is undefined",
+ "E:Template variable error: 'missing' is undefined",
"W:Template variable warning: 'missing' is undefined",
"W:Template variable warning: 'int object' has no attribute 'missing'",
"W:Template variable warning: 'missing' is undefined",
- ]
-
- def test_default_undefined(self):
- env = Environment(undefined=Undefined)
+ ]
+
+ def test_default_undefined(self):
+ env = Environment(undefined=Undefined)
assert env.from_string("{{ missing }}").render() == ""
pytest.raises(UndefinedError, env.from_string("{{ missing.attribute }}").render)
assert env.from_string("{{ missing|list }}").render() == "[]"
@@ -324,7 +324,7 @@ class TestUndefined:
assert hash(und1) == hash(und2) == hash(Undefined())
with pytest.raises(AttributeError):
getattr(Undefined, "__slots__") # noqa: B009
-
+
def test_chainable_undefined(self):
env = Environment(undefined=ChainableUndefined)
# The following tests are copied from test_default_undefined
@@ -351,8 +351,8 @@ class TestUndefined:
== "baz"
)
- def test_debug_undefined(self):
- env = Environment(undefined=DebugUndefined)
+ def test_debug_undefined(self):
+ env = Environment(undefined=DebugUndefined)
assert env.from_string("{{ missing }}").render() == "{{ missing }}"
pytest.raises(UndefinedError, env.from_string("{{ missing.attribute }}").render)
assert env.from_string("{{ missing|list }}").render() == "[]"
@@ -369,9 +369,9 @@ class TestUndefined:
)
with pytest.raises(AttributeError):
getattr(DebugUndefined, "__slots__") # noqa: B009
-
- def test_strict_undefined(self):
- env = Environment(undefined=StrictUndefined)
+
+ def test_strict_undefined(self):
+ env = Environment(undefined=StrictUndefined)
pytest.raises(UndefinedError, env.from_string("{{ missing }}").render)
pytest.raises(UndefinedError, env.from_string("{{ missing.attribute }}").render)
pytest.raises(UndefinedError, env.from_string("{{ missing|list }}").render)
@@ -388,47 +388,47 @@ class TestUndefined:
with pytest.raises(AttributeError):
getattr(StrictUndefined, "__slots__") # noqa: B009
assert env.from_string('{{ "foo" if false }}').render() == ""
-
- def test_indexing_gives_undefined(self):
- t = Template("{{ var[42].foo }}")
- pytest.raises(UndefinedError, t.render, var=0)
-
- def test_none_gives_proper_error(self):
+
+ def test_indexing_gives_undefined(self):
+ t = Template("{{ var[42].foo }}")
+ pytest.raises(UndefinedError, t.render, var=0)
+
+ def test_none_gives_proper_error(self):
with pytest.raises(UndefinedError, match="'None' has no attribute 'split'"):
Environment().getattr(None, "split")()
-
- def test_object_repr(self):
+
+ def test_object_repr(self):
with pytest.raises(
UndefinedError, match="'int object' has no attribute 'upper'"
):
Undefined(obj=42, name="upper")()
-
-
+
+
class TestLowLevel:
- def test_custom_code_generator(self):
- class CustomCodeGenerator(CodeGenerator):
- def visit_Const(self, node, frame=None):
- # This method is pure nonsense, but works fine for testing...
+ def test_custom_code_generator(self):
+ class CustomCodeGenerator(CodeGenerator):
+ def visit_Const(self, node, frame=None):
+ # This method is pure nonsense, but works fine for testing...
if node.value == "foo":
self.write(repr("bar"))
- else:
+ else:
super().visit_Const(node, frame)
-
- class CustomEnvironment(Environment):
- code_generator_class = CustomCodeGenerator
-
- env = CustomEnvironment()
- tmpl = env.from_string('{% set foo = "foo" %}{{ foo }}')
+
+ class CustomEnvironment(Environment):
+ code_generator_class = CustomCodeGenerator
+
+ env = CustomEnvironment()
+ tmpl = env.from_string('{% set foo = "foo" %}{{ foo }}')
assert tmpl.render() == "bar"
-
- def test_custom_context(self):
- class CustomContext(Context):
- def resolve_or_missing(self, key):
+
+ def test_custom_context(self):
+ class CustomContext(Context):
+ def resolve_or_missing(self, key):
return "resolve-" + key
-
- class CustomEnvironment(Environment):
- context_class = CustomContext
-
- env = CustomEnvironment()
+
+ class CustomEnvironment(Environment):
+ context_class = CustomContext
+
+ env = CustomEnvironment()
tmpl = env.from_string("{{ foo }}")
assert tmpl.render() == "resolve-foo"