+++ /dev/null
-"""\r
-Unit tests for refactor.py.\r
-"""\r
-\r
-from __future__ import with_statement\r
-\r
-import sys\r
-import os\r
-import codecs\r
-import operator\r
-import StringIO\r
-import tempfile\r
-import shutil\r
-import unittest\r
-import warnings\r
-\r
-from lib2to3 import refactor, pygram, fixer_base\r
-from lib2to3.pgen2 import token\r
-\r
-from . import support\r
-\r
-\r
-TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")\r
-FIXER_DIR = os.path.join(TEST_DATA_DIR, "fixers")\r
-\r
-sys.path.append(FIXER_DIR)\r
-try:\r
- _DEFAULT_FIXERS = refactor.get_fixers_from_package("myfixes")\r
-finally:\r
- sys.path.pop()\r
-\r
-_2TO3_FIXERS = refactor.get_fixers_from_package("lib2to3.fixes")\r
-\r
-class TestRefactoringTool(unittest.TestCase):\r
-\r
- def setUp(self):\r
- sys.path.append(FIXER_DIR)\r
-\r
- def tearDown(self):\r
- sys.path.pop()\r
-\r
- def check_instances(self, instances, classes):\r
- for inst, cls in zip(instances, classes):\r
- if not isinstance(inst, cls):\r
- self.fail("%s are not instances of %s" % instances, classes)\r
-\r
- def rt(self, options=None, fixers=_DEFAULT_FIXERS, explicit=None):\r
- return refactor.RefactoringTool(fixers, options, explicit)\r
-\r
- def test_print_function_option(self):\r
- rt = self.rt({"print_function" : True})\r
- self.assertTrue(rt.grammar is pygram.python_grammar_no_print_statement)\r
- self.assertTrue(rt.driver.grammar is\r
- pygram.python_grammar_no_print_statement)\r
-\r
- def test_fixer_loading_helpers(self):\r
- contents = ["explicit", "first", "last", "parrot", "preorder"]\r
- non_prefixed = refactor.get_all_fix_names("myfixes")\r
- prefixed = refactor.get_all_fix_names("myfixes", False)\r
- full_names = refactor.get_fixers_from_package("myfixes")\r
- self.assertEqual(prefixed, ["fix_" + name for name in contents])\r
- self.assertEqual(non_prefixed, contents)\r
- self.assertEqual(full_names,\r
- ["myfixes.fix_" + name for name in contents])\r
-\r
- def test_detect_future_features(self):\r
- run = refactor._detect_future_features\r
- fs = frozenset\r
- empty = fs()\r
- self.assertEqual(run(""), empty)\r
- self.assertEqual(run("from __future__ import print_function"),\r
- fs(("print_function",)))\r
- self.assertEqual(run("from __future__ import generators"),\r
- fs(("generators",)))\r
- self.assertEqual(run("from __future__ import generators, feature"),\r
- fs(("generators", "feature")))\r
- inp = "from __future__ import generators, print_function"\r
- self.assertEqual(run(inp), fs(("generators", "print_function")))\r
- inp ="from __future__ import print_function, generators"\r
- self.assertEqual(run(inp), fs(("print_function", "generators")))\r
- inp = "from __future__ import (print_function,)"\r
- self.assertEqual(run(inp), fs(("print_function",)))\r
- inp = "from __future__ import (generators, print_function)"\r
- self.assertEqual(run(inp), fs(("generators", "print_function")))\r
- inp = "from __future__ import (generators, nested_scopes)"\r
- self.assertEqual(run(inp), fs(("generators", "nested_scopes")))\r
- inp = """from __future__ import generators\r
-from __future__ import print_function"""\r
- self.assertEqual(run(inp), fs(("generators", "print_function")))\r
- invalid = ("from",\r
- "from 4",\r
- "from x",\r
- "from x 5",\r
- "from x im",\r
- "from x import",\r
- "from x import 4",\r
- )\r
- for inp in invalid:\r
- self.assertEqual(run(inp), empty)\r
- inp = "'docstring'\nfrom __future__ import print_function"\r
- self.assertEqual(run(inp), fs(("print_function",)))\r
- inp = "'docstring'\n'somng'\nfrom __future__ import print_function"\r
- self.assertEqual(run(inp), empty)\r
- inp = "# comment\nfrom __future__ import print_function"\r
- self.assertEqual(run(inp), fs(("print_function",)))\r
- inp = "# comment\n'doc'\nfrom __future__ import print_function"\r
- self.assertEqual(run(inp), fs(("print_function",)))\r
- inp = "class x: pass\nfrom __future__ import print_function"\r
- self.assertEqual(run(inp), empty)\r
-\r
- def test_get_headnode_dict(self):\r
- class NoneFix(fixer_base.BaseFix):\r
- pass\r
-\r
- class FileInputFix(fixer_base.BaseFix):\r
- PATTERN = "file_input< any * >"\r
-\r
- class SimpleFix(fixer_base.BaseFix):\r
- PATTERN = "'name'"\r
-\r
- no_head = NoneFix({}, [])\r
- with_head = FileInputFix({}, [])\r
- simple = SimpleFix({}, [])\r
- d = refactor._get_headnode_dict([no_head, with_head, simple])\r
- top_fixes = d.pop(pygram.python_symbols.file_input)\r
- self.assertEqual(top_fixes, [with_head, no_head])\r
- name_fixes = d.pop(token.NAME)\r
- self.assertEqual(name_fixes, [simple, no_head])\r
- for fixes in d.itervalues():\r
- self.assertEqual(fixes, [no_head])\r
-\r
- def test_fixer_loading(self):\r
- from myfixes.fix_first import FixFirst\r
- from myfixes.fix_last import FixLast\r
- from myfixes.fix_parrot import FixParrot\r
- from myfixes.fix_preorder import FixPreorder\r
-\r
- rt = self.rt()\r
- pre, post = rt.get_fixers()\r
-\r
- self.check_instances(pre, [FixPreorder])\r
- self.check_instances(post, [FixFirst, FixParrot, FixLast])\r
-\r
- def test_naughty_fixers(self):\r
- self.assertRaises(ImportError, self.rt, fixers=["not_here"])\r
- self.assertRaises(refactor.FixerError, self.rt, fixers=["no_fixer_cls"])\r
- self.assertRaises(refactor.FixerError, self.rt, fixers=["bad_order"])\r
-\r
- def test_refactor_string(self):\r
- rt = self.rt()\r
- input = "def parrot(): pass\n\n"\r
- tree = rt.refactor_string(input, "<test>")\r
- self.assertNotEqual(str(tree), input)\r
-\r
- input = "def f(): pass\n\n"\r
- tree = rt.refactor_string(input, "<test>")\r
- self.assertEqual(str(tree), input)\r
-\r
- def test_refactor_stdin(self):\r
-\r
- class MyRT(refactor.RefactoringTool):\r
-\r
- def print_output(self, old_text, new_text, filename, equal):\r
- results.extend([old_text, new_text, filename, equal])\r
-\r
- results = []\r
- rt = MyRT(_DEFAULT_FIXERS)\r
- save = sys.stdin\r
- sys.stdin = StringIO.StringIO("def parrot(): pass\n\n")\r
- try:\r
- rt.refactor_stdin()\r
- finally:\r
- sys.stdin = save\r
- expected = ["def parrot(): pass\n\n",\r
- "def cheese(): pass\n\n",\r
- "<stdin>", False]\r
- self.assertEqual(results, expected)\r
-\r
- def check_file_refactoring(self, test_file, fixers=_2TO3_FIXERS):\r
- def read_file():\r
- with open(test_file, "rb") as fp:\r
- return fp.read()\r
- old_contents = read_file()\r
- rt = self.rt(fixers=fixers)\r
-\r
- rt.refactor_file(test_file)\r
- self.assertEqual(old_contents, read_file())\r
-\r
- try:\r
- rt.refactor_file(test_file, True)\r
- new_contents = read_file()\r
- self.assertNotEqual(old_contents, new_contents)\r
- finally:\r
- with open(test_file, "wb") as fp:\r
- fp.write(old_contents)\r
- return new_contents\r
-\r
- def test_refactor_file(self):\r
- test_file = os.path.join(FIXER_DIR, "parrot_example.py")\r
- self.check_file_refactoring(test_file, _DEFAULT_FIXERS)\r
-\r
- def test_refactor_dir(self):\r
- def check(structure, expected):\r
- def mock_refactor_file(self, f, *args):\r
- got.append(f)\r
- save_func = refactor.RefactoringTool.refactor_file\r
- refactor.RefactoringTool.refactor_file = mock_refactor_file\r
- rt = self.rt()\r
- got = []\r
- dir = tempfile.mkdtemp(prefix="2to3-test_refactor")\r
- try:\r
- os.mkdir(os.path.join(dir, "a_dir"))\r
- for fn in structure:\r
- open(os.path.join(dir, fn), "wb").close()\r
- rt.refactor_dir(dir)\r
- finally:\r
- refactor.RefactoringTool.refactor_file = save_func\r
- shutil.rmtree(dir)\r
- self.assertEqual(got,\r
- [os.path.join(dir, path) for path in expected])\r
- check([], [])\r
- tree = ["nothing",\r
- "hi.py",\r
- ".dumb",\r
- ".after.py",\r
- "notpy.npy",\r
- "sappy"]\r
- expected = ["hi.py"]\r
- check(tree, expected)\r
- tree = ["hi.py",\r
- os.path.join("a_dir", "stuff.py")]\r
- check(tree, tree)\r
-\r
- def test_file_encoding(self):\r
- fn = os.path.join(TEST_DATA_DIR, "different_encoding.py")\r
- self.check_file_refactoring(fn)\r
-\r
- def test_bom(self):\r
- fn = os.path.join(TEST_DATA_DIR, "bom.py")\r
- data = self.check_file_refactoring(fn)\r
- self.assertTrue(data.startswith(codecs.BOM_UTF8))\r
-\r
- def test_crlf_newlines(self):\r
- old_sep = os.linesep\r
- os.linesep = "\r\n"\r
- try:\r
- fn = os.path.join(TEST_DATA_DIR, "crlf.py")\r
- fixes = refactor.get_fixers_from_package("lib2to3.fixes")\r
- self.check_file_refactoring(fn, fixes)\r
- finally:\r
- os.linesep = old_sep\r
-\r
- def test_refactor_docstring(self):\r
- rt = self.rt()\r
-\r
- doc = """\r
->>> example()\r
-42\r
-"""\r
- out = rt.refactor_docstring(doc, "<test>")\r
- self.assertEqual(out, doc)\r
-\r
- doc = """\r
->>> def parrot():\r
-... return 43\r
-"""\r
- out = rt.refactor_docstring(doc, "<test>")\r
- self.assertNotEqual(out, doc)\r
-\r
- def test_explicit(self):\r
- from myfixes.fix_explicit import FixExplicit\r
-\r
- rt = self.rt(fixers=["myfixes.fix_explicit"])\r
- self.assertEqual(len(rt.post_order), 0)\r
-\r
- rt = self.rt(explicit=["myfixes.fix_explicit"])\r
- for fix in rt.post_order:\r
- if isinstance(fix, FixExplicit):\r
- break\r
- else:\r
- self.fail("explicit fixer not loaded")\r