aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2014-11-29 22:12:09 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2014-11-29 22:12:09 -0800
commitf85e4d89559d396d90e822d5876e3833fdfb69d6 (patch)
treefcb5ec83d48b22a6821264008dbaaeafc1f81e6c
parent2528c87c0cf08e02eb3e201c149cb3acf521e0c8 (diff)
Reorganized code in spec_tests.py.
-rwxr-xr-xtest/spec_tests.py91
1 files changed, 45 insertions, 46 deletions
diff --git a/test/spec_tests.py b/test/spec_tests.py
index e6ab9d5..8abc835 100755
--- a/test/spec_tests.py
+++ b/test/spec_tests.py
@@ -36,13 +36,10 @@ if not args.dump_tests:
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, normalize):
- real_markdown_text = ''.join(markdown_lines).replace('→','\t')
- [retcode, actual_html, err] = cmark.to_html(real_markdown_text)
+def do_test(test, normalize):
+ [retcode, actual_html, err] = cmark.to_html(test['markdown'])
if retcode == 0:
- actual_html_lines = actual_html.splitlines(True)
- expected_html = ''.join(expected_html_lines)
+ expected_html = test['html']
if normalize:
passed = normalize_html(actual_html) == normalize_html(expected_html)
else:
@@ -51,71 +48,50 @@ def do_test(markdown_lines, expected_html_lines, headertext,
return 'pass'
else:
print_test_header(headertext, example_number,start_line,end_line)
- sys.stdout.write(real_markdown_text)
+ sys.stdout.write(test['markdown'])
+ expected_html_lines = '\n'.split(expected_html)
+ actual_html_lines = actual_html.splitlines(True)
for diffline in unified_diff(expected_html_lines, actual_html_lines,
"expected HTML", "actual HTML"):
sys.stdout.write(diffline)
sys.stdout.write('\n')
return 'fail'
else:
- print_test_header(headertext, example_number, start_line, end_line)
+ print_test_header(test['section'], test['example'], test['start_line'], test['end_line'])
print "program returned error code %d" % retcode
print(err)
return 'error'
-def do_tests(specfile, pattern, normalize, dump_tests):
+def get_tests(specfile):
line_number = 0
start_line = 0
end_line = 0
example_number = 0
- passed = 0
- failed = 0
- errored = 0
markdown_lines = []
html_lines = []
- active = True
state = 0 # 0 regular text, 1 markdown example, 2 html output
headertext = ''
- tests_json = []
+ tests = []
header_re = re.compile('#+ ')
- if pattern:
- pattern_re = re.compile(pattern, re.IGNORECASE)
with open(specfile, 'r') as specf:
for line in specf:
line_number = line_number + 1
if state == 0 and re.match(header_re, line):
headertext = header_re.sub('', line).strip()
- if pattern:
- if re.search(pattern_re, line):
- active = True
- else:
- active = False
if line.strip() == ".":
state = (state + 1) % 3
if state == 0:
example_number = example_number + 1
end_line = line_number
- if active:
- if dump_tests:
- tests_json.append({
- "markdown":''.join(markdown_lines).replace('→',"\t"),
- "html":''.join(html_lines),
- "example": example_number,
- "start_line": start_line,
- "end_line": end_line,
- "section": headertext})
- else:
- result = do_test(markdown_lines, html_lines,
- headertext, example_number,
- start_line, end_line, normalize)
- if result == 'pass':
- passed = passed + 1
- elif result == 'fail':
- failed = failed + 1
- else:
- errored = errored + 1
+ tests.append({
+ "markdown":''.join(markdown_lines).replace('→',"\t"),
+ "html":''.join(html_lines),
+ "example": example_number,
+ "start_line": start_line,
+ "end_line": end_line,
+ "section": headertext})
start_line = 0
markdown_lines = []
html_lines = []
@@ -125,18 +101,41 @@ def do_tests(specfile, pattern, normalize, dump_tests):
markdown_lines.append(line)
elif state == 2:
html_lines.append(line)
+ return tests
- if dump_tests:
- print json.dumps(tests_json, ensure_ascii=False, indent=2)
- return True
+def do_tests(tests, pattern, normalize):
+ passed = 0
+ errored = 0
+ failed = 0
+ skipped = 0
+ if pattern:
+ pattern_re = re.compile(pattern, re.IGNORECASE)
else:
- print "%d passed, %d failed, %d errored" % (passed, failed, errored)
- return (failed == 0 and errored == 0)
+ pattern_re = re.compile('.')
+ for test in tests:
+ if re.search(pattern_re, test['section']):
+ result = do_test(test, normalize)
+ if result == 'pass':
+ passed += 1
+ elif result == 'fail':
+ failed += 1
+ else:
+ errored += 1
+ else:
+ skipped += 1
+ print "%d passed, %d failed, %d errored, %d skipped" % (passed, failed, errored, skipped)
+ return (failed == 0 and errored == 0)
if __name__ == "__main__":
if args.debug_normalization:
print normalize_html(sys.stdin.read())
- elif do_tests(args.spec, args.pattern, args.normalize, args.dump_tests):
+ exit(0)
+
+ tests = get_tests(args.spec)
+ if args.dump_tests:
+ print json.dumps(tests, ensure_ascii=False, indent=2)
+ exit(0)
+ elif do_tests(tests, args.pattern, args.normalize):
exit(0)
else:
exit(1)