Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/template_tests/filter_tests/test_escape.py
blob: 83bfb36444b0dccd6b47d6da6bf731cc1723bdfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from django.template.defaultfilters import escape
from django.test import SimpleTestCase
from django.utils.safestring import mark_safe

from ..utils import setup


class EscapeTests(SimpleTestCase):
    """
    The "escape" filter works the same whether autoescape is on or off,
    but it has no effect on strings already marked as safe.
    """

    @setup({'escape01': '{{ a|escape }} {{ b|escape }}'})
    def test_escape01(self):
        output = self.engine.render_to_string('escape01', {"a": "x&y", "b": mark_safe("x&y")})
        self.assertEqual(output, "x&y x&y")

    @setup({'escape02': '{% autoescape off %}{{ a|escape }} {{ b|escape }}{% endautoescape %}'})
    def test_escape02(self):
        output = self.engine.render_to_string('escape02', {"a": "x&y", "b": mark_safe("x&y")})
        self.assertEqual(output, "x&y x&y")

    # It is only applied once, regardless of the number of times it
    # appears in a chain.
    @setup({'escape03': '{% autoescape off %}{{ a|escape|escape }}{% endautoescape %}'})
    def test_escape03(self):
        output = self.engine.render_to_string('escape03', {"a": "x&y"})
        self.assertEqual(output, "x&y")

    @setup({'escape04': '{{ a|escape|escape }}'})
    def test_escape04(self):
        output = self.engine.render_to_string('escape04', {"a": "x&y"})
        self.assertEqual(output, "x&y")


class FunctionTests(SimpleTestCase):

    def test_non_string_input(self):
        self.assertEqual(escape(123), '123')