blob: 6f2d214bd4320b138f5f0c893584fb932ed2b229 [file] [log] [blame]
Martin v. Löwisef04c442008-03-19 05:04:44 +00001"""Support code for test_*.py files"""
2# Author: Collin Winter
3
4# Python imports
5import unittest
6import sys
7import os
8import os.path
9import re
10from textwrap import dedent
11
Martin v. Löwisef04c442008-03-19 05:04:44 +000012# Local imports
Benjamin Petersond481e3d2009-05-09 19:42:23 +000013from lib2to3 import pytree, refactor
14from lib2to3.pgen2 import driver
Martin v. Löwisef04c442008-03-19 05:04:44 +000015
16test_dir = os.path.dirname(__file__)
17proj_dir = os.path.normpath(os.path.join(test_dir, ".."))
18grammar_path = os.path.join(test_dir, "..", "Grammar.txt")
19grammar = driver.load_grammar(grammar_path)
20driver = driver.Driver(grammar, convert=pytree.convert)
21
22def parse_string(string):
23 return driver.parse_string(reformat(string), debug=True)
24
Martin v. Löwisef04c442008-03-19 05:04:44 +000025def run_all_tests(test_mod=None, tests=None):
26 if tests is None:
27 tests = unittest.TestLoader().loadTestsFromModule(test_mod)
28 unittest.TextTestRunner(verbosity=2).run(tests)
29
30def reformat(string):
31 return dedent(string) + "\n\n"
32
Benjamin Peterson2c3ac6b2009-06-11 23:47:38 +000033def get_refactorer(fixer_pkg="lib2to3", fixers=None, options=None):
Benjamin Peterson8951b612008-09-03 02:27:16 +000034 """
35 A convenience function for creating a RefactoringTool for tests.
36
37 fixers is a list of fixers for the RefactoringTool to use. By default
38 "lib2to3.fixes.*" is used. options is an optional dictionary of options to
39 be passed to the RefactoringTool.
40 """
41 if fixers is not None:
Benjamin Peterson2c3ac6b2009-06-11 23:47:38 +000042 fixers = [fixer_pkg + ".fixes.fix_" + fix for fix in fixers]
Benjamin Peterson8951b612008-09-03 02:27:16 +000043 else:
Benjamin Peterson2c3ac6b2009-06-11 23:47:38 +000044 fixers = refactor.get_fixers_from_package(fixer_pkg + ".fixes")
Benjamin Peterson8951b612008-09-03 02:27:16 +000045 options = options or {}
46 return refactor.RefactoringTool(fixers, options, explicit=True)
47
Martin v. Löwisef04c442008-03-19 05:04:44 +000048def all_project_files():
49 for dirpath, dirnames, filenames in os.walk(proj_dir):
50 for filename in filenames:
51 if filename.endswith(".py"):
52 yield os.path.join(dirpath, filename)
53
54TestCase = unittest.TestCase