diff options
author | Benjamin Peterson <benjamin@python.org> | 2009-07-20 16:42:03 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2009-07-20 16:42:03 (GMT) |
commit | 3059b00f65f8b01aa7b214b7030783c748ecc70c (patch) | |
tree | 6db73654752e33d2a51ed8b1321ae347115ca3ee /Lib/lib2to3/tests | |
parent | 6919427e9462d05f402faa5f846f43e08347cebe (diff) | |
download | cpython-3059b00f65f8b01aa7b214b7030783c748ecc70c.zip cpython-3059b00f65f8b01aa7b214b7030783c748ecc70c.tar.gz cpython-3059b00f65f8b01aa7b214b7030783c748ecc70c.tar.bz2 |
Merged revisions 74114 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r74114 | benjamin.peterson | 2009-07-20 10:33:09 -0500 (Mon, 20 Jul 2009) | 110 lines
Merged revisions 73771,73811,73840,73842,73848-73849,73861,73957-73960,73964-73969,73972-73974,73977,73981,73984,74065,74113 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r73771 | benjamin.peterson | 2009-07-02 10:56:55 -0500 (Thu, 02 Jul 2009) | 1 line
force the imports fixer to be run after the import one #6400
........
r73811 | benjamin.peterson | 2009-07-03 09:03:14 -0500 (Fri, 03 Jul 2009) | 1 line
check for sep, not pathsep when looking for a subpackage #6408
........
r73840 | benjamin.peterson | 2009-07-04 09:52:28 -0500 (Sat, 04 Jul 2009) | 1 line
don't print diffs by default; it's annoying
........
r73842 | benjamin.peterson | 2009-07-04 09:58:46 -0500 (Sat, 04 Jul 2009) | 1 line
complain when not showing diffs or writing
........
r73848 | alexandre.vassalotti | 2009-07-04 23:38:19 -0500 (Sat, 04 Jul 2009) | 2 lines
Fix test_refactor_stdin to handle print_output() method with 4 arguments.
........
r73849 | alexandre.vassalotti | 2009-07-04 23:43:18 -0500 (Sat, 04 Jul 2009) | 5 lines
Issue 2370: Add fixer for the removal of operator.isCallable() and
operator.sequenceIncludes().
Patch contributed by Jeff Balogh (and updated by me).
........
r73861 | benjamin.peterson | 2009-07-05 09:15:53 -0500 (Sun, 05 Jul 2009) | 1 line
cleanup and use unicode where appropiate
........
r73957 | benjamin.peterson | 2009-07-11 15:49:56 -0500 (Sat, 11 Jul 2009) | 1 line
fix calls to str() with unicode()
........
r73958 | benjamin.peterson | 2009-07-11 15:51:51 -0500 (Sat, 11 Jul 2009) | 1 line
more str() -> unicode()
........
r73959 | benjamin.peterson | 2009-07-11 16:40:08 -0500 (Sat, 11 Jul 2009) | 1 line
add tests for refactor_dir()
........
r73960 | benjamin.peterson | 2009-07-11 16:44:32 -0500 (Sat, 11 Jul 2009) | 1 line
don't parse files just because they end with 'py' (no dot)
........
r73964 | benjamin.peterson | 2009-07-11 17:30:15 -0500 (Sat, 11 Jul 2009) | 1 line
simplify
........
r73965 | benjamin.peterson | 2009-07-11 17:31:30 -0500 (Sat, 11 Jul 2009) | 1 line
remove usage of get_prefix()
........
r73966 | benjamin.peterson | 2009-07-11 17:33:35 -0500 (Sat, 11 Jul 2009) | 1 line
revert unintended change in 73965
........
r73967 | benjamin.peterson | 2009-07-11 17:34:44 -0500 (Sat, 11 Jul 2009) | 1 line
avoid expensive checks and assume the node did change
........
r73968 | benjamin.peterson | 2009-07-11 20:46:46 -0500 (Sat, 11 Jul 2009) | 1 line
use a regular dict for the heads to avoid adding lists in the loop
........
r73969 | benjamin.peterson | 2009-07-11 20:50:43 -0500 (Sat, 11 Jul 2009) | 1 line
prefix headnode functions with '_'
........
r73972 | benjamin.peterson | 2009-07-11 21:25:45 -0500 (Sat, 11 Jul 2009) | 1 line
try to make the head node dict as sparse as possible
........
r73973 | benjamin.peterson | 2009-07-11 21:59:49 -0500 (Sat, 11 Jul 2009) | 1 line
a better idea; add an option to *not* print diffs
........
r73974 | benjamin.peterson | 2009-07-11 22:00:29 -0500 (Sat, 11 Jul 2009) | 1 line
add space
........
r73977 | benjamin.peterson | 2009-07-12 10:16:07 -0500 (Sun, 12 Jul 2009) | 1 line
update get_headnode_dict tests for recent changes
........
r73981 | benjamin.peterson | 2009-07-12 12:06:39 -0500 (Sun, 12 Jul 2009) | 4 lines
detect when "from __future__ import print_function" is given
Deprecate the 'print_function' option and the -p flag
........
r73984 | benjamin.peterson | 2009-07-12 16:16:37 -0500 (Sun, 12 Jul 2009) | 1 line
add tests for Call; thanks Joe Amenta
........
r74065 | benjamin.peterson | 2009-07-17 12:52:49 -0500 (Fri, 17 Jul 2009) | 1 line
pathname2url and url2pathname are in urllib.request not urllib.parse #6496
........
r74113 | benjamin.peterson | 2009-07-20 08:56:57 -0500 (Mon, 20 Jul 2009) | 1 line
fix deprecation warnings in tests
........
................
Diffstat (limited to 'Lib/lib2to3/tests')
-rw-r--r-- | Lib/lib2to3/tests/data/different_encoding.py | 7 | ||||
-rwxr-xr-x | Lib/lib2to3/tests/test_fixers.py | 89 | ||||
-rw-r--r-- | Lib/lib2to3/tests/test_refactor.py | 117 | ||||
-rw-r--r-- | Lib/lib2to3/tests/test_util.py | 37 |
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 |