summaryrefslogtreecommitdiff
path: root/django/contrib/markup/tests.py
blob: 6903dd7d85d6357e0b3fc3f8594180309663d8a5 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Quick tests for the markup templatetags (django.contrib.markup)
import re

from django.template import Template, Context, add_to_builtins
from django.utils import unittest
from django.utils.html import escape

add_to_builtins('django.contrib.markup.templatetags.markup')

try:
    import textile
except ImportError:
    textile = None

try:
    import markdown
except ImportError:
    markdown = None

try:
    import docutils
except ImportError:
    docutils = None

class Templates(unittest.TestCase):

    textile_content = """Paragraph 1

Paragraph 2 with "quotes" and @code@"""

    markdown_content = """Paragraph 1

## An h2"""

    rest_content = """Paragraph 1

Paragraph 2 with a link_

.. _link: http://www.example.com/"""


    @unittest.skipUnless(textile, 'texttile not installed')
    def test_textile(self):
        t = Template("{{ textile_content|textile }}")
        rendered = t.render(Context({'textile_content':self.textile_content})).strip()
        self.assertEqual(rendered.replace('\t', ''), """<p>Paragraph 1</p>

<p>Paragraph 2 with &#8220;quotes&#8221; and <code>code</code></p>""")

    @unittest.skipIf(textile, 'texttile is installed')
    def test_no_textile(self):
        t = Template("{{ textile_content|textile }}")
        rendered = t.render(Context({'textile_content':self.textile_content})).strip()
        self.assertEqual(rendered, escape(self.textile_content))

    @unittest.skipUnless(markdown, 'markdown not installed')
    def test_markdown(self):
        t = Template("{{ markdown_content|markdown }}")
        rendered = t.render(Context({'markdown_content':self.markdown_content})).strip()
        pattern = re.compile("""<p>Paragraph 1\s*</p>\s*<h2>\s*An h2</h2>""")
        self.assertTrue(pattern.match(rendered))

    @unittest.skipUnless(markdown, 'markdown no installed')
    def test_markdown_attribute_disable(self):
        t = Template("{% load markup %}{{ markdown_content|markdown:'safe' }}")
        markdown_content = "{@onclick=alert('hi')}some paragraph"
        rendered = t.render(Context({'markdown_content':markdown_content})).strip()
        self.assertTrue('@' in rendered)

    @unittest.skipUnless(markdown, 'markdown no installed')
    def test_markdown_attribute_enable(self):
        t = Template("{% load markup %}{{ markdown_content|markdown }}")
        markdown_content = "{@onclick=alert('hi')}some paragraph"
        rendered = t.render(Context({'markdown_content':markdown_content})).strip()
        self.assertFalse('@' in rendered)

    @unittest.skipIf(markdown, 'markdown is installed')
    def test_no_markdown(self):
        t = Template("{{ markdown_content|markdown }}")
        rendered = t.render(Context({'markdown_content':self.markdown_content})).strip()
        self.assertEqual(rendered, self.markdown_content)

    @unittest.skipUnless(docutils, 'docutils not installed')
    def test_docutils(self):
        t = Template("{{ rest_content|restructuredtext }}")
        rendered = t.render(Context({'rest_content':self.rest_content})).strip()
        # Different versions of docutils return slightly different HTML
        try:
            # Docutils v0.4 and earlier
            self.assertEqual(rendered, """<p>Paragraph 1</p>
<p>Paragraph 2 with a <a class="reference" href="http://www.example.com/">link</a></p>""")
        except AssertionError, e:
            # Docutils from SVN (which will become 0.5)
            self.assertEqual(rendered, """<p>Paragraph 1</p>
<p>Paragraph 2 with a <a class="reference external" href="http://www.example.com/">link</a></p>""")

    @unittest.skipIf(docutils, 'docutils is installed')
    def test_no_docutils(self):
        t = Template("{{ rest_content|restructuredtext }}")
        rendered = t.render(Context({'rest_content':self.rest_content})).strip()
        self.assertEqual(rendered, self.rest_content)


if __name__ == '__main__':
    unittest.main()