Created
March 28, 2015 03:13
-
-
Save waylan/ec8ddb2664e21782ec4f to your computer and use it in GitHub Desktop.
Alternate testing framework for python-markdown without nose.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
import os | |
import markdown | |
import codecs | |
import difflib | |
try: | |
import tidylib | |
except ImportError: | |
tidylib = None | |
Kwargs = dict | |
class MarkdownSyntaxError(Exception): | |
pass | |
def normalize_whitespace(text): | |
""" Normalize whitespace for a string of html using tidylib. """ | |
output, errors = tidylib.tidy_fragment(text, options={ | |
'drop_empty_paras': 0, | |
'fix_backslash': 0, | |
'fix_bad_comments': 0, | |
'fix_uri': 0, | |
'join_styles': 0, | |
'lower_literals': 0, | |
'merge_divs': 0, | |
'output_xhtml': 1, | |
'quote_ampersand': 0, | |
'newline': 'LF' | |
}) | |
return output | |
class SyntaxTestMeta(type): | |
def __new__(cls, name, bases, dct): | |
def gen_test(infile, outfile, normalize, kwargs): | |
def test(self): | |
with codecs.open(infile, encoding="utf-8") as f: | |
input = f.read() | |
with codecs.open(outfile, encoding="utf-8") as f: | |
# Normalize line endings | |
# (on windows, git may have altered line endings). | |
expected_output = f.read().replace("\r\n", "\n") | |
output = markdown.markdown(input, **kwargs) | |
if tidylib and normalize: | |
expected_output = normalize_whitespace(expected_output) | |
output = normalize_whitespace(output) | |
elif normalize: | |
self.skipTest('Tidylib not available.') | |
diff = [l for l in difflib.unified_diff( | |
expected_output.splitlines(True), | |
output.splitlines(True), | |
outfile, | |
'actual_output.html', | |
n=3 | |
)] | |
if diff: | |
raise MarkdownSyntaxError( | |
'Output from "%s" failed to match expected ' | |
'output.\n\n%s' % (infile, ''.join(diff)) | |
) | |
return test | |
location = dct.get('location', '') | |
exclude = dct.get('exclude', []) | |
normalize = dct.get('normalize', False) | |
input_ext = dct.get('input_ext', '.txt') | |
output_ext = dct.get('ouput_ext', '.html') | |
if os.path.isdir(location): | |
for file in os.listdir(location): | |
infile = os.path.join(location, file) | |
if os.path.isfile(infile): | |
tname, ext = os.path.splitext(file) | |
if ext == input_ext: | |
outfile = os.path.join(location, tname + output_ext) | |
kwargs = dct[tname] if tname in dct else Kwargs() | |
test_name = 'test_%s' % tname | |
if tname not in exclude: | |
dct[test_name] = gen_test(infile, outfile, normalize, kwargs) | |
else: | |
dct[test_name] = unittest.skip('Excluded')(lambda : None) | |
return type.__new__(cls, name, bases, dct) | |
class SyntaxTest(unittest.TestCase): | |
__metaclass__ = SyntaxTestMeta |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from foo import SyntaxTest, Kwargs | |
import os | |
test_dir = os.path.abspath(os.path.dirname(__file__)) | |
class TestExtensions(SyntaxTest): | |
location = os.path.join(test_dir, 'tests/extensions') | |
exclude = ['codehilite'] | |
normalize = False | |
input_ext = '.txt' | |
output_ext = '.html' | |
attr_list = Kwargs( | |
extensions=[ | |
'markdown.extensions.attr_list', | |
'markdown.extensions.def_list', | |
'markdown.extensions.smarty' | |
] | |
) | |
codehilite = Kwargs(extensions=['markdown.extensions.codehilite']) | |
toc = Kwargs(extensions=['markdown.extensions.toc']) | |
toc_invalid = Kwargs(extensions=['markdown.extensions.toc']) | |
toc_out_of_order = Kwargs(extensions=['markdown.extensions.toc']) | |
toc_nested = Kwargs( | |
extensions=['markdown.extensions.toc'], | |
extension_configs={'markdown.extensions.toc': {'permalink': True} } | |
) | |
toc_nested2 = Kwargs( | |
extensions=['markdown.extensions.toc'], | |
extension_configs={'markdown.extensions.toc': {'permalink': "[link]"} } | |
) | |
toc_nested_list = Kwargs(extensions=['markdown.extensions.toc']) | |
wikilinks = Kwargs(extensions=['markdown.extensions.wikilinks']) | |
fenced_code = Kwargs(extensions=['markdown.extensions.fenced_code']) | |
github_flavored = Kwargs(extensions=['markdown.extensions.fenced_code']) | |
sane_lists = Kwargs(extensions=['markdown.extensions.sane_lists']) | |
nl2br_w_attr_list = Kwargs( | |
extensions=[ | |
'markdown.extensions.nl2br', | |
'markdown.extensions.attr_list' | |
] | |
) | |
admonition = Kwargs(extensions=['markdown.extensions.admonition']) | |
smarty = Kwargs( | |
extensions=['markdown.extensions.smarty'], | |
extension_configs={'markdown.extensions.smarty': {'smart_angled_quotes': True} } | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment