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