Skip to content

Commit

Permalink
Improve test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
waylan committed Jan 13, 2018
1 parent 3fad730 commit 9ea3bde
Show file tree
Hide file tree
Showing 21 changed files with 79 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[run]
omit=
markdown/__version__.py
*site-packages*
tests/*
markdown/test_tools.py
2 changes: 1 addition & 1 deletion markdown/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
__version_info__ = (3, 0, 0, 'alpha', 0)


def _get_version():
def _get_version(): # pragma: no cover
" Returns a PEP 386-compliant version number from version_info. "
assert len(__version_info__) == 5
assert __version_info__[3] in ('alpha', 'beta', 'rc', 'final')
Expand Down
30 changes: 0 additions & 30 deletions markdown/__version__.py

This file was deleted.

9 changes: 4 additions & 5 deletions markdown/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ def build_extension(self, ext_name, configs):
return ext(**configs)

# Get class name (if provided): `path.to.module:ClassName`
ext_name, class_name = ext_name.split(':', 1) \
if ':' in ext_name else (ext_name, '')
ext_name, class_name = ext_name.split(':', 1) if ':' in ext_name else (ext_name, '')

try:
module = importlib.import_module(ext_name)
Expand Down Expand Up @@ -238,7 +237,7 @@ def convert(self, source):

try:
source = util.text_type(source)
except UnicodeDecodeError as e:
except UnicodeDecodeError as e: # pragma: no cover
# Customise error message while maintaining original trackback
e.reason += '. -- Note: Markdown only accepts unicode input!'
raise
Expand Down Expand Up @@ -313,7 +312,7 @@ def convertFile(self, input=None, output=None, encoding=None):
input_file.close()
else:
text = sys.stdin.read()
if not isinstance(text, util.text_type):
if not isinstance(text, util.text_type): # pragma: no cover
text = text.decode(encoding)

text = text.lstrip('\ufeff') # remove the byte-order mark
Expand All @@ -340,7 +339,7 @@ def convertFile(self, input=None, output=None, encoding=None):
try:
# Write bytes directly to buffer (Python 3).
sys.stdout.buffer.write(html)
except AttributeError:
except AttributeError: # pragma: no cover
# Probably Python 2, which works with bytes by default.
sys.stdout.write(html)

Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/abbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ def handleMatch(self, m):
return abbr


def makeExtension(*args, **kwargs):
def makeExtension(*args, **kwargs): # pragma: no cover
return AbbrExtension(*args, **kwargs)
2 changes: 1 addition & 1 deletion markdown/extensions/admonition.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@ def get_class_and_title(self, match):
return klass, title


def makeExtension(*args, **kwargs):
def makeExtension(*args, **kwargs): # pragma: no cover
return AdmonitionExtension(*args, **kwargs)
2 changes: 1 addition & 1 deletion markdown/extensions/attr_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,5 @@ def extendMarkdown(self, md, md_globals):
)


def makeExtension(*args, **kwargs):
def makeExtension(*args, **kwargs): # pragma: no cover
return AttrListExtension(*args, **kwargs)
2 changes: 1 addition & 1 deletion markdown/extensions/codehilite.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,5 +260,5 @@ def extendMarkdown(self, md, md_globals):
md.registerExtension(self)


def makeExtension(*args, **kwargs):
def makeExtension(*args, **kwargs): # pragma: no cover
return CodeHiliteExtension(*args, **kwargs)
2 changes: 1 addition & 1 deletion markdown/extensions/def_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,5 @@ def extendMarkdown(self, md, md_globals):
'>ulist')


def makeExtension(*args, **kwargs):
def makeExtension(*args, **kwargs): # pragma: no cover
return DefListExtension(*args, **kwargs)
2 changes: 1 addition & 1 deletion markdown/extensions/extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def extendMarkdown(self, md, md_globals):
r'^(p|h[1-6]|li|dd|dt|td|th|legend|address)$', re.IGNORECASE)


def makeExtension(*args, **kwargs):
def makeExtension(*args, **kwargs): # pragma: no cover
return ExtraExtension(*args, **kwargs)


Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/fenced_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,5 @@ def _escape(self, txt):
return txt


def makeExtension(*args, **kwargs):
def makeExtension(*args, **kwargs): # pragma: no cover
return FencedCodeExtension(*args, **kwargs)
2 changes: 1 addition & 1 deletion markdown/extensions/footnotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,6 @@ def run(self, text):
return text.replace(NBSP_PLACEHOLDER, " ")


def makeExtension(*args, **kwargs):
def makeExtension(*args, **kwargs): # pragma: no cover
""" Return an instance of the FootnoteExtension """
return FootnoteExtension(*args, **kwargs)
2 changes: 1 addition & 1 deletion markdown/extensions/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ def run(self, lines):
return lines


def makeExtension(*args, **kwargs):
def makeExtension(*args, **kwargs): # pragma: no cover
return MetaExtension(*args, **kwargs)
2 changes: 1 addition & 1 deletion markdown/extensions/nl2br.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ def extendMarkdown(self, md, md_globals):
md.inlinePatterns.add('nl', br_tag, '_end')


def makeExtension(*args, **kwargs):
def makeExtension(*args, **kwargs): # pragma: no cover
return Nl2BrExtension(*args, **kwargs)
2 changes: 1 addition & 1 deletion markdown/extensions/sane_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ def extendMarkdown(self, md, md_globals):
md.parser.blockprocessors['ulist'] = SaneUListProcessor(md.parser)


def makeExtension(*args, **kwargs):
def makeExtension(*args, **kwargs): # pragma: no cover
return SaneListExtension(*args, **kwargs)
2 changes: 1 addition & 1 deletion markdown/extensions/smart_strong.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ def extendMarkdown(self, md, md_globals):
)


def makeExtension(*args, **kwargs):
def makeExtension(*args, **kwargs): # pragma: no cover
return SmartEmphasisExtension(*args, **kwargs)
2 changes: 1 addition & 1 deletion markdown/extensions/smarty.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,5 +264,5 @@ def extendMarkdown(self, md, md_globals):
md.ESCAPED_CHARS.extend(['"', "'"])


def makeExtension(*args, **kwargs):
def makeExtension(*args, **kwargs): # pragma: no cover
return SmartyExtension(*args, **kwargs)
2 changes: 1 addition & 1 deletion markdown/extensions/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,5 @@ def extendMarkdown(self, md, md_globals):
'<hashheader')


def makeExtension(*args, **kwargs):
def makeExtension(*args, **kwargs): # pragma: no cover
return TableExtension(*args, **kwargs)
4 changes: 2 additions & 2 deletions markdown/extensions/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def add_anchor(self, c, elem_id): # @ReservedAssignment
c.text = ""
for elem in c:
anchor.append(elem)
while c:
while len(c):
c.remove(c[0])
c.append(anchor)

Expand Down Expand Up @@ -304,5 +304,5 @@ def reset(self):
self.md.toc = ''


def makeExtension(*args, **kwargs):
def makeExtension(*args, **kwargs): # pragma: no cover
return TocExtension(*args, **kwargs)
2 changes: 1 addition & 1 deletion markdown/extensions/wikilinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ def _getMeta(self):
return base_url, end_url, html_class


def makeExtension(*args, **kwargs):
def makeExtension(*args, **kwargs): # pragma: no cover
return WikiLinkExtension(*args, **kwargs)
58 changes: 56 additions & 2 deletions tests/test_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@
from logging import DEBUG, WARNING, CRITICAL
import yaml
import tempfile
from io import BytesIO


PY3 = sys.version_info[0] == 3


if not PY3:
def bytes(string, encoding):
return string.encode(encoding)


class TestMarkdownBasics(unittest.TestCase):
""" Tests basics of the Markdown class. """

Expand Down Expand Up @@ -53,11 +60,50 @@ def testDotNotationExtension(self):
""" Test Extension loading with Name (`path.to.module`). """
markdown.Markdown(extensions=['markdown.extensions.footnotes'])

def TestDotNotationExtensionWithClass(self):
def testDotNotationExtensionWithClass(self):
""" Test Extension loading with class name (`path.to.module:Class`). """
markdown.Markdown(extensions=['markdown.extensions.footnotes:FootnoteExtension'])


class TestConvertFile(unittest.TestCase):
""" Tests of ConvertFile. """

def setUp(self):
self.saved = sys.stdin, sys.stdout
sys.stdin = BytesIO(bytes('foo', encoding='utf-8'))
sys.stdout = BytesIO()

def tearDown(self):
sys.stdin, sys.stdout = self.saved

def getTempFiles(self, src):
""" Return the file names for two temp files. """
infd, infile = tempfile.mkstemp(suffix='.txt')
with os.fdopen(infd, 'w') as fp:
fp.write(src)
outfd, outfile = tempfile.mkstemp(suffix='.html')
return infile, outfile, outfd

def testFileNames(self):
infile, outfile, outfd = self.getTempFiles('foo')
markdown.markdownFromFile(input=infile, output=outfile)
with os.fdopen(outfd, 'r') as fp:
output = fp.read()
self.assertEqual(output, '<p>foo</p>')

def testFileObjects(self):
infile = BytesIO(bytes('foo', encoding='utf-8'))
outfile = BytesIO()
markdown.markdownFromFile(input=infile, output=outfile)
outfile.seek(0)
self.assertEqual(outfile.read().decode('utf-8'), '<p>foo</p>')

def testStdinStdout(self):
markdown.markdownFromFile()
sys.stdout.seek(0)
self.assertEqual(sys.stdout.read().decode('utf-8'), '<p>foo</p>')


class TestBlockParser(unittest.TestCase):
""" Tests of the BlockParser class. """

Expand Down Expand Up @@ -316,7 +362,7 @@ def tearDown(self):

def testNonUnicodeSource(self):
""" Test falure on non-unicode source text. """
if sys.version_info < (3, 0):
if not PY3:
source = "foo".encode('utf-16')
self.assertRaises(UnicodeDecodeError, markdown.markdown, source)

Expand All @@ -339,6 +385,14 @@ def testNonExtension(self):
""" Test loading a non Extension object as an extension. """
self.assertRaises(TypeError, markdown.Markdown, extensions=[object])

def testDotNotationExtensionWithBadClass(self):
""" Test Extension loading with non-existant class name (`path.to.module:Class`). """
self.assertRaises(
AttributeError,
markdown.Markdown,
extensions=['markdown.extensions.footnotes:MissingExtension']
)

def testBaseExtention(self):
""" Test that the base Extension class will raise NotImplemented. """
self.assertRaises(
Expand Down

0 comments on commit 9ea3bde

Please sign in to comment.