|
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 |
|
12 #sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) |
|
13 |
|
14 # Local imports |
|
15 from .. import pytree |
|
16 from .. import refactor |
|
17 from ..pgen2 import driver |
|
18 |
|
19 test_dir = os.path.dirname(__file__) |
|
20 proj_dir = os.path.normpath(os.path.join(test_dir, "..")) |
|
21 grammar_path = os.path.join(test_dir, "..", "Grammar.txt") |
|
22 grammar = driver.load_grammar(grammar_path) |
|
23 driver = driver.Driver(grammar, convert=pytree.convert) |
|
24 |
|
25 def parse_string(string): |
|
26 return driver.parse_string(reformat(string), debug=True) |
|
27 |
|
28 # Python 2.3's TestSuite is not iter()-able |
|
29 if sys.version_info < (2, 4): |
|
30 def TestSuite_iter(self): |
|
31 return iter(self._tests) |
|
32 unittest.TestSuite.__iter__ = TestSuite_iter |
|
33 |
|
34 def run_all_tests(test_mod=None, tests=None): |
|
35 if tests is None: |
|
36 tests = unittest.TestLoader().loadTestsFromModule(test_mod) |
|
37 unittest.TextTestRunner(verbosity=2).run(tests) |
|
38 |
|
39 def reformat(string): |
|
40 return dedent(string) + "\n\n" |
|
41 |
|
42 def get_refactorer(fixers=None, options=None): |
|
43 """ |
|
44 A convenience function for creating a RefactoringTool for tests. |
|
45 |
|
46 fixers is a list of fixers for the RefactoringTool to use. By default |
|
47 "lib2to3.fixes.*" is used. options is an optional dictionary of options to |
|
48 be passed to the RefactoringTool. |
|
49 """ |
|
50 if fixers is not None: |
|
51 fixers = ["lib2to3.fixes.fix_" + fix for fix in fixers] |
|
52 else: |
|
53 fixers = refactor.get_fixers_from_package("lib2to3.fixes") |
|
54 options = options or {} |
|
55 return refactor.RefactoringTool(fixers, options, explicit=True) |
|
56 |
|
57 def all_project_files(): |
|
58 for dirpath, dirnames, filenames in os.walk(proj_dir): |
|
59 for filename in filenames: |
|
60 if filename.endswith(".py"): |
|
61 yield os.path.join(dirpath, filename) |
|
62 |
|
63 TestCase = unittest.TestCase |