From dac075e9103ba961af4f70b4011616daa72985d4 Mon Sep 17 00:00:00 2001 From: Alasdair Nicol Date: Thu, 28 Apr 2016 22:51:40 +0100 Subject: [PATCH] Refs #26479 -- Documented is/is not if tag operator behavior for nonexistent variables. --- docs/ref/templates/builtins.txt | 21 ++++++++++++++------ tests/template_tests/syntax_tests/test_if.py | 20 +++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt index 40fa5d9e03..6c766c8a14 100644 --- a/docs/ref/templates/builtins.txt +++ b/docs/ref/templates/builtins.txt @@ -523,8 +523,12 @@ Not contained within. This is the negation of the ``in`` operator. Object identity. Tests if two values are the same object. Example:: - {% if value is None %} - This will output if and only if value is None. + {% if somevar is True %} + This appears if and only if somevar is True. + {% endif %} + + {% if somevar is None %} + This appears if somevar is None, or if somevar is not found in the context. {% endif %} ``is not`` operator @@ -532,11 +536,16 @@ Object identity. Tests if two values are the same object. Example:: .. versionadded:: 1.10 -Tests if two values are not the same object. This is the negation of -the ``is`` operator. Example:: +Negated object identity. Tests if two values are not the same object. This is +the negation of the ``is`` operator. Example:: - {% if value is not None %} - This will output if and only if value is not None. + {% if somevar is not True %} + This appears if somevar is not True, or if somevar is not found in the + context. + {% endif %} + + {% if somevar is not None %} + This appears if and only if somevar is not None. {% endif %} Filters diff --git a/tests/template_tests/syntax_tests/test_if.py b/tests/template_tests/syntax_tests/test_if.py index 861a9818d2..703f3d352c 100644 --- a/tests/template_tests/syntax_tests/test_if.py +++ b/tests/template_tests/syntax_tests/test_if.py @@ -564,6 +564,16 @@ class IfTagTests(SimpleTestCase): output = self.engine.render_to_string('template', {'foo': 1}) self.assertEqual(output, 'no') + @setup({'template': '{% if foo is bar %}yes{% else %}no{% endif %}'}) + def test_if_is_variable_missing(self): + output = self.engine.render_to_string('template', {'foo': 1}) + self.assertEqual(output, 'no') + + @setup({'template': '{% if foo is bar %}yes{% else %}no{% endif %}'}) + def test_if_is_both_variables_missing(self): + output = self.engine.render_to_string('template', {}) + self.assertEqual(output, 'yes') + @setup({'template': '{% if foo is not None %}yes{% else %}no{% endif %}'}) def test_if_is_not_match(self): # For this to act as a regression test, it's important not to use @@ -575,3 +585,13 @@ class IfTagTests(SimpleTestCase): def test_if_is_not_no_match(self): output = self.engine.render_to_string('template', {'foo': None}) self.assertEqual(output, 'no') + + @setup({'template': '{% if foo is not bar %}yes{% else %}no{% endif %}'}) + def test_if_is_not_variable_missing(self): + output = self.engine.render_to_string('template', {'foo': False}) + self.assertEqual(output, 'yes') + + @setup({'template': '{% if foo is not bar %}yes{% else %}no{% endif %}'}) + def test_if_is_not_both_variables_missing(self): + output = self.engine.render_to_string('template', {}) + self.assertEqual(output, 'no')