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