From ce859b2defe1fc90c7a16551291799fc37284add Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 18:06:13 +0200 Subject: Adding upstream version 3.1.2. Signed-off-by: Daniel Baumann --- tests/conftest.py | 49 ++ tests/res/__init__.py | 0 tests/res/package.zip | Bin 0 -> 1036 bytes tests/res/templates/broken.html | 3 + tests/res/templates/foo/test.html | 1 + tests/res/templates/mojibake.txt | 1 + tests/res/templates/syntaxerror.html | 4 + tests/res/templates/test.html | 1 + tests/res/templates2/foo | 2 + tests/test_api.py | 434 ++++++++++++++ tests/test_async.py | 660 ++++++++++++++++++++++ tests/test_async_filters.py | 273 +++++++++ tests/test_bytecode_cache.py | 77 +++ tests/test_compile.py | 28 + tests/test_core_tags.py | 595 ++++++++++++++++++++ tests/test_debug.py | 117 ++++ tests/test_ext.py | 726 ++++++++++++++++++++++++ tests/test_filters.py | 873 ++++++++++++++++++++++++++++ tests/test_idtracking.py | 290 ++++++++++ tests/test_imports.py | 205 +++++++ tests/test_inheritance.py | 405 +++++++++++++ tests/test_lexnparse.py | 1030 ++++++++++++++++++++++++++++++++++ tests/test_loader.py | 412 ++++++++++++++ tests/test_nativetypes.py | 162 ++++++ tests/test_nodes.py | 3 + tests/test_pickle.py | 6 + tests/test_regression.py | 744 ++++++++++++++++++++++++ tests/test_runtime.py | 75 +++ tests/test_security.py | 173 ++++++ tests/test_tests.py | 233 ++++++++ tests/test_utils.py | 185 ++++++ 31 files changed, 7767 insertions(+) create mode 100644 tests/conftest.py create mode 100644 tests/res/__init__.py create mode 100644 tests/res/package.zip create mode 100644 tests/res/templates/broken.html create mode 100644 tests/res/templates/foo/test.html create mode 100644 tests/res/templates/mojibake.txt create mode 100644 tests/res/templates/syntaxerror.html create mode 100644 tests/res/templates/test.html create mode 100644 tests/res/templates2/foo create mode 100644 tests/test_api.py create mode 100644 tests/test_async.py create mode 100644 tests/test_async_filters.py create mode 100644 tests/test_bytecode_cache.py create mode 100644 tests/test_compile.py create mode 100644 tests/test_core_tags.py create mode 100644 tests/test_debug.py create mode 100644 tests/test_ext.py create mode 100644 tests/test_filters.py create mode 100644 tests/test_idtracking.py create mode 100644 tests/test_imports.py create mode 100644 tests/test_inheritance.py create mode 100644 tests/test_lexnparse.py create mode 100644 tests/test_loader.py create mode 100644 tests/test_nativetypes.py create mode 100644 tests/test_nodes.py create mode 100644 tests/test_pickle.py create mode 100644 tests/test_regression.py create mode 100644 tests/test_runtime.py create mode 100644 tests/test_security.py create mode 100644 tests/test_tests.py create mode 100644 tests/test_utils.py (limited to 'tests') diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..e225ab9 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,49 @@ +from pathlib import Path + +import pytest + +from jinja2 import loaders +from jinja2.environment import Environment + + +@pytest.fixture +def env(): + """returns a new environment.""" + return Environment() + + +@pytest.fixture +def dict_loader(): + """returns DictLoader""" + return loaders.DictLoader({"justdict.html": "FOO"}) + + +@pytest.fixture +def package_loader(): + """returns PackageLoader initialized from templates""" + return loaders.PackageLoader("res", "templates") + + +@pytest.fixture +def filesystem_loader(): + """returns FileSystemLoader initialized to res/templates directory""" + here = Path(__file__).parent.resolve() + return loaders.FileSystemLoader(here / "res" / "templates") + + +@pytest.fixture +def function_loader(): + """returns a FunctionLoader""" + return loaders.FunctionLoader({"justfunction.html": "FOO"}.get) + + +@pytest.fixture +def choice_loader(dict_loader, package_loader): + """returns a ChoiceLoader""" + return loaders.ChoiceLoader([dict_loader, package_loader]) + + +@pytest.fixture +def prefix_loader(filesystem_loader, dict_loader): + """returns a PrefixLoader""" + return loaders.PrefixLoader({"a": filesystem_loader, "b": dict_loader}) diff --git a/tests/res/__init__.py b/tests/res/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/res/package.zip b/tests/res/package.zip new file mode 100644 index 0000000..d4c9ce9 Binary files /dev/null and b/tests/res/package.zip differ diff --git a/tests/res/templates/broken.html b/tests/res/templates/broken.html new file mode 100644 index 0000000..77669fa --- /dev/null +++ b/tests/res/templates/broken.html @@ -0,0 +1,3 @@ +Before +{{ fail() }} +After diff --git a/tests/res/templates/foo/test.html b/tests/res/templates/foo/test.html new file mode 100644 index 0000000..b7d6715 --- /dev/null +++ b/tests/res/templates/foo/test.html @@ -0,0 +1 @@ +FOO diff --git a/tests/res/templates/mojibake.txt b/tests/res/templates/mojibake.txt new file mode 100644 index 0000000..4b94aa6 --- /dev/null +++ b/tests/res/templates/mojibake.txt @@ -0,0 +1 @@ +文字化け diff --git a/tests/res/templates/syntaxerror.html b/tests/res/templates/syntaxerror.html new file mode 100644 index 0000000..f21b817 --- /dev/null +++ b/tests/res/templates/syntaxerror.html @@ -0,0 +1,4 @@ +Foo +{% for item in broken %} + ... +{% endif %} diff --git a/tests/res/templates/test.html b/tests/res/templates/test.html new file mode 100644 index 0000000..ba578e4 --- /dev/null +++ b/tests/res/templates/test.html @@ -0,0 +1 @@ +BAR diff --git a/tests/res/templates2/foo b/tests/res/templates2/foo new file mode 100644 index 0000000..1c4ad3e --- /dev/null +++ b/tests/res/templates2/foo @@ -0,0 +1,2 @@ +Looks like the start of templates/foo/test.html +Tested by test_filesystem_loader_overlapping_names diff --git a/tests/test_api.py b/tests/test_api.py new file mode 100644 index 0000000..4db3b4a --- /dev/null +++ b/tests/test_api.py @@ -0,0 +1,434 @@ +import shutil +import tempfile +from pathlib import Path + +import pytest + +from jinja2 import ChainableUndefined +from jinja2 import DebugUndefined +from jinja2 import DictLoader +from jinja2 import Environment +from jinja2 import is_undefined +from jinja2 import make_logging_undefined +from jinja2 import meta +from jinja2 import StrictUndefined +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.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(): + tmpl = env.from_string("{{ foo.items()|list }}") + assert tmpl.render(foo={"items": 42}) == "[('items', 42)]" + tmpl = env.from_string('{{ foo|attr("items")()|list }}') + assert tmpl.render(foo={"items": 42}) == "[('items', 42)]" + 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 }}>") + assert t.render() == "<>" + + def test_no_finalize_template_data(self): + e = Environment(finalize=lambda v: type(v).__name__) + t = e.from_string("<{{ value }}>") + # If template data was finalized, it would print "strintstr". + assert t.render(value=123) == "" + + def test_context_finalize(self): + @pass_context + def finalize(context, value): + return value * context["scale"] + + e = Environment(finalize=finalize) + t = e.from_string("{{ value }}") + assert t.render(value=5, scale=3) == "15" + + def test_eval_finalize(self): + @pass_eval_context + def finalize(eval_ctx, value): + return str(eval_ctx.autoescape) + value + + e = Environment(finalize=finalize, autoescape=True) + t = e.from_string("{{ value }}") + assert t.render(value="