aboutsummaryrefslogtreecommitdiff
path: root/pathological_tests.py
blob: 999a467859cf5ebcd43d6752ad72e3a973e289e5 (plain)
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from ctypes import CDLL, c_char_p, c_long
  4. from subprocess import *
  5. import re
  6. import argparse
  7. import sys
  8. import platform
  9. if __name__ == "__main__":
  10. parser = argparse.ArgumentParser(description='Run cmark tests.')
  11. parser.add_argument('--program', dest='program', nargs='?', default=None,
  12. help='program to test')
  13. parser.add_argument('--library-dir', dest='library_dir', nargs='?',
  14. default=None, help='directory containing dynamic library')
  15. args = parser.parse_args(sys.argv[1:])
  16. if not args.program:
  17. sysname = platform.system()
  18. libname = "libcmark"
  19. if sysname == 'Darwin':
  20. libname += ".dylib"
  21. elif sysname == 'Windows':
  22. libname += ".dll"
  23. else:
  24. libname += ".so"
  25. if args and args.library_dir:
  26. libpath = args.library_dir + "/" + libname
  27. else:
  28. libpath = "build/src/" + libname
  29. cmark = CDLL(libpath)
  30. markdown = cmark.cmark_markdown_to_html
  31. markdown.restype = c_char_p
  32. markdown.argtypes = [c_char_p, c_long]
  33. def md2html(text, prog):
  34. if prog:
  35. p1 = Popen(prog.split(), stdout=PIPE, stdin=PIPE, stderr=PIPE)
  36. [result, err] = p1.communicate(input=text)
  37. return [p1.returncode, result, err]
  38. else:
  39. return [0, markdown(text, len(text)), '']
  40. pathological = {
  41. "nested strong emph":
  42. (("*a **a " * 100000) + "b" + (" a** a*" * 100000),
  43. "<p>" + ("<em>a <strong>a " * 100000) + "b" +
  44. (" a</strong> a</em>" * 100000) + "</p>"),
  45. "nested brackets":
  46. (("[" * 50000) + "a" + ("]" * 50000),
  47. "<p>" + ("[" * 50000) + "a" + ("]" * 50000) + "</p>")
  48. }
  49. whitespace_re = re.compile('/s+/')
  50. passed = 0
  51. errored = 0
  52. failed = 0
  53. print "Testing pathological cases:"
  54. for description in pathological:
  55. print description
  56. (inp, expected) = pathological[description]
  57. [rc, actual, err] = md2html(inp, args.program)
  58. if rc != 0:
  59. errored += 1
  60. print description
  61. print "program returned error code %d" % rc
  62. print(err)
  63. elif whitespace_re.sub(' ', actual.rstrip()) == expected.rstrip():
  64. passed += 1
  65. else:
  66. print description, 'failed'
  67. print(actual)
  68. failed += 1
  69. print "%d passed, %d failed, %d errored" % (passed, failed, errored)
  70. if (failed == 0 and errored == 0):
  71. exit(0)
  72. else:
  73. exit(1)