-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_commonmark.py
162 lines (127 loc) · 4.5 KB
/
test_commonmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from __future__ import print_function
import traceback
import re
import os
import sys
import argparse
from pprint import pprint, pformat
import commonmark
def print_exc_plus():
"""
Print the usual traceback information, followed by a listing of all the
local variables in each frame.
"""
msg = []
tb = sys.exc_info()[2]
while 1:
if not tb.tb_next:
break
tb = tb.tb_next
stack = []
f = tb.tb_frame
stack.append(f)
# while f:
# stack.append(f)
# f = f.f_back
stack.reverse()
msg.append(traceback.format_exc())
msg.append("Locals by frame, innermost last")
for frame in stack:
msg.append('')
msg.append("Frame %s in %s at line %s" % (frame.f_code.co_name,
frame.f_code.co_filename,
frame.f_lineno))
for key, value in frame.f_locals.items():
# We have to be careful not to cause a new error in our error
# printer! Calling str() on an unknown object could cause an
# error we don't want.
try:
msg.append("\t%20s = %r" % (key, value))
except:
msg.append("{} = <ERROR WHILE PRINTING VALUE>".format(key))
return '\n'.join(msg)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--stop', action='store_true')
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('-t', '--test', type=int, default=None)
args = parser.parse_args()
writer = commonmark.HtmlRenderer()
reader = commonmark.DocParser()
print('Reading spec...')
fp = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'spec.txt')
with open(fp, 'rU') as f:
text = f.read()
text = text.decode('utf8')
text = text.replace(u'\u2192', '\t')
text = re.sub(r'^<!-- END TESTS -->(.|[\n])*', '', text, flags=re.M)
print('Parsing tests...')
regex = re.compile(r'^\.\n(?P<markdown>[\s\S]*?)^\.\n(?P<html>[\s\S]*?)^\.$|^#{1,6} *(?P<section>.*)$', flags=re.M)
tests = []
test_number = 1
current_section = None
for match in regex.finditer(text):
if match.group('section'):
current_section = match.group('section')
# print('Found Section: {}'.format(current_section))
else:
test = {
'section': current_section,
'markdown': match.group('markdown'),
'html': match.group('html'),
'number': test_number,
}
# print('Found Test: {}'.format(test_number))
test_number += 1
tests.append(test)
passed = 0
failed = 0
print('Running Tests...')
cnt = len(tests)
current_section = None
for test in tests:
number = test['number']
if args.test is not None and number != args.test:
continue
section = test['section']
if section != current_section:
print('SECTION: {0}'.format(section))
current_section = section
markdown = test['markdown']
html = test['html']
try:
actual = writer.render_block(reader.parse(markdown))
except Exception:
actual = None
tmsg = print_exc_plus()
tmsg = tmsg.encode('ascii', errors='replace')
else:
tmsg = None
if actual == html:
passed += 1
print('TEST {0} of {1}: PASS'.format(number, cnt))
else:
failed += 1
print('TEST {0} of {1}: FAIL'.format(number, cnt))
if args.verbose:
print('.')
print(markdown.encode('ascii', errors='replace'))
print('.')
print(html.encode('ascii', errors='replace'))
print('.')
print(actual.encode('ascii', errors='replace') if actual else actual)
print('.')
if tmsg:
print(tmsg)
with open(r'C:\temp\commonmark_actual.txt', 'w') as f:
f.write(actual.encode('utf8'))
with open(r'C:\temp\commonmark_html.txt', 'w') as f:
f.write(html.encode('utf8'))
if args.stop:
print('DUMP')
pprint(reader.dump())
return None
print('PASSED: {}'.format(passed))
print('FAILED: {}'.format(failed))
if __name__ == '__main__':
main()