Waylan Limberg avatar Waylan Limberg committed 8899ef5

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.

Comments (0)

Files changed (1)

markdown/tests/__init__.py

 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_section(file, config):
+    """ Get name of config section for given file. """
+    filename = os.path.basename(file)
+    if config.has_section(filename):
+        return filename
+    else:
+        return 'DEFAULT'
+
 def get_args(file, config):
     """ Get args to pass to markdown from config for a given file. """
     args = {}
-    filename = os.path.basename(file)
-    if config.has_section(filename):
-        section = filename
-    else:
-        section = 'DEFAULT'
+    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 '
         # 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:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.