regression-tests.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #!/usr/bin/python
  2. """
  3. Python-Markdown Regression Tests
  4. ================================
  5. Tests of the various APIs with the python markdown lib.
  6. """
  7. import unittest
  8. from doctest import DocTestSuite
  9. import os
  10. import markdown
  11. class TestMarkdown(unittest.TestCase):
  12. """ Tests basics of the Markdown class. """
  13. def setUp(self):
  14. """ Create instance of Markdown. """
  15. self.md = markdown.Markdown()
  16. def testBlankInput(self):
  17. """ Test blank input. """
  18. self.assertEqual(self.md.convert(''), '')
  19. def testWhitespaceOnly(self):
  20. """ Test input of only whitespace. """
  21. self.assertEqual(self.md.convert(' '), '')
  22. def testSimpleInput(self):
  23. """ Test simple input. """
  24. self.assertEqual(self.md.convert('foo'), '<p>foo</p>')
  25. class TestBlockParser(unittest.TestCase):
  26. """ Tests of the BlockParser class. """
  27. def setUp(self):
  28. """ Create instance of BlockParser. """
  29. self.parser = markdown.Markdown().parser
  30. def testParseChunk(self):
  31. """ Test BlockParser.parseChunk. """
  32. root = markdown.etree.Element("div")
  33. text = 'foo'
  34. self.parser.parseChunk(root, text)
  35. self.assertEqual(markdown.etree.tostring(root), "<div><p>foo</p></div>")
  36. def testParseDocument(self):
  37. """ Test BlockParser.parseDocument. """
  38. lines = ['#foo', '', 'bar', '', ' baz']
  39. tree = self.parser.parseDocument(lines)
  40. self.assert_(isinstance(tree, markdown.etree.ElementTree))
  41. self.assert_(markdown.etree.iselement(tree.getroot()))
  42. self.assertEqual(markdown.etree.tostring(tree.getroot()),
  43. "<div><h1>foo</h1><p>bar</p><pre><code>baz\n</code></pre></div>")
  44. class TestBlockParserState(unittest.TestCase):
  45. """ Tests of the State class for BlockParser. """
  46. def setUp(self):
  47. self.state = markdown.blockparser.State()
  48. def testBlankState(self):
  49. """ Test State when empty. """
  50. self.assertEqual(self.state, [])
  51. def testSetSate(self):
  52. """ Test State.set(). """
  53. self.state.set('a_state')
  54. self.assertEqual(self.state, ['a_state'])
  55. self.state.set('state2')
  56. self.assertEqual(self.state, ['a_state', 'state2'])
  57. def testIsSate(self):
  58. """ Test State.isstate(). """
  59. self.assertEqual(self.state.isstate('anything'), False)
  60. self.state.set('a_state')
  61. self.assertEqual(self.state.isstate('a_state'), True)
  62. self.state.set('state2')
  63. self.assertEqual(self.state.isstate('state2'), True)
  64. self.assertEqual(self.state.isstate('a_state'), False)
  65. self.assertEqual(self.state.isstate('missing'), False)
  66. def testReset(self):
  67. """ Test State.reset(). """
  68. self.state.set('a_state')
  69. self.state.reset()
  70. self.assertEqual(self.state, [])
  71. self.state.set('state1')
  72. self.state.set('state2')
  73. self.state.reset()
  74. self.assertEqual(self.state, ['state1'])
  75. class TestHtmlStash(unittest.TestCase):
  76. """ Test Markdown's HtmlStash. """
  77. def setUp(self):
  78. self.stash = markdown.preprocessors.HtmlStash()
  79. self.placeholder = self.stash.store('foo')
  80. def testSimpleStore(self):
  81. """ Test HtmlStash.store. """
  82. self.assertEqual(self.placeholder,
  83. markdown.preprocessors.HTML_PLACEHOLDER % 0)
  84. self.assertEqual(self.stash.html_counter, 1)
  85. self.assertEqual(self.stash.rawHtmlBlocks, [('foo', False)])
  86. def testStoreMore(self):
  87. """ Test HtmlStash.store with additional blocks. """
  88. placeholder = self.stash.store('bar')
  89. self.assertEqual(placeholder,
  90. markdown.preprocessors.HTML_PLACEHOLDER % 1)
  91. self.assertEqual(self.stash.html_counter, 2)
  92. self.assertEqual(self.stash.rawHtmlBlocks,
  93. [('foo', False), ('bar', False)])
  94. def testSafeStore(self):
  95. """ Test HtmlStash.store with 'safe' html. """
  96. self.stash.store('bar', True)
  97. self.assertEqual(self.stash.rawHtmlBlocks,
  98. [('foo', False), ('bar', True)])
  99. def testReset(self):
  100. """ Test HtmlStash.reset. """
  101. self.stash.reset()
  102. self.assertEqual(self.stash.html_counter, 0)
  103. self.assertEqual(self.stash.rawHtmlBlocks, [])
  104. class TestOrderedDict(unittest.TestCase):
  105. """ Test OrderedDict storage class. """
  106. def setUp(self):
  107. self.odict = markdown.odict.OrderedDict()
  108. self.odict['first'] = 'This'
  109. self.odict['third'] = 'a'
  110. self.odict['fourth'] = 'self'
  111. self.odict['fifth'] = 'test'
  112. def testValues(self):
  113. """ Test output of OrderedDict.values(). """
  114. self.assertEqual(self.odict.values(), ['This', 'a', 'self', 'test'])
  115. def testKeys(self):
  116. """ Test output of OrderedDict.keys(). """
  117. self.assertEqual(self.odict.keys(),
  118. ['first', 'third', 'fourth', 'fifth'])
  119. def testItems(self):
  120. """ Test output of OrderedDict.items(). """
  121. self.assertEqual(self.odict.items(),
  122. [('first', 'This'), ('third', 'a'),
  123. ('fourth', 'self'), ('fifth', 'test')])
  124. def testAddBefore(self):
  125. """ Test adding an OrderedDict item before a given key. """
  126. self.odict.add('second', 'is', '<third')
  127. self.assertEqual(self.odict.items(),
  128. [('first', 'This'), ('second', 'is'), ('third', 'a'),
  129. ('fourth', 'self'), ('fifth', 'test')])
  130. def testAddAfter(self):
  131. """ Test adding an OrderDict item after a given key. """
  132. self.odict.add('second', 'is', '>first')
  133. self.assertEqual(self.odict.items(),
  134. [('first', 'This'), ('second', 'is'), ('third', 'a'),
  135. ('fourth', 'self'), ('fifth', 'test')])
  136. def testAddAfterEnd(self):
  137. """ Test adding an OrderedDict item after the last key. """
  138. self.odict.add('sixth', '.', '>fifth')
  139. self.assertEqual(self.odict.items(),
  140. [('first', 'This'), ('third', 'a'),
  141. ('fourth', 'self'), ('fifth', 'test'), ('sixth', '.')])
  142. def testAdd_begin(self):
  143. """ Test adding an OrderedDict item using "_begin". """
  144. self.odict.add('zero', 'CRAZY', '_begin')
  145. self.assertEqual(self.odict.items(),
  146. [('zero', 'CRAZY'), ('first', 'This'), ('third', 'a'),
  147. ('fourth', 'self'), ('fifth', 'test')])
  148. def testAdd_end(self):
  149. """ Test adding an OrderedDict item using "_end". """
  150. self.odict.add('sixth', '.', '_end')
  151. self.assertEqual(self.odict.items(),
  152. [('first', 'This'), ('third', 'a'),
  153. ('fourth', 'self'), ('fifth', 'test'), ('sixth', '.')])
  154. def testAddBadLocation(self):
  155. """ Test Error on bad location in OrderedDict.add(). """
  156. self.assertRaises(ValueError, self.odict.add, 'sixth', '.', '<seventh')
  157. self.assertRaises(ValueError, self.odict.add, 'second', 'is', 'third')
  158. def testDeleteItem(self):
  159. """ Test deletion of an OrderedDict item. """
  160. del self.odict['fourth']
  161. self.assertEqual(self.odict.items(),
  162. [('first', 'This'), ('third', 'a'), ('fifth', 'test')])
  163. def testChangeValue(self):
  164. """ Test OrderedDict change value. """
  165. self.odict['fourth'] = 'CRAZY'
  166. self.assertEqual(self.odict.items(),
  167. [('first', 'This'), ('third', 'a'),
  168. ('fourth', 'CRAZY'), ('fifth', 'test')])
  169. def testChangeOrder(self):
  170. """ Test OrderedDict change order. """
  171. self.odict.link('fourth', '<third')
  172. self.assertEqual(self.odict.items(),
  173. [('first', 'This'), ('fourth', 'self'),
  174. ('third', 'a'), ('fifth', 'test')])
  175. def suite():
  176. """ Build a test suite of the above tests and extension doctests. """
  177. suite = unittest.TestSuite()
  178. suite.addTest(unittest.makeSuite(TestMarkdown))
  179. suite.addTest(unittest.makeSuite(TestBlockParser))
  180. suite.addTest(unittest.makeSuite(TestBlockParserState))
  181. suite.addTest(unittest.makeSuite(TestHtmlStash))
  182. suite.addTest(unittest.makeSuite(TestOrderedDict))
  183. for filename in os.listdir('markdown/extensions'):
  184. if filename.endswith('.py'):
  185. module = 'markdown.extensions.%s' % filename[:-3]
  186. try:
  187. suite.addTest(DocTestSuite(module))
  188. except: ValueError
  189. # No tests
  190. return suite
  191. if __name__ == '__main__':
  192. unittest.TextTestRunner(verbosity=2).run(suite())