PageRenderTime 52ms CodeModel.GetById 46ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/markup/tests.py

https://code.google.com/p/mango-py/
Python | 91 lines | 83 code | 7 blank | 1 comment | 0 complexity | 7ce0a6c0feff727e0652befcafb7c79c MD5 | raw file
 1# Quick tests for the markup templatetags (django.contrib.markup)
 2import re
 3
 4from django.template import Template, Context, add_to_builtins
 5from django.utils import unittest
 6from django.utils.html import escape
 7
 8add_to_builtins('django.contrib.markup.templatetags.markup')
 9
10try:
11    import textile
12except ImportError:
13    textile = None
14
15try:
16    import markdown
17except ImportError:
18    markdown = None
19
20try:
21    import docutils
22except ImportError:
23    docutils = None
24
25class Templates(unittest.TestCase):
26
27    textile_content = """Paragraph 1
28
29Paragraph 2 with "quotes" and @code@"""
30
31    markdown_content = """Paragraph 1
32
33## An h2"""
34
35    rest_content = """Paragraph 1
36
37Paragraph 2 with a link_
38
39.. _link: http://www.example.com/"""
40
41
42    @unittest.skipUnless(textile, 'texttile not installed')
43    def test_textile(self):
44        t = Template("{{ textile_content|textile }}")
45        rendered = t.render(Context({'textile_content':self.textile_content})).strip()
46        self.assertEqual(rendered.replace('\t', ''), """<p>Paragraph 1</p>
47
48<p>Paragraph 2 with &#8220;quotes&#8221; and <code>code</code></p>""")
49
50    @unittest.skipIf(textile, 'texttile is installed')
51    def test_no_textile(self):
52        t = Template("{{ textile_content|textile }}")
53        rendered = t.render(Context({'textile_content':self.textile_content})).strip()
54        self.assertEqual(rendered, escape(self.textile_content))
55
56    @unittest.skipUnless(markdown, 'markdown not installed')
57    def test_markdown(self):
58        t = Template("{{ markdown_content|markdown }}")
59        rendered = t.render(Context({'markdown_content':self.markdown_content})).strip()
60        pattern = re.compile("""<p>Paragraph 1\s*</p>\s*<h2>\s*An h2</h2>""")
61        self.assertTrue(pattern.match(rendered))
62
63    @unittest.skipIf(markdown, 'markdown is installed')
64    def test_no_markdown(self):
65        t = Template("{{ markdown_content|markdown }}")
66        rendered = t.render(Context({'markdown_content':self.markdown_content})).strip()
67        self.assertEqual(rendered, self.markdown_content)
68
69    @unittest.skipUnless(docutils, 'docutils not installed')
70    def test_docutils(self):
71        t = Template("{{ rest_content|restructuredtext }}")
72        rendered = t.render(Context({'rest_content':self.rest_content})).strip()
73        # Different versions of docutils return slightly different HTML
74        try:
75            # Docutils v0.4 and earlier
76            self.assertEqual(rendered, """<p>Paragraph 1</p>
77<p>Paragraph 2 with a <a class="reference" href="http://www.example.com/">link</a></p>""")
78        except AssertionError, e:
79            # Docutils from SVN (which will become 0.5)
80            self.assertEqual(rendered, """<p>Paragraph 1</p>
81<p>Paragraph 2 with a <a class="reference external" href="http://www.example.com/">link</a></p>""")
82
83    @unittest.skipIf(docutils, 'docutils is installed')
84    def test_no_docutils(self):
85        t = Template("{{ rest_content|restructuredtext }}")
86        rendered = t.render(Context({'rest_content':self.rest_content})).strip()
87        self.assertEqual(rendered, self.rest_content)
88
89
90if __name__ == '__main__':
91    unittest.main()