1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
#!/usr/bin/python
"""
Python-Markdown Regression Tests
================================
Tests of the various APIs with the python markdown lib.
"""
import unittest
from doctest import DocTestSuite
import os
import sys
import types
import markdown
from markdown.md_logging import MarkdownException, MarkdownWarning
from logging import DEBUG, INFO, WARN, ERROR, CRITICAL
import warnings
class TestMarkdownBasics(unittest.TestCase):
""" Tests basics of the Markdown class. """
def setUp(self):
""" Create instance of Markdown. """
self.md = markdown.Markdown()
def testBlankInput(self):
""" Test blank input. """
self.assertEqual(self.md.convert(''), '')
def testWhitespaceOnly(self):
""" Test input of only whitespace. """
self.assertEqual(self.md.convert(' '), '')
def testSimpleInput(self):
""" Test simple input. """
self.assertEqual(self.md.convert('foo'), '<p>foo</p>')
class TestBlockParser(unittest.TestCase):
""" Tests of the BlockParser class. """
def setUp(self):
""" Create instance of BlockParser. """
self.parser = markdown.Markdown().parser
def testParseChunk(self):
""" Test BlockParser.parseChunk. """
root = markdown.etree.Element("div")
text = 'foo'
self.parser.parseChunk(root, text)
self.assertEqual(markdown.etree.tostring(root), "<div><p>foo</p></div>")
def testParseDocument(self):
""" Test BlockParser.parseDocument. """
lines = ['#foo', '', 'bar', '', ' baz']
tree = self.parser.parseDocument(lines)
self.assert_(isinstance(tree, markdown.etree.ElementTree))
self.assert_(markdown.etree.iselement(tree.getroot()))
self.assertEqual(markdown.etree.tostring(tree.getroot()),
"<div><h1>foo</h1><p>bar</p><pre><code>baz\n</code></pre></div>")
class TestBlockParserState(unittest.TestCase):
""" Tests of the State class for BlockParser. """
def setUp(self):
self.state = markdown.blockparser.State()
def testBlankState(self):
""" Test State when empty. """
self.assertEqual(self.state, [])
def testSetSate(self):
""" Test State.set(). """
self.state.set('a_state')
self.assertEqual(self.state, ['a_state'])
self.state.set('state2')
self.assertEqual(self.state, ['a_state', 'state2'])
def testIsSate(self):
""" Test State.isstate(). """
self.assertEqual(self.state.isstate('anything'), False)
self.state.set('a_state')
self.assertEqual(self.state.isstate('a_state'), True)
self.state.set('state2')
self.assertEqual(self.state.isstate('state2'), True)
self.assertEqual(self.state.isstate('a_state'), False)
self.assertEqual(self.state.isstate('missing'), False)
def testReset(self):
""" Test State.reset(). """
self.state.set('a_state')
self.state.reset()
self.assertEqual(self.state, [])
self.state.set('state1')
self.state.set('state2')
self.state.reset()
self.assertEqual(self.state, ['state1'])
class TestHtmlStash(unittest.TestCase):
""" Test Markdown's HtmlStash. """
def setUp(self):
self.stash = markdown.util.HtmlStash()
self.placeholder = self.stash.store('foo')
def testSimpleStore(self):
""" Test HtmlStash.store. """
self.assertEqual(self.placeholder, self.stash.get_placeholder(0))
self.assertEqual(self.stash.html_counter, 1)
self.assertEqual(self.stash.rawHtmlBlocks, [('foo', False)])
def testStoreMore(self):
""" Test HtmlStash.store with additional blocks. """
placeholder = self.stash.store('bar')
self.assertEqual(placeholder, self.stash.get_placeholder(1))
self.assertEqual(self.stash.html_counter, 2)
self.assertEqual(self.stash.rawHtmlBlocks,
[('foo', False), ('bar', False)])
def testSafeStore(self):
""" Test HtmlStash.store with 'safe' html. """
self.stash.store('bar', True)
self.assertEqual(self.stash.rawHtmlBlocks,
[('foo', False), ('bar', True)])
def testReset(self):
""" Test HtmlStash.reset. """
self.stash.reset()
self.assertEqual(self.stash.html_counter, 0)
self.assertEqual(self.stash.rawHtmlBlocks, [])
class TestOrderedDict(unittest.TestCase):
""" Test OrderedDict storage class. """
def setUp(self):
self.odict = markdown.odict.OrderedDict()
self.odict['first'] = 'This'
self.odict['third'] = 'a'
self.odict['fourth'] = 'self'
self.odict['fifth'] = 'test'
def testValues(self):
""" Test output of OrderedDict.values(). """
self.assertEqual(self.odict.values(), ['This', 'a', 'self', 'test'])
def testKeys(self):
""" Test output of OrderedDict.keys(). """
self.assertEqual(self.odict.keys(),
['first', 'third', 'fourth', 'fifth'])
def testItems(self):
""" Test output of OrderedDict.items(). """
self.assertEqual(self.odict.items(),
[('first', 'This'), ('third', 'a'),
('fourth', 'self'), ('fifth', 'test')])
def testAddBefore(self):
""" Test adding an OrderedDict item before a given key. """
self.odict.add('second', 'is', '<third')
self.assertEqual(self.odict.items(),
[('first', 'This'), ('second', 'is'), ('third', 'a'),
('fourth', 'self'), ('fifth', 'test')])
def testAddAfter(self):
""" Test adding an OrderDict item after a given key. """
self.odict.add('second', 'is', '>first')
self.assertEqual(self.odict.items(),
[('first', 'This'), ('second', 'is'), ('third', 'a'),
('fourth', 'self'), ('fifth', 'test')])
def testAddAfterEnd(self):
""" Test adding an OrderedDict item after the last key. """
self.odict.add('sixth', '.', '>fifth')
self.assertEqual(self.odict.items(),
[('first', 'This'), ('third', 'a'),
('fourth', 'self'), ('fifth', 'test'), ('sixth', '.')])
def testAdd_begin(self):
""" Test adding an OrderedDict item using "_begin". """
self.odict.add('zero', 'CRAZY', '_begin')
self.assertEqual(self.odict.items(),
[('zero', 'CRAZY'), ('first', 'This'), ('third', 'a'),
('fourth', 'self'), ('fifth', 'test')])
def testAdd_end(self):
""" Test adding an OrderedDict item using "_end". """
self.odict.add('sixth', '.', '_end')
self.assertEqual(self.odict.items(),
[('first', 'This'), ('third', 'a'),
('fourth', 'self'), ('fifth', 'test'), ('sixth', '.')])
def testAddBadLocation(self):
""" Test Error on bad location in OrderedDict.add(). """
self.assertRaises(ValueError, self.odict.add, 'sixth', '.', '<seventh')
self.assertRaises(ValueError, self.odict.add, 'second', 'is', 'third')
def testDeleteItem(self):
""" Test deletion of an OrderedDict item. """
del self.odict['fourth']
self.assertEqual(self.odict.items(),
[('first', 'This'), ('third', 'a'), ('fifth', 'test')])
def testChangeValue(self):
""" Test OrderedDict change value. """
self.odict['fourth'] = 'CRAZY'
self.assertEqual(self.odict.items(),
[('first', 'This'), ('third', 'a'),
('fourth', 'CRAZY'), ('fifth', 'test')])
def testChangeOrder(self):
""" Test OrderedDict change order. """
self.odict.link('fourth', '<third')
self.assertEqual(self.odict.items(),
[('first', 'This'), ('fourth', 'self'),
('third', 'a'), ('fifth', 'test')])
class TestErrors(unittest.TestCase):
""" Test Error Reporting. """
def setUp(self):
# Set warnings to be raised as errors
warnings.simplefilter('error')
def tearDown(self):
# Reset warning behavior back to default
warnings.simplefilter('default')
def testMessageWarn(self):
""" Test message WARN. """
self.assertRaises(MarkdownWarning, markdown.md_logging.message,
WARN, 'This should raise a MardownWarning')
def testMessageError(self):
""" Test message ERROR. """
self.assertRaises(MarkdownException, markdown.md_logging.message,
ERROR, 'This should raise a MarkdownException')
def testNonUnicodeSource(self):
""" Test falure on non-unicode source text. """
source = "foo".encode('utf-16')
self.assertRaises(MarkdownException, markdown.markdown, source)
def testBadOutputFormat(self):
""" Test failure on bad output_format. """
self.assertRaises(MarkdownException,
markdown.Markdown, output_format='invalid')
def testLoadExtensionFailure(self):
""" Test failure of an extension to load. """
self.assertRaises(MarkdownWarning,
markdown.Markdown, extensions=['non_existant_ext'])
def testLoadBadExtension(self):
""" Test loading of an Extension with no makeExtension function. """
_create_fake_extension(name='fake', has_factory_func=False)
self.assertRaises(MarkdownException,
markdown.Markdown, extensions=['fake'])
def testNonExtension(self):
""" Test loading a non Extension object as an extension. """
_create_fake_extension(name='fake', is_wrong_type=True)
self.assertRaises(MarkdownException,
markdown.Markdown, extensions=['fake'])
def testBaseExtention(self):
""" Test that the base Extension class will raise NotImplemented. """
_create_fake_extension(name='fake')
self.assertRaises(MarkdownException,
markdown.Markdown, extensions=['fake'])
def _create_fake_extension(name, has_factory_func=True, is_wrong_type=False):
""" Create a fake extension module for testing. """
mod_name = '_'.join(['mdx', name])
ext_mod = types.ModuleType(mod_name)
def makeExtension(configs=None):
if is_wrong_type:
return object
else:
return markdown.extensions.Extension(configs=configs)
if has_factory_func:
ext_mod.makeExtension = makeExtension
# Warning: this brute forces the extenson module onto the system. Either
# this needs to be specificly overriden or a new python session needs to
# be started to get rid of this. This should be ok in a testing context.
sys.modules[mod_name] = ext_mod
|