aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorWaylan Limberg <waylan.limberg@icloud.com>2015-03-14 23:22:10 -0400
committerWaylan Limberg <waylan.limberg@icloud.com>2018-01-13 15:43:54 -0500
commit9ea3bdefbb62165c5c060852c60bfb5acd2574f1 (patch)
tree70d14a172fe302d5b233fe8ad70efc2713d1cae6 /tests
parent3fad73031e544de6c9f74621de923da3806a6c21 (diff)
downloadmarkdown-9ea3bdefbb62165c5c060852c60bfb5acd2574f1.tar.gz
markdown-9ea3bdefbb62165c5c060852c60bfb5acd2574f1.tar.bz2
markdown-9ea3bdefbb62165c5c060852c60bfb5acd2574f1.zip
Improve test coverage.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_apis.py58
1 files changed, 56 insertions, 2 deletions
diff --git a/tests/test_apis.py b/tests/test_apis.py
index 42e7496..aa43e52 100644
--- a/tests/test_apis.py
+++ b/tests/test_apis.py
@@ -17,10 +17,17 @@ from markdown.__main__ import parse_options
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. """
@@ -53,11 +60,50 @@ class TestMarkdownBasics(unittest.TestCase):
""" 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. """
@@ -316,7 +362,7 @@ class TestErrors(unittest.TestCase):
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)
@@ -339,6 +385,14 @@ class TestErrors(unittest.TestCase):
""" 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(