diff options
Diffstat (limited to 'Lib/lib2to3/tests/test_fixers.py')
-rw-r--r-- | Lib/lib2to3/tests/test_fixers.py | 174 |
1 files changed, 39 insertions, 135 deletions
diff --git a/Lib/lib2to3/tests/test_fixers.py b/Lib/lib2to3/tests/test_fixers.py index 3da5dd8..c7d5ff9 100644 --- a/Lib/lib2to3/tests/test_fixers.py +++ b/Lib/lib2to3/tests/test_fixers.py @@ -2,11 +2,12 @@ # Python imports import os +import unittest from itertools import chain from operator import itemgetter # Local imports -from lib2to3 import pygram, fixer_util +from lib2to3 import pygram, pytree, refactor, fixer_util from lib2to3.tests import support @@ -19,7 +20,7 @@ class FixerTestCase(support.TestCase): fix_list = [self.fixer] self.refactor = support.get_refactorer(fixer_pkg, fix_list, options) self.fixer_log = [] - self.filename = "<string>" + self.filename = u"<string>" for fixer in chain(self.refactor.pre_order, self.refactor.post_order): @@ -29,7 +30,7 @@ class FixerTestCase(support.TestCase): before = support.reformat(before) after = support.reformat(after) tree = self.refactor.refactor_string(before, self.filename) - self.assertEqual(after, str(tree)) + self.assertEqual(after, unicode(tree)) return tree def check(self, before, after, ignore_warnings=False): @@ -285,65 +286,6 @@ class Test_apply(FixerTestCase): b = """f(*args, **kwds)""" self.check(a, b) -class Test_reload(FixerTestCase): - fixer = "reload" - - def test(self): - b = """reload(a)""" - a = """import importlib\nimportlib.reload(a)""" - self.check(b, a) - - def test_comment(self): - b = """reload( a ) # comment""" - a = """import importlib\nimportlib.reload( a ) # comment""" - self.check(b, a) - - # PEP 8 comments - b = """reload( a ) # comment""" - a = """import importlib\nimportlib.reload( a ) # comment""" - self.check(b, a) - - def test_space(self): - b = """reload( a )""" - a = """import importlib\nimportlib.reload( a )""" - self.check(b, a) - - b = """reload( a)""" - a = """import importlib\nimportlib.reload( a)""" - self.check(b, a) - - b = """reload(a )""" - a = """import importlib\nimportlib.reload(a )""" - self.check(b, a) - - def test_unchanged(self): - s = """reload(a=1)""" - self.unchanged(s) - - s = """reload(f, g)""" - self.unchanged(s) - - s = """reload(f, *h)""" - self.unchanged(s) - - s = """reload(f, *h, **i)""" - self.unchanged(s) - - s = """reload(f, **i)""" - self.unchanged(s) - - s = """reload(*h, **i)""" - self.unchanged(s) - - s = """reload(*h)""" - self.unchanged(s) - - s = """reload(**i)""" - self.unchanged(s) - - s = """reload()""" - self.unchanged(s) - class Test_intern(FixerTestCase): fixer = "intern" @@ -2806,7 +2748,7 @@ class Test_renames(FixerTestCase): } def test_import_from(self): - for mod, (old, new) in list(self.modules.items()): + for mod, (old, new) in self.modules.items(): b = "from %s import %s" % (mod, old) a = "from %s import %s" % (mod, new) self.check(b, a) @@ -2815,13 +2757,13 @@ class Test_renames(FixerTestCase): self.unchanged(s) def test_import_from_as(self): - for mod, (old, new) in list(self.modules.items()): + for mod, (old, new) in self.modules.items(): b = "from %s import %s as foo_bar" % (mod, old) a = "from %s import %s as foo_bar" % (mod, new) self.check(b, a) def test_import_module_usage(self): - for mod, (old, new) in list(self.modules.items()): + for mod, (old, new) in self.modules.items(): b = """ import %s foo(%s, %s.%s) @@ -2834,7 +2776,7 @@ class Test_renames(FixerTestCase): def XXX_test_from_import_usage(self): # not implemented yet - for mod, (old, new) in list(self.modules.items()): + for mod, (old, new) in self.modules.items(): b = """ from %s import %s foo(%s, %s) @@ -2887,40 +2829,40 @@ class Test_unicode(FixerTestCase): self.check(b, a) def test_native_literal_escape_u(self): - b = r"""'\\\u20ac\U0001d121\\u20ac'""" - a = r"""'\\\\u20ac\\U0001d121\\u20ac'""" + b = """'\\\\\\u20ac\\U0001d121\\\\u20ac'""" + a = """'\\\\\\\\u20ac\\\\U0001d121\\\\u20ac'""" self.check(b, a) - b = r"""r'\\\u20ac\U0001d121\\u20ac'""" - a = r"""r'\\\u20ac\U0001d121\\u20ac'""" + b = """r'\\\\\\u20ac\\U0001d121\\\\u20ac'""" + a = """r'\\\\\\u20ac\\U0001d121\\\\u20ac'""" self.check(b, a) def test_bytes_literal_escape_u(self): - b = r"""b'\\\u20ac\U0001d121\\u20ac'""" - a = r"""b'\\\u20ac\U0001d121\\u20ac'""" + b = """b'\\\\\\u20ac\\U0001d121\\\\u20ac'""" + a = """b'\\\\\\u20ac\\U0001d121\\\\u20ac'""" self.check(b, a) - b = r"""br'\\\u20ac\U0001d121\\u20ac'""" - a = r"""br'\\\u20ac\U0001d121\\u20ac'""" + b = """br'\\\\\\u20ac\\U0001d121\\\\u20ac'""" + a = """br'\\\\\\u20ac\\U0001d121\\\\u20ac'""" self.check(b, a) def test_unicode_literal_escape_u(self): - b = r"""u'\\\u20ac\U0001d121\\u20ac'""" - a = r"""'\\\u20ac\U0001d121\\u20ac'""" + b = """u'\\\\\\u20ac\\U0001d121\\\\u20ac'""" + a = """'\\\\\\u20ac\\U0001d121\\\\u20ac'""" self.check(b, a) - b = r"""ur'\\\u20ac\U0001d121\\u20ac'""" - a = r"""r'\\\u20ac\U0001d121\\u20ac'""" + b = """ur'\\\\\\u20ac\\U0001d121\\\\u20ac'""" + a = """r'\\\\\\u20ac\\U0001d121\\\\u20ac'""" self.check(b, a) def test_native_unicode_literal_escape_u(self): f = 'from __future__ import unicode_literals\n' - b = f + r"""'\\\u20ac\U0001d121\\u20ac'""" - a = f + r"""'\\\u20ac\U0001d121\\u20ac'""" + b = f + """'\\\\\\u20ac\\U0001d121\\\\u20ac'""" + a = f + """'\\\\\\u20ac\\U0001d121\\\\u20ac'""" self.check(b, a) - b = f + r"""r'\\\u20ac\U0001d121\\u20ac'""" - a = f + r"""r'\\\u20ac\U0001d121\\u20ac'""" + b = f + """r'\\\\\\u20ac\\U0001d121\\\\u20ac'""" + a = f + """r'\\\\\\u20ac\\U0001d121\\\\u20ac'""" self.check(b, a) @@ -2954,23 +2896,10 @@ class Test_filter(FixerTestCase): a = """x = [x for x in range(10) if x%2 == 0]""" self.check(b, a) - def test_filter_trailers(self): - b = """x = filter(None, 'abc')[0]""" - a = """x = [_f for _f in 'abc' if _f][0]""" - self.check(b, a) - - b = """x = len(filter(f, 'abc')[0])""" - a = """x = len(list(filter(f, 'abc'))[0])""" - self.check(b, a) - - b = """x = filter(lambda x: x%2 == 0, range(10))[0]""" - a = """x = [x for x in range(10) if x%2 == 0][0]""" - self.check(b, a) - - # Note the parens around x - b = """x = filter(lambda (x): x%2 == 0, range(10))[0]""" - a = """x = [x for x in range(10) if x%2 == 0][0]""" - self.check(b, a) + # XXX This (rare) case is not supported +## b = """x = filter(f, 'abc')[0]""" +## a = """x = list(filter(f, 'abc'))[0]""" +## self.check(b, a) def test_filter_nochange(self): a = """b.join(filter(f, 'abc'))""" @@ -3035,23 +2964,6 @@ class Test_map(FixerTestCase): a = """x = list(map( f, 'abc' ))""" self.check(b, a) - def test_map_trailers(self): - b = """x = map(f, 'abc')[0]""" - a = """x = list(map(f, 'abc'))[0]""" - self.check(b, a) - - b = """x = map(None, l)[0]""" - a = """x = list(l)[0]""" - self.check(b, a) - - b = """x = map(lambda x:x, l)[0]""" - a = """x = [x for x in l][0]""" - self.check(b, a) - - b = """x = map(f, 'abc')[0][1]""" - a = """x = list(map(f, 'abc'))[0][1]""" - self.check(b, a) - def test_trailing_comment(self): b = """x = map(f, 'abc') # foo""" a = """x = list(map(f, 'abc')) # foo""" @@ -3096,6 +3008,11 @@ class Test_map(FixerTestCase): """ self.warns(b, a, "You should use a for loop here") + # XXX This (rare) case is not supported +## b = """x = map(f, 'abc')[0]""" +## a = """x = list(map(f, 'abc'))[0]""" +## self.check(b, a) + def test_map_nochange(self): a = """b.join(map(f, 'abc'))""" self.unchanged(a) @@ -3155,10 +3072,6 @@ class Test_zip(FixerTestCase): super(Test_zip, self).check(b, a) def test_zip_basic(self): - b = """x = zip()""" - a = """x = list(zip())""" - self.check(b, a) - b = """x = zip(a, b, c)""" a = """x = list(zip(a, b, c))""" self.check(b, a) @@ -3167,15 +3080,6 @@ class Test_zip(FixerTestCase): a = """x = len(list(zip(a, b)))""" self.check(b, a) - def test_zip_trailers(self): - b = """x = zip(a, b, c)[0]""" - a = """x = list(zip(a, b, c))[0]""" - self.check(b, a) - - b = """x = zip(a, b, c)[0][1]""" - a = """x = list(zip(a, b, c))[0][1]""" - self.check(b, a) - def test_zip_nochange(self): a = """b.join(zip(a, b))""" self.unchanged(a) @@ -4409,7 +4313,7 @@ class Test_operator(FixerTestCase): def test_operator_isCallable(self): b = "operator.isCallable(x)" - a = "callable(x)" + a = "hasattr(x, '__call__')" self.check(b, a) def test_operator_sequenceIncludes(self): @@ -4427,12 +4331,12 @@ class Test_operator(FixerTestCase): def test_operator_isSequenceType(self): b = "operator.isSequenceType(x)" - a = "import collections.abc\nisinstance(x, collections.abc.Sequence)" + a = "import collections\nisinstance(x, collections.Sequence)" self.check(b, a) def test_operator_isMappingType(self): b = "operator.isMappingType(x)" - a = "import collections.abc\nisinstance(x, collections.abc.Mapping)" + a = "import collections\nisinstance(x, collections.Mapping)" self.check(b, a) def test_operator_isNumberType(self): @@ -4468,7 +4372,7 @@ class Test_operator(FixerTestCase): def test_bare_isCallable(self): s = "isCallable(x)" - t = "You should use 'callable(x)' here." + t = "You should use 'hasattr(x, '__call__')' here." self.warns_unchanged(s, t) def test_bare_sequenceIncludes(self): @@ -4478,12 +4382,12 @@ class Test_operator(FixerTestCase): def test_bare_operator_isSequenceType(self): s = "isSequenceType(z)" - t = "You should use 'isinstance(z, collections.abc.Sequence)' here." + t = "You should use 'isinstance(z, collections.Sequence)' here." self.warns_unchanged(s, t) def test_bare_operator_isMappingType(self): s = "isMappingType(x)" - t = "You should use 'isinstance(x, collections.abc.Mapping)' here." + t = "You should use 'isinstance(x, collections.Mapping)' here." self.warns_unchanged(s, t) def test_bare_operator_isNumberType(self): |