Skip to content

Commit e56f0cd

Browse files
committed
Added assertStartsWith to tests to give better failure messages
The failure printed when self.assertTrue was used with str.startswith did not show the string that was being searched.
1 parent 429cc98 commit e56f0cd

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

tests/test_extensions.py

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
import unittest
1212
import markdown
1313

14+
class TestCaseWithAssertStartsWith(unittest.TestCase):
15+
16+
def assertStartsWith(self, expectedPrefix, text, msg=None):
17+
if not text.startswith(expectedPrefix):
18+
if len(expectedPrefix) + 5 < len(text):
19+
text = text[:len(expectedPrefix) + 5] + '...'
20+
standardMsg = '%s not found at the start of %s' % (repr(expectedPrefix),
21+
repr(text))
22+
self.fail(self._formatMessage(msg, standardMsg))
1423

1524
class TestExtensionClass(unittest.TestCase):
1625
""" Test markdown.extensions.Extension. """
@@ -85,8 +94,7 @@ def testNestedAbbr(self):
8594
'and <em><abbr title="Abreviation">ABBR</abbr></em></p>'
8695
)
8796

88-
89-
class TestCodeHilite(unittest.TestCase):
97+
class TestCodeHilite(TestCaseWithAssertStartsWith):
9098
""" Test codehilite extension. """
9199

