| 1 |
"""Support code for test_*.py files""" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import unittest |
| 6 |
import os |
| 7 |
import os.path |
| 8 |
from textwrap import dedent |
| 9 |
|
| 10 |
|
| 11 |
from lib2to3 import pytree, refactor |
| 12 |
from lib2to3.pgen2 import driver as pgen2_driver |
| 13 |
|
| 14 |
test_dir = os.path.dirname(__file__) |
| 15 |
proj_dir = os.path.normpath(os.path.join(test_dir, "..")) |
| 16 |
grammar_path = os.path.join(test_dir, "..", "Grammar.txt") |
| 17 |
grammar = pgen2_driver.load_grammar(grammar_path) |
| 18 |
grammar_no_print_statement = pgen2_driver.load_grammar(grammar_path) |
| 19 |
del grammar_no_print_statement.keywords["print"] |
| 20 |
driver = pgen2_driver.Driver(grammar, convert=pytree.convert) |
| 21 |
driver_no_print_statement = pgen2_driver.Driver( |
| 22 |
grammar_no_print_statement, |
| 23 |
convert=pytree.convert |
| 24 |
) |
| 25 |
|
| 26 |
def parse_string(string): |
| 27 |
return driver.parse_string(reformat(string), debug=True) |
| 28 |
|
| 29 |
def run_all_tests(test_mod=None, tests=None): |
| 30 |
if tests is None: |
| 31 |
tests = unittest.TestLoader().loadTestsFromModule(test_mod) |
| 32 |
unittest.TextTestRunner(verbosity=2).run(tests) |
| 33 |
|
| 34 |
def reformat(string): |
| 35 |
return dedent(string) + "\n\n" |
| 36 |
|
| 37 |
def get_refactorer(fixer_pkg="lib2to3", fixers=None, options=None): |
| 38 |
""" |
| 39 |
A convenience function for creating a RefactoringTool for tests. |
| 40 |
|
| 41 |
fixers is a list of fixers for the RefactoringTool to use. By default |
| 42 |
"lib2to3.fixes.*" is used. options is an optional dictionary of options to |
| 43 |
be passed to the RefactoringTool. |
| 44 |
""" |
| 45 |
if fixers is not None: |
| 46 |
fixers = [fixer_pkg + ".fixes.fix_" + fix for fix in fixers] |
| 47 |
else: |
| 48 |
fixers = refactor.get_fixers_from_package(fixer_pkg + ".fixes") |
| 49 |
options = options or {} |
| 50 |
return refactor.RefactoringTool(fixers, options, explicit=True) |
| 51 |
|
| 52 |
def all_project_files(): |
| 53 |
for dirpath, dirnames, filenames in os.walk(proj_dir): |
| 54 |
for filename in filenames: |
| 55 |
if filename.endswith(".py"): |
| 56 |
yield os.path.join(dirpath, filename) |
| 57 |
|
| 58 |
TestCase = unittest.TestCase |