""" Python-Markdown Extension Regression Tests ========================================== A collection of regression tests to confirm that the included extensions continue to work as advertised. This used to be accomplished by doctests. """ from __future__ import unicode_literals import unittest import markdown class TestAbbr(unittest.TestCase): """ Test abbr extension. """ def setUp(self): self.md = markdown.Markdown(extensions=['abbr']) def testSimpleAbbr(self): """ Test Abbreviations. """ text = 'Some text with an ABBR and a REF. Ignore REFERENCE and ref.' + \ '\n\n*[ABBR]: Abbreviation\n' + \ '*[REF]: Abbreviation Reference' self.assertEqual(self.md.convert(text), '
Some text with an ABBR ' 'and a REF. Ignore ' 'REFERENCE and ref.
') class TestCodeHilite(unittest.TestCase): """ Test codehilite extension. """ def setUp(self): self.md = markdown.Markdown(extensions=['codehilite']) self.has_pygments = True try: import pygments except ImportError: self.has_pygments = False def testBasicCodeHilite(self): text = '\t# A Code Comment' if self.has_pygments: self.assertEqual(self.md.convert(text), '# A Code Comment\n'
'
# A Code Comment'
'
')
class TestFencedCode(unittest.TestCase):
""" Test fenced_code extension. """
def setUp(self):
self.md = markdown.Markdown(extensions=['fenced_code'])
def testBasicFence(self):
""" Test Fenced Code Blocks. """
text = '''
A paragraph before a fenced code block:
~~~
Fenced code block
~~~'''
self.assertEqual(self.md.convert(text),
'A paragraph before a fenced code block:
\n' 'Fenced code block\n'
'
')
def testSafeFence(self):
""" Test Fenced Code with safe_mode. """
text = '~~~\nCode\n~~~'
self.md.safeMode = 'replace'
self.assertEqual(self.md.convert(text),
'Code\n'
'
')
def testNestedFence(self):
""" Test nested fence. """
text = '''
~~~~~~~~
~~~~
~~~~~~~~'''
self.assertEqual(self.md.convert(text),
'\n'
'~~~~\n'
'
')
def testFencedLanguage(self):
""" Test Language Tags. """
text = '''
~~~~{.python}
# Some python code
~~~~'''
self.assertEqual(self.md.convert(text),
'# Some python code\n'
'
')
def testFencedBackticks(self):
""" Test Code Fenced with Backticks. """
text = '''
`````
# Arbitrary code
~~~~~ # these tildes will not close the block
`````'''
self.assertEqual(self.md.convert(text),
'# Arbitrary code\n'
'~~~~~ # these tildes will not close the block\n'
'
')
class TestHeaderId(unittest.TestCase):
""" Test HeaderId Extension. """
def setUp(self):
self.md = markdown.Markdown(extensions=['headerid'])
def testBasicHeaderId(self):
""" Test Basic HeaderID """
text = "# Some Header #"
self.assertEqual(self.md.convert(text),
'The body. This is paragraph one.
') self.assertEqual(self.md.Meta, {'author': ['Waylan Limberg', 'John Doe'], 'blank_data': [''], 'title': ['A Test Doc.']}) def testMissingMetaData(self): """ Test document without Meta Data. """ text = ' Some Code - not extra lines of meta data.' self.assertEqual(self.md.convert(text), 'Some Code - not extra lines of meta data.\n'
'
')
self.assertEqual(self.md.Meta, {})
class TestWikiLinks(unittest.TestCase):
""" Test Wikilinks Extension. """
def setUp(self):
self.md = markdown.Markdown(extensions=['wikilinks'])
self.text = "Some text with a [[WikiLink]]."
def testBasicWikilinks(self):
""" Test [[wikilinks]]. """
self.assertEqual(self.md.convert(self.text),
'Some text with a ' 'WikiLink.
') def testWikilinkWhitespace(self): """ Test whitespace in wikilinks. """ self.assertEqual(self.md.convert('[[ foo bar_baz ]]'), '') self.assertEqual(self.md.convert('foo [[ ]] bar'), 'foo bar
') def testSimpleSettings(self): """ Test Simple Settings. """ self.assertEqual(markdown.markdown(self.text, ['wikilinks(base_url=/wiki/,end_url=.html,html_class=foo)']), 'Some text with a ' 'WikiLink.
') def testComplexSettings(self): """ Test Complex Settings. """ md = markdown.Markdown( extensions = ['wikilinks'], extension_configs = {'wikilinks': [ ('base_url', 'http://example.com/'), ('end_url', '.html'), ('html_class', '') ]}, safe_mode = True) self.assertEqual(md.convert(self.text), 'Some text with a ' 'WikiLink.
') def testWikilinksMetaData(self): """ test MetaData with Wikilinks Extension. """ text = """wiki_base_url: http://example.com/ wiki_end_url: .html wiki_html_class: Some text with a [[WikiLink]].""" md = markdown.Markdown(extensions=['meta', 'wikilinks']) self.assertEqual(md.convert(text), 'Some text with a ' 'WikiLink.
') # MetaData should not carry over to next document: self.assertEqual(md.convert("No [[MetaData]] here."), 'No MetaData ' 'here.
') def testURLCallback(self): """ Test used of a custom URL builder. """ def my_url_builder(label, base, end): return '/bar/' md = markdown.Markdown(extensions=['wikilinks'], extension_configs={'wikilinks' : [('build_url', my_url_builder)]}) self.assertEqual(md.convert('[[foo]]'), '') class TestAdmonition(unittest.TestCase): """ Test Admonition Extension. """ def setUp(self): self.md = markdown.Markdown(extensions=['admonition']) def testRE(self): RE = self.md.parser.blockprocessors['admonition'].RE tests = [ ('!!! note', ('note', None)), ('!!! note "Please Note"', ('note', 'Please Note')), ('!!! note ""', ('note', '')), ] for test, expected in tests: self.assertEqual(RE.match(test).groups(), expected)