92100
def setUp(self):
@@ -101,7 +109,7 @@ def testBasicCodeHilite(self):
101109
md = markdown.Markdown(extensions=['markdown.extensions.codehilite'])
102110
if self.has_pygments:
103111
# Pygments can use random lexer here as we did not specify the language
104-
self.assertTrue(md.convert(text).startswith('<div class="codehilite"><pre>'))
112+
self.assertStartsWith('<div class="codehilite"><pre>', md.convert(text))
105113
else:
106114
self.assertEqual(
107115
md.convert(text),
@@ -117,10 +125,9 @@ def testLinenumsTrue(self):
117125
# Different versions of pygments output slightly different markup.
118126
# So we use 'startwith' and test just enough to confirm that
119127
# pygments received and processed linenums.
120-
self.assertTrue(
121-
md.convert(text).startswith(
122-
'<table class="codehilitetable"><tr><td class="linenos">'
123-
)
128+
self.assertStartsWith(
129+
'<table class="codehilitetable"><tr><td class="linenos">',
130+
md.convert(text)
124131
)
125132
else:
126133
self.assertEqual(
@@ -134,7 +141,7 @@ def testLinenumsFalse(self):
134141
md = markdown.Markdown(
135142
extensions=[markdown.extensions.codehilite.CodeHiliteExtension(linenums=False)])
136143
if self.has_pygments:
137-
self.assertTrue(md.convert(text).startswith('<div class="codehilite"><pre><span'))
144+
self.assertStartsWith('<div class="codehilite"><pre><span', md.convert(text))
138145
else:
139146
self.assertEqual(
140147
md.convert(text),
@@ -148,7 +155,7 @@ def testLinenumsNone(self):
148155
extensions=[markdown.extensions.codehilite.CodeHiliteExtension(linenums=None)])
149156
if self.has_pygments:
150157
# Pygments can use random lexer here as we did not specify the language
151-
self.assertTrue(md.convert(text).startswith('<div class="codehilite"><pre>'))
158+
self.assertStartsWith('<div class="codehilite"><pre>', md.convert(text))
152159
else:
153160
self.assertEqual(
154161
md.convert(text),
@@ -164,10 +171,9 @@ def testLinenumsNoneWithShebang(self):
164171
# Differant versions of pygments output slightly different markup.
165172
# So we use 'startwith' and test just enough to confirm that
166173
# pygments received and processed linenums.
167-
self.assertTrue(
168-
md.convert(text).startswith(
169-
'<table class="codehilitetable"><tr><td class="linenos">'
170-
)
174+
self.assertStartsWith(
175+
'<table class="codehilitetable"><tr><td class="linenos">',
176+
md.convert(text)
171177
)
172178
else:
173179
self.assertEqual(
@@ -182,7 +188,7 @@ def testLinenumsNoneWithColon(self):
182188
extensions=[markdown.extensions.codehilite.CodeHiliteExtension(linenums=None)]
183189
)
184190
if self.has_pygments:
185-
self.assertTrue(md.convert(text).startswith('<div class="codehilite"><pre><span'))
191+
self.assertStartsWith('<div class="codehilite"><pre><span', md.convert(text))
186192
else:
187193
self.assertEqual(
188194
md.convert(text),
@@ -198,10 +204,9 @@ def testHighlightLinesWithColon(self):
198204
for text in (text0, text1):
199205
md = markdown.Markdown(extensions=['markdown.extensions.codehilite'])
200206
if self.has_pygments:
201-
self.assertTrue(
202-
md.convert(text).startswith(
203-
'<div class="codehilite"><pre><span class="hll"'
204-
)
207+
self.assertStartsWith(
208+
'<div class="codehilite"><pre><span class="hll"',
209+
md.convert(text)
205210
)
206211
else:
207212
self.assertEqual(
@@ -224,7 +229,7 @@ def testUsePygmentsFalse(self):
224229
)
225230

226231

227-
class TestFencedCode(unittest.TestCase):
232+
class TestFencedCode(TestCaseWithAssertStartsWith):
228233
""" Test fenced_code extension. """
229234

230235
def setUp(self):
@@ -320,8 +325,9 @@ def testFencedCodeWithHighlightLines(self):
320325
)
321326

322327
if self.has_pygments:
323-
self.assertTrue(
324-
md.convert(text).startswith('<div class="codehilite"><pre><span class="hll"')
328+
self.assertStartsWith(
329+
'<div class="codehilite"><pre><span class="hll"',
330+
md.convert(text)
325331
)
326332
else:
327333
self.assertEqual(
@@ -354,8 +360,9 @@ def testFencedLanguageAndHighlightLines(self):
354360
]
355361
)
356362
if self.has_pygments:
357-
self.assertTrue(
358-
md.convert(text).startswith('<div class="codehilite"><pre><span class="hll"')
363+
self.assertStartsWith(
364+
'<div class="codehilite"><pre><span class="hll"',
365+
md.convert(text)
359366
)
360367
else:
361368
self.assertEqual(
@@ -602,7 +609,7 @@ def testRE(self):
602609
self.assertEqual(RE.match(test).groups(), expected)
603610

604611

605-
class TestTOC(unittest.TestCase):
612+
class TestTOC(TestCaseWithAssertStartsWith):
606613
""" Test TOC Extension. """
607614

608615
def setUp(self):
@@ -680,13 +687,13 @@ def testDisabledMarker(self):
680687
'<h1 id="header-1">Header 1</h1>\n'
681688
'<h2 id="header-2">Header 2</h2>'
682689
)
683-
self.assertTrue(md.toc.startswith('<div class="toc">'))
690+
self.assertStartsWith('<div class="toc">', md.toc)
684691

685692
def testReset(self):
686693
""" Test TOC Reset. """
687694
self.assertEqual(self.md.toc, '')
688695
self.md.convert('# Header 1\n\n## Header 2')
689-
self.assertTrue(self.md.toc.startswith('<div class="toc">'))
696+
self.assertStartsWith('<div class="toc">', self.md.toc)
690697
self.md.reset()
691698
self.assertEqual(self.md.toc, '')
692699

@@ -771,7 +778,10 @@ def testTitle(self):
771778
extensions=[markdown.extensions.toc.TocExtension(title='Table of Contents')]
772779
)
773780
md.convert('# Header 1\n\n## Header 2')
774-
self.assertTrue(md.toc.startswith('<div class="toc"><span class="toctitle">Table of Contents</span><ul>'))
781+
self.assertStartsWith(
782+
'<div class="toc"><span class="toctitle">Table of Contents</span><ul>',
783+
md.toc
784+
)
775785

776786
def testWithAttrList(self):
777787
""" Test TOC with attr_list Extension. """

0 commit comments

Comments
 (0)