Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 1 | """Support code for test_*.py files""" |
| 2 | # Author: Collin Winter |
| 3 | |
| 4 | # Python imports |
| 5 | import unittest |
| 6 | import sys |
| 7 | import os |
| 8 | import os.path |
| 9 | import re |
| 10 | from textwrap import dedent |
| 11 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 12 | # Local imports |
Benjamin Peterson | d481e3d | 2009-05-09 19:42:23 +0000 | [diff] [blame] | 13 | from lib2to3 import pytree, refactor |
| 14 | from lib2to3.pgen2 import driver |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 15 | |
| 16 | test_dir = os.path.dirname(__file__) |
| 17 | proj_dir = os.path.normpath(os.path.join(test_dir, "..")) |
| 18 | grammar_path = os.path.join(test_dir, "..", "Grammar.txt") |
| 19 | grammar = driver.load_grammar(grammar_path) |
| 20 | driver = driver.Driver(grammar, convert=pytree.convert) |
| 21 | |
| 22 | def parse_string(string): |
| 23 | return driver.parse_string(reformat(string), debug=True) |
| 24 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 25 | def 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 | |
| 30 | def reformat(string): |
| 31 | return dedent(string) + "\n\n" |
| 32 | |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 33 | def get_refactorer(fixer_pkg="lib2to3", fixers=None, options=None): |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 34 | """ |
| 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 Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 42 | fixers = [fixer_pkg + ".fixes.fix_" + fix for fix in fixers] |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 43 | else: |
Benjamin Peterson | 2c3ac6b | 2009-06-11 23:47:38 +0000 | [diff] [blame] | 44 | fixers = refactor.get_fixers_from_package(fixer_pkg + ".fixes") |
Benjamin Peterson | 8951b61 | 2008-09-03 02:27:16 +0000 | [diff] [blame] | 45 | options = options or {} |
| 46 | return refactor.RefactoringTool(fixers, options, explicit=True) |
| 47 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 48 | def 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 | |
| 54 | TestCase = unittest.TestCase |