1"""Support code for test_*.py files""" 2# Author: Collin Winter 3 4# Python imports 5import unittest 6import os 7import os.path 8from textwrap import dedent 9 10# Local imports 11from lib2to3 import pytree, refactor 12from lib2to3.pgen2 import driver as pgen2_driver 13 14test_dir = os.path.dirname(__file__) 15proj_dir = os.path.normpath(os.path.join(test_dir, "..")) 16grammar_path = os.path.join(test_dir, "..", "Grammar.txt") 17grammar = pgen2_driver.load_grammar(grammar_path) 18grammar_no_print_statement = pgen2_driver.load_grammar(grammar_path) 19del grammar_no_print_statement.keywords["print"] 20driver = pgen2_driver.Driver(grammar, convert=pytree.convert) 21driver_no_print_statement = pgen2_driver.Driver( 22 grammar_no_print_statement, 23 convert=pytree.convert 24) 25 26def parse_string(string): 27 return driver.parse_string(reformat(string), debug=True) 28 29def 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 34def reformat(string): 35 return dedent(string) + "\n\n" 36 37def 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 52def 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 58TestCase = unittest.TestCase 59