diff options
author | Waylan Limberg <waylan@gmail.com> | 2009-06-06 00:50:42 -0400 |
---|---|---|
committer | Waylan Limberg <waylan@gmail.com> | 2009-06-06 00:50:42 -0400 |
commit | fb08b0c4718f92144b83ba57e594eca2c61dd93b (patch) | |
tree | 5a9c8ffe7c9b5a0b88331b5c8548294c8fc53f20 | |
parent | 14152628fff5c4c3da483746c653ad3b46916a39 (diff) | |
download | markdown-fb08b0c4718f92144b83ba57e594eca2c61dd93b.tar.gz markdown-fb08b0c4718f92144b83ba57e594eca2c61dd93b.tar.bz2 markdown-fb08b0c4718f92144b83ba57e594eca2c61dd93b.zip |
Added support for a 'normalize' setting in test.cfg which uses HTML Tidy to normalize whitespace for running tests against other testing framework's tests files (i.e.: perl or php). Hopefully, I got Tidy's settings right so that only unsignificant whitespace is altered. There's always the possability this could hide some bugs.
-rw-r--r-- | markdown/tests/__init__.py | 57 |
1 files changed, 46 insertions, 11 deletions
diff --git a/markdown/tests/__init__.py b/markdown/tests/__init__.py index 29e8d45..97966a3 100644 --- a/markdown/tests/__init__.py +++ b/markdown/tests/__init__.py @@ -5,34 +5,68 @@ import difflib import nose import util from plugins import HtmlOutput, Markdown +try: + import tidy +except ImportError: + tidy = None + test_dir = os.path.abspath(os.path.dirname(__file__)) -def normalize(text): - """ normalize lines for better diff output. """ +def splitlines(text): + """ Split lines for better diff output. """ return ['%s\n' % l for l in text.strip().split('\n')] -def get_args(file, config): - """ Get args to pass to markdown from config for a given file. """ - args = {} +def get_section(file, config): + """ Get name of config section for given file. """ filename = os.path.basename(file) if config.has_section(filename): - section = filename + return filename else: - section = 'DEFAULT' + return 'DEFAULT' + +def get_args(file, config): + """ Get args to pass to markdown from config for a given file. """ + args = {} + section = get_section(file, config) for key in ['extensions', 'safe_mode', 'output_format']: args[key] = config.get(section, key) return args +def normalize(text): + """ Normalize whitespace for a string of html using tidy. """ + return unicode(tidy.parseString(text.encode('utf-8'), + drop_empty_paras=0, + fix_backslash=0, + fix_bad_comments=0, + fix_uri=0, + join_styles=0, + lower_literals=0, + merge_divs=0, + #merge_spans=0, + output_xhtml=1, + #preserve_entities=1, + quote_ampersand=0, + show_body_only=1, + char_encoding='utf8', + newline='LF')) + def check_syntax(file, config): """ Compare expected output to actual output and report result. """ input_file = file + ".txt" input = codecs.open(input_file, encoding="utf-8").read() output_file = file + ".html" expected_output = codecs.open(output_file, encoding="utf-8").read() - output = normalize(markdown.markdown(input, **get_args(file, config))) - diff = [l for l in difflib.unified_diff(normalize(expected_output), - output, output_file, + output = markdown.markdown(input, **get_args(file, config)) + if tidy and config.getboolean(get_section(file, config), 'normalize'): + # Normalize whitespace before comparing. + expected_output = normalize(expected_output) + output = normalize(output) + elif config.getboolean(get_section(file, config), 'normalize'): + # Tidy is not available. Skip this test. + raise nose.plugins.skip.SkipTest, 'Skipped test. Tidy not available in system.' + diff = [l for l in difflib.unified_diff(splitlines(expected_output), + splitlines(output), output_file, 'actual_output.html', n=3)] if diff: raise util.MarkdownSyntaxError('Output from "%s" failed to match expected ' @@ -43,7 +77,8 @@ def TestSyntax(): # Get dir specific config settings. config = util.CustomConfigParser({'extensions': '', 'safe_mode': False, - 'output_format': 'xhtml1'}) + 'output_format': 'xhtml1', + 'normalize': '0'}) config.read(os.path.join(dir_name, 'test.cfg')) # Loop through files and generate tests. for file in files: |