Back to index

python3.2  3.2.2
Public Member Functions
lib2to3.tests.test_refactor.TestRefactoringTool Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def check_instances
def rt
def test_print_function_option
def test_fixer_loading_helpers
def test_detect_future_features
def test_get_headnode_dict
def test_fixer_loading
def test_naughty_fixers
def test_refactor_string
def test_refactor_stdin
def check_file_refactoring
def test_refactor_file
def test_refactor_dir
def test_file_encoding
def test_bom
def test_crlf_newlines
def test_refactor_docstring
def test_explicit

Detailed Description

Definition at line 34 of file test_refactor.py.


Member Function Documentation

Definition at line 179 of file test_refactor.py.

00179 
00180     def check_file_refactoring(self, test_file, fixers=_2TO3_FIXERS):
00181         tmpdir = tempfile.mkdtemp(prefix="2to3-test_refactor")
00182         self.addCleanup(shutil.rmtree, tmpdir)
00183         # make a copy of the tested file that we can write to
00184         shutil.copy(test_file, tmpdir)
00185         test_file = os.path.join(tmpdir, os.path.basename(test_file))
00186         os.chmod(test_file, 0o644)
00187 
00188         def read_file():
00189             with open(test_file, "rb") as fp:
00190                 return fp.read()
00191 
00192         old_contents = read_file()
00193         rt = self.rt(fixers=fixers)
00194 
00195         rt.refactor_file(test_file)
00196         self.assertEqual(old_contents, read_file())
00197 
00198         rt.refactor_file(test_file, True)
00199         new_contents = read_file()
00200         self.assertNotEqual(old_contents, new_contents)
00201         return new_contents

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.tests.test_refactor.TestRefactoringTool.check_instances (   self,
  instances,
  classes 
)

Definition at line 42 of file test_refactor.py.

00042 
00043     def check_instances(self, instances, classes):
00044         for inst, cls in zip(instances, classes):
00045             if not isinstance(inst, cls):
00046                 self.fail("%s are not instances of %s" % instances, classes)

Here is the call graph for this function:

Here is the caller graph for this function:

def lib2to3.tests.test_refactor.TestRefactoringTool.rt (   self,
  options = None,
  fixers = _DEFAULT_FIXERS,
  explicit = None 
)

Definition at line 47 of file test_refactor.py.

00047 
00048     def rt(self, options=None, fixers=_DEFAULT_FIXERS, explicit=None):
00049         return refactor.RefactoringTool(fixers, options, explicit)

Here is the caller graph for this function:

Definition at line 36 of file test_refactor.py.

00036 
00037     def setUp(self):
00038         sys.path.append(FIXER_DIR)

Here is the caller graph for this function:

Definition at line 39 of file test_refactor.py.

00039 
00040     def tearDown(self):
00041         sys.path.pop()

Here is the caller graph for this function:

Definition at line 242 of file test_refactor.py.

00242 
00243     def test_bom(self):
00244         fn = os.path.join(TEST_DATA_DIR, "bom.py")
00245         data = self.check_file_refactoring(fn)
00246         self.assertTrue(data.startswith(codecs.BOM_UTF8))

Here is the call graph for this function:

Definition at line 247 of file test_refactor.py.

00247 
00248     def test_crlf_newlines(self):
00249         old_sep = os.linesep
00250         os.linesep = "\r\n"
00251         try:
00252             fn = os.path.join(TEST_DATA_DIR, "crlf.py")
00253             fixes = refactor.get_fixers_from_package("lib2to3.fixes")
00254             self.check_file_refactoring(fn, fixes)
00255         finally:
00256             os.linesep = old_sep

Here is the call graph for this function:

Definition at line 66 of file test_refactor.py.

