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