aboutsummaryrefslogtreecommitdiff
path: root/test/spec_tests.py
blob: 99ac0dda4441bbb24a01a6810d02fd3131e52515 (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. args = parser.parse_args(sys.argv[1:])
  30. def print_test_header(headertext, example_number, start_line, end_line):
  31. print "Example %d (lines %d-%d) %s" % (example_number,start_line,end_line,headertext)
  32. def do_test(test, normalize):
  33. [retcode, actual_html, err] = cmark.to_html(test['markdown'])
  34. if retcode == 0:
  35. expected_html = test['html']
  36. unicode_error = None
  37. if normalize:
  38. try:
  39. passed = normalize_html(actual_html) == normalize_html(expected_html)
  40. except UnicodeDecodeError, e:
  41. unicode_error = e
  42. passed = False
  43. else:
  44. passed = actual_html == expected_html
  45. if passed:
  46. return 'pass'
  47. else:
  48. print_test_header(test['section'], test['example'], test['start_line'], test['end_line'])
  49. sys.stdout.write(test['markdown'])
  50. if unicode_error:
  51. print "Unicode error: " + str(unicode_error)
  52. print repr(expected_html)
  53. print repr(actual_html)
  54. else:
  55. expected_html_lines = expected_html.splitlines(True)
  56. actual_html_lines = actual_html.splitlines(True)
  57. for diffline in unified_diff(expected_html_lines, actual_html_lines,
  58. "expected HTML", "actual HTML"):
  59. sys.stdout.write(diffline)
  60. sys.stdout.write('\n')
  61. return 'fail'
  62. else:
  63. print_test_header(test['section'], test['example'], test['start_line'], test['end_line'])
  64. print "program returned error code %d" % retcode
  65. print(err)
  66. return 'error'
  67. def get_tests(specfile):
  68. line_number = 0
  69. start_line = 0
  70. end_line = 0
  71. example_number = 0
  72. markdown_lines = []
  73. html_lines = []
  74. state = 0 # 0 regular text, 1 markdown example, 2 html output
  75. headertext = ''
  76. tests = []
  77. header_re = re.compile('#+ ')
  78. with open(specfile, 'r') as specf:
  79. for line in specf:
  80. line_number = line_number + 1
  81. if state == 0 and re.match(header_re, line):
  82. headertext = header_re.sub('', line).strip()
  83. if line.strip() == ".":
  84. state = (state + 1) % 3
  85. if state == 0:
  86. example_number = example_number + 1
  87. end_line = line_number
  88. tests.append({
  89. "markdown":''.join(markdown_lines).replace('→',"\t"),
  90. "html":''.join(html_lines),
  91. "example": example_number,
  92. "start_line": start_line,
  93. "end_line": end_line,
  94. "section": headertext})
  95. start_line = 0
  96. markdown_lines = []
  97. html_lines = []
  98. elif state == 1:
  99. if start_line == 0:
  100. start_line = line_number - 1
  101. markdown_lines.append(line)
  102. elif state == 2:
  103. html_lines.append(line)
  104. return tests
  105. def do_tests(cmark, tests, pattern, normalize):
  106. passed = 0
  107. errored = 0
  108. failed = 0
  109. skipped = 0
  110. if pattern:
  111. pattern_re = re.compile(pattern, re.IGNORECASE)
  112. else:
  113. pattern_re = re.compile('.')
  114. for test in tests:
  115. if re.search(pattern_re, test['section']):
  116. result = do_test(test, normalize)
  117. if result == 'pass':
  118. passed += 1
  119. elif result == 'fail':
  120. failed += 1
  121. else:
  122. errored += 1
  123. else:
  124. skipped += 1
  125. print "%d passed, %d failed, %d errored, %d skipped" % (passed, failed, errored, skipped)
  126. return (failed == 0 and errored == 0)
  127. if __name__ == "__main__":
  128. if args.debug_normalization:
  129. print normalize_html(sys.stdin.read())
  130. exit(0)
  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)