1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4import sys
5from difflib import unified_diff
6import argparse
7import re
8import json
9from cmark import CMark
10from normalize import normalize_html
11
12if __name__ == "__main__":
13    parser = argparse.ArgumentParser(description='Run cmark tests.')
14    parser.add_argument('--program', dest='program', nargs='?', default=None,
15            help='program to test')
16    parser.add_argument('--spec', dest='spec', nargs='?', default='spec.txt',
17            help='path to spec')
18    parser.add_argument('--pattern', dest='pattern', nargs='?',
19            default=None, help='limit to sections matching regex pattern')
20    parser.add_argument('--library-dir', dest='library_dir', nargs='?',
21            default=None, help='directory containing dynamic library')
22    parser.add_argument('--no-normalize', dest='normalize',
23            action='store_const', const=False, default=True,
24            help='do not normalize HTML')
25    parser.add_argument('--dump-tests', dest='dump_tests',
26            action='store_const', const=True, default=False,
27            help='dump tests in JSON format')
28    parser.add_argument('--debug-normalization', dest='debug_normalization',
29            action='store_const', const=True,
30            default=False, help='filter stdin through normalizer for testing')
31    args = parser.parse_args(sys.argv[1:])
32
33def print_test_header(headertext, example_number, start_line, end_line):
34    print "Example %d (lines %d-%d) %s" % (example_number,start_line,end_line,headertext)
35
36def do_test(test, normalize):
37    [retcode, actual_html, err] = cmark.to_html(test['markdown'])
38    if retcode == 0:
39        expected_html = test['html']
40        if normalize:
41            passed = normalize_html(actual_html) == normalize_html(expected_html)
42        else:
43            passed = actual_html == expected_html
44        if passed:
45            return 'pass'
46        else:
47            print_test_header(test['section'], test['example'], test['start_line'], test['end_line'])
48            sys.stdout.write(test['markdown'])
49            expected_html_lines = expected_html.splitlines(True)
50            actual_html_lines = actual_html.splitlines(True)
51            for diffline in unified_diff(expected_html_lines, actual_html_lines,
52                            "expected HTML", "actual HTML"):
53                sys.stdout.write(diffline)
54            sys.stdout.write('\n')
55            return 'fail'
56    else:
57        print_test_header(test['section'], test['example'], test['start_line'], test['end_line'])
58        print "program returned error code %d" % retcode
59        print(err)
60        return 'error'
61
62def get_tests(specfile):
63    line_number = 0
64    start_line = 0
65    end_line = 0
66    example_number = 0
67    markdown_lines = []
68    html_lines = []
69    state = 0  # 0 regular text, 1 markdown example, 2 html output
70    headertext = ''
71    tests = []
72
73    header_re = re.compile('#+ ')
74
75    with open(specfile, 'r') as specf:
76        for line in specf:
77            line_number = line_number + 1
78            if state == 0 and re.match(header_re, line):
79                headertext = header_re.sub('', line).strip()
80            if line.strip() == ".":
81                state = (state + 1) % 3
82                if state == 0:
83                    example_number = example_number + 1
84                    end_line = line_number
85                    tests.append({
86                        "markdown":''.join(markdown_lines).replace('→',"\t"),
87                        "html":''.join(html_lines),
88                        "example": example_number,
89                        "start_line": start_line,
90                        "end_line": end_line,
91                        "section": headertext})
92                    start_line = 0
93                    markdown_lines = []
94                    html_lines = []
95            elif state == 1:
96                if start_line == 0:
97                    start_line = line_number - 1
98                markdown_lines.append(line)
99            elif state == 2:
100                html_lines.append(line)
101    return tests
102
103def do_tests(cmark, tests, pattern, normalize):
104    passed = 0
105    errored = 0
106    failed = 0
107    skipped = 0
108    if pattern:
109        pattern_re = re.compile(pattern, re.IGNORECASE)
110    else:
111        pattern_re = re.compile('.')
112    for test in tests:
113        if re.search(pattern_re, test['section']):
114            result = do_test(test, normalize)
115            if result == 'pass':
116                passed += 1
117            elif result == 'fail':
118                failed += 1
119            else:
120                errored += 1
121        else:
122            skipped += 1
123    print "%d passed, %d failed, %d errored, %d skipped" % (passed, failed, errored, skipped)
124    return (failed == 0 and errored == 0)
125
126if __name__ == "__main__":
127    if args.debug_normalization:
128        print normalize_html(sys.stdin.read())
129        exit(0)
130
131    tests = get_tests(args.spec)
132    if args.dump_tests:
133        print json.dumps(tests, ensure_ascii=False, indent=2)
134        exit(0)
135    else:
136        cmark = CMark(prog=args.program, library_dir=args.library_dir)
137        if do_tests(cmark, tests, args.pattern, args.normalize):
138            exit(0)
139        else:
140            exit(1)
141