/Demo/parser/test_parser.py

http://unladen-swallow.googlecode.com/ · Python · 48 lines · 36 code · 6 blank · 6 comment · 7 complexity · 405591025fb2694062c2f3e2d046ad3e MD5 · raw file

  1. #! /usr/bin/env python
  2. # (Force the script to use the latest build.)
  3. #
  4. # test_parser.py
  5. import parser, traceback
  6. _numFailed = 0
  7. def testChunk(t, fileName):
  8. global _numFailed
  9. print '----', fileName,
  10. try:
  11. ast = parser.suite(t)
  12. tup = parser.ast2tuple(ast)
  13. # this discards the first AST; a huge memory savings when running
  14. # against a large source file like Tkinter.py.
  15. ast = None
  16. new = parser.tuple2ast(tup)
  17. except parser.ParserError, err:
  18. print
  19. print 'parser module raised exception on input file', fileName + ':'
  20. traceback.print_exc()
  21. _numFailed = _numFailed + 1
  22. else:
  23. if tup != parser.ast2tuple(new):
  24. print
  25. print 'parser module failed on input file', fileName
  26. _numFailed = _numFailed + 1
  27. else:
  28. print 'o.k.'
  29. def testFile(fileName):
  30. t = open(fileName).read()
  31. testChunk(t, fileName)
  32. def test():
  33. import sys
  34. args = sys.argv[1:]
  35. if not args:
  36. import glob
  37. args = glob.glob("*.py")
  38. args.sort()
  39. map(testFile, args)
  40. sys.exit(_numFailed != 0)
  41. if __name__ == '__main__':
  42. test()