From e56f0cd9b0c6f934bac62751234c0f4df2050ac6 Mon Sep 17 00:00:00 2001 From: Maurice van der Pot Date: Sat, 27 Feb 2016 00:34:42 +0100 Subject: 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. --- tests/test_extensions.py | 64 ++++++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/tests/test_extensions.py b/tests/test_extensions.py index 72ce212..e9b6112 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -11,6 +11,15 @@ from __future__ import unicode_literals 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. """ @@ -85,8 +94,7 @@ class TestAbbr(unittest.TestCase): 'and ABBR

' ) - -class TestCodeHilite(unittest.TestCase): +class TestCodeHilite(TestCaseWithAssertStartsWith): """ Test codehilite extension. """ def setUp(self): @@ -101,7 +109,7 @@ class TestCodeHilite(unittest.TestCase): 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('
'))
+            self.assertStartsWith('
', md.convert(text))
         else:
             self.assertEqual(
                 md.convert(text),
@@ -117,10 +125,9 @@ class TestCodeHilite(unittest.TestCase):
             # 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(
-                    '
' - ) + self.assertStartsWith( + '
', + md.convert(text) ) else: self.assertEqual( @@ -134,7 +141,7 @@ class TestCodeHilite(unittest.TestCase): md = markdown.Markdown( extensions=[markdown.extensions.codehilite.CodeHiliteExtension(linenums=False)]) if self.has_pygments: - self.assertTrue(md.convert(text).startswith('
'))
+            self.assertStartsWith('
', md.convert(text))
         else:
             self.assertEqual(
                 md.convert(text),
@@ -164,10 +171,9 @@ class TestCodeHilite(unittest.TestCase):
             # 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(
-                    '
' - ) + self.assertStartsWith( + '
', + md.convert(text) ) else: self.assertEqual( @@ -182,7 +188,7 @@ class TestCodeHilite(unittest.TestCase): extensions=[markdown.extensions.codehilite.CodeHiliteExtension(linenums=None)] ) if self.has_pygments: - self.assertTrue(md.convert(text).startswith('
Header 1\n'
             '

Header 2

' ) - self.assertTrue(md.toc.startswith('
')) + self.assertStartsWith('
', 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('
')) + self.assertStartsWith('
', self.md.toc) self.md.reset() self.assertEqual(self.md.toc, '') @@ -771,7 +778,10 @@ class TestTOC(unittest.TestCase): extensions=[markdown.extensions.toc.TocExtension(title='Table of Contents')] ) md.convert('# Header 1\n\n## Header 2') - self.assertTrue(md.toc.startswith('
Table of Contents
    ')) + self.assertStartsWith( + '
    Table of Contents
      ', + md.toc + ) def testWithAttrList(self): """ Test TOC with attr_list Extension. """ -- cgit v1.2.3