summaryrefslogtreecommitdiffstats
path: root/Lib/lib2to3/tests
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/lib2to3/tests')
-rw-r--r--Lib/lib2to3/tests/data/different_encoding.py7
-rwxr-xr-xLib/lib2to3/tests/test_fixers.py89
-rw-r--r--Lib/lib2to3/tests/test_refactor.py117
-rw-r--r--Lib/lib2to3/tests/test_util.py37
4 files changed, 182 insertions, 68 deletions
diff --git a/Lib/lib2to3/tests/data/different_encoding.py b/Lib/lib2to3/tests/data/different_encoding.py
index 888f51f..9f32bd0 100644
--- a/Lib/lib2to3/tests/data/different_encoding.py
+++ b/Lib/lib2to3/tests/data/different_encoding.py
@@ -1,3 +1,6 @@
#!/usr/bin/env python
-# -*- coding: iso-8859-1 -*-
-print u''
+# -*- coding: utf-8 -*-
+print u'ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ'
+
+def f(x):
+ print '%s\t-> α(%2i):%s β(%s)'
diff --git a/Lib/lib2to3/tests/test_fixers.py b/Lib/lib2to3/tests/test_fixers.py
index c0f1e37..a25f5a8 100755
--- a/Lib/lib2to3/tests/test_fixers.py
+++ b/Lib/lib2to3/tests/test_fixers.py
@@ -18,8 +18,6 @@ class FixerTestCase(support.TestCase):
def setUp(self, fix_list=None, fixer_pkg="lib2to3", options=None):
if fix_list is None:
fix_list = [self.fixer]
- if options is None:
- options = {"print_function" : False}
self.refactor = support.get_refactorer(fixer_pkg, fix_list, options)
self.fixer_log = []
self.filename = "<string>"
@@ -58,8 +56,7 @@ class FixerTestCase(support.TestCase):
def assert_runs_after(self, *names):
fixes = [self.fixer]
fixes.extend(names)
- options = {"print_function" : False}
- r = support.get_refactorer("lib2to3", fixes, options)
+ r = support.get_refactorer("lib2to3", fixes)
(pre, post) = r.get_fixers()
n = "fix_" + self.fixer
if post and post[-1].__class__.__module__.endswith(n):
@@ -379,18 +376,15 @@ class Test_print(FixerTestCase):
self.unchanged(s)
def test_idempotency_print_as_function(self):
- print_stmt = pygram.python_grammar.keywords.pop("print")
- try:
- s = """print(1, 1+1, 1+1+1)"""
- self.unchanged(s)
+ self.refactor.driver.grammar = pygram.python_grammar_no_print_statement
+ s = """print(1, 1+1, 1+1+1)"""
+ self.unchanged(s)
- s = """print()"""
- self.unchanged(s)
+ s = """print()"""
+ self.unchanged(s)
- s = """print('')"""
- self.unchanged(s)
- finally:
- pygram.python_grammar.keywords["print"] = print_stmt
+ s = """print('')"""
+ self.unchanged(s)
def test_1(self):
b = """print 1, 1+1, 1+1+1"""
@@ -462,31 +456,15 @@ class Test_print(FixerTestCase):
a = """print(file=sys.stderr)"""
self.check(b, a)
- # With from __future__ import print_function
def test_with_future_print_function(self):
- # XXX: These tests won't actually do anything until the parser
- # is fixed so it won't crash when it sees print(x=y).
- # When #2412 is fixed, the try/except block can be taken
- # out and the tests can be run like normal.
- # MvL: disable entirely for now, so that it doesn't print to stdout
- return
- try:
- s = "from __future__ import print_function\n"\
- "print('Hai!', end=' ')"
- self.unchanged(s)
+ s = "from __future__ import print_function\n" \
+ "print('Hai!', end=' ')"
+ self.unchanged(s)
- b = "print 'Hello, world!'"
- a = "print('Hello, world!')"
- self.check(b, a)
+ b = "print 'Hello, world!'"
+ a = "print('Hello, world!')"
+ self.check(b, a)
- s = "from __future__ import *\n"\
- "print('Hai!', end=' ')"
- self.unchanged(s)
- except:
- return
- else:
- self.assertFalse(True, "#2421 has been fixed -- printing tests "\
- "need to be updated!")
class Test_exec(FixerTestCase):
fixer = "exec"
@@ -1705,6 +1683,11 @@ class Test_imports_fixer_order(FixerTestCase, ImportsFixerTests):
for key in ('dbhash', 'dumbdbm', 'dbm', 'gdbm'):
self.modules[key] = mapping1[key]
+ def test_after_local_imports_refactoring(self):
+ for fix in ("imports", "imports2"):
+ self.fixer = fix
+ self.assert_runs_after("import")
+
class Test_urllib(FixerTestCase):
fixer = "urllib"
@@ -3504,6 +3487,7 @@ class Test_itertools_imports(FixerTestCase):
s = "from itertools import foo"
self.unchanged(s)
+
class Test_import(FixerTestCase):
fixer = "import"
@@ -3538,8 +3522,7 @@ class Test_import(FixerTestCase):
self.always_exists = False
self.present_files = set(['__init__.py'])
- expected_extensions = ('.py', os.path.pathsep, '.pyc', '.so',
- '.sl', '.pyd')
+ expected_extensions = ('.py', os.path.sep, '.pyc', '.so', '.sl', '.pyd')
names_to_test = (p("/spam/eggs.py"), "ni.py", p("../../shrubbery.py"))
for name in names_to_test:
@@ -3569,6 +3552,13 @@ class Test_import(FixerTestCase):
self.present_files = set(["__init__.py", "bar.py"])
self.check(b, a)
+ def test_import_from_package(self):
+ b = "import bar"
+ a = "from . import bar"
+ self.always_exists = False
+ self.present_files = set(["__init__.py", "bar/"])
+ self.check(b, a)
+
def test_comments_and_indent(self):
b = "import bar # Foo"
a = "from . import bar # Foo"
@@ -4095,3 +4085,26 @@ class Test_getcwdu(FixerTestCase):
b = """os.getcwdu ( )"""
a = """os.getcwd ( )"""
self.check(b, a)
+
+
+class Test_operator(FixerTestCase):
+
+ fixer = "operator"
+
+ def test_operator_isCallable(self):
+ b = "operator.isCallable(x)"
+ a = "hasattr(x, '__call__')"
+ self.check(b, a)
+
+ def test_operator_sequenceIncludes(self):
+ b = "operator.sequenceIncludes(x, y)"
+ a = "operator.contains(x, y)"
+ self.check(b, a)
+
+ def test_bare_isCallable(self):
+ s = "isCallable(x)"
+ self.warns_unchanged(s, "You should use hasattr(x, '__call__') here.")
+
+ def test_bare_sequenceIncludes(self):
+ s = "sequenceIncludes(x, y)"
+ self.warns_unchanged(s, "You should use operator.contains here.")
diff --git a/Lib/lib2to3/tests/test_refactor.py b/Lib/lib2to3/tests/test_refactor.py
index e55f555..b282cba 100644
--- a/Lib/lib2to3/tests/test_refactor.py
+++ b/Lib/lib2to3/tests/test_refactor.py
@@ -7,9 +7,12 @@ import os
import operator
import io
import tempfile
+import shutil
import unittest
+import warnings
from lib2to3 import refactor, pygram, fixer_base
+from lib2to3.pgen2 import token
from . import support
@@ -42,14 +45,11 @@ class TestRefactoringTool(unittest.TestCase):
return refactor.RefactoringTool(fixers, options, explicit)
def test_print_function_option(self):
- gram = pygram.python_grammar
- save = gram.keywords["print"]
- try:
- rt = self.rt({"print_function" : True})
- self.assertRaises(KeyError, operator.itemgetter("print"),
- gram.keywords)
- finally:
- gram.keywords["print"] = save
+ with warnings.catch_warnings(record=True) as w:
+ refactor.RefactoringTool(_DEFAULT_FIXERS, {"print_function" : True})
+ self.assertEqual(len(w), 1)
+ msg, = w
+ self.assertTrue(msg.category is DeprecationWarning)
def test_fixer_loading_helpers(self):
contents = ["explicit", "first", "last", "parrot", "preorder"]
@@ -61,19 +61,63 @@ class TestRefactoringTool(unittest.TestCase):
self.assertEqual(full_names,
["myfixes.fix_" + name for name in contents])
+ def test_detect_future_print(self):
+ run = refactor._detect_future_print
+ self.assertFalse(run(""))
+ self.assertTrue(run("from __future__ import print_function"))
+ self.assertFalse(run("from __future__ import generators"))
+ self.assertFalse(run("from __future__ import generators, feature"))
+ input = "from __future__ import generators, print_function"
+ self.assertTrue(run(input))
+ input ="from __future__ import print_function, generators"
+ self.assertTrue(run(input))
+ input = "from __future__ import (print_function,)"
+ self.assertTrue(run(input))
+ input = "from __future__ import (generators, print_function)"
+ self.assertTrue(run(input))
+ input = "from __future__ import (generators, nested_scopes)"
+ self.assertFalse(run(input))
+ input = """from __future__ import generators
+from __future__ import print_function"""
+ self.assertTrue(run(input))
+ self.assertFalse(run("from"))
+ self.assertFalse(run("from 4"))
+ self.assertFalse(run("from x"))
+ self.assertFalse(run("from x 5"))
+ self.assertFalse(run("from x im"))
+ self.assertFalse(run("from x import"))
+ self.assertFalse(run("from x import 4"))
+ input = "'docstring'\nfrom __future__ import print_function"
+ self.assertTrue(run(input))
+ input = "'docstring'\n'somng'\nfrom __future__ import print_function"
+ self.assertFalse(run(input))
+ input = "# comment\nfrom __future__ import print_function"
+ self.assertTrue(run(input))
+ input = "# comment\n'doc'\nfrom __future__ import print_function"
+ self.assertTrue(run(input))
+ input = "class x: pass\nfrom __future__ import print_function"
+ self.assertFalse(run(input))
+
def test_get_headnode_dict(self):
class NoneFix(fixer_base.BaseFix):
- PATTERN = None
+ pass
class FileInputFix(fixer_base.BaseFix):
PATTERN = "file_input< any * >"
+ class SimpleFix(fixer_base.BaseFix):
+ PATTERN = "'name'"
+
no_head = NoneFix({}, [])
with_head = FileInputFix({}, [])
- d = refactor.get_headnode_dict([no_head, with_head])
- expected = {None: [no_head],
- pygram.python_symbols.file_input : [with_head]}
- self.assertEqual(d, expected)
+ simple = SimpleFix({}, [])
+ d = refactor._get_headnode_dict([no_head, with_head, simple])
+ top_fixes = d.pop(pygram.python_symbols.file_input)
+ self.assertEqual(top_fixes, [with_head, no_head])
+ name_fixes = d.pop(token.NAME)
+ self.assertEqual(name_fixes, [simple, no_head])
+ for fixes in d.values():
+ self.assertEqual(fixes, [no_head])
def test_fixer_loading(self):
from myfixes.fix_first import FixFirst
@@ -106,10 +150,10 @@ class TestRefactoringTool(unittest.TestCase):
class MyRT(refactor.RefactoringTool):
- def print_output(self, lines):
- diff_lines.extend(lines)
+ def print_output(self, old_text, new_text, filename, equal):
+ results.extend([old_text, new_text, filename, equal])
- diff_lines = []
+ results = []
rt = MyRT(_DEFAULT_FIXERS)
save = sys.stdin
sys.stdin = io.StringIO("def parrot(): pass\n\n")
@@ -117,12 +161,10 @@ class TestRefactoringTool(unittest.TestCase):
rt.refactor_stdin()
finally:
sys.stdin = save
- expected = """--- <stdin> (original)
-+++ <stdin> (refactored)
-@@ -1,2 +1,2 @@
--def parrot(): pass
-+def cheese(): pass""".splitlines()
- self.assertEqual(diff_lines[:-1], expected)
+ expected = ["def parrot(): pass\n\n",
+ "def cheese(): pass\n\n",
+ "<stdin>", False]
+ self.assertEqual(results, expected)
def check_file_refactoring(self, test_file, fixers=_2TO3_FIXERS):
def read_file():
@@ -145,6 +187,37 @@ class TestRefactoringTool(unittest.TestCase):
test_file = os.path.join(FIXER_DIR, "parrot_example.py")
self.check_file_refactoring(test_file, _DEFAULT_FIXERS)
+ def test_refactor_dir(self):
+ def check(structure, expected):
+ def mock_refactor_file(self, f, *args):
+ got.append(f)
+ save_func = refactor.RefactoringTool.refactor_file
+ refactor.RefactoringTool.refactor_file = mock_refactor_file
+ rt = self.rt()
+ got = []
+ dir = tempfile.mkdtemp(prefix="2to3-test_refactor")
+ try:
+ os.mkdir(os.path.join(dir, "a_dir"))
+ for fn in structure:
+ open(os.path.join(dir, fn), "wb").close()
+ rt.refactor_dir(dir)
+ finally:
+ refactor.RefactoringTool.refactor_file = save_func
+ shutil.rmtree(dir)
+ self.assertEqual(got,
+ [os.path.join(dir, path) for path in expected])
+ check([], [])
+ tree = ["nothing",
+ "hi.py",
+ ".dumb",
+ ".after.py",
+ "sappy"]
+ expected = ["hi.py"]
+ check(tree, expected)
+ tree = ["hi.py",
+ "a_dir/stuff.py"]
+ check(tree, tree)
+
def test_file_encoding(self):
fn = os.path.join(TEST_DATA_DIR, "different_encoding.py")
self.check_file_refactoring(fn)
diff --git a/Lib/lib2to3/tests/test_util.py b/Lib/lib2to3/tests/test_util.py
index ef3256d..6186b4f 100644
--- a/Lib/lib2to3/tests/test_util.py
+++ b/Lib/lib2to3/tests/test_util.py
@@ -1,4 +1,4 @@
-""" Test suite for the code in fixes.util """
+""" Test suite for the code in fixer_util """
# Testing imports
from . import support
@@ -7,10 +7,10 @@ from . import support
import os.path
# Local imports
-from .. import pytree
-from .. import fixer_util
-from ..fixer_util import Attr, Name
-
+from lib2to3.pytree import Node, Leaf
+from lib2to3 import fixer_util
+from lib2to3.fixer_util import Attr, Name, Call, Comma
+from lib2to3.pgen2 import token
def parse(code, strip_levels=0):
# The topmost node is file_input, which we don't care about.
@@ -24,7 +24,7 @@ def parse(code, strip_levels=0):
class MacroTestCase(support.TestCase):
def assertStr(self, node, string):
if isinstance(node, (tuple, list)):
- node = pytree.Node(fixer_util.syms.simple_stmt, node)
+ node = Node(fixer_util.syms.simple_stmt, node)
self.assertEqual(str(node), string)
@@ -78,6 +78,31 @@ class Test_Name(MacroTestCase):
self.assertStr(Name("a", prefix="b"), "ba")
+class Test_Call(MacroTestCase):
+ def _Call(self, name, args=None, prefix=None):
+ """Help the next test"""
+ children = []
+ if isinstance(args, list):
+ for arg in args:
+ children.append(arg)
+ children.append(Comma())
+ children.pop()
+ return Call(Name(name), children, prefix)
+
+ def test(self):
+ kids = [None,
+ [Leaf(token.NUMBER, 1), Leaf(token.NUMBER, 2),
+ Leaf(token.NUMBER, 3)],
+ [Leaf(token.NUMBER, 1), Leaf(token.NUMBER, 3),
+ Leaf(token.NUMBER, 2), Leaf(token.NUMBER, 4)],
+ [Leaf(token.STRING, "b"), Leaf(token.STRING, "j", prefix=" ")]
+ ]
+ self.assertStr(self._Call("A"), "A()")
+ self.assertStr(self._Call("b", kids[1]), "b(1,2,3)")
+ self.assertStr(self._Call("a.b().c", kids[2]), "a.b().c(1,3,2,4)")
+ self.assertStr(self._Call("d", kids[3], prefix=" "), " d(b, j)")
+
+
class Test_does_tree_import(support.TestCase):
def _find_bind_rec(self, name, node):
# Search a tree for a binding -- used to find the starting