summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDavid Lord <davidism@gmail.com>2021-04-04 17:20:23 -0700
committerDavid Lord <davidism@gmail.com>2021-04-04 17:20:23 -0700
commit6c733602ac1b2d68d9b39acb7b1d96faae6a7717 (patch)
tree5b06d102ff40b6d7eba1d8b1369fa6415ccd60e4 /tests
parentaf5d80e999944b6204e99a07940ae13382909efd (diff)
downloadjinja2-6c733602ac1b2d68d9b39acb7b1d96faae6a7717.tar.gz
add 'is filter' and 'is test' tests
This required allowing tests to be decorated with '@environmentfilter'. Tests are essentially the same as filters now, the node, compiler, and environment have been refactored to extract common behavior.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_filters.py12
-rw-r--r--tests/test_tests.py25
2 files changed, 31 insertions, 6 deletions
diff --git a/tests/test_filters.py b/tests/test_filters.py
index 44be6ad..5a11d3f 100644
--- a/tests/test_filters.py
+++ b/tests/test_filters.py
@@ -778,13 +778,13 @@ class TestFilter:
assert result == "Hello!\nThis is Jinja saying\nsomething."
def test_filter_undefined(self, env):
- with pytest.raises(TemplateAssertionError, match="no filter named 'f'"):
+ with pytest.raises(TemplateAssertionError, match="No filter named 'f'"):
env.from_string("{{ var|f }}")
def test_filter_undefined_in_if(self, env):
t = env.from_string("{%- if x is defined -%}{{ x|f }}{%- else -%}x{% endif %}")
assert t.render() == "x"
- with pytest.raises(TemplateRuntimeError, match="no filter named 'f'"):
+ with pytest.raises(TemplateRuntimeError, match="No filter named 'f'"):
t.render(x=42)
def test_filter_undefined_in_elif(self, env):
@@ -793,7 +793,7 @@ class TestFilter:
"{{ y|f }}{%- else -%}foo{%- endif -%}"
)
assert t.render() == "foo"
- with pytest.raises(TemplateRuntimeError, match="no filter named 'f'"):
+ with pytest.raises(TemplateRuntimeError, match="No filter named 'f'"):
t.render(y=42)
def test_filter_undefined_in_else(self, env):
@@ -801,7 +801,7 @@ class TestFilter:
"{%- if x is not defined -%}foo{%- else -%}{{ x|f }}{%- endif -%}"
)
assert t.render() == "foo"
- with pytest.raises(TemplateRuntimeError, match="no filter named 'f'"):
+ with pytest.raises(TemplateRuntimeError, match="No filter named 'f'"):
t.render(x=42)
def test_filter_undefined_in_nested_if(self, env):
@@ -811,7 +811,7 @@ class TestFilter:
)
assert t.render() == "foo"
assert t.render(x=42) == "42"
- with pytest.raises(TemplateRuntimeError, match="no filter named 'f'"):
+ with pytest.raises(TemplateRuntimeError, match="No filter named 'f'"):
t.render(x=24, y=42)
def test_filter_undefined_in_condexpr(self, env):
@@ -819,6 +819,6 @@ class TestFilter:
t2 = env.from_string("{{ 'foo' if x is not defined else x|f }}")
assert t1.render() == t2.render() == "foo"
- with pytest.raises(TemplateRuntimeError, match="no filter named 'f'"):
+ with pytest.raises(TemplateRuntimeError, match="No filter named 'f'"):
t1.render(x=42)
t2.render(x=42)
diff --git a/tests/test_tests.py b/tests/test_tests.py
index d363653..4d56a15 100644
--- a/tests/test_tests.py
+++ b/tests/test_tests.py
@@ -2,6 +2,8 @@ import pytest
from jinja2 import Environment
from jinja2 import Markup
+from jinja2 import TemplateAssertionError
+from jinja2 import TemplateRuntimeError
class MyDict(dict):
@@ -206,3 +208,26 @@ class TestTestsCase:
'{{ "baz" is in {"bar": 1}}}'
)
assert tmpl.render() == "True|True|False|True|False|True|False|True|False"
+
+
+def test_name_undefined(env):
+ with pytest.raises(TemplateAssertionError, match="No test named 'f'"):
+ env.from_string("{{ x is f }}")
+
+
+def test_name_undefined_in_if(env):
+ t = env.from_string("{% if x is defined %}{{ x is f }}{% endif %}")
+ assert t.render() == ""
+
+ with pytest.raises(TemplateRuntimeError, match="No test named 'f'"):
+ t.render(x=1)
+
+
+def test_is_filter(env):
+ assert env.call_test("filter", "title")
+ assert not env.call_test("filter", "bad-name")
+
+
+def test_is_test(env):
+ assert env.call_test("test", "number")
+ assert not env.call_test("test", "bad-name")