00066 
00067     def test_detect_future_features(self):
00068         run = refactor._detect_future_features
00069         fs = frozenset
00070         empty = fs()
00071         self.assertEqual(run(""), empty)
00072         self.assertEqual(run("from __future__ import print_function"),
00073                          fs(("print_function",)))
00074         self.assertEqual(run("from __future__ import generators"),
00075                          fs(("generators",)))
00076         self.assertEqual(run("from __future__ import generators, feature"),
00077                          fs(("generators", "feature")))
00078         inp = "from __future__ import generators, print_function"
00079         self.assertEqual(run(inp), fs(("generators", "print_function")))
00080         inp ="from __future__ import print_function, generators"
00081         self.assertEqual(run(inp), fs(("print_function", "generators")))
00082         inp = "from __future__ import (print_function,)"
00083         self.assertEqual(run(inp), fs(("print_function",)))
00084         inp = "from __future__ import (generators, print_function)"
00085         self.assertEqual(run(inp), fs(("generators", "print_function")))
00086         inp = "from __future__ import (generators, nested_scopes)"
00087         self.assertEqual(run(inp), fs(("generators", "nested_scopes")))
00088         inp = """from __future__ import generators
00089 from __future__ import print_function"""
00090         self.assertEqual(run(inp), fs(("generators", "print_function")))
00091         invalid = ("from",
00092                    "from 4",
00093                    "from x",
00094                    "from x 5",
00095                    "from x im",
00096                    "from x import",
00097                    "from x import 4",
00098                    )
00099         for inp in invalid:
00100             self.assertEqual(run(inp), empty)
00101         inp = "'docstring'\nfrom __future__ import print_function"
00102         self.assertEqual(run(inp), fs(("print_function",)))
00103         inp = "'docstring'\n'somng'\nfrom __future__ import print_function"
00104         self.assertEqual(run(inp), empty)
00105         inp = "# comment\nfrom __future__ import print_function"
00106         self.assertEqual(run(inp), fs(("print_function",)))
00107         inp = "# comment\n'doc'\nfrom __future__ import print_function"
00108         self.assertEqual(run(inp), fs(("print_function",)))
00109         inp = "class x: pass\nfrom __future__ import print_function"
00110         self.assertEqual(run(inp), empty)

Here is the call graph for this function:

Definition at line 274 of file test_refactor.py.

00274 
00275     def test_explicit(self):
00276         from myfixes.fix_explicit import FixExplicit
00277 
00278         rt = self.rt(fixers=["myfixes.fix_explicit"])
00279         self.assertEqual(len(rt.post_order), 0)
00280 
00281         rt = self.rt(explicit=["myfixes.fix_explicit"])
00282         for fix in rt.post_order:
00283             if isinstance(fix, FixExplicit):
00284                 break
00285         else:
00286             self.fail("explicit fixer not loaded")

Here is the call graph for this function:

Definition at line 238 of file test_refactor.py.

00238 
00239     def test_file_encoding(self):
00240         fn = os.path.join(TEST_DATA_DIR, "different_encoding.py")
00241         self.check_file_refactoring(fn)

Here is the call graph for this function:

Definition at line 132 of file test_refactor.py.

00132 
00133     def test_fixer_loading(self):
00134         from myfixes.fix_first import FixFirst
00135         from myfixes.fix_last import FixLast
00136         from myfixes.fix_parrot import FixParrot
00137         from myfixes.fix_preorder import FixPreorder
00138 
00139         rt = self.rt()
00140         pre, post = rt.get_fixers()
00141 
00142         self.check_instances(pre, [FixPreorder])
00143         self.check_instances(post, [FixFirst, FixParrot, FixLast])

Here is the call graph for this function:

Definition at line 56 of file test_refactor.py.

00056 
00057     def test_fixer_loading_helpers(self):
00058         contents = ["explicit", "first", "last", "parrot", "preorder"]
00059         non_prefixed = refactor.get_all_fix_names("myfixes")
00060         prefixed = refactor.get_all_fix_names("myfixes", False)
00061         full_names = refactor.get_fixers_from_package("myfixes")
00062         self.assertEqual(prefixed, ["fix_" + name for name in contents])
00063         self.assertEqual(non_prefixed, contents)
00064         self.assertEqual(full_names,
00065                          ["myfixes.fix_" + name for name in contents])

Here is the call graph for this function:

Definition at line 111 of file test_refactor.py.

00111 
00112     def test_get_headnode_dict(self):
00113         class NoneFix(fixer_base.BaseFix):
00114             pass
00115 
00116         class FileInputFix(fixer_base.BaseFix):
00117             PATTERN = "file_input< any * >"
00118 
00119         class SimpleFix(fixer_base.BaseFix):
00120             PATTERN = "'name'"
00121 
00122         no_head = NoneFix({}, [])
00123         with_head = FileInputFix({}, [])
00124         simple = SimpleFix({}, [])
00125         d = refactor._get_headnode_dict([no_head, with_head, simple])
00126         top_fixes = d.pop(pygram.python_symbols.file_input)
00127         self.assertEqual(top_fixes, [with_head, no_head])
00128         name_fixes = d.pop(token.NAME)
00129         self.assertEqual(name_fixes, [simple, no_head])
00130         for fixes in d.values():
00131             self.assertEqual(fixes, [no_head])

