""" 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 TestExtensionClass(unittest.TestCase): """ Test markdown.extensions.Extension. """ def setUp(self): class TestExtension(markdown.extensions.Extension): config = { 'foo': ['bar', 'Description of foo'], 'bar': ['baz', 'Description of bar'] } self.ext = TestExtension() self.ExtKlass = TestExtension def testGetConfig(self): self.assertEqual(self.ext.getConfig('foo'), 'bar') def testGetConfigDefault(self): self.assertEqual(self.ext.getConfig('baz'), '') self.assertEqual(self.ext.getConfig('baz', default='missing'), 'missing') def testGetConfigs(self): self.assertEqual(self.ext.getConfigs(), {'foo': 'bar', 'bar': 'baz'}) def testGetConfigInfo(self): self.assertEqual(dict(self.ext.getConfigInfo()), dict([('foo', 'Description of foo'), ('bar', 'Description of bar')]) def testSetConfig(self): self.ext.setConfig('foo', 'baz') self.assertEqual(self.ext.getConfigs(), {'foo': 'baz', 'bar': 'baz'}) def testSetConfigWithBadKey(self): # self.ext.setConfig('bad', 'baz) ==> KeyError self.assertRaises(KeyError, self.ext.setConfig, 'bad', 'baz') def testConfigAsArgListOnInit(self): ext = self.ExtKlass([('foo', 'baz'), ('bar', 'blah')]) self.assertEqual(ext.getConfigs(), {'foo': 'baz', 'bar': 'blah'}) def testConfigAsArgDictOnInit(self): ext = self.ExtKlass({'foo': 'baz', 'bar': 'blah', 'bar': 'blah'}) self.assertEqual(ext.getConfigs(), {'foo': 'baz', 'bar': 'blah'}) def testConfigAsKwargListOnInit(self): ext = self.ExtKlass(configs=[('foo', 'baz'), ('bar', 'blah')]) self.assertEqual(ext.getConfigs(), {'foo': 'baz', 'bar': 'blah'}) def testConfigAsKwargDictOnInit(self): ext = self.ExtKlass(configs={'foo': 'baz', 'bar': 'blah'}) self.assertEqual(ext.getConfigs(), {'foo': 'baz', 'bar': 'blah'}) def testConfigAsKwargsOnInit(self): ext = self.ExtKlass(foo='baz', bar='blah') self.assertEqual(ext.getConfigs(), {'foo': 'baz', 'bar': 'blah'}) 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.
') def testNestedAbbr(self): """ Test Nested Abbreviations. """ text = '[ABBR](/foo) and _ABBR_\n\n' + \ '*[ABBR]: Abreviation' self.assertEqual(self.md.convert(text), 'ABBR ' 'and ABBR
') class TestCodeHilite(unittest.TestCase): """ Test codehilite extension. """ def setUp(self): self.has_pygments = True try: import pygments except ImportError: self.has_pygments = False def testBasicCodeHilite(self): text = '\t# A Code Comment' md = markdown.Markdown(extensions=['codehilite']) if self.has_pygments: self.assertEqual(md.convert(text), '# A Code Comment\n'
'
# A Code Comment'
'
')
def testLinenumsTrue(self):
text = '\t# A Code Comment'
md = markdown.Markdown(extensions=['codehilite(linenums=True)'])
if self.has_pygments:
# Differant versions of pygments output slightly different markup.
# So we use 'startwith' and test just enough to confirm that
# pygments received and processed linenums.
self.assertTrue(md.convert(text).startswith(
''))
else:
self.assertEqual(md.convert(text),
' ')
def testLinenumsFalse(self):
text = '\t#!Python\n\t# A Code Comment'
md = markdown.Markdown(extensions=['codehilite(linenums=False)'])
if self.has_pygments:
self.assertEqual(md.convert(text),
''
' ')
else:
self.assertEqual(md.convert(text),
'# A Code Comment\n'
' ')
def testLinenumsNone(self):
text = '\t# A Code Comment'
md = markdown.Markdown(extensions=['codehilite(linenums=None)'])
if self.has_pygments:
self.assertEqual(md.convert(text),
''
' ')
else:
self.assertEqual(md.convert(text),
'# A Code Comment\n'
' ')
def testLinenumsNoneWithShebang(self):
text = '\t#!Python\n\t# A Code Comment'
md = markdown.Markdown(extensions=['codehilite(linenums=None)'])
if self.has_pygments:
# Differant versions of pygments output slightly different markup.
# So we use 'startwith' and test just enough to confirm that
# pygments received and processed linenums.
self.assertTrue(md.convert(text).startswith(
'
|