/Lib/lib2to3/tests/support.py

http://unladen-swallow.googlecode.com/ · Python · 63 lines · 42 code · 11 blank · 10 comment · 9 complexity · a6fa869036ac2d67729bfaef53816ea8 MD5 · raw file

  1. """Support code for test_*.py files"""
  2. # Author: Collin Winter
  3. # Python imports
  4. import unittest
  5. import sys
  6. import os
  7. import os.path
  8. import re
  9. from textwrap import dedent
  10. #sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
  11. # Local imports
  12. from .. import pytree
  13. from .. import refactor
  14. from ..pgen2 import driver
  15. test_dir = os.path.dirname(__file__)
  16. proj_dir = os.path.normpath(os.path.join(test_dir, ".."))
  17. grammar_path = os.path.join(test_dir, "..", "Grammar.txt")
  18. grammar = driver.load_grammar(grammar_path)
  19. driver = driver.Driver(grammar, convert=pytree.convert)
  20. def parse_string(string):
  21. return driver.parse_string(reformat(string), debug=True)
  22. # Python 2.3's TestSuite is not iter()-able
  23. if sys.version_info < (2, 4):
  24. def TestSuite_iter(self):
  25. return iter(self._tests)
  26. unittest.TestSuite.__iter__ = TestSuite_iter
  27. def run_all_tests(test_mod=None, tests=None):
  28. if tests is None:
  29. tests = unittest.TestLoader().loadTestsFromModule(test_mod)
  30. unittest.TextTestRunner(verbosity=2).run(tests)
  31. def reformat(string):
  32. return dedent(string) + "\n\n"
  33. def get_refactorer(fixers=None, options=None):
  34. """
  35. A convenience function for creating a RefactoringTool for tests.
  36. fixers is a list of fixers for the RefactoringTool to use. By default
  37. "lib2to3.fixes.*" is used. options is an optional dictionary of options to
  38. be passed to the RefactoringTool.
  39. """
  40. if fixers is not None:
  41. fixers = ["lib2to3.fixes.fix_" + fix for fix in fixers]
  42. else:
  43. fixers = refactor.get_fixers_from_package("lib2to3.fixes")
  44. options = options or {}
  45. return refactor.RefactoringTool(fixers, options, explicit=True)
  46. def all_project_files():
  47. for dirpath, dirnames, filenames in os.walk(proj_dir):
  48. for filename in filenames:
  49. if filename.endswith(".py"):
  50. yield os.path.join(dirpath, filename)
  51. TestCase = unittest.TestCase