Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/template_tests/filter_tests/test_wordwrap.py
blob: dc8ab10868a9f4ad1dd63b6f89c232b524f84aba (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
from django.template.defaultfilters import wordwrap
from django.test import SimpleTestCase
from django.utils.safestring import mark_safe

from ..utils import setup


class WordwrapTests(SimpleTestCase):

    @setup({'wordwrap01':
        '{% autoescape off %}{{ a|wordwrap:"3" }} {{ b|wordwrap:"3" }}{% endautoescape %}'})
    def test_wordwrap01(self):
        output = self.engine.render_to_string('wordwrap01', {'a': 'a & b', 'b': mark_safe('a & b')})
        self.assertEqual(output, 'a &\nb a &\nb')

    @setup({'wordwrap02': '{{ a|wordwrap:"3" }} {{ b|wordwrap:"3" }}'})
    def test_wordwrap02(self):
        output = self.engine.render_to_string('wordwrap02', {'a': 'a & b', 'b': mark_safe('a & b')})
        self.assertEqual(output, 'a &\nb a &\nb')


class FunctionTests(SimpleTestCase):

    def test_wrap(self):
        self.assertEqual(
            wordwrap('this is a long paragraph of text that really needs to be wrapped I\'m afraid', 14),
            'this is a long\nparagraph of\ntext that\nreally needs\nto be wrapped\nI\'m afraid',
        )

    def test_indent(self):
        self.assertEqual(
            wordwrap('this is a short paragraph of text.\n  But this line should be indented', 14),
            'this is a\nshort\nparagraph of\ntext.\n  But this\nline should be\nindented',
        )

    def test_indent2(self):
        self.assertEqual(
            wordwrap('this is a short paragraph of text.\n  But this line should be indented', 15),
            'this is a short\nparagraph of\ntext.\n  But this line\nshould be\nindented',
        )

    def test_non_string_input(self):
        self.assertEqual(wordwrap(123, 2), '123')