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