PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 6ms RepoModel.GetById 35ms app.codeStats 0ms

/Lib/lib2to3/tests/support.py

http://unladen-swallow.googlecode.com/
Python | 63 lines | 44 code | 9 blank | 10 comment | 2 complexity | a6fa869036ac2d67729bfaef53816ea8 MD5 | raw file
 1"""Support code for test_*.py files"""
 2# Author: Collin Winter
 3
 4# Python imports
 5import unittest
 6import sys
 7import os
 8import os.path
 9import re
10from textwrap import dedent
11
12#sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
13
14# Local imports
15from .. import pytree
16from .. import refactor
17from ..pgen2 import driver
18
19test_dir = os.path.dirname(__file__)
20proj_dir = os.path.normpath(os.path.join(test_dir, ".."))
21grammar_path = os.path.join(test_dir, "..", "Grammar.txt")
22grammar = driver.load_grammar(grammar_path)
23driver = driver.Driver(grammar, convert=pytree.convert)
24
25def parse_string(string):
26    return driver.parse_string(reformat(string), debug=True)
27
28# Python 2.3's TestSuite is not iter()-able
29if sys.version_info < (2, 4):
30    def TestSuite_iter(self):
31        return iter(self._tests)
32    unittest.TestSuite.__iter__ = TestSuite_iter
33
34def 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
39def reformat(string):
40    return dedent(string) + "\n\n"
41
42def 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
57def 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
63TestCase = unittest.TestCase