diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2008-03-22 00:07:09 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2008-03-22 00:07:09 (GMT) |
commit | 3faa84f18e2a340c2a805a343f3b10a05dfe5fdd (patch) | |
tree | d5c29c05c07aeaec9280cf3c6e6b3edaddb8edba /Lib/lib2to3/tests/test_fixers.py | |
parent | 896c317b43260451b3773d905b8fc90027ea50bf (diff) | |
download | cpython-3faa84f18e2a340c2a805a343f3b10a05dfe5fdd.zip cpython-3faa84f18e2a340c2a805a343f3b10a05dfe5fdd.tar.gz cpython-3faa84f18e2a340c2a805a343f3b10a05dfe5fdd.tar.bz2 |
Merged revisions 61724-61725 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r61724 | martin.v.loewis | 2008-03-22 01:01:12 +0100 (Sa, 22 Mär 2008) | 49 lines
Merged revisions 61602-61723 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r61626 | david.wolever | 2008-03-19 17:19:16 +0100 (Mi, 19 Mär 2008) | 1 line
Added fixer for implicit local imports. See #2414.
........
r61628 | david.wolever | 2008-03-19 17:57:43 +0100 (Mi, 19 Mär 2008) | 1 line
Added a class for tests which should not run if a particular import is found.
........
r61629 | collin.winter | 2008-03-19 17:58:19 +0100 (Mi, 19 Mär 2008) | 1 line
Two more relative import fixes in pgen2.
........
r61635 | david.wolever | 2008-03-19 20:16:03 +0100 (Mi, 19 Mär 2008) | 1 line
Fixed print fixer so it will do the Right Thing when it encounters __future__.print_function. 2to3 gets upset, though, so the tests have been commented out.
........
r61637 | david.wolever | 2008-03-19 21:37:17 +0100 (Mi, 19 Mär 2008) | 3 lines
Added a fixer for itertools imports (from itertools import imap, ifilterfalse --> from itertools import filterfalse)
........
r61645 | david.wolever | 2008-03-19 23:22:35 +0100 (Mi, 19 Mär 2008) | 1 line
SVN is happier when you add the files you create... -_-'
........
r61654 | david.wolever | 2008-03-20 01:09:56 +0100 (Do, 20 Mär 2008) | 1 line
Added an explicit sort order to fixers -- fixes problems like #2427
........
r61664 | david.wolever | 2008-03-20 04:32:40 +0100 (Do, 20 Mär 2008) | 3 lines
Fixes #2428 -- comments are no longer eatten by __future__ fixer.
........
r61673 | david.wolever | 2008-03-20 17:22:40 +0100 (Do, 20 Mär 2008) | 1 line
Added 2to3 node pretty-printer
........
r61679 | david.wolever | 2008-03-20 20:50:42 +0100 (Do, 20 Mär 2008) | 1 line
Made node printing a little bit prettier
........
r61723 | martin.v.loewis | 2008-03-22 00:59:27 +0100 (Sa, 22 Mär 2008) | 2 lines
Fix whitespace.
........
................
r61725 | martin.v.loewis | 2008-03-22 01:02:41 +0100 (Sa, 22 Mär 2008) | 2 lines
Install lib2to3.
................
Diffstat (limited to 'Lib/lib2to3/tests/test_fixers.py')
-rwxr-xr-x | Lib/lib2to3/tests/test_fixers.py | 191 |
1 files changed, 175 insertions, 16 deletions
diff --git a/Lib/lib2to3/tests/test_fixers.py b/Lib/lib2to3/tests/test_fixers.py index eacb787..056e95b 100755 --- a/Lib/lib2to3/tests/test_fixers.py +++ b/Lib/lib2to3/tests/test_fixers.py @@ -10,6 +10,7 @@ except ImportError: # Python imports import unittest +from os.path import dirname, pathsep # Local imports from .. import pygram @@ -28,6 +29,7 @@ class FixerTestCase(support.TestCase): options = Options(fix=[self.fixer], print_function=False) self.refactor = refactor.RefactoringTool(options) self.fixer_log = [] + self.filename = "<string>" for order in (self.refactor.pre_order, self.refactor.post_order): for fixer in order: @@ -36,7 +38,7 @@ class FixerTestCase(support.TestCase): def _check(self, before, after): before = support.reformat(before) after = support.reformat(after) - tree = self.refactor.refactor_string(before, "<string>") + tree = self.refactor.refactor_string(before, self.filename) self.failUnlessEqual(after, str(tree)) return tree @@ -60,6 +62,21 @@ class FixerTestCase(support.TestCase): if not ignore_warnings: self.failUnlessEqual(self.fixer_log, []) + def assert_runs_after(self, *names): + fix = [self.fixer] + fix.extend(names) + options = Options(fix=fix, print_function=False) + r = refactor.RefactoringTool(options) + (pre, post) = r.get_fixers() + n = "fix_" + self.fixer + if post and post[-1].__class__.__module__.endswith(n): + # We're the last fixer to run + return + if pre and pre[-1].__class__.__module__.endswith(n) and not post: + # We're the last in pre and post is empty + return + self.fail("Fixer run order (%s) is incorrect; %s should be last."\ + %(", ".join([x.__class__.__module__ for x in (pre+post)]), n)) class Test_ne(FixerTestCase): fixer = "ne" @@ -412,6 +429,29 @@ 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. + try: + 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) + + 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" @@ -464,7 +504,6 @@ class Test_exec(FixerTestCase): s = """exec(code, ns1, ns2)""" self.unchanged(s) - class Test_repr(FixerTestCase): fixer = "repr" @@ -666,7 +705,6 @@ class Test_except(FixerTestCase): pass""" self.unchanged(s) - class Test_raise(FixerTestCase): fixer = "raise" @@ -789,7 +827,6 @@ class Test_raise(FixerTestCase): b = 6""" self.check(b, a) - class Test_throw(FixerTestCase): fixer = "throw" @@ -915,7 +952,6 @@ class Test_throw(FixerTestCase): b = 6""" self.check(b, a) - class Test_long(FixerTestCase): fixer = "long" @@ -961,7 +997,6 @@ class Test_long(FixerTestCase): a = """x = int( x )""" self.check(b, a) - class Test_dict(FixerTestCase): fixer = "dict" @@ -1171,7 +1206,6 @@ class Test_xrange(FixerTestCase): a = """for i in range(10):\n j=i""" self.check(b, a) - class Test_raw_input(FixerTestCase): fixer = "raw_input" @@ -1204,7 +1238,6 @@ class Test_raw_input(FixerTestCase): a = """x = input(foo(a) + 6)""" self.check(b, a) - class Test_funcattrs(FixerTestCase): fixer = "funcattrs" @@ -1231,7 +1264,6 @@ class Test_funcattrs(FixerTestCase): s = "f(foo.__%s__.foo)" % attr self.unchanged(s) - class Test_xreadlines(FixerTestCase): fixer = "xreadlines" @@ -1274,7 +1306,6 @@ class Test_xreadlines(FixerTestCase): s = "foo(xreadlines)" self.unchanged(s) - class Test_imports(FixerTestCase): fixer = "imports" @@ -1352,7 +1383,6 @@ class Test_imports(FixerTestCase): """ % (new, member, member, member) self.check(b, a) - class Test_input(FixerTestCase): fixer = "input" @@ -1400,7 +1430,6 @@ class Test_input(FixerTestCase): a = """x = eval(input(foo(5) + 9))""" self.check(b, a) - class Test_tuple_params(FixerTestCase): fixer = "tuple_params" @@ -1620,7 +1649,6 @@ class Test_methodattrs(FixerTestCase): s = "f(foo.__%s__.foo)" % attr self.unchanged(s) - class Test_next(FixerTestCase): fixer = "next" @@ -2250,7 +2278,6 @@ class Test_renames(FixerTestCase): """ % (mod, new, mod, new) self.check(b, a) - class Test_unicode(FixerTestCase): fixer = "unicode" @@ -2904,7 +2931,6 @@ class Test_idioms(FixerTestCase): """ self.unchanged(s) - class Test_basestring(FixerTestCase): fixer = "basestring" @@ -2913,7 +2939,6 @@ class Test_basestring(FixerTestCase): a = """isinstance(x, str)""" self.check(b, a) - class Test_buffer(FixerTestCase): fixer = "buffer" @@ -2930,6 +2955,17 @@ class Test_future(FixerTestCase): a = """""" self.check(b, a) + b = """# comment\nfrom __future__ import braces""" + a = """# comment\n""" + self.check(b, a) + + b = """from __future__ import braces\n# comment""" + a = """\n# comment""" + self.check(b, a) + + def test_run_order(self): + self.assert_runs_after('print') + class Test_itertools(FixerTestCase): fixer = "itertools" @@ -2975,6 +3011,129 @@ class Test_itertools(FixerTestCase): a = """ itertools.filterfalse(a, b)""" self.check(b, a) + def test_run_order(self): + self.assert_runs_after('map', 'zip', 'filter') + +class Test_itertools_imports(FixerTestCase): + fixer = 'itertools_imports' + + def test_reduced(self): + b = "from itertools import imap, izip, foo" + a = "from itertools import foo" + self.check(b, a) + + b = "from itertools import bar, imap, izip, foo" + a = "from itertools import bar, foo" + self.check(b, a) + + def test_comments(self): + b = "#foo\nfrom itertools import imap, izip" + a = "#foo\n" + self.check(b, a) + + def test_none(self): + b = "from itertools import imap, izip" + a = "" + self.check(b, a) + + def test_import_as(self): + b = "from itertools import izip, bar as bang, imap" + a = "from itertools import bar as bang" + self.check(b, a) + + s = "from itertools import bar as bang" + self.unchanged(s) + + def test_ifilter(self): + b = "from itertools import ifilterfalse" + a = "from itertools import filterfalse" + self.check(b, a) + + b = "from itertools import imap, ifilterfalse, foo" + a = "from itertools import filterfalse, foo" + self.check(b, a) + + b = "from itertools import bar, ifilterfalse, foo" + a = "from itertools import bar, filterfalse, foo" + self.check(b, a) + + + def test_unchanged(self): + s = "from itertools import foo" + self.unchanged(s) + +class Test_import(FixerTestCase): + fixer = "import" + + def setUp(self): + FixerTestCase.setUp(self) + # Need to replace fix_import's isfile and isdir method + # so we can check that it's doing the right thing + self.files_checked = [] + self.always_exists = True + def fake_exists(name): + self.files_checked.append(name) + return self.always_exists + + from ..fixes import fix_import + fix_import.exists = fake_exists + + def check_both(self, b, a): + self.always_exists = True + FixerTestCase.check(self, b, a) + self.always_exists = False + FixerTestCase.unchanged(self, b) + + def test_files_checked(self): + def p(path): + # Takes a unix path and returns a path with correct separators + return pathsep.join(path.split("/")) + + self.always_exists = False + expected_extensions = ('.py', pathsep, '.pyc', '.so', '.sl', '.pyd') + names_to_test = (p("/spam/eggs.py"), "ni.py", p("../../shrubbery.py")) + + for name in names_to_test: + self.files_checked = [] + self.filename = name + self.unchanged("import jam") + + if dirname(name): name = dirname(name) + '/jam' + else: name = 'jam' + expected_checks = set(name + ext for ext in expected_extensions) + + self.failUnlessEqual(set(self.files_checked), expected_checks) + + def test_from(self): + b = "from foo import bar" + a = "from .foo import bar" + self.check_both(b, a) + + def test_dotted_from(self): + b = "from green.eggs import ham" + a = "from .green.eggs import ham" + self.check_both(b, a) + + def test_from_as(self): + b = "from green.eggs import ham as spam" + a = "from .green.eggs import ham as spam" + self.check_both(b, a) + + def test_import(self): + b = "import foo" + a = "import .foo" + self.check_both(b, a) + + def test_dotted_import(self): + b = "import foo.bar" + a = "import .foo.bar" + self.check_both(b, a) + + def test_dotted_import_as(self): + b = "import foo.bar as bang" + a = "import .foo.bar as bang" + self.check_both(b, a) + if __name__ == "__main__": import __main__ |