diff options
author | John MacFarlane <jgm@berkeley.edu> | 2014-11-29 18:32:44 -0800 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2014-11-29 18:32:44 -0800 |
commit | 9fedb89af38b5a43eb0f7944e938dbbdb17a499d (patch) | |
tree | 6b69dd19edcd3abc5b0a673bcbb31e592262bdaa | |
parent | 2570a08178f95dc9340f13924d412169dd57fdbb (diff) |
Factored out cmark.py from test programs.
-rw-r--r-- | test/cmark.py | 38 | ||||
-rw-r--r-- | test/pathological_tests.py | 32 | ||||
-rwxr-xr-x | test/spec_tests.py | 43 |
3 files changed, 48 insertions, 65 deletions
diff --git a/test/cmark.py b/test/cmark.py new file mode 100644 index 0000000..432a3e0 --- /dev/null +++ b/test/cmark.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from ctypes import CDLL, c_char_p, c_long +from subprocess import * +import platform + +def pipe_through_prog(prog, text): + p1 = Popen(prog.split(), stdout=PIPE, stdin=PIPE, stderr=PIPE) + [result, err] = p1.communicate(input=text) + return [p1.returncode, result, err] + +def use_library(lib, text): + return [0, lib(text, len(text)), ''] + +class CMark: + def __init__(self, prog=None, library_dir=None): + self.prog = prog + if prog: + self.to_html = lambda x: pipe_through_prog(prog, x) + else: + sysname = platform.system() + libname = "libcmark" + if sysname == 'Darwin': + libname += ".dylib" + elif sysname == 'Windows': + libname += ".dll" + else: + libname += ".so" + if library_dir: + libpath = library_dir + "/" + libname + else: + libpath = "build/src/" + libname + cmark = CDLL(libpath) + markdown = cmark.cmark_markdown_to_html + markdown.restype = c_char_p + markdown.argtypes = [c_char_p, c_long] + self.to_html = lambda x: use_library(markdown, x) diff --git a/test/pathological_tests.py b/test/pathological_tests.py index 999a467..3cf1662 100644 --- a/test/pathological_tests.py +++ b/test/pathological_tests.py @@ -1,12 +1,11 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from ctypes import CDLL, c_char_p, c_long -from subprocess import * import re import argparse import sys import platform +from cmark import CMark if __name__ == "__main__": parser = argparse.ArgumentParser(description='Run cmark tests.') @@ -16,32 +15,7 @@ if __name__ == "__main__": default=None, help='directory containing dynamic library') args = parser.parse_args(sys.argv[1:]) -if not args.program: - sysname = platform.system() - libname = "libcmark" - if sysname == 'Darwin': - libname += ".dylib" - elif sysname == 'Windows': - libname += ".dll" - else: - libname += ".so" - if args and args.library_dir: - libpath = args.library_dir + "/" + libname - else: - libpath = "build/src/" + libname - cmark = CDLL(libpath) - - markdown = cmark.cmark_markdown_to_html - markdown.restype = c_char_p - markdown.argtypes = [c_char_p, c_long] - -def md2html(text, prog): - if prog: - p1 = Popen(prog.split(), stdout=PIPE, stdin=PIPE, stderr=PIPE) - [result, err] = p1.communicate(input=text) - return [p1.returncode, result, err] - else: - return [0, markdown(text, len(text)), ''] +cmark = CMark(prog=args.program, library_dir=args.library_dir) pathological = { "nested strong emph": @@ -62,7 +36,7 @@ print "Testing pathological cases:" for description in pathological: print description (inp, expected) = pathological[description] - [rc, actual, err] = md2html(inp, args.program) + [rc, actual, err] = cmark.to_html(inp) if rc != 0: errored += 1 print description diff --git a/test/spec_tests.py b/test/spec_tests.py index 6e0d4cc..394d1a2 100755 --- a/test/spec_tests.py +++ b/test/spec_tests.py @@ -1,17 +1,15 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from ctypes import CDLL, c_char_p, c_long import sys -import platform from difflib import unified_diff -from subprocess import * import argparse from HTMLParser import HTMLParser, HTMLParseError from htmlentitydefs import name2codepoint import re import cgi import json +from cmark import CMark if __name__ == "__main__": parser = argparse.ArgumentParser(description='Run cmark tests.') @@ -34,32 +32,7 @@ if __name__ == "__main__": default=False, help='filter stdin through normalizer for testing') args = parser.parse_args(sys.argv[1:]) -if not (args.program or args.dump_tests or args.debug_normalization): - sysname = platform.system() - libname = "libcmark" - if sysname == 'Darwin': - libname += ".dylib" - elif sysname == 'Windows': - libname += ".dll" - else: - libname += ".so" - if args and args.library_dir: - libpath = args.library_dir + "/" + libname - else: - libpath = "build/src/" + libname - cmark = CDLL(libpath) - - markdown = cmark.cmark_markdown_to_html - markdown.restype = c_char_p - markdown.argtypes = [c_char_p, c_long] - -def md2html(text, prog): - if prog: - p1 = Popen(prog.split(), stdout=PIPE, stdin=PIPE, stderr=PIPE) - [result, err] = p1.communicate(input=text) - return [p1.returncode, result, err] - else: - return [0, markdown(text, len(text)), ''] +cmark = CMark(prog=args.program, library_dir=args.library_dir) # Normalization code, adapted from # https://github.com/karlcow/markdown-testsuite/ @@ -201,9 +174,9 @@ def print_test_header(headertext, example_number, start_line, end_line): print "Example %d (lines %d-%d) %s" % (example_number,start_line,end_line,headertext) def do_test(markdown_lines, expected_html_lines, headertext, - example_number, start_line, end_line, prog, normalize): + example_number, start_line, end_line, normalize): real_markdown_text = ''.join(markdown_lines).replace('→','\t') - [retcode, actual_html, err] = md2html(real_markdown_text, prog) + [retcode, actual_html, err] = cmark.to_html(real_markdown_text) if retcode == 0: actual_html_lines = actual_html.splitlines(True) expected_html = ''.join(expected_html_lines) @@ -227,7 +200,7 @@ def do_test(markdown_lines, expected_html_lines, headertext, print(err) return 'error' -def do_tests(specfile, prog, pattern, normalize, dump_tests): +def do_tests(specfile, pattern, normalize, dump_tests): line_number = 0 start_line = 0 end_line = 0 @@ -273,8 +246,7 @@ def do_tests(specfile, prog, pattern, normalize, dump_tests): else: result = do_test(markdown_lines, html_lines, headertext, example_number, - start_line, end_line, prog, - normalize) + start_line, end_line, normalize) if result == 'pass': passed = passed + 1 elif result == 'fail': @@ -301,8 +273,7 @@ def do_tests(specfile, prog, pattern, normalize, dump_tests): if __name__ == "__main__": if args.debug_normalization: print normalize_html(sys.stdin.read()) - elif do_tests(args.spec, args.program, args.pattern, args.normalize, - args.dump_tests): + elif do_tests(args.spec, args.pattern, args.normalize, args.dump_tests): exit(0) else: exit(1) |