app/django/contrib/markup/tests.py
changeset 54 03e267d67478
equal deleted inserted replaced
53:57b4279d8c4e 54:03e267d67478
       
     1 # Quick tests for the markup templatetags (django.contrib.markup)
       
     2 
       
     3 import re
       
     4 import unittest
       
     5 
       
     6 from django.template import Template, Context, add_to_builtins
       
     7 from django.utils.html import escape
       
     8 
       
     9 add_to_builtins('django.contrib.markup.templatetags.markup')
       
    10 
       
    11 class Templates(unittest.TestCase):
       
    12     def test_textile(self):
       
    13         try:
       
    14             import textile
       
    15         except ImportError:
       
    16             textile = None
       
    17 
       
    18         textile_content = """Paragraph 1
       
    19 
       
    20 Paragraph 2 with "quotes" and @code@"""
       
    21 
       
    22         t = Template("{{ textile_content|textile }}")
       
    23         rendered = t.render(Context(locals())).strip()
       
    24         if textile:
       
    25             self.assertEqual(rendered, """<p>Paragraph 1</p>
       
    26 
       
    27 <p>Paragraph 2 with &#8220;quotes&#8221; and <code>code</code></p>""")
       
    28         else:
       
    29             self.assertEqual(rendered, escape(textile_content))
       
    30 
       
    31     def test_markdown(self):
       
    32         try:
       
    33             import markdown
       
    34         except ImportError:
       
    35             markdown = None
       
    36 
       
    37         markdown_content = """Paragraph 1
       
    38 
       
    39 ## An h2"""
       
    40 
       
    41         t = Template("{{ markdown_content|markdown }}")
       
    42         rendered = t.render(Context(locals())).strip()
       
    43         if markdown:
       
    44             pattern = re.compile("""<p>Paragraph 1\s*</p>\s*<h2>\s*An h2</h2>""")
       
    45             self.assert_(pattern.match(rendered))
       
    46         else:
       
    47             self.assertEqual(rendered, markdown_content)
       
    48 
       
    49     def test_docutils(self):
       
    50         try:
       
    51             import docutils
       
    52         except ImportError:
       
    53             docutils = None
       
    54 
       
    55         rest_content = """Paragraph 1
       
    56 
       
    57 Paragraph 2 with a link_
       
    58 
       
    59 .. _link: http://www.example.com/"""
       
    60 
       
    61         t = Template("{{ rest_content|restructuredtext }}")
       
    62         rendered = t.render(Context(locals())).strip()
       
    63         if docutils:
       
    64             # Different versions of docutils return slightly different HTML
       
    65             try:
       
    66                 # Docutils v0.4 and earlier
       
    67                 self.assertEqual(rendered, """<p>Paragraph 1</p>
       
    68 <p>Paragraph 2 with a <a class="reference" href="http://www.example.com/">link</a></p>""")
       
    69             except AssertionError, e:
       
    70                 # Docutils from SVN (which will become 0.5)
       
    71                 self.assertEqual(rendered, """<p>Paragraph 1</p>
       
    72 <p>Paragraph 2 with a <a class="reference external" href="http://www.example.com/">link</a></p>""")
       
    73         else:
       
    74             self.assertEqual(rendered, rest_content)
       
    75 
       
    76 
       
    77 if __name__ == '__main__':
       
    78     unittest.main()