Skip to content

Commit

Permalink
Added assertStartsWith to tests to give better failure messages
Browse files Browse the repository at this point in the history
The failure printed when self.assertTrue was used with str.startswith
did not show the string that was being searched.
  • Loading branch information
Griffon26 committed Feb 26, 2016
1 parent 429cc98 commit e56f0cd
Showing 1 changed file with 37 additions and 27 deletions.
64 changes: 37 additions & 27 deletions tests/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
import unittest
import markdown

class TestCaseWithAssertStartsWith(unittest.TestCase):

def assertStartsWith(self, expectedPrefix, text, msg=None):
if not text.startswith(expectedPrefix):
if len(expectedPrefix) + 5 < len(text):
text = text[:len(expectedPrefix) + 5] + '...'
standardMsg = '%s not found at the start of %s' % (repr(expectedPrefix),
repr(text))
self.fail(self._formatMessage(msg, standardMsg))

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


class TestCodeHilite(unittest.TestCase):
class TestCodeHilite(TestCaseWithAssertStartsWith):
""" Test codehilite extension. """

def setUp(self):
Expand All @@ -101,7 +109,7 @@ def testBasicCodeHilite(self):
md = markdown.Markdown(extensions=['markdown.extensions.codehilite'])
if self.has_pygments:
# Pygments can use random lexer here as we did not specify the language
self.assertTrue(md.convert(text).startswith('<div class="codehilite"><pre>'))
self.assertStartsWith('<div class="codehilite"><pre>', md.convert(text))
else:
self.assertEqual(
md.convert(text),
Expand All @@ -117,10 +125,9 @@ def testLinenumsTrue(self):
# Different 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(
'<table class="codehilitetable"><tr><td class="linenos">'
)
self.assertStartsWith(
'<table class="codehilitetable"><tr><td class="linenos">',
md.convert(text)
)
else:
self.assertEqual(
Expand All @@ -134,7 +141,7 @@ def testLinenumsFalse(self):
md = markdown.Markdown(
extensions=[markdown.extensions.codehilite.CodeHiliteExtension(linenums=False)])
if self.has_pygments:
self.assertTrue(md.convert(text).startswith('<div class="codehilite"><pre><span'))
self.assertStartsWith('<div class="codehilite"><pre><span', md.convert(text))
else:
self.assertEqual(
md.convert(text),
Expand All @@ -148,7 +155,7 @@ def testLinenumsNone(self):
extensions=[markdown.extensions.codehilite.CodeHiliteExtension(linenums=None)])
if self.has_pygments:
# Pygments can use random lexer here as we did not specify the language
self.assertTrue(md.convert(text).startswith('<div class="codehilite"><pre>'))
self.assertStartsWith('<div class="codehilite"><pre>', md.convert(text))
else:
self.assertEqual(
md.convert(text),
Expand All @@ -164,10 +171,9 @@ def testLinenumsNoneWithShebang(self):
# 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(
'<table class="codehilitetable"><tr><td class="linenos">'
)
self.assertStartsWith(
'<table class="codehilitetable"><tr><td class="linenos">',
md.convert(text)
)
else:
self.assertEqual(
Expand All @@ -182,7 +188,7 @@ def testLinenumsNoneWithColon(self):
extensions=[markdown.extensions.codehilite.CodeHiliteExtension(linenums=None)]
)
if self.has_pygments:
self.assertTrue(md.convert(text).startswith('<div class="codehilite"><pre><span'))
self.assertStartsWith('<div class="codehilite"><pre><span', md.convert(text))
else:
self.assertEqual(
md.convert(text),
Expand All @@ -198,10 +204,9 @@ def testHighlightLinesWithColon(self):
for text in (text0, text1):
md = markdown.Markdown(extensions=['markdown.extensions.codehilite'])
if self.has_pygments:
self.assertTrue(
md.convert(text).startswith(
'<div class="codehilite"><pre><span class="hll"'
)
self.assertStartsWith(
'<div class="codehilite"><pre><span class="hll"',
md.convert(text)
)
else:
self.assertEqual(
Expand All @@ -224,7 +229,7 @@ def testUsePygmentsFalse(self):
)


class TestFencedCode(unittest.TestCase):
class TestFencedCode(TestCaseWithAssertStartsWith):
""" Test fenced_code extension. """

def setUp(self):
Expand Down Expand Up @@ -320,8 +325,9 @@ def testFencedCodeWithHighlightLines(self):
)

if self.has_pygments:
self.assertTrue(
md.convert(text).startswith('<div class="codehilite"><pre><span class="hll"')
self.assertStartsWith(
'<div class="codehilite"><pre><span class="hll"',
md.convert(text)
)
else:
self.assertEqual(
Expand Down Expand Up @@ -354,8 +360,9 @@ def testFencedLanguageAndHighlightLines(self):
]
)
if self.has_pygments:
self.assertTrue(
md.convert(text).startswith('<div class="codehilite"><pre><span class="hll"')
self.assertStartsWith(
'<div class="codehilite"><pre><span class="hll"',
md.convert(text)
)
else:
self.assertEqual(
Expand Down Expand Up @@ -602,7 +609,7 @@ def testRE(self):
self.assertEqual(RE.match(test).groups(), expected)


class TestTOC(unittest.TestCase):
class TestTOC(TestCaseWithAssertStartsWith):
""" Test TOC Extension. """

def setUp(self):
Expand Down Expand Up @@ -680,13 +687,13 @@ def testDisabledMarker(self):
'<h1 id="header-1">Header 1</h1>\n'
'<h2 id="header-2">Header 2</h2>'
)
self.assertTrue(md.toc.startswith('<div class="toc">'))
self.assertStartsWith('<div class="toc">', md.toc)

def testReset(self):
""" Test TOC Reset. """
self.assertEqual(self.md.toc, '')
self.md.convert('# Header 1\n\n## Header 2')
self.assertTrue(self.md.toc.startswith('<div class="toc">'))
self.assertStartsWith('<div class="toc">', self.md.toc)
self.md.reset()
self.assertEqual(self.md.toc, '')

Expand Down Expand Up @@ -771,7 +778,10 @@ def testTitle(self):
extensions=[markdown.extensions.toc.TocExtension(title='Table of Contents')]
)
md.convert('# Header 1\n\n## Header 2')
self.assertTrue(md.toc.startswith('<div class="toc"><span class="toctitle">Table of Contents</span><ul>'))
self.assertStartsWith(
'<div class="toc"><span class="toctitle">Table of Contents</span><ul>',
md.toc
)

def testWithAttrList(self):
""" Test TOC with attr_list Extension. """
Expand Down

0 comments on commit e56f0cd

Please sign in to comment.