Debian Codegraph @

test/roundtrip_tests.py

raw
1 import re
2 import sys
3 from spec_tests import get_tests, do_test
4 from cmark import CMark
5 import argparse
6
7 parser = argparse.ArgumentParser(description='Run cmark roundtrip tests.')
8 parser.add_argument('-p', '--program', dest='program', nargs='?', default=None,
9 help='program to test')
10 parser.add_argument('-s', '--spec', dest='spec', nargs='?', default='spec.txt',
11 help='path to spec')
12 parser.add_argument('-P', '--pattern', dest='pattern', nargs='?',
13 default=None, help='limit to sections matching regex pattern')
14 parser.add_argument('--library-dir', dest='library_dir', nargs='?',
15 default=None, help='directory containing dynamic library')
16 parser.add_argument('--no-normalize', dest='normalize',
17 action='store_const', const=False, default=True,
18 help='do not normalize HTML')
19 parser.add_argument('-n', '--number', type=int, default=None,
20 help='only consider the test with the given number')
21 args = parser.parse_args(sys.argv[1:])
22
23 spec = sys.argv[1]
24
25 def converter(md):
26 cmark = CMark(prog=args.program, library_dir=args.library_dir)
27 [ec, result, err] = cmark.to_commonmark(md)
28 if ec == 0:
29 [ec, html, err] = cmark.to_html(result)
30 if ec == 0:
31 # In the commonmark writer we insert dummy HTML
32 # comments between lists, and between lists and code
33 # blocks. Strip these out, since the spec uses
34 # two blank lines instead:
35 return [ec, re.sub('<!-- end list -->\n', '', html), '']
36 else:
37 return [ec, html, err]
38 else:
39 return [ec, result, err]
40
41 tests = get_tests(args.spec)
42 result_counts = {'pass': 0, 'fail': 0, 'error': 0, 'skip': 0}
43 for test in tests:
44 do_test(converter, test, args.normalize, result_counts)
45
46 exit(result_counts['fail'] + result_counts['error'])