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