Definition at line 144 of file test_refactor.py.

00144 
00145     def test_naughty_fixers(self):
00146         self.assertRaises(ImportError, self.rt, fixers=["not_here"])
00147         self.assertRaises(refactor.FixerError, self.rt, fixers=["no_fixer_cls"])
00148         self.assertRaises(refactor.FixerError, self.rt, fixers=["bad_order"])

Here is the call graph for this function:

Definition at line 50 of file test_refactor.py.

00050 
00051     def test_print_function_option(self):
00052         rt = self.rt({"print_function" : True})
00053         self.assertTrue(rt.grammar is pygram.python_grammar_no_print_statement)
00054         self.assertTrue(rt.driver.grammar is
00055                         pygram.python_grammar_no_print_statement)

Here is the call graph for this function:

Definition at line 206 of file test_refactor.py.

00206 
00207     def test_refactor_dir(self):
00208         def check(structure, expected):
00209             def mock_refactor_file(self, f, *args):
00210                 got.append(f)
00211             save_func = refactor.RefactoringTool.refactor_file
00212             refactor.RefactoringTool.refactor_file = mock_refactor_file
00213             rt = self.rt()
00214             got = []
00215             dir = tempfile.mkdtemp(prefix="2to3-test_refactor")
00216             try:
00217                 os.mkdir(os.path.join(dir, "a_dir"))
00218                 for fn in structure:
00219                     open(os.path.join(dir, fn), "wb").close()
00220                 rt.refactor_dir(dir)
00221             finally:
00222                 refactor.RefactoringTool.refactor_file = save_func
00223                 shutil.rmtree(dir)
00224             self.assertEqual(got,
00225                              [os.path.join(dir, path) for path in expected])
00226         check([], [])
00227         tree = ["nothing",
00228                 "hi.py",
00229                 ".dumb",
00230                 ".after.py",
00231                 "notpy.npy",
00232                 "sappy"]
00233         expected = ["hi.py"]
00234         check(tree, expected)
00235         tree = ["hi.py",
00236                 os.path.join("a_dir", "stuff.py")]
00237         check(tree, tree)

Here is the call graph for this function:

Definition at line 257 of file test_refactor.py.

00257 
00258     def test_refactor_docstring(self):
00259         rt = self.rt()
00260 
00261         doc = """
00262 >>> example()
00263 42
00264 """
00265         out = rt.refactor_docstring(doc, "<test>")
00266         self.assertEqual(out, doc)
00267 
00268         doc = """
00269 >>> def parrot():
00270 ...      return 43
00271 """
00272         out = rt.refactor_docstring(doc, "<test>")
00273         self.assertNotEqual(out, doc)

Here is the call graph for this function:

Definition at line 202 of file test_refactor.py.

00202 
00203     def test_refactor_file(self):
00204         test_file = os.path.join(FIXER_DIR, "parrot_example.py")
00205         self.check_file_refactoring(test_file, _DEFAULT_FIXERS)

Here is the call graph for this function:

Definition at line 159 of file test_refactor.py.

00159 
00160     def test_refactor_stdin(self):
00161 
00162         class MyRT(refactor.RefactoringTool):
00163 
00164             def print_output(self, old_text, new_text, filename, equal):
00165                 results.extend([old_text, new_text, filename, equal])
00166 
00167         results = []
00168         rt = MyRT(_DEFAULT_FIXERS)
00169         save = sys.stdin
00170         sys.stdin = io.StringIO("def parrot(): pass\n\n")
00171         try:
00172             rt.refactor_stdin()
00173         finally:
00174             sys.stdin = save
00175         expected = ["def parrot(): pass\n\n",
00176                     "def cheese(): pass\n\n",
00177                     "<stdin>", False]
00178         self.assertEqual(results, expected)

Here is the call graph for this function:

Definition at line 149 of file test_refactor.py.

00149 
00150     def test_refactor_string(self):
00151         rt = self.rt()
00152         input = "def parrot(): pass\n\n"
00153         tree = rt.refactor_string(input, "<test>")
00154         self.assertNotEqual(str(tree), input)
00155 
00156         input = "def f(): pass\n\n"
00157         tree = rt.refactor_string(input, "<test>")
00158         self.assertEqual(str(tree), input)

Here is the call graph for this function:


The documentation for this class was generated from the following file: