Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/template_tests/filter_tests/test_linenumbers.py
blob: 36ec8de0d525c3af4ed41831c706e5252f25b87d (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from django.template.defaultfilters import linenumbers
from django.test import SimpleTestCase
from django.utils.safestring import mark_safe

from ..utils import setup


class LinenumbersTests(SimpleTestCase):
    """
    The contents of "linenumbers" is escaped according to the current
    autoescape setting.
    """

    @setup({'linenumbers01': '{{ a|linenumbers }} {{ b|linenumbers }}'})
    def test_linenumbers01(self):
        output = self.engine.render_to_string(
            'linenumbers01',
            {'a': 'one\n<two>\nthree', 'b': mark_safe('one\n&lt;two&gt;\nthree')},
        )
        self.assertEqual(output, '1. one\n2. &lt;two&gt;\n3. three '
                                 '1. one\n2. &lt;two&gt;\n3. three')

    @setup({'linenumbers02':
        '{% autoescape off %}{{ a|linenumbers }} {{ b|linenumbers }}{% endautoescape %}'})
    def test_linenumbers02(self):
        output = self.engine.render_to_string(
            'linenumbers02',
            {'a': 'one\n<two>\nthree', 'b': mark_safe('one\n&lt;two&gt;\nthree')},
        )
        self.assertEqual(output, '1. one\n2. <two>\n3. three '
                                 '1. one\n2. &lt;two&gt;\n3. three')


class FunctionTests(SimpleTestCase):

    def test_linenumbers(self):
        self.assertEqual(linenumbers('line 1\nline 2'), '1. line 1\n2. line 2')

    def test_linenumbers2(self):
        self.assertEqual(
            linenumbers('\n'.join(['x'] * 10)),
            '01. x\n02. x\n03. x\n04. x\n05. x\n06. x\n07. x\n08. x\n09. x\n10. x',
        )

    def test_non_string_input(self):
        self.assertEqual(linenumbers(123), '1. 123')

    def test_autoescape(self):
        self.assertEqual(
            linenumbers('foo\n<a>bar</a>\nbuz'),
            '1. foo\n2. &lt;a&gt;bar&lt;/a&gt;\n3. buz',
        )

    def test_autoescape_off(self):
        self.assertEqual(
            linenumbers('foo\n<a>bar</a>\nbuz', autoescape=False),
            '1. foo\n2. <a>bar</a>\n3. buz'
        )