diff options
author | Waylan Limberg <waylan@gmail.com> | 2009-06-05 22:55:30 -0400 |
---|---|---|
committer | Waylan Limberg <waylan@gmail.com> | 2009-06-05 22:55:30 -0400 |
commit | 583fb40a499693ce8f0ae1fca2ecececad4bcd64 (patch) | |
tree | 4eb4d8867a2ad690066e4c21e8d8f5f6bfc1ce62 | |
parent | 048b9ead4428b8942900ece9d47462d3be5a48ea (diff) | |
download | markdown-583fb40a499693ce8f0ae1fca2ecececad4bcd64.tar.gz markdown-583fb40a499693ce8f0ae1fca2ecececad4bcd64.tar.bz2 markdown-583fb40a499693ce8f0ae1fca2ecececad4bcd64.zip |
More cleanup. Added some doc strings. Removed old testing framework files.
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | markdown/tests/__init__.py | 13 | ||||
-rwxr-xr-x | regression-tests.py | 234 | ||||
-rwxr-xr-x | test-markdown.py | 347 |
4 files changed, 13 insertions, 582 deletions
@@ -5,3 +5,4 @@ tmp/* build/* dist/* +MANIFEST diff --git a/markdown/tests/__init__.py b/markdown/tests/__init__.py index eca1bba..4a191a3 100644 --- a/markdown/tests/__init__.py +++ b/markdown/tests/__init__.py @@ -5,14 +5,17 @@ import difflib import nose import util from plugins import HtmlOutput, Markdown +# import api tests for autodiscovery from test_apis import * test_dir = os.path.abspath(os.path.dirname(__file__)) def normalize(text): + """ normalize 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 = {} filename = os.path.basename(file) if config.has_section(filename): @@ -24,6 +27,7 @@ def get_args(file, config): return args 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" @@ -47,6 +51,13 @@ def TestSyntax(): for file in files: root, ext = os.path.splitext(file) if ext == '.txt': + # check_syntax.description = root yield check_syntax, os.path.join(dir_name, root), config -nose.main(addplugins=[HtmlOutput(), Markdown()]) +def run(): + nose.main(addplugins=[HtmlOutput(), Markdown()]) + +# Hack to make nose run with extensions. Once extensions can be added from +# setup.cfg, the below line can be removed. +# See nose [Issue 271](http://code.google.com/p/python-nose/issues/detail?id=271) +run() diff --git a/regression-tests.py b/regression-tests.py deleted file mode 100755 index 7601061..0000000 --- a/regression-tests.py +++ /dev/null @@ -1,234 +0,0 @@ -#!/usr/bin/python -""" -Python-Markdown Regression Tests -================================ - -Tests of the various APIs with the python markdown lib. - -""" - -import unittest -from doctest import DocTestSuite -import os -import markdown - -class TestMarkdown(unittest.TestCase): - """ Tests basics of the Markdown class. """ - - def setUp(self): - """ Create instance of Markdown. """ - self.md = markdown.Markdown() - - def testBlankInput(self): - """ Test blank input. """ - self.assertEqual(self.md.convert(''), '') - - def testWhitespaceOnly(self): - """ Test input of only whitespace. """ - self.assertEqual(self.md.convert(' '), '') - - def testSimpleInput(self): - """ Test simple input. """ - self.assertEqual(self.md.convert('foo'), '<p>foo</p>') - -class TestBlockParser(unittest.TestCase): - """ Tests of the BlockParser class. """ - - def setUp(self): - """ Create instance of BlockParser. """ - self.parser = markdown.Markdown().parser - - def testParseChunk(self): - """ Test BlockParser.parseChunk. """ - root = markdown.etree.Element("div") - text = 'foo' - self.parser.parseChunk(root, text) - self.assertEqual(markdown.etree.tostring(root), "<div><p>foo</p></div>") - - def testParseDocument(self): - """ Test BlockParser.parseDocument. """ - lines = ['#foo', '', 'bar', '', ' baz'] - tree = self.parser.parseDocument(lines) - self.assert_(isinstance(tree, markdown.etree.ElementTree)) - self.assert_(markdown.etree.iselement(tree.getroot())) - self.assertEqual(markdown.etree.tostring(tree.getroot()), - "<div><h1>foo</h1><p>bar</p><pre><code>baz\n</code></pre></div>") - - -class TestBlockParserState(unittest.TestCase): - """ Tests of the State class for BlockParser. """ - - def setUp(self): - self.state = markdown.blockparser.State() - - def testBlankState(self): - """ Test State when empty. """ - self.assertEqual(self.state, []) - - def testSetSate(self): - """ Test State.set(). """ - self.state.set('a_state') - self.assertEqual(self.state, ['a_state']) - self.state.set('state2') - self.assertEqual(self.state, ['a_state', 'state2']) - - def testIsSate(self): - """ Test State.isstate(). """ - self.assertEqual(self.state.isstate('anything'), False) - self.state.set('a_state') - self.assertEqual(self.state.isstate('a_state'), True) - self.state.set('state2') - self.assertEqual(self.state.isstate('state2'), True) - self.assertEqual(self.state.isstate('a_state'), False) - self.assertEqual(self.state.isstate('missing'), False) - - def testReset(self): - """ Test State.reset(). """ - self.state.set('a_state') - self.state.reset() - self.assertEqual(self.state, []) - self.state.set('state1') - self.state.set('state2') - self.state.reset() - self.assertEqual(self.state, ['state1']) - -class TestHtmlStash(unittest.TestCase): - """ Test Markdown's HtmlStash. """ - - def setUp(self): - self.stash = markdown.preprocessors.HtmlStash() - self.placeholder = self.stash.store('foo') - - def testSimpleStore(self): - """ Test HtmlStash.store. """ - self.assertEqual(self.placeholder, - markdown.preprocessors.HTML_PLACEHOLDER % 0) - self.assertEqual(self.stash.html_counter, 1) - self.assertEqual(self.stash.rawHtmlBlocks, [('foo', False)]) - - def testStoreMore(self): - """ Test HtmlStash.store with additional blocks. """ - placeholder = self.stash.store('bar') - self.assertEqual(placeholder, - markdown.preprocessors.HTML_PLACEHOLDER % 1) - self.assertEqual(self.stash.html_counter, 2) - self.assertEqual(self.stash.rawHtmlBlocks, - [('foo', False), ('bar', False)]) - - def testSafeStore(self): - """ Test HtmlStash.store with 'safe' html. """ - self.stash.store('bar', True) - self.assertEqual(self.stash.rawHtmlBlocks, - [('foo', False), ('bar', True)]) - - def testReset(self): - """ Test HtmlStash.reset. """ - self.stash.reset() - self.assertEqual(self.stash.html_counter, 0) - self.assertEqual(self.stash.rawHtmlBlocks, []) - -class TestOrderedDict(unittest.TestCase): - """ Test OrderedDict storage class. """ - - def setUp(self): - self.odict = markdown.odict.OrderedDict() - self.odict['first'] = 'This' - self.odict['third'] = 'a' - self.odict['fourth'] = 'self' - self.odict['fifth'] = 'test' - - def testValues(self): - """ Test output of OrderedDict.values(). """ - self.assertEqual(self.odict.values(), ['This', 'a', 'self', 'test']) - - def testKeys(self): - """ Test output of OrderedDict.keys(). """ - self.assertEqual(self.odict.keys(), - ['first', 'third', 'fourth', 'fifth']) - - def testItems(self): - """ Test output of OrderedDict.items(). """ - self.assertEqual(self.odict.items(), - [('first', 'This'), ('third', 'a'), - ('fourth', 'self'), ('fifth', 'test')]) - - def testAddBefore(self): - """ Test adding an OrderedDict item before a given key. """ - self.odict.add('second', 'is', '<third') - self.assertEqual(self.odict.items(), - [('first', 'This'), ('second', 'is'), ('third', 'a'), - ('fourth', 'self'), ('fifth', 'test')]) - - def testAddAfter(self): - """ Test adding an OrderDict item after a given key. """ - self.odict.add('second', 'is', '>first') - self.assertEqual(self.odict.items(), - [('first', 'This'), ('second', 'is'), ('third', 'a'), - ('fourth', 'self'), ('fifth', 'test')]) - - def testAddAfterEnd(self): - """ Test adding an OrderedDict item after the last key. """ - self.odict.add('sixth', '.', '>fifth') - self.assertEqual(self.odict.items(), - [('first', 'This'), ('third', 'a'), - ('fourth', 'self'), ('fifth', 'test'), ('sixth', '.')]) - - def testAdd_begin(self): - """ Test adding an OrderedDict item using "_begin". """ - self.odict.add('zero', 'CRAZY', '_begin') - self.assertEqual(self.odict.items(), - [('zero', 'CRAZY'), ('first', 'This'), ('third', 'a'), - ('fourth', 'self'), ('fifth', 'test')]) - - def testAdd_end(self): - """ Test adding an OrderedDict item using "_end". """ - self.odict.add('sixth', '.', '_end') - self.assertEqual(self.odict.items(), - [('first', 'This'), ('third', 'a'), - ('fourth', 'self'), ('fifth', 'test'), ('sixth', '.')]) - - def testAddBadLocation(self): - """ Test Error on bad location in OrderedDict.add(). """ - self.assertRaises(ValueError, self.odict.add, 'sixth', '.', '<seventh') - self.assertRaises(ValueError, self.odict.add, 'second', 'is', 'third') - - def testDeleteItem(self): - """ Test deletion of an OrderedDict item. """ - del self.odict['fourth'] - self.assertEqual(self.odict.items(), - [('first', 'This'), ('third', 'a'), ('fifth', 'test')]) - - def testChangeValue(self): - """ Test OrderedDict change value. """ - self.odict['fourth'] = 'CRAZY' - self.assertEqual(self.odict.items(), - [('first', 'This'), ('third', 'a'), - ('fourth', 'CRAZY'), ('fifth', 'test')]) - - def testChangeOrder(self): - """ Test OrderedDict change order. """ - self.odict.link('fourth', '<third') - self.assertEqual(self.odict.items(), - [('first', 'This'), ('fourth', 'self'), - ('third', 'a'), ('fifth', 'test')]) - -def suite(): - """ Build a test suite of the above tests and extension doctests. """ - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(TestMarkdown)) - suite.addTest(unittest.makeSuite(TestBlockParser)) - suite.addTest(unittest.makeSuite(TestBlockParserState)) - suite.addTest(unittest.makeSuite(TestHtmlStash)) - suite.addTest(unittest.makeSuite(TestOrderedDict)) - - for filename in os.listdir('markdown/extensions'): - if filename.endswith('.py'): - module = 'markdown.extensions.%s' % filename[:-3] - try: - suite.addTest(DocTestSuite(module)) - except: ValueError - # No tests - return suite - -if __name__ == '__main__': - unittest.TextTestRunner(verbosity=2).run(suite()) diff --git a/test-markdown.py b/test-markdown.py deleted file mode 100755 index 95914c4..0000000 --- a/test-markdown.py +++ /dev/null @@ -1,347 +0,0 @@ -#!/usr/bin/env python - -import os, difflib, time, gc, codecs, platform, sys -from pprint import pprint -import textwrap - -# Setup a logger manually for compatibility with Python 2.3 -import logging -logging.getLogger('MARKDOWN').addHandler(logging.StreamHandler()) -import markdown - -TEST_DIR = "tests" -TMP_DIR = "./tmp/" -WRITE_BENCHMARK = True -WRITE_BENCHMARK = False -ACTUALLY_MEASURE_MEMORY = True - -###################################################################### - -if platform.system().lower() == "darwin": # Darwin - _proc_status = '/proc/%d/stat' % os.getpid() -else: # Linux - _proc_status = '/proc/%d/status' % os.getpid() - -_scale = {'kB': 1024.0, 'mB': 1024.0*1024.0, - 'KB': 1024.0, 'MB': 1024.0*1024.0} - -def _VmB(VmKey): - '''Private. - ''' - global _proc_status, _scale - # get pseudo file /proc/<pid>/status - try: - t = open(_proc_status) - v = t.read() - t.close() - except: - return 0.0 # non-Linux? - # get VmKey line e.g. 'VmRSS: 9999 kB\n ...' - i = v.index(VmKey) - v = v[i:].split(None, 3) # whitespace - if len(v) < 3: - return 0.0 # invalid format? - # convert Vm value to bytes - return float(v[1]) * _scale[v[2]] - - -def memory(since=0.0): - '''Return memory usage in bytes. - ''' - if ACTUALLY_MEASURE_MEMORY : - return _VmB('VmSize:') - since - - -def resident(since=0.0): - '''Return resident memory usage in bytes. - ''' - return _VmB('VmRSS:') - since - - -def stacksize(since=0.0): - '''Return stack size in bytes. - ''' - return _VmB('VmStk:') - since - - -############################################################ - -DIFF_FILE_TEMPLATE = """ -<html> - <head> - <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> - <style> - td { - padding-left: 10px; - padding-right: 10px; - } - colgroup { - margin: 10px; - } - .diff_header { - color: gray; - } - .ok { - color: green; - } - .gray { - color: gray; - } - .failed a { - color: red; - } - .failed { - color: red; - } - </style> -</head> -<body> -<h1>Results Summary</h1> -<table rules="groups" > - <colgroup></colgroup> - <colgroup></colgroup> - <colgroup></colgroup> - <colgroup></colgroup> - <colgroup></colgroup> - <th> - <td></td> - <td>Seconds</td> - <td></td> - <td>Memory</td> - </th> - <tbody> - """ - -FOOTER = """ -</body> -</html> -""" - -DIFF_TABLE_TEMPLATE = """ - <table class="diff" rules="groups" > - <colgroup></colgroup> - <colgroup></colgroup> - <colgroup></colgroup> - <colgroup></colgroup> - <colgroup></colgroup> - <colgroup></colgroup> - <th> - <td></td> - <td>Expected</td> - <td></td> - <td></td> - <td>Actual</td> - </th> - <tbody> - %s - </tbody> - </table> -""" - - -def smart_split(text) : - result = [] - for x in text.splitlines() : - for y in textwrap.wrap(textwrap.dedent(x), 40): - result.append(y) - return result - - -differ = difflib.Differ() -try : - htmldiff = difflib.HtmlDiff() -except: - htmldiff = None - -class TestRunner : - - def __init__ (self) : - self.failedTests = [] - if not os.path.exists(TMP_DIR): - os.mkdir(TMP_DIR) - - def test_directory(self, dir, measure_time=False, safe_mode=False, encoding="utf8", output_format='xhtml1') : - self.encoding = encoding - benchmark_file_name = os.path.join(dir, "benchmark.dat") - self.saved_benchmarks = {} - - if measure_time : - if os.path.exists(benchmark_file_name) : - file = open(benchmark_file_name) - for line in file.readlines() : - test, str_time, str_mem = line.strip().split(":") - self.saved_benchmarks[test] = (float(str_time), float(str_mem)) - repeat = range(10) - else : - repeat = (0,) - - # First, determine from the name of the directory if any extensions - # need to be loaded. - - parts = os.path.split(dir)[-1].split("-x-") - if len(parts) > 1 : - extensions = parts[1].split("-") - print extensions - else : - extensions = [] - - mem = memory() - start = time.clock() - self.md = markdown.Markdown(extensions=extensions, safe_mode = safe_mode, output_format=output_format) - construction_time = time.clock() - start - construction_mem = memory(mem) - - self.benchmark_buffer = "construction:%f:%f\n" % (construction_time, - construction_mem) - - html_diff_file_path = os.path.join(TMP_DIR, os.path.split(dir)[-1]) + ".html" - self.html_diff_file = codecs.open(html_diff_file_path, "w", encoding=encoding) - self.html_diff_file.write(DIFF_FILE_TEMPLATE) - - self.diffs_buffer = "" - - tests = [x.replace(".txt", "") - for x in os.listdir(dir) if x.endswith(".txt")] - tests.sort() - for test in tests : - self.run_test(dir, test, repeat) - - self.html_diff_file.write("</table>") - - if sys.version < "3.0": - self.html_diff_file.write(self.diffs_buffer.decode("utf8")) - - self.html_diff_file.write(FOOTER) - self.html_diff_file.close() - print "Diff written to %s" % html_diff_file_path - - benchmark_output_file_name = benchmark_file_name - - if not WRITE_BENCHMARK: - benchmark_output_file_name += ".tmp" - - self.benchmark_file = open(benchmark_output_file_name, "w") - self.benchmark_file.write(self.benchmark_buffer) - self.benchmark_file.close() - - -#################### - - - def run_test(self, dir, test, repeat): - - print "--- %s ---" % test - self.html_diff_file.write("<tr><td>%s</td>" % test) - input_file = os.path.join(dir, test + ".txt") - output_file = os.path.join(dir, test + ".html") - - expected_output = codecs.open(output_file, encoding=self.encoding).read() - input = codecs.open(input_file, encoding=self.encoding).read() - actual_output = "" - actual_lines = [] - self.md.source = "" - gc.collect() - mem = memory() - start = time.clock() - for x in repeat: - actual_output = self.md.convert(input) - conversion_time = time.clock() - start - conversion_mem = memory(mem) - self.md.reset() - - expected_lines = [x.encode("utf8") for x in smart_split(expected_output)] - actual_lines = [x.encode("utf8") for x in smart_split(actual_output)] - - #diff = difflib.ndiff(expected_output.split("\n"), - # actual_output.split("\n")) - - diff = [x for x in differ.compare(expected_lines, - actual_lines) - if not x.startswith(" ")] - - if not diff: - self.html_diff_file.write("<td class='ok'>OK</td>") - else : - self.failedTests.append(test) - self.html_diff_file.write("<td class='failed'>" + - "<a href='#diff-%s'>FAILED</a></td>" % test) - print "MISMATCH on %s/%s.txt" % (dir, test) - print - for line in diff : - print line - if htmldiff!=None : - htmlDiff = htmldiff.make_table(expected_lines, actual_lines, - context=True) - htmlDiff = "\n".join( [x for x in htmlDiff.splitlines() - if x.strip().startswith("<tr>")] ) - self.diffs_buffer += "<a name='diff-%s'/><h2>%s</h2>" % (test, test) - self.diffs_buffer += DIFF_TABLE_TEMPLATE % htmlDiff - - expected_time, expected_mem = self.saved_benchmarks.get(test, ("na", "na")) - - self.html_diff_file.write(get_benchmark_html(conversion_time, expected_time)) - self.html_diff_file.write(get_benchmark_html(conversion_mem, expected_mem)) - self.html_diff_file.write("</tr>\n") - - self.benchmark_buffer += "%s:%f:%f\n" % (test, - conversion_time, conversion_mem) - - - - - -def get_benchmark_html (actual, expected) : - buffer = "" - if not expected == "na": - if actual > expected * 1.5: - tdiff = "failed" - elif actual * 1.5 < expected : - tdiff = "ok" - else : - tdiff = "same" - if ( (actual <= 0 and expected < 0.015) or - (expected <= 0 and actual < 0.015)) : - tdiff = "same" - else : - tdiff = "same" - buffer += "<td class='%s'>%.2f</td>" % (tdiff, actual) - if not expected == "na": - buffer += "<td class='gray'>%.2f</td>" % (expected) - return buffer - - -def run_tests() : - - tester = TestRunner() - #test.test_directory("tests/basic") - tester.test_directory("tests/markdown-test", measure_time=True) - tester.test_directory("tests/misc", measure_time=True) - tester.test_directory("tests/extensions-x-tables") - tester.test_directory("tests/extensions-x-footnotes") - #tester.test_directory("tests/extensions-x-ext1-ext2") - tester.test_directory("tests/safe_mode", measure_time=True, safe_mode="escape") - tester.test_directory("tests/extensions-x-wikilinks") - tester.test_directory("tests/extensions-x-toc") - tester.test_directory("tests/extensions-x-def_list") - tester.test_directory("tests/extensions-x-abbr") - tester.test_directory("tests/html4", output_format='html4') - - try: - import pygments - except ImportError: - # Dependancy not avalable - skip test - pass - else: - tester.test_directory("tests/extensions-x-codehilite") - - print "\n### Final result ###" - if len(tester.failedTests): - print "%d failed tests: %s" % (len(tester.failedTests), str(tester.failedTests)) - else: - print "All tests passed, no errors!" - -run_tests() - - - - |