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