Home | History | Annotate | Download | only in tests
      1 """ Test suite for the fixer modules """
      2 
      3 # Python imports
      4 import os
      5 import unittest
      6 from itertools import chain
      7 from operator import itemgetter
      8 
      9 # Local imports
     10 from lib2to3 import pygram, pytree, refactor, fixer_util
     11 from lib2to3.tests import support
     12 
     13 
     14 class FixerTestCase(support.TestCase):
     15 
     16     # Other test cases can subclass this class and replace "fixer_pkg" with
     17     # their own.
     18     def setUp(self, fix_list=None, fixer_pkg="lib2to3", options=None):
     19         if fix_list is None:
     20             fix_list = [self.fixer]
     21         self.refactor = support.get_refactorer(fixer_pkg, fix_list, options)
     22         self.fixer_log = []
     23         self.filename = u"<string>"
     24 
     25         for fixer in chain(self.refactor.pre_order,
     26                            self.refactor.post_order):
     27             fixer.log = self.fixer_log
     28 
     29     def _check(self, before, after):
     30         before = support.reformat(before)
     31         after = support.reformat(after)
     32         tree = self.refactor.refactor_string(before, self.filename)
     33         self.assertEqual(after, unicode(tree))
     34         return tree
     35 
     36     def check(self, before, after, ignore_warnings=False):
     37         tree = self._check(before, after)
     38         self.assertTrue(tree.was_changed)
     39         if not ignore_warnings:
     40             self.assertEqual(self.fixer_log, [])
     41 
     42     def warns(self, before, after, message, unchanged=False):
     43         tree = self._check(before, after)
     44         self.assertTrue(message in "".join(self.fixer_log))
     45         if not unchanged:
     46             self.assertTrue(tree.was_changed)
     47 
     48     def warns_unchanged(self, before, message):
     49         self.warns(before, before, message, unchanged=True)
     50 
     51     def unchanged(self, before, ignore_warnings=False):
     52         self._check(before, before)
     53         if not ignore_warnings:
     54             self.assertEqual(self.fixer_log, [])
     55 
     56     def assert_runs_after(self, *names):
     57         fixes = [self.fixer]
     58         fixes.extend(names)
     59         r = support.get_refactorer("lib2to3", fixes)
     60         (pre, post) = r.get_fixers()
     61         n = "fix_" + self.fixer
     62         if post and post[-1].__class__.__module__.endswith(n):
     63             # We're the last fixer to run
     64             return
     65         if pre and pre[-1].__class__.__module__.endswith(n) and not post:
     66             # We're the last in pre and post is empty
     67             return
     68         self.fail("Fixer run order (%s) is incorrect; %s should be last."\
     69                %(", ".join([x.__class__.__module__ for x in (pre+post)]), n))
     70 
     71 class Test_ne(FixerTestCase):
     72     fixer = "ne"
     73 
     74     def test_basic(self):
     75         b = """if x <> y:
     76             pass"""
     77 
     78         a = """if x != y:
     79             pass"""
     80         self.check(b, a)
     81 
     82     def test_no_spaces(self):
     83         b = """if x<>y:
     84             pass"""
     85 
     86         a = """if x!=y:
     87             pass"""
     88         self.check(b, a)
     89 
     90     def test_chained(self):
     91         b = """if x<>y<>z:
     92             pass"""
     93 
     94         a = """if x!=y!=z:
     95             pass"""
     96         self.check(b, a)
     97 
     98 class Test_has_key(FixerTestCase):
     99     fixer = "has_key"
    100 
    101     def test_1(self):
    102         b = """x = d.has_key("x") or d.has_key("y")"""
    103         a = """x = "x" in d or "y" in d"""
    104         self.check(b, a)
    105 
    106     def test_2(self):
    107         b = """x = a.b.c.d.has_key("x") ** 3"""
    108         a = """x = ("x" in a.b.c.d) ** 3"""
    109         self.check(b, a)
    110 
    111     def test_3(self):
    112         b = """x = a.b.has_key(1 + 2).__repr__()"""
    113         a = """x = (1 + 2 in a.b).__repr__()"""
    114         self.check(b, a)
    115 
    116     def test_4(self):
    117         b = """x = a.b.has_key(1 + 2).__repr__() ** -3 ** 4"""
    118         a = """x = (1 + 2 in a.b).__repr__() ** -3 ** 4"""
    119         self.check(b, a)
    120 
    121     def test_5(self):
    122         b = """x = a.has_key(f or g)"""
    123         a = """x = (f or g) in a"""
    124         self.check(b, a)
    125 
    126     def test_6(self):
    127         b = """x = a + b.has_key(c)"""
    128         a = """x = a + (c in b)"""
    129         self.check(b, a)
    130 
    131     def test_7(self):
    132         b = """x = a.has_key(lambda: 12)"""
    133         a = """x = (lambda: 12) in a"""
    134         self.check(b, a)
    135 
    136     def test_8(self):
    137         b = """x = a.has_key(a for a in b)"""
    138         a = """x = (a for a in b) in a"""
    139         self.check(b, a)
    140 
    141     def test_9(self):
    142         b = """if not a.has_key(b): pass"""
    143         a = """if b not in a: pass"""
    144         self.check(b, a)
    145 
    146     def test_10(self):
    147         b = """if not a.has_key(b).__repr__(): pass"""
    148         a = """if not (b in a).__repr__(): pass"""
    149         self.check(b, a)
    150 
    151     def test_11(self):
    152         b = """if not a.has_key(b) ** 2: pass"""
    153         a = """if not (b in a) ** 2: pass"""
    154         self.check(b, a)
    155 
    156 class Test_apply(FixerTestCase):
    157     fixer = "apply"
    158 
    159     def test_1(self):
    160         b = """x = apply(f, g + h)"""
    161         a = """x = f(*g + h)"""
    162         self.check(b, a)
    163 
    164     def test_2(self):
    165         b = """y = apply(f, g, h)"""
    166         a = """y = f(*g, **h)"""
    167         self.check(b, a)
    168 
    169     def test_3(self):
    170         b = """z = apply(fs[0], g or h, h or g)"""
    171         a = """z = fs[0](*g or h, **h or g)"""
    172         self.check(b, a)
    173 
    174     def test_4(self):
    175         b = """apply(f, (x, y) + t)"""
    176         a = """f(*(x, y) + t)"""
    177         self.check(b, a)
    178 
    179     def test_5(self):
    180         b = """apply(f, args,)"""
    181         a = """f(*args)"""
    182         self.check(b, a)
    183 
    184     def test_6(self):
    185         b = """apply(f, args, kwds,)"""
    186         a = """f(*args, **kwds)"""
    187         self.check(b, a)
    188 
    189     # Test that complex functions are parenthesized
    190 
    191     def test_complex_1(self):
    192         b = """x = apply(f+g, args)"""
    193         a = """x = (f+g)(*args)"""
    194         self.check(b, a)
    195 
    196     def test_complex_2(self):
    197         b = """x = apply(f*g, args)"""
    198         a = """x = (f*g)(*args)"""
    199         self.check(b, a)
    200 
    201     def test_complex_3(self):
    202         b = """x = apply(f**g, args)"""
    203         a = """x = (f**g)(*args)"""
    204         self.check(b, a)
    205 
    206     # But dotted names etc. not
    207 
    208     def test_dotted_name(self):
    209         b = """x = apply(f.g, args)"""
    210         a = """x = f.g(*args)"""
    211         self.check(b, a)
    212 
    213     def test_subscript(self):
    214         b = """x = apply(f[x], args)"""
    215         a = """x = f[x](*args)"""
    216         self.check(b, a)
    217 
    218     def test_call(self):
    219         b = """x = apply(f(), args)"""
    220         a = """x = f()(*args)"""
    221         self.check(b, a)
    222 
    223     # Extreme case
    224     def test_extreme(self):
    225         b = """x = apply(a.b.c.d.e.f, args, kwds)"""
    226         a = """x = a.b.c.d.e.f(*args, **kwds)"""
    227         self.check(b, a)
    228 
    229     # XXX Comments in weird places still get lost
    230     def test_weird_comments(self):
    231         b = """apply(   # foo
    232           f, # bar
    233           args)"""
    234         a = """f(*args)"""
    235         self.check(b, a)
    236 
    237     # These should *not* be touched
    238 
    239     def test_unchanged_1(self):
    240         s = """apply()"""
    241         self.unchanged(s)
    242 
    243     def test_unchanged_2(self):
    244         s = """apply(f)"""
    245         self.unchanged(s)
    246 
    247     def test_unchanged_3(self):
    248         s = """apply(f,)"""
    249         self.unchanged(s)
    250 
    251     def test_unchanged_4(self):
    252         s = """apply(f, args, kwds, extras)"""
    253         self.unchanged(s)
    254 
    255     def test_unchanged_5(self):
    256         s = """apply(f, *args, **kwds)"""
    257         self.unchanged(s)
    258 
    259     def test_unchanged_6(self):
    260         s = """apply(f, *args)"""
    261         self.unchanged(s)
    262 
    263     def test_unchanged_7(self):
    264         s = """apply(func=f, args=args, kwds=kwds)"""
    265         self.unchanged(s)
    266 
    267     def test_unchanged_8(self):
    268         s = """apply(f, args=args, kwds=kwds)"""
    269         self.unchanged(s)
    270 
    271     def test_unchanged_9(self):
    272         s = """apply(f, args, kwds=kwds)"""
    273         self.unchanged(s)
    274 
    275     def test_space_1(self):
    276         a = """apply(  f,  args,   kwds)"""
    277         b = """f(*args, **kwds)"""
    278         self.check(a, b)
    279 
    280     def test_space_2(self):
    281         a = """apply(  f  ,args,kwds   )"""
    282         b = """f(*args, **kwds)"""
    283         self.check(a, b)
    284 
    285 class Test_intern(FixerTestCase):
    286     fixer = "intern"
    287 
    288     def test_prefix_preservation(self):
    289         b = """x =   intern(  a  )"""
    290         a = """import sys\nx =   sys.intern(  a  )"""
    291         self.check(b, a)
    292 
    293         b = """y = intern("b" # test
    294               )"""
    295         a = """import sys\ny = sys.intern("b" # test
    296               )"""
    297         self.check(b, a)
    298 
    299         b = """z = intern(a+b+c.d,   )"""
    300         a = """import sys\nz = sys.intern(a+b+c.d,   )"""
    301         self.check(b, a)
    302 
    303     def test(self):
    304         b = """x = intern(a)"""
    305         a = """import sys\nx = sys.intern(a)"""
    306         self.check(b, a)
    307 
    308         b = """z = intern(a+b+c.d,)"""
    309         a = """import sys\nz = sys.intern(a+b+c.d,)"""
    310         self.check(b, a)
    311 
    312         b = """intern("y%s" % 5).replace("y", "")"""
    313         a = """import sys\nsys.intern("y%s" % 5).replace("y", "")"""
    314         self.check(b, a)
    315 
    316     # These should not be refactored
    317 
    318     def test_unchanged(self):
    319         s = """intern(a=1)"""
    320         self.unchanged(s)
    321 
    322         s = """intern(f, g)"""
    323         self.unchanged(s)
    324 
    325         s = """intern(*h)"""
    326         self.unchanged(s)
    327 
    328         s = """intern(**i)"""
    329         self.unchanged(s)
    330 
    331         s = """intern()"""
    332         self.unchanged(s)
    333 
    334 class Test_reduce(FixerTestCase):
    335     fixer = "reduce"
    336 
    337     def test_simple_call(self):
    338         b = "reduce(a, b, c)"
    339         a = "from functools import reduce\nreduce(a, b, c)"
    340         self.check(b, a)
    341 
    342     def test_bug_7253(self):
    343         # fix_tuple_params was being bad and orphaning nodes in the tree.
    344         b = "def x(arg): reduce(sum, [])"
    345         a = "from functools import reduce\ndef x(arg): reduce(sum, [])"
    346         self.check(b, a)
    347 
    348     def test_call_with_lambda(self):
    349         b = "reduce(lambda x, y: x + y, seq)"
    350         a = "from functools import reduce\nreduce(lambda x, y: x + y, seq)"
    351         self.check(b, a)
    352 
    353     def test_unchanged(self):
    354         s = "reduce(a)"
    355         self.unchanged(s)
    356 
    357         s = "reduce(a, b=42)"
    358         self.unchanged(s)
    359 
    360         s = "reduce(a, b, c, d)"
    361         self.unchanged(s)
    362 
    363         s = "reduce(**c)"
    364         self.unchanged(s)
    365 
    366         s = "reduce()"
    367         self.unchanged(s)
    368 
    369 class Test_print(FixerTestCase):
    370     fixer = "print"
    371 
    372     def test_prefix_preservation(self):
    373         b = """print 1,   1+1,   1+1+1"""
    374         a = """print(1,   1+1,   1+1+1)"""
    375         self.check(b, a)
    376 
    377     def test_idempotency(self):
    378         s = """print()"""
    379         self.unchanged(s)
    380 
    381         s = """print('')"""
    382         self.unchanged(s)
    383 
    384     def test_idempotency_print_as_function(self):
    385         self.refactor.driver.grammar = pygram.python_grammar_no_print_statement
    386         s = """print(1, 1+1, 1+1+1)"""
    387         self.unchanged(s)
    388 
    389         s = """print()"""
    390         self.unchanged(s)
    391 
    392         s = """print('')"""
    393         self.unchanged(s)
    394 
    395     def test_1(self):
    396         b = """print 1, 1+1, 1+1+1"""
    397         a = """print(1, 1+1, 1+1+1)"""
    398         self.check(b, a)
    399 
    400     def test_2(self):
    401         b = """print 1, 2"""
    402         a = """print(1, 2)"""
    403         self.check(b, a)
    404 
    405     def test_3(self):
    406         b = """print"""
    407         a = """print()"""
    408         self.check(b, a)
    409 
    410     def test_4(self):
    411         # from bug 3000
    412         b = """print whatever; print"""
    413         a = """print(whatever); print()"""
    414         self.check(b, a)
    415 
    416     def test_5(self):
    417         b = """print; print whatever;"""
    418         a = """print(); print(whatever);"""
    419         self.check(b, a)
    420 
    421     def test_tuple(self):
    422         b = """print (a, b, c)"""
    423         a = """print((a, b, c))"""
    424         self.check(b, a)
    425 
    426     # trailing commas
    427 
    428     def test_trailing_comma_1(self):
    429         b = """print 1, 2, 3,"""
    430         a = """print(1, 2, 3, end=' ')"""
    431         self.check(b, a)
    432 
    433     def test_trailing_comma_2(self):
    434         b = """print 1, 2,"""
    435         a = """print(1, 2, end=' ')"""
    436         self.check(b, a)
    437 
    438     def test_trailing_comma_3(self):
    439         b = """print 1,"""
    440         a = """print(1, end=' ')"""
    441         self.check(b, a)
    442 
    443     # >> stuff
    444 
    445     def test_vargs_without_trailing_comma(self):
    446         b = """print >>sys.stderr, 1, 2, 3"""
    447         a = """print(1, 2, 3, file=sys.stderr)"""
    448         self.check(b, a)
    449 
    450     def test_with_trailing_comma(self):
    451         b = """print >>sys.stderr, 1, 2,"""
    452         a = """print(1, 2, end=' ', file=sys.stderr)"""
    453         self.check(b, a)
    454 
    455     def test_no_trailing_comma(self):
    456         b = """print >>sys.stderr, 1+1"""
    457         a = """print(1+1, file=sys.stderr)"""
    458         self.check(b, a)
    459 
    460     def test_spaces_before_file(self):
    461         b = """print >>  sys.stderr"""
    462         a = """print(file=sys.stderr)"""
    463         self.check(b, a)
    464 
    465     def test_with_future_print_function(self):
    466         s = "from __future__ import print_function\n" \
    467             "print('Hai!', end=' ')"
    468         self.unchanged(s)
    469 
    470         b = "print 'Hello, world!'"
    471         a = "print('Hello, world!')"
    472         self.check(b, a)
    473 
    474 
    475 class Test_exec(FixerTestCase):
    476     fixer = "exec"
    477 
    478     def test_prefix_preservation(self):
    479         b = """  exec code in ns1,   ns2"""
    480         a = """  exec(code, ns1,   ns2)"""
    481         self.check(b, a)
    482 
    483     def test_basic(self):
    484         b = """exec code"""
    485         a = """exec(code)"""
    486         self.check(b, a)
    487 
    488     def test_with_globals(self):
    489         b = """exec code in ns"""
    490         a = """exec(code, ns)"""
    491         self.check(b, a)
    492 
    493     def test_with_globals_locals(self):
    494         b = """exec code in ns1, ns2"""
    495         a = """exec(code, ns1, ns2)"""
    496         self.check(b, a)
    497 
    498     def test_complex_1(self):
    499         b = """exec (a.b()) in ns"""
    500         a = """exec((a.b()), ns)"""
    501         self.check(b, a)
    502 
    503     def test_complex_2(self):
    504         b = """exec a.b() + c in ns"""
    505         a = """exec(a.b() + c, ns)"""
    506         self.check(b, a)
    507 
    508     # These should not be touched
    509 
    510     def test_unchanged_1(self):
    511         s = """exec(code)"""
    512         self.unchanged(s)
    513 
    514     def test_unchanged_2(self):
    515         s = """exec (code)"""
    516         self.unchanged(s)
    517 
    518     def test_unchanged_3(self):
    519         s = """exec(code, ns)"""
    520         self.unchanged(s)
    521 
    522     def test_unchanged_4(self):
    523         s = """exec(code, ns1, ns2)"""
    524         self.unchanged(s)
    525 
    526 class Test_repr(FixerTestCase):
    527     fixer = "repr"
    528 
    529     def test_prefix_preservation(self):
    530         b = """x =   `1 + 2`"""
    531         a = """x =   repr(1 + 2)"""
    532         self.check(b, a)
    533 
    534     def test_simple_1(self):
    535         b = """x = `1 + 2`"""
    536         a = """x = repr(1 + 2)"""
    537         self.check(b, a)
    538 
    539     def test_simple_2(self):
    540         b = """y = `x`"""
    541         a = """y = repr(x)"""
    542         self.check(b, a)
    543 
    544     def test_complex(self):
    545         b = """z = `y`.__repr__()"""
    546         a = """z = repr(y).__repr__()"""
    547         self.check(b, a)
    548 
    549     def test_tuple(self):
    550         b = """x = `1, 2, 3`"""
    551         a = """x = repr((1, 2, 3))"""
    552         self.check(b, a)
    553 
    554     def test_nested(self):
    555         b = """x = `1 + `2``"""
    556         a = """x = repr(1 + repr(2))"""
    557         self.check(b, a)
    558 
    559     def test_nested_tuples(self):
    560         b = """x = `1, 2 + `3, 4``"""
    561         a = """x = repr((1, 2 + repr((3, 4))))"""
    562         self.check(b, a)
    563 
    564 class Test_except(FixerTestCase):
    565     fixer = "except"
    566 
    567     def test_prefix_preservation(self):
    568         b = """
    569             try:
    570                 pass
    571             except (RuntimeError, ImportError),    e:
    572                 pass"""
    573         a = """
    574             try:
    575                 pass
    576             except (RuntimeError, ImportError) as    e:
    577                 pass"""
    578         self.check(b, a)
    579 
    580     def test_simple(self):
    581         b = """
    582             try:
    583                 pass
    584             except Foo, e:
    585                 pass"""
    586         a = """
    587             try:
    588                 pass
    589             except Foo as e:
    590                 pass"""
    591         self.check(b, a)
    592 
    593     def test_simple_no_space_before_target(self):
    594         b = """
    595             try:
    596                 pass
    597             except Foo,e:
    598                 pass"""
    599         a = """
    600             try:
    601                 pass
    602             except Foo as e:
    603                 pass"""
    604         self.check(b, a)
    605 
    606     def test_tuple_unpack(self):
    607         b = """
    608             def foo():
    609                 try:
    610                     pass
    611                 except Exception, (f, e):
    612                     pass
    613                 except ImportError, e:
    614                     pass"""
    615 
    616         a = """
    617             def foo():
    618                 try:
    619                     pass
    620                 except Exception as xxx_todo_changeme:
    621                     (f, e) = xxx_todo_changeme.args
    622                     pass
    623                 except ImportError as e:
    624                     pass"""
    625         self.check(b, a)
    626 
    627     def test_multi_class(self):
    628         b = """
    629             try:
    630                 pass
    631             except (RuntimeError, ImportError), e:
    632                 pass"""
    633 
    634         a = """
    635             try:
    636                 pass
    637             except (RuntimeError, ImportError) as e:
    638                 pass"""
    639         self.check(b, a)
    640 
    641     def test_list_unpack(self):
    642         b = """
    643             try:
    644                 pass
    645             except Exception, [a, b]:
    646                 pass"""
    647 
    648         a = """
    649             try:
    650                 pass
    651             except Exception as xxx_todo_changeme:
    652                 [a, b] = xxx_todo_changeme.args
    653                 pass"""
    654         self.check(b, a)
    655 
    656     def test_weird_target_1(self):
    657         b = """
    658             try:
    659                 pass
    660             except Exception, d[5]:
    661                 pass"""
    662 
    663         a = """
    664             try:
    665                 pass
    666             except Exception as xxx_todo_changeme:
    667                 d[5] = xxx_todo_changeme
    668                 pass"""
    669         self.check(b, a)
    670 
    671     def test_weird_target_2(self):
    672         b = """
    673             try:
    674                 pass
    675             except Exception, a.foo:
    676                 pass"""
    677 
    678         a = """
    679             try:
    680                 pass
    681             except Exception as xxx_todo_changeme:
    682                 a.foo = xxx_todo_changeme
    683                 pass"""
    684         self.check(b, a)
    685 
    686     def test_weird_target_3(self):
    687         b = """
    688             try:
    689                 pass
    690             except Exception, a().foo:
    691                 pass"""
    692 
    693         a = """
    694             try:
    695                 pass
    696             except Exception as xxx_todo_changeme:
    697                 a().foo = xxx_todo_changeme
    698                 pass"""
    699         self.check(b, a)
    700 
    701     def test_bare_except(self):
    702         b = """
    703             try:
    704                 pass
    705             except Exception, a:
    706                 pass
    707             except:
    708                 pass"""
    709 
    710         a = """
    711             try:
    712                 pass
    713             except Exception as a:
    714                 pass
    715             except:
    716                 pass"""
    717         self.check(b, a)
    718 
    719     def test_bare_except_and_else_finally(self):
    720         b = """
    721             try:
    722                 pass
    723             except Exception, a:
    724                 pass
    725             except:
    726                 pass
    727             else:
    728                 pass
    729             finally:
    730                 pass"""
    731 
    732         a = """
    733             try:
    734                 pass
    735             except Exception as a:
    736                 pass
    737             except:
    738                 pass
    739             else:
    740                 pass
    741             finally:
    742                 pass"""
    743         self.check(b, a)
    744 
    745     def test_multi_fixed_excepts_before_bare_except(self):
    746         b = """
    747             try:
    748                 pass
    749             except TypeError, b:
    750                 pass
    751             except Exception, a:
    752                 pass
    753             except:
    754                 pass"""
    755 
    756         a = """
    757             try:
    758                 pass
    759             except TypeError as b:
    760                 pass
    761             except Exception as a:
    762                 pass
    763             except:
    764                 pass"""
    765         self.check(b, a)
    766 
    767     def test_one_line_suites(self):
    768         b = """
    769             try: raise TypeError
    770             except TypeError, e:
    771                 pass
    772             """
    773         a = """
    774             try: raise TypeError
    775             except TypeError as e:
    776                 pass
    777             """
    778         self.check(b, a)
    779         b = """
    780             try:
    781                 raise TypeError
    782             except TypeError, e: pass
    783             """
    784         a = """
    785             try:
    786                 raise TypeError
    787             except TypeError as e: pass
    788             """
    789         self.check(b, a)
    790         b = """
    791             try: raise TypeError
    792             except TypeError, e: pass
    793             """
    794         a = """
    795             try: raise TypeError
    796             except TypeError as e: pass
    797             """
    798         self.check(b, a)
    799         b = """
    800             try: raise TypeError
    801             except TypeError, e: pass
    802             else: function()
    803             finally: done()
    804             """
    805         a = """
    806             try: raise TypeError
    807             except TypeError as e: pass
    808             else: function()
    809             finally: done()
    810             """
    811         self.check(b, a)
    812 
    813     # These should not be touched:
    814 
    815     def test_unchanged_1(self):
    816         s = """
    817             try:
    818                 pass
    819             except:
    820                 pass"""
    821         self.unchanged(s)
    822 
    823     def test_unchanged_2(self):
    824         s = """
    825             try:
    826                 pass
    827             except Exception:
    828                 pass"""
    829         self.unchanged(s)
    830 
    831     def test_unchanged_3(self):
    832         s = """
    833             try:
    834                 pass
    835             except (Exception, SystemExit):
    836                 pass"""
    837         self.unchanged(s)
    838 
    839 class Test_raise(FixerTestCase):
    840     fixer = "raise"
    841 
    842     def test_basic(self):
    843         b = """raise Exception, 5"""
    844         a = """raise Exception(5)"""
    845         self.check(b, a)
    846 
    847     def test_prefix_preservation(self):
    848         b = """raise Exception,5"""
    849         a = """raise Exception(5)"""
    850         self.check(b, a)
    851 
    852         b = """raise   Exception,    5"""
    853         a = """raise   Exception(5)"""
    854         self.check(b, a)
    855 
    856     def test_with_comments(self):
    857         b = """raise Exception, 5 # foo"""
    858         a = """raise Exception(5) # foo"""
    859         self.check(b, a)
    860 
    861         b = """raise E, (5, 6) % (a, b) # foo"""
    862         a = """raise E((5, 6) % (a, b)) # foo"""
    863         self.check(b, a)
    864 
    865         b = """def foo():
    866                     raise Exception, 5, 6 # foo"""
    867         a = """def foo():
    868                     raise Exception(5).with_traceback(6) # foo"""
    869         self.check(b, a)
    870 
    871     def test_None_value(self):
    872         b = """raise Exception(5), None, tb"""
    873         a = """raise Exception(5).with_traceback(tb)"""
    874         self.check(b, a)
    875 
    876     def test_tuple_value(self):
    877         b = """raise Exception, (5, 6, 7)"""
    878         a = """raise Exception(5, 6, 7)"""
    879         self.check(b, a)
    880 
    881     def test_tuple_detection(self):
    882         b = """raise E, (5, 6) % (a, b)"""
    883         a = """raise E((5, 6) % (a, b))"""
    884         self.check(b, a)
    885 
    886     def test_tuple_exc_1(self):
    887         b = """raise (((E1, E2), E3), E4), V"""
    888         a = """raise E1(V)"""
    889         self.check(b, a)
    890 
    891     def test_tuple_exc_2(self):
    892         b = """raise (E1, (E2, E3), E4), V"""
    893         a = """raise E1(V)"""
    894         self.check(b, a)
    895 
    896     # These should produce a warning
    897 
    898     def test_string_exc(self):
    899         s = """raise 'foo'"""
    900         self.warns_unchanged(s, "Python 3 does not support string exceptions")
    901 
    902     def test_string_exc_val(self):
    903         s = """raise "foo", 5"""
    904         self.warns_unchanged(s, "Python 3 does not support string exceptions")
    905 
    906     def test_string_exc_val_tb(self):
    907         s = """raise "foo", 5, 6"""
    908         self.warns_unchanged(s, "Python 3 does not support string exceptions")
    909 
    910     # These should result in traceback-assignment
    911 
    912     def test_tb_1(self):
    913         b = """def foo():
    914                     raise Exception, 5, 6"""
    915         a = """def foo():
    916                     raise Exception(5).with_traceback(6)"""
    917         self.check(b, a)
    918 
    919     def test_tb_2(self):
    920         b = """def foo():
    921                     a = 5
    922                     raise Exception, 5, 6
    923                     b = 6"""
    924         a = """def foo():
    925                     a = 5
    926                     raise Exception(5).with_traceback(6)
    927                     b = 6"""
    928         self.check(b, a)
    929 
    930     def test_tb_3(self):
    931         b = """def foo():
    932                     raise Exception,5,6"""
    933         a = """def foo():
    934                     raise Exception(5).with_traceback(6)"""
    935         self.check(b, a)
    936 
    937     def test_tb_4(self):
    938         b = """def foo():
    939                     a = 5
    940                     raise Exception,5,6
    941                     b = 6"""
    942         a = """def foo():
    943                     a = 5
    944                     raise Exception(5).with_traceback(6)
    945                     b = 6"""
    946         self.check(b, a)
    947 
    948     def test_tb_5(self):
    949         b = """def foo():
    950                     raise Exception, (5, 6, 7), 6"""
    951         a = """def foo():
    952                     raise Exception(5, 6, 7).with_traceback(6)"""
    953         self.check(b, a)
    954 
    955     def test_tb_6(self):
    956         b = """def foo():
    957                     a = 5
    958                     raise Exception, (5, 6, 7), 6
    959                     b = 6"""
    960         a = """def foo():
    961                     a = 5
    962                     raise Exception(5, 6, 7).with_traceback(6)
    963                     b = 6"""
    964         self.check(b, a)
    965 
    966 class Test_throw(FixerTestCase):
    967     fixer = "throw"
    968 
    969     def test_1(self):
    970         b = """g.throw(Exception, 5)"""
    971         a = """g.throw(Exception(5))"""
    972         self.check(b, a)
    973 
    974     def test_2(self):
    975         b = """g.throw(Exception,5)"""
    976         a = """g.throw(Exception(5))"""
    977         self.check(b, a)
    978 
    979     def test_3(self):
    980         b = """g.throw(Exception, (5, 6, 7))"""
    981         a = """g.throw(Exception(5, 6, 7))"""
    982         self.check(b, a)
    983 
    984     def test_4(self):
    985         b = """5 + g.throw(Exception, 5)"""
    986         a = """5 + g.throw(Exception(5))"""
    987         self.check(b, a)
    988 
    989     # These should produce warnings
    990 
    991     def test_warn_1(self):
    992         s = """g.throw("foo")"""
    993         self.warns_unchanged(s, "Python 3 does not support string exceptions")
    994 
    995     def test_warn_2(self):
    996         s = """g.throw("foo", 5)"""
    997         self.warns_unchanged(s, "Python 3 does not support string exceptions")
    998 
    999     def test_warn_3(self):
   1000         s = """g.throw("foo", 5, 6)"""
   1001         self.warns_unchanged(s, "Python 3 does not support string exceptions")
   1002 
   1003     # These should not be touched
   1004 
   1005     def test_untouched_1(self):
   1006         s = """g.throw(Exception)"""
   1007         self.unchanged(s)
   1008 
   1009     def test_untouched_2(self):
   1010         s = """g.throw(Exception(5, 6))"""
   1011         self.unchanged(s)
   1012 
   1013     def test_untouched_3(self):
   1014         s = """5 + g.throw(Exception(5, 6))"""
   1015         self.unchanged(s)
   1016 
   1017     # These should result in traceback-assignment
   1018 
   1019     def test_tb_1(self):
   1020         b = """def foo():
   1021                     g.throw(Exception, 5, 6)"""
   1022         a = """def foo():
   1023                     g.throw(Exception(5).with_traceback(6))"""
   1024         self.check(b, a)
   1025 
   1026     def test_tb_2(self):
   1027         b = """def foo():
   1028                     a = 5
   1029                     g.throw(Exception, 5, 6)
   1030                     b = 6"""
   1031         a = """def foo():
   1032                     a = 5
   1033                     g.throw(Exception(5).with_traceback(6))
   1034                     b = 6"""
   1035         self.check(b, a)
   1036 
   1037     def test_tb_3(self):
   1038         b = """def foo():
   1039                     g.throw(Exception,5,6)"""
   1040         a = """def foo():
   1041                     g.throw(Exception(5).with_traceback(6))"""
   1042         self.check(b, a)
   1043 
   1044     def test_tb_4(self):
   1045         b = """def foo():
   1046                     a = 5
   1047                     g.throw(Exception,5,6)
   1048                     b = 6"""
   1049         a = """def foo():
   1050                     a = 5
   1051                     g.throw(Exception(5).with_traceback(6))
   1052                     b = 6"""
   1053         self.check(b, a)
   1054 
   1055     def test_tb_5(self):
   1056         b = """def foo():
   1057                     g.throw(Exception, (5, 6, 7), 6)"""
   1058         a = """def foo():
   1059                     g.throw(Exception(5, 6, 7).with_traceback(6))"""
   1060         self.check(b, a)
   1061 
   1062     def test_tb_6(self):
   1063         b = """def foo():
   1064                     a = 5
   1065                     g.throw(Exception, (5, 6, 7), 6)
   1066                     b = 6"""
   1067         a = """def foo():
   1068                     a = 5
   1069                     g.throw(Exception(5, 6, 7).with_traceback(6))
   1070                     b = 6"""
   1071         self.check(b, a)
   1072 
   1073     def test_tb_7(self):
   1074         b = """def foo():
   1075                     a + g.throw(Exception, 5, 6)"""
   1076         a = """def foo():
   1077                     a + g.throw(Exception(5).with_traceback(6))"""
   1078         self.check(b, a)
   1079 
   1080     def test_tb_8(self):
   1081         b = """def foo():
   1082                     a = 5
   1083                     a + g.throw(Exception, 5, 6)
   1084                     b = 6"""
   1085         a = """def foo():
   1086                     a = 5
   1087                     a + g.throw(Exception(5).with_traceback(6))
   1088                     b = 6"""
   1089         self.check(b, a)
   1090 
   1091 class Test_long(FixerTestCase):
   1092     fixer = "long"
   1093 
   1094     def test_1(self):
   1095         b = """x = long(x)"""
   1096         a = """x = int(x)"""
   1097         self.check(b, a)
   1098 
   1099     def test_2(self):
   1100         b = """y = isinstance(x, long)"""
   1101         a = """y = isinstance(x, int)"""
   1102         self.check(b, a)
   1103 
   1104     def test_3(self):
   1105         b = """z = type(x) in (int, long)"""
   1106         a = """z = type(x) in (int, int)"""
   1107         self.check(b, a)
   1108 
   1109     def test_unchanged(self):
   1110         s = """long = True"""
   1111         self.unchanged(s)
   1112 
   1113         s = """s.long = True"""
   1114         self.unchanged(s)
   1115 
   1116         s = """def long(): pass"""
   1117         self.unchanged(s)
   1118 
   1119         s = """class long(): pass"""
   1120         self.unchanged(s)
   1121 
   1122         s = """def f(long): pass"""
   1123         self.unchanged(s)
   1124 
   1125         s = """def f(g, long): pass"""
   1126         self.unchanged(s)
   1127 
   1128         s = """def f(x, long=True): pass"""
   1129         self.unchanged(s)
   1130 
   1131     def test_prefix_preservation(self):
   1132         b = """x =   long(  x  )"""
   1133         a = """x =   int(  x  )"""
   1134         self.check(b, a)
   1135 
   1136 
   1137 class Test_execfile(FixerTestCase):
   1138     fixer = "execfile"
   1139 
   1140     def test_conversion(self):
   1141         b = """execfile("fn")"""
   1142         a = """exec(compile(open("fn").read(), "fn", 'exec'))"""
   1143         self.check(b, a)
   1144 
   1145         b = """execfile("fn", glob)"""
   1146         a = """exec(compile(open("fn").read(), "fn", 'exec'), glob)"""
   1147         self.check(b, a)
   1148 
   1149         b = """execfile("fn", glob, loc)"""
   1150         a = """exec(compile(open("fn").read(), "fn", 'exec'), glob, loc)"""
   1151         self.check(b, a)
   1152 
   1153         b = """execfile("fn", globals=glob)"""
   1154         a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob)"""
   1155         self.check(b, a)
   1156 
   1157         b = """execfile("fn", locals=loc)"""
   1158         a = """exec(compile(open("fn").read(), "fn", 'exec'), locals=loc)"""
   1159         self.check(b, a)
   1160 
   1161         b = """execfile("fn", globals=glob, locals=loc)"""
   1162         a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob, locals=loc)"""
   1163         self.check(b, a)
   1164 
   1165     def test_spacing(self):
   1166         b = """execfile( "fn" )"""
   1167         a = """exec(compile(open( "fn" ).read(), "fn", 'exec'))"""
   1168         self.check(b, a)
   1169 
   1170         b = """execfile("fn",  globals = glob)"""
   1171         a = """exec(compile(open("fn").read(), "fn", 'exec'),  globals = glob)"""
   1172         self.check(b, a)
   1173 
   1174 
   1175 class Test_isinstance(FixerTestCase):
   1176     fixer = "isinstance"
   1177 
   1178     def test_remove_multiple_items(self):
   1179         b = """isinstance(x, (int, int, int))"""
   1180         a = """isinstance(x, int)"""
   1181         self.check(b, a)
   1182 
   1183         b = """isinstance(x, (int, float, int, int, float))"""
   1184         a = """isinstance(x, (int, float))"""
   1185         self.check(b, a)
   1186 
   1187         b = """isinstance(x, (int, float, int, int, float, str))"""
   1188         a = """isinstance(x, (int, float, str))"""
   1189         self.check(b, a)
   1190 
   1191         b = """isinstance(foo() + bar(), (x(), y(), x(), int, int))"""
   1192         a = """isinstance(foo() + bar(), (x(), y(), x(), int))"""
   1193         self.check(b, a)
   1194 
   1195     def test_prefix_preservation(self):
   1196         b = """if    isinstance(  foo(), (  bar, bar, baz )) : pass"""
   1197         a = """if    isinstance(  foo(), (  bar, baz )) : pass"""
   1198         self.check(b, a)
   1199 
   1200     def test_unchanged(self):
   1201         self.unchanged("isinstance(x, (str, int))")
   1202 
   1203 class Test_dict(FixerTestCase):
   1204     fixer = "dict"
   1205 
   1206     def test_prefix_preservation(self):
   1207         b = "if   d. keys  (  )  : pass"
   1208         a = "if   list(d. keys  (  ))  : pass"
   1209         self.check(b, a)
   1210 
   1211         b = "if   d. items  (  )  : pass"
   1212         a = "if   list(d. items  (  ))  : pass"
   1213         self.check(b, a)
   1214 
   1215         b = "if   d. iterkeys  ( )  : pass"
   1216         a = "if   iter(d. keys  ( ))  : pass"
   1217         self.check(b, a)
   1218 
   1219         b = "[i for i in    d.  iterkeys(  )  ]"
   1220         a = "[i for i in    d.  keys(  )  ]"
   1221         self.check(b, a)
   1222 
   1223         b = "if   d. viewkeys  ( )  : pass"
   1224         a = "if   d. keys  ( )  : pass"
   1225         self.check(b, a)
   1226 
   1227         b = "[i for i in    d.  viewkeys(  )  ]"
   1228         a = "[i for i in    d.  keys(  )  ]"
   1229         self.check(b, a)
   1230 
   1231     def test_trailing_comment(self):
   1232         b = "d.keys() # foo"
   1233         a = "list(d.keys()) # foo"
   1234         self.check(b, a)
   1235 
   1236         b = "d.items()  # foo"
   1237         a = "list(d.items())  # foo"
   1238         self.check(b, a)
   1239 
   1240         b = "d.iterkeys()  # foo"
   1241         a = "iter(d.keys())  # foo"
   1242         self.check(b, a)
   1243 
   1244         b = """[i for i in d.iterkeys() # foo
   1245                ]"""
   1246         a = """[i for i in d.keys() # foo
   1247                ]"""
   1248         self.check(b, a)
   1249 
   1250         b = """[i for i in d.iterkeys() # foo
   1251                ]"""
   1252         a = """[i for i in d.keys() # foo
   1253                ]"""
   1254         self.check(b, a)
   1255 
   1256         b = "d.viewitems()  # foo"
   1257         a = "d.items()  # foo"
   1258         self.check(b, a)
   1259 
   1260     def test_unchanged(self):
   1261         for wrapper in fixer_util.consuming_calls:
   1262             s = "s = %s(d.keys())" % wrapper
   1263             self.unchanged(s)
   1264 
   1265             s = "s = %s(d.values())" % wrapper
   1266             self.unchanged(s)
   1267 
   1268             s = "s = %s(d.items())" % wrapper
   1269             self.unchanged(s)
   1270 
   1271     def test_01(self):
   1272         b = "d.keys()"
   1273         a = "list(d.keys())"
   1274         self.check(b, a)
   1275 
   1276         b = "a[0].foo().keys()"
   1277         a = "list(a[0].foo().keys())"
   1278         self.check(b, a)
   1279 
   1280     def test_02(self):
   1281         b = "d.items()"
   1282         a = "list(d.items())"
   1283         self.check(b, a)
   1284 
   1285     def test_03(self):
   1286         b = "d.values()"
   1287         a = "list(d.values())"
   1288         self.check(b, a)
   1289 
   1290     def test_04(self):
   1291         b = "d.iterkeys()"
   1292         a = "iter(d.keys())"
   1293         self.check(b, a)
   1294 
   1295     def test_05(self):
   1296         b = "d.iteritems()"
   1297         a = "iter(d.items())"
   1298         self.check(b, a)
   1299 
   1300     def test_06(self):
   1301         b = "d.itervalues()"
   1302         a = "iter(d.values())"
   1303         self.check(b, a)
   1304 
   1305     def test_07(self):
   1306         s = "list(d.keys())"
   1307         self.unchanged(s)
   1308 
   1309     def test_08(self):
   1310         s = "sorted(d.keys())"
   1311         self.unchanged(s)
   1312 
   1313     def test_09(self):
   1314         b = "iter(d.keys())"
   1315         a = "iter(list(d.keys()))"
   1316         self.check(b, a)
   1317 
   1318     def test_10(self):
   1319         b = "foo(d.keys())"
   1320         a = "foo(list(d.keys()))"
   1321         self.check(b, a)
   1322 
   1323     def test_11(self):
   1324         b = "for i in d.keys(): print i"
   1325         a = "for i in list(d.keys()): print i"
   1326         self.check(b, a)
   1327 
   1328     def test_12(self):
   1329         b = "for i in d.iterkeys(): print i"
   1330         a = "for i in d.keys(): print i"
   1331         self.check(b, a)
   1332 
   1333     def test_13(self):
   1334         b = "[i for i in d.keys()]"
   1335         a = "[i for i in list(d.keys())]"
   1336         self.check(b, a)
   1337 
   1338     def test_14(self):
   1339         b = "[i for i in d.iterkeys()]"
   1340         a = "[i for i in d.keys()]"
   1341         self.check(b, a)
   1342 
   1343     def test_15(self):
   1344         b = "(i for i in d.keys())"
   1345         a = "(i for i in list(d.keys()))"
   1346         self.check(b, a)
   1347 
   1348     def test_16(self):
   1349         b = "(i for i in d.iterkeys())"
   1350         a = "(i for i in d.keys())"
   1351         self.check(b, a)
   1352 
   1353     def test_17(self):
   1354         b = "iter(d.iterkeys())"
   1355         a = "iter(d.keys())"
   1356         self.check(b, a)
   1357 
   1358     def test_18(self):
   1359         b = "list(d.iterkeys())"
   1360         a = "list(d.keys())"
   1361         self.check(b, a)
   1362 
   1363     def test_19(self):
   1364         b = "sorted(d.iterkeys())"
   1365         a = "sorted(d.keys())"
   1366         self.check(b, a)
   1367 
   1368     def test_20(self):
   1369         b = "foo(d.iterkeys())"
   1370         a = "foo(iter(d.keys()))"
   1371         self.check(b, a)
   1372 
   1373     def test_21(self):
   1374         b = "print h.iterkeys().next()"
   1375         a = "print iter(h.keys()).next()"
   1376         self.check(b, a)
   1377 
   1378     def test_22(self):
   1379         b = "print h.keys()[0]"
   1380         a = "print list(h.keys())[0]"
   1381         self.check(b, a)
   1382 
   1383     def test_23(self):
   1384         b = "print list(h.iterkeys().next())"
   1385         a = "print list(iter(h.keys()).next())"
   1386         self.check(b, a)
   1387 
   1388     def test_24(self):
   1389         b = "for x in h.keys()[0]: print x"
   1390         a = "for x in list(h.keys())[0]: print x"
   1391         self.check(b, a)
   1392 
   1393     def test_25(self):
   1394         b = "d.viewkeys()"
   1395         a = "d.keys()"
   1396         self.check(b, a)
   1397 
   1398     def test_26(self):
   1399         b = "d.viewitems()"
   1400         a = "d.items()"
   1401         self.check(b, a)
   1402 
   1403     def test_27(self):
   1404         b = "d.viewvalues()"
   1405         a = "d.values()"
   1406         self.check(b, a)
   1407 
   1408     def test_14(self):
   1409         b = "[i for i in d.viewkeys()]"
   1410         a = "[i for i in d.keys()]"
   1411         self.check(b, a)
   1412 
   1413     def test_15(self):
   1414         b = "(i for i in d.viewkeys())"
   1415         a = "(i for i in d.keys())"
   1416         self.check(b, a)
   1417 
   1418     def test_17(self):
   1419         b = "iter(d.viewkeys())"
   1420         a = "iter(d.keys())"
   1421         self.check(b, a)
   1422 
   1423     def test_18(self):
   1424         b = "list(d.viewkeys())"
   1425         a = "list(d.keys())"
   1426         self.check(b, a)
   1427 
   1428     def test_19(self):
   1429         b = "sorted(d.viewkeys())"
   1430         a = "sorted(d.keys())"
   1431         self.check(b, a)
   1432 
   1433 class Test_xrange(FixerTestCase):
   1434     fixer = "xrange"
   1435 
   1436     def test_prefix_preservation(self):
   1437         b = """x =    xrange(  10  )"""
   1438         a = """x =    range(  10  )"""
   1439         self.check(b, a)
   1440 
   1441         b = """x = xrange(  1  ,  10   )"""
   1442         a = """x = range(  1  ,  10   )"""
   1443         self.check(b, a)
   1444 
   1445         b = """x = xrange(  0  ,  10 ,  2 )"""
   1446         a = """x = range(  0  ,  10 ,  2 )"""
   1447         self.check(b, a)
   1448 
   1449     def test_single_arg(self):
   1450         b = """x = xrange(10)"""
   1451         a = """x = range(10)"""
   1452         self.check(b, a)
   1453 
   1454     def test_two_args(self):
   1455         b = """x = xrange(1, 10)"""
   1456         a = """x = range(1, 10)"""
   1457         self.check(b, a)
   1458 
   1459     def test_three_args(self):
   1460         b = """x = xrange(0, 10, 2)"""
   1461         a = """x = range(0, 10, 2)"""
   1462         self.check(b, a)
   1463 
   1464     def test_wrap_in_list(self):
   1465         b = """x = range(10, 3, 9)"""
   1466         a = """x = list(range(10, 3, 9))"""
   1467         self.check(b, a)
   1468 
   1469         b = """x = foo(range(10, 3, 9))"""
   1470         a = """x = foo(list(range(10, 3, 9)))"""
   1471         self.check(b, a)
   1472 
   1473         b = """x = range(10, 3, 9) + [4]"""
   1474         a = """x = list(range(10, 3, 9)) + [4]"""
   1475         self.check(b, a)
   1476 
   1477         b = """x = range(10)[::-1]"""
   1478         a = """x = list(range(10))[::-1]"""
   1479         self.check(b, a)
   1480 
   1481         b = """x = range(10)  [3]"""
   1482         a = """x = list(range(10))  [3]"""
   1483         self.check(b, a)
   1484 
   1485     def test_xrange_in_for(self):
   1486         b = """for i in xrange(10):\n    j=i"""
   1487         a = """for i in range(10):\n    j=i"""
   1488         self.check(b, a)
   1489 
   1490         b = """[i for i in xrange(10)]"""
   1491         a = """[i for i in range(10)]"""
   1492         self.check(b, a)
   1493 
   1494     def test_range_in_for(self):
   1495         self.unchanged("for i in range(10): pass")
   1496         self.unchanged("[i for i in range(10)]")
   1497 
   1498     def test_in_contains_test(self):
   1499         self.unchanged("x in range(10, 3, 9)")
   1500 
   1501     def test_in_consuming_context(self):
   1502         for call in fixer_util.consuming_calls:
   1503             self.unchanged("a = %s(range(10))" % call)
   1504 
   1505 class Test_xrange_with_reduce(FixerTestCase):
   1506 
   1507     def setUp(self):
   1508         super(Test_xrange_with_reduce, self).setUp(["xrange", "reduce"])
   1509 
   1510     def test_double_transform(self):
   1511         b = """reduce(x, xrange(5))"""
   1512         a = """from functools import reduce
   1513 reduce(x, range(5))"""
   1514         self.check(b, a)
   1515 
   1516 class Test_raw_input(FixerTestCase):
   1517     fixer = "raw_input"
   1518 
   1519     def test_prefix_preservation(self):
   1520         b = """x =    raw_input(   )"""
   1521         a = """x =    input(   )"""
   1522         self.check(b, a)
   1523 
   1524         b = """x = raw_input(   ''   )"""
   1525         a = """x = input(   ''   )"""
   1526         self.check(b, a)
   1527 
   1528     def test_1(self):
   1529         b = """x = raw_input()"""
   1530         a = """x = input()"""
   1531         self.check(b, a)
   1532 
   1533     def test_2(self):
   1534         b = """x = raw_input('')"""
   1535         a = """x = input('')"""
   1536         self.check(b, a)
   1537 
   1538     def test_3(self):
   1539         b = """x = raw_input('prompt')"""
   1540         a = """x = input('prompt')"""
   1541         self.check(b, a)
   1542 
   1543     def test_4(self):
   1544         b = """x = raw_input(foo(a) + 6)"""
   1545         a = """x = input(foo(a) + 6)"""
   1546         self.check(b, a)
   1547 
   1548     def test_5(self):
   1549         b = """x = raw_input(invite).split()"""
   1550         a = """x = input(invite).split()"""
   1551         self.check(b, a)
   1552 
   1553     def test_6(self):
   1554         b = """x = raw_input(invite) . split ()"""
   1555         a = """x = input(invite) . split ()"""
   1556         self.check(b, a)
   1557 
   1558     def test_8(self):
   1559         b = "x = int(raw_input())"
   1560         a = "x = int(input())"
   1561         self.check(b, a)
   1562 
   1563 class Test_funcattrs(FixerTestCase):
   1564     fixer = "funcattrs"
   1565 
   1566     attrs = ["closure", "doc", "name", "defaults", "code", "globals", "dict"]
   1567 
   1568     def test(self):
   1569         for attr in self.attrs:
   1570             b = "a.func_%s" % attr
   1571             a = "a.__%s__" % attr
   1572             self.check(b, a)
   1573 
   1574             b = "self.foo.func_%s.foo_bar" % attr
   1575             a = "self.foo.__%s__.foo_bar" % attr
   1576             self.check(b, a)
   1577 
   1578     def test_unchanged(self):
   1579         for attr in self.attrs:
   1580             s = "foo(func_%s + 5)" % attr
   1581             self.unchanged(s)
   1582 
   1583             s = "f(foo.__%s__)" % attr
   1584             self.unchanged(s)
   1585 
   1586             s = "f(foo.__%s__.foo)" % attr
   1587             self.unchanged(s)
   1588 
   1589 class Test_xreadlines(FixerTestCase):
   1590     fixer = "xreadlines"
   1591 
   1592     def test_call(self):
   1593         b = "for x in f.xreadlines(): pass"
   1594         a = "for x in f: pass"
   1595         self.check(b, a)
   1596 
   1597         b = "for x in foo().xreadlines(): pass"
   1598         a = "for x in foo(): pass"
   1599         self.check(b, a)
   1600 
   1601         b = "for x in (5 + foo()).xreadlines(): pass"
   1602         a = "for x in (5 + foo()): pass"
   1603         self.check(b, a)
   1604 
   1605     def test_attr_ref(self):
   1606         b = "foo(f.xreadlines + 5)"
   1607         a = "foo(f.__iter__ + 5)"
   1608         self.check(b, a)
   1609 
   1610         b = "foo(f().xreadlines + 5)"
   1611         a = "foo(f().__iter__ + 5)"
   1612         self.check(b, a)
   1613 
   1614         b = "foo((5 + f()).xreadlines + 5)"
   1615         a = "foo((5 + f()).__iter__ + 5)"
   1616         self.check(b, a)
   1617 
   1618     def test_unchanged(self):
   1619         s = "for x in f.xreadlines(5): pass"
   1620         self.unchanged(s)
   1621 
   1622         s = "for x in f.xreadlines(k=5): pass"
   1623         self.unchanged(s)
   1624 
   1625         s = "for x in f.xreadlines(*k, **v): pass"
   1626         self.unchanged(s)
   1627 
   1628         s = "foo(xreadlines)"
   1629         self.unchanged(s)
   1630 
   1631 
   1632 class ImportsFixerTests:
   1633 
   1634     def test_import_module(self):
   1635         for old, new in self.modules.items():
   1636             b = "import %s" % old
   1637             a = "import %s" % new
   1638             self.check(b, a)
   1639 
   1640             b = "import foo, %s, bar" % old
   1641             a = "import foo, %s, bar" % new
   1642             self.check(b, a)
   1643 
   1644     def test_import_from(self):
   1645         for old, new in self.modules.items():
   1646             b = "from %s import foo" % old
   1647             a = "from %s import foo" % new
   1648             self.check(b, a)
   1649 
   1650             b = "from %s import foo, bar" % old
   1651             a = "from %s import foo, bar" % new
   1652             self.check(b, a)
   1653 
   1654             b = "from %s import (yes, no)" % old
   1655             a = "from %s import (yes, no)" % new
   1656             self.check(b, a)
   1657 
   1658     def test_import_module_as(self):
   1659         for old, new in self.modules.items():
   1660             b = "import %s as foo_bar" % old
   1661             a = "import %s as foo_bar" % new
   1662             self.check(b, a)
   1663 
   1664             b = "import %s as foo_bar" % old
   1665             a = "import %s as foo_bar" % new
   1666             self.check(b, a)
   1667 
   1668     def test_import_from_as(self):
   1669         for old, new in self.modules.items():
   1670             b = "from %s import foo as bar" % old
   1671             a = "from %s import foo as bar" % new
   1672             self.check(b, a)
   1673 
   1674     def test_star(self):
   1675         for old, new in self.modules.items():
   1676             b = "from %s import *" % old
   1677             a = "from %s import *" % new
   1678             self.check(b, a)
   1679 
   1680     def test_import_module_usage(self):
   1681         for old, new in self.modules.items():
   1682             b = """
   1683                 import %s
   1684                 foo(%s.bar)
   1685                 """ % (old, old)
   1686             a = """
   1687                 import %s
   1688                 foo(%s.bar)
   1689                 """ % (new, new)
   1690             self.check(b, a)
   1691 
   1692             b = """
   1693                 from %s import x
   1694                 %s = 23
   1695                 """ % (old, old)
   1696             a = """
   1697                 from %s import x
   1698                 %s = 23
   1699                 """ % (new, old)
   1700             self.check(b, a)
   1701 
   1702             s = """
   1703                 def f():
   1704                     %s.method()
   1705                 """ % (old,)
   1706             self.unchanged(s)
   1707 
   1708             # test nested usage
   1709             b = """
   1710                 import %s
   1711                 %s.bar(%s.foo)
   1712                 """ % (old, old, old)
   1713             a = """
   1714                 import %s
   1715                 %s.bar(%s.foo)
   1716                 """ % (new, new, new)
   1717             self.check(b, a)
   1718 
   1719             b = """
   1720                 import %s
   1721                 x.%s
   1722                 """ % (old, old)
   1723             a = """
   1724                 import %s
   1725                 x.%s
   1726                 """ % (new, old)
   1727             self.check(b, a)
   1728 
   1729 
   1730 class Test_imports(FixerTestCase, ImportsFixerTests):
   1731     fixer = "imports"
   1732     from ..fixes.fix_imports import MAPPING as modules
   1733 
   1734     def test_multiple_imports(self):
   1735         b = """import urlparse, cStringIO"""
   1736         a = """import urllib.parse, io"""
   1737         self.check(b, a)
   1738 
   1739     def test_multiple_imports_as(self):
   1740         b = """
   1741             import copy_reg as bar, HTMLParser as foo, urlparse
   1742             s = urlparse.spam(bar.foo())
   1743             """
   1744         a = """
   1745             import copyreg as bar, html.parser as foo, urllib.parse
   1746             s = urllib.parse.spam(bar.foo())
   1747             """
   1748         self.check(b, a)
   1749 
   1750 
   1751 class Test_imports2(FixerTestCase, ImportsFixerTests):
   1752     fixer = "imports2"
   1753     from ..fixes.fix_imports2 import MAPPING as modules
   1754 
   1755 
   1756 class Test_imports_fixer_order(FixerTestCase, ImportsFixerTests):
   1757 
   1758     def setUp(self):
   1759         super(Test_imports_fixer_order, self).setUp(['imports', 'imports2'])
   1760         from ..fixes.fix_imports2 import MAPPING as mapping2
   1761         self.modules = mapping2.copy()
   1762         from ..fixes.fix_imports import MAPPING as mapping1
   1763         for key in ('dbhash', 'dumbdbm', 'dbm', 'gdbm'):
   1764             self.modules[key] = mapping1[key]
   1765 
   1766     def test_after_local_imports_refactoring(self):
   1767         for fix in ("imports", "imports2"):
   1768             self.fixer = fix
   1769             self.assert_runs_after("import")
   1770 
   1771 
   1772 class Test_urllib(FixerTestCase):
   1773     fixer = "urllib"
   1774     from ..fixes.fix_urllib import MAPPING as modules
   1775 
   1776     def test_import_module(self):
   1777         for old, changes in self.modules.items():
   1778             b = "import %s" % old
   1779             a = "import %s" % ", ".join(map(itemgetter(0), changes))
   1780             self.check(b, a)
   1781 
   1782     def test_import_from(self):
   1783         for old, changes in self.modules.items():
   1784             all_members = []
   1785             for new, members in changes:
   1786                 for member in members:
   1787                     all_members.append(member)
   1788                     b = "from %s import %s" % (old, member)
   1789                     a = "from %s import %s" % (new, member)
   1790                     self.check(b, a)
   1791 
   1792                     s = "from foo import %s" % member
   1793                     self.unchanged(s)
   1794 
   1795                 b = "from %s import %s" % (old, ", ".join(members))
   1796                 a = "from %s import %s" % (new, ", ".join(members))
   1797                 self.check(b, a)
   1798 
   1799                 s = "from foo import %s" % ", ".join(members)
   1800                 self.unchanged(s)
   1801 
   1802             # test the breaking of a module into multiple replacements
   1803             b = "from %s import %s" % (old, ", ".join(all_members))
   1804             a = "\n".join(["from %s import %s" % (new, ", ".join(members))
   1805                             for (new, members) in changes])
   1806             self.check(b, a)
   1807 
   1808     def test_import_module_as(self):
   1809         for old in self.modules:
   1810             s = "import %s as foo" % old
   1811             self.warns_unchanged(s, "This module is now multiple modules")
   1812 
   1813     def test_import_from_as(self):
   1814         for old, changes in self.modules.items():
   1815             for new, members in changes:
   1816                 for member in members:
   1817                     b = "from %s import %s as foo_bar" % (old, member)
   1818                     a = "from %s import %s as foo_bar" % (new, member)
   1819                     self.check(b, a)
   1820                     b = "from %s import %s as blah, %s" % (old, member, member)
   1821                     a = "from %s import %s as blah, %s" % (new, member, member)
   1822                     self.check(b, a)
   1823 
   1824     def test_star(self):
   1825         for old in self.modules:
   1826             s = "from %s import *" % old
   1827             self.warns_unchanged(s, "Cannot handle star imports")
   1828 
   1829     def test_indented(self):
   1830         b = """
   1831 def foo():
   1832     from urllib import urlencode, urlopen
   1833 """
   1834         a = """
   1835 def foo():
   1836     from urllib.parse import urlencode
   1837     from urllib.request import urlopen
   1838 """
   1839         self.check(b, a)
   1840 
   1841         b = """
   1842 def foo():
   1843     other()
   1844     from urllib import urlencode, urlopen
   1845 """
   1846         a = """
   1847 def foo():
   1848     other()
   1849     from urllib.parse import urlencode
   1850     from urllib.request import urlopen
   1851 """
   1852         self.check(b, a)
   1853 
   1854 
   1855 
   1856     def test_import_module_usage(self):
   1857         for old, changes in self.modules.items():
   1858             for new, members in changes:
   1859                 for member in members:
   1860                     new_import = ", ".join([n for (n, mems)
   1861                                             in self.modules[old]])
   1862                     b = """
   1863                         import %s
   1864                         foo(%s.%s)
   1865                         """ % (old, old, member)
   1866                     a = """
   1867                         import %s
   1868                         foo(%s.%s)
   1869                         """ % (new_import, new, member)
   1870                     self.check(b, a)
   1871                     b = """
   1872                         import %s
   1873                         %s.%s(%s.%s)
   1874                         """ % (old, old, member, old, member)
   1875                     a = """
   1876                         import %s
   1877                         %s.%s(%s.%s)
   1878                         """ % (new_import, new, member, new, member)
   1879                     self.check(b, a)
   1880 
   1881 
   1882 class Test_input(FixerTestCase):
   1883     fixer = "input"
   1884 
   1885     def test_prefix_preservation(self):
   1886         b = """x =   input(   )"""
   1887         a = """x =   eval(input(   ))"""
   1888         self.check(b, a)
   1889 
   1890         b = """x = input(   ''   )"""
   1891         a = """x = eval(input(   ''   ))"""
   1892         self.check(b, a)
   1893 
   1894     def test_trailing_comment(self):
   1895         b = """x = input()  #  foo"""
   1896         a = """x = eval(input())  #  foo"""
   1897         self.check(b, a)
   1898 
   1899     def test_idempotency(self):
   1900         s = """x = eval(input())"""
   1901         self.unchanged(s)
   1902 
   1903         s = """x = eval(input(''))"""
   1904         self.unchanged(s)
   1905 
   1906         s = """x = eval(input(foo(5) + 9))"""
   1907         self.unchanged(s)
   1908 
   1909     def test_1(self):
   1910         b = """x = input()"""
   1911         a = """x = eval(input())"""
   1912         self.check(b, a)
   1913 
   1914     def test_2(self):
   1915         b = """x = input('')"""
   1916         a = """x = eval(input(''))"""
   1917         self.check(b, a)
   1918 
   1919     def test_3(self):
   1920         b = """x = input('prompt')"""
   1921         a = """x = eval(input('prompt'))"""
   1922         self.check(b, a)
   1923 
   1924     def test_4(self):
   1925         b = """x = input(foo(5) + 9)"""
   1926         a = """x = eval(input(foo(5) + 9))"""
   1927         self.check(b, a)
   1928 
   1929 class Test_tuple_params(FixerTestCase):
   1930     fixer = "tuple_params"
   1931 
   1932     def test_unchanged_1(self):
   1933         s = """def foo(): pass"""
   1934         self.unchanged(s)
   1935 
   1936     def test_unchanged_2(self):
   1937         s = """def foo(a, b, c): pass"""
   1938         self.unchanged(s)
   1939 
   1940     def test_unchanged_3(self):
   1941         s = """def foo(a=3, b=4, c=5): pass"""
   1942         self.unchanged(s)
   1943 
   1944     def test_1(self):
   1945         b = """
   1946             def foo(((a, b), c)):
   1947                 x = 5"""
   1948 
   1949         a = """
   1950             def foo(xxx_todo_changeme):
   1951                 ((a, b), c) = xxx_todo_changeme
   1952                 x = 5"""
   1953         self.check(b, a)
   1954 
   1955     def test_2(self):
   1956         b = """
   1957             def foo(((a, b), c), d):
   1958                 x = 5"""
   1959 
   1960         a = """
   1961             def foo(xxx_todo_changeme, d):
   1962                 ((a, b), c) = xxx_todo_changeme
   1963                 x = 5"""
   1964         self.check(b, a)
   1965 
   1966     def test_3(self):
   1967         b = """
   1968             def foo(((a, b), c), d) -> e:
   1969                 x = 5"""
   1970 
   1971         a = """
   1972             def foo(xxx_todo_changeme, d) -> e:
   1973                 ((a, b), c) = xxx_todo_changeme
   1974                 x = 5"""
   1975         self.check(b, a)
   1976 
   1977     def test_semicolon(self):
   1978         b = """
   1979             def foo(((a, b), c)): x = 5; y = 7"""
   1980 
   1981         a = """
   1982             def foo(xxx_todo_changeme): ((a, b), c) = xxx_todo_changeme; x = 5; y = 7"""
   1983         self.check(b, a)
   1984 
   1985     def test_keywords(self):
   1986         b = """
   1987             def foo(((a, b), c), d, e=5) -> z:
   1988                 x = 5"""
   1989 
   1990         a = """
   1991             def foo(xxx_todo_changeme, d, e=5) -> z:
   1992                 ((a, b), c) = xxx_todo_changeme
   1993                 x = 5"""
   1994         self.check(b, a)
   1995 
   1996     def test_varargs(self):
   1997         b = """
   1998             def foo(((a, b), c), d, *vargs, **kwargs) -> z:
   1999                 x = 5"""
   2000 
   2001         a = """
   2002             def foo(xxx_todo_changeme, d, *vargs, **kwargs) -> z:
   2003                 ((a, b), c) = xxx_todo_changeme
   2004                 x = 5"""
   2005         self.check(b, a)
   2006 
   2007     def test_multi_1(self):
   2008         b = """
   2009             def foo(((a, b), c), (d, e, f)) -> z:
   2010                 x = 5"""
   2011 
   2012         a = """
   2013             def foo(xxx_todo_changeme, xxx_todo_changeme1) -> z:
   2014                 ((a, b), c) = xxx_todo_changeme
   2015                 (d, e, f) = xxx_todo_changeme1
   2016                 x = 5"""
   2017         self.check(b, a)
   2018 
   2019     def test_multi_2(self):
   2020         b = """
   2021             def foo(x, ((a, b), c), d, (e, f, g), y) -> z:
   2022                 x = 5"""
   2023 
   2024         a = """
   2025             def foo(x, xxx_todo_changeme, d, xxx_todo_changeme1, y) -> z:
   2026                 ((a, b), c) = xxx_todo_changeme
   2027                 (e, f, g) = xxx_todo_changeme1
   2028                 x = 5"""
   2029         self.check(b, a)
   2030 
   2031     def test_docstring(self):
   2032         b = """
   2033             def foo(((a, b), c), (d, e, f)) -> z:
   2034                 "foo foo foo foo"
   2035                 x = 5"""
   2036 
   2037         a = """
   2038             def foo(xxx_todo_changeme, xxx_todo_changeme1) -> z:
   2039                 "foo foo foo foo"
   2040                 ((a, b), c) = xxx_todo_changeme
   2041                 (d, e, f) = xxx_todo_changeme1
   2042                 x = 5"""
   2043         self.check(b, a)
   2044 
   2045     def test_lambda_no_change(self):
   2046         s = """lambda x: x + 5"""
   2047         self.unchanged(s)
   2048 
   2049     def test_lambda_parens_single_arg(self):
   2050         b = """lambda (x): x + 5"""
   2051         a = """lambda x: x + 5"""
   2052         self.check(b, a)
   2053 
   2054         b = """lambda(x): x + 5"""
   2055         a = """lambda x: x + 5"""
   2056         self.check(b, a)
   2057 
   2058         b = """lambda ((((x)))): x + 5"""
   2059         a = """lambda x: x + 5"""
   2060         self.check(b, a)
   2061 
   2062         b = """lambda((((x)))): x + 5"""
   2063         a = """lambda x: x + 5"""
   2064         self.check(b, a)
   2065 
   2066     def test_lambda_simple(self):
   2067         b = """lambda (x, y): x + f(y)"""
   2068         a = """lambda x_y: x_y[0] + f(x_y[1])"""
   2069         self.check(b, a)
   2070 
   2071         b = """lambda(x, y): x + f(y)"""
   2072         a = """lambda x_y: x_y[0] + f(x_y[1])"""
   2073         self.check(b, a)
   2074 
   2075         b = """lambda (((x, y))): x + f(y)"""
   2076         a = """lambda x_y: x_y[0] + f(x_y[1])"""
   2077         self.check(b, a)
   2078 
   2079         b = """lambda(((x, y))): x + f(y)"""
   2080         a = """lambda x_y: x_y[0] + f(x_y[1])"""
   2081         self.check(b, a)
   2082 
   2083     def test_lambda_one_tuple(self):
   2084         b = """lambda (x,): x + f(x)"""
   2085         a = """lambda x1: x1[0] + f(x1[0])"""
   2086         self.check(b, a)
   2087 
   2088         b = """lambda (((x,))): x + f(x)"""
   2089         a = """lambda x1: x1[0] + f(x1[0])"""
   2090         self.check(b, a)
   2091 
   2092     def test_lambda_simple_multi_use(self):
   2093         b = """lambda (x, y): x + x + f(x) + x"""
   2094         a = """lambda x_y: x_y[0] + x_y[0] + f(x_y[0]) + x_y[0]"""
   2095         self.check(b, a)
   2096 
   2097     def test_lambda_simple_reverse(self):
   2098         b = """lambda (x, y): y + x"""
   2099         a = """lambda x_y: x_y[1] + x_y[0]"""
   2100         self.check(b, a)
   2101 
   2102     def test_lambda_nested(self):
   2103         b = """lambda (x, (y, z)): x + y + z"""
   2104         a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]"""
   2105         self.check(b, a)
   2106 
   2107         b = """lambda (((x, (y, z)))): x + y + z"""
   2108         a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]"""
   2109         self.check(b, a)
   2110 
   2111     def test_lambda_nested_multi_use(self):
   2112         b = """lambda (x, (y, z)): x + y + f(y)"""
   2113         a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + f(x_y_z[1][0])"""
   2114         self.check(b, a)
   2115 
   2116 class Test_methodattrs(FixerTestCase):
   2117     fixer = "methodattrs"
   2118 
   2119     attrs = ["func", "self", "class"]
   2120 
   2121     def test(self):
   2122         for attr in self.attrs:
   2123             b = "a.im_%s" % attr
   2124             if attr == "class":
   2125                 a = "a.__self__.__class__"
   2126             else:
   2127                 a = "a.__%s__" % attr
   2128             self.check(b, a)
   2129 
   2130             b = "self.foo.im_%s.foo_bar" % attr
   2131             if attr == "class":
   2132                 a = "self.foo.__self__.__class__.foo_bar"
   2133             else:
   2134                 a = "self.foo.__%s__.foo_bar" % attr
   2135             self.check(b, a)
   2136 
   2137     def test_unchanged(self):
   2138         for attr in self.attrs:
   2139             s = "foo(im_%s + 5)" % attr
   2140             self.unchanged(s)
   2141 
   2142             s = "f(foo.__%s__)" % attr
   2143             self.unchanged(s)
   2144 
   2145             s = "f(foo.__%s__.foo)" % attr
   2146             self.unchanged(s)
   2147 
   2148 class Test_next(FixerTestCase):
   2149     fixer = "next"
   2150 
   2151     def test_1(self):
   2152         b = """it.next()"""
   2153         a = """next(it)"""
   2154         self.check(b, a)
   2155 
   2156     def test_2(self):
   2157         b = """a.b.c.d.next()"""
   2158         a = """next(a.b.c.d)"""
   2159         self.check(b, a)
   2160 
   2161     def test_3(self):
   2162         b = """(a + b).next()"""
   2163         a = """next((a + b))"""
   2164         self.check(b, a)
   2165 
   2166     def test_4(self):
   2167         b = """a().next()"""
   2168         a = """next(a())"""
   2169         self.check(b, a)
   2170 
   2171     def test_5(self):
   2172         b = """a().next() + b"""
   2173         a = """next(a()) + b"""
   2174         self.check(b, a)
   2175 
   2176     def test_6(self):
   2177         b = """c(      a().next() + b)"""
   2178         a = """c(      next(a()) + b)"""
   2179         self.check(b, a)
   2180 
   2181     def test_prefix_preservation_1(self):
   2182         b = """
   2183             for a in b:
   2184                 foo(a)
   2185                 a.next()
   2186             """
   2187         a = """
   2188             for a in b:
   2189                 foo(a)
   2190                 next(a)
   2191             """
   2192         self.check(b, a)
   2193 
   2194     def test_prefix_preservation_2(self):
   2195         b = """
   2196             for a in b:
   2197                 foo(a) # abc
   2198                 # def
   2199                 a.next()
   2200             """
   2201         a = """
   2202             for a in b:
   2203                 foo(a) # abc
   2204                 # def
   2205                 next(a)
   2206             """
   2207         self.check(b, a)
   2208 
   2209     def test_prefix_preservation_3(self):
   2210         b = """
   2211             next = 5
   2212             for a in b:
   2213                 foo(a)
   2214                 a.next()
   2215             """
   2216         a = """
   2217             next = 5
   2218             for a in b:
   2219                 foo(a)
   2220                 a.__next__()
   2221             """
   2222         self.check(b, a, ignore_warnings=True)
   2223 
   2224     def test_prefix_preservation_4(self):
   2225         b = """
   2226             next = 5
   2227             for a in b:
   2228                 foo(a) # abc
   2229                 # def
   2230                 a.next()
   2231             """
   2232         a = """
   2233             next = 5
   2234             for a in b:
   2235                 foo(a) # abc
   2236                 # def
   2237                 a.__next__()
   2238             """
   2239         self.check(b, a, ignore_warnings=True)
   2240 
   2241     def test_prefix_preservation_5(self):
   2242         b = """
   2243             next = 5
   2244             for a in b:
   2245                 foo(foo(a), # abc
   2246                     a.next())
   2247             """
   2248         a = """
   2249             next = 5
   2250             for a in b:
   2251                 foo(foo(a), # abc
   2252                     a.__next__())
   2253             """
   2254         self.check(b, a, ignore_warnings=True)
   2255 
   2256     def test_prefix_preservation_6(self):
   2257         b = """
   2258             for a in b:
   2259                 foo(foo(a), # abc
   2260                     a.next())
   2261             """
   2262         a = """
   2263             for a in b:
   2264                 foo(foo(a), # abc
   2265                     next(a))
   2266             """
   2267         self.check(b, a)
   2268 
   2269     def test_method_1(self):
   2270         b = """
   2271             class A:
   2272                 def next(self):
   2273                     pass
   2274             """
   2275         a = """
   2276             class A:
   2277                 def __next__(self):
   2278                     pass
   2279             """
   2280         self.check(b, a)
   2281 
   2282     def test_method_2(self):
   2283         b = """
   2284             class A(object):
   2285                 def next(self):
   2286                     pass
   2287             """
   2288         a = """
   2289             class A(object):
   2290                 def __next__(self):
   2291                     pass
   2292             """
   2293         self.check(b, a)
   2294 
   2295     def test_method_3(self):
   2296         b = """
   2297             class A:
   2298                 def next(x):
   2299                     pass
   2300             """
   2301         a = """
   2302             class A:
   2303                 def __next__(x):
   2304                     pass
   2305             """
   2306         self.check(b, a)
   2307 
   2308     def test_method_4(self):
   2309         b = """
   2310             class A:
   2311                 def __init__(self, foo):
   2312                     self.foo = foo
   2313 
   2314                 def next(self):
   2315                     pass
   2316 
   2317                 def __iter__(self):
   2318                     return self
   2319             """
   2320         a = """
   2321             class A:
   2322                 def __init__(self, foo):
   2323                     self.foo = foo
   2324 
   2325                 def __next__(self):
   2326                     pass
   2327 
   2328                 def __iter__(self):
   2329                     return self
   2330             """
   2331         self.check(b, a)
   2332 
   2333     def test_method_unchanged(self):
   2334         s = """
   2335             class A:
   2336                 def next(self, a, b):
   2337                     pass
   2338             """
   2339         self.unchanged(s)
   2340 
   2341     def test_shadowing_assign_simple(self):
   2342         s = """
   2343             next = foo
   2344 
   2345             class A:
   2346                 def next(self, a, b):
   2347                     pass
   2348             """
   2349         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2350 
   2351     def test_shadowing_assign_tuple_1(self):
   2352         s = """
   2353             (next, a) = foo
   2354 
   2355             class A:
   2356                 def next(self, a, b):
   2357                     pass
   2358             """
   2359         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2360 
   2361     def test_shadowing_assign_tuple_2(self):
   2362         s = """
   2363             (a, (b, (next, c)), a) = foo
   2364 
   2365             class A:
   2366                 def next(self, a, b):
   2367                     pass
   2368             """
   2369         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2370 
   2371     def test_shadowing_assign_list_1(self):
   2372         s = """
   2373             [next, a] = foo
   2374 
   2375             class A:
   2376                 def next(self, a, b):
   2377                     pass
   2378             """
   2379         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2380 
   2381     def test_shadowing_assign_list_2(self):
   2382         s = """
   2383             [a, [b, [next, c]], a] = foo
   2384 
   2385             class A:
   2386                 def next(self, a, b):
   2387                     pass
   2388             """
   2389         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2390 
   2391     def test_builtin_assign(self):
   2392         s = """
   2393             def foo():
   2394                 __builtin__.next = foo
   2395 
   2396             class A:
   2397                 def next(self, a, b):
   2398                     pass
   2399             """
   2400         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2401 
   2402     def test_builtin_assign_in_tuple(self):
   2403         s = """
   2404             def foo():
   2405                 (a, __builtin__.next) = foo
   2406 
   2407             class A:
   2408                 def next(self, a, b):
   2409                     pass
   2410             """
   2411         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2412 
   2413     def test_builtin_assign_in_list(self):
   2414         s = """
   2415             def foo():
   2416                 [a, __builtin__.next] = foo
   2417 
   2418             class A:
   2419                 def next(self, a, b):
   2420                     pass
   2421             """
   2422         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2423 
   2424     def test_assign_to_next(self):
   2425         s = """
   2426             def foo():
   2427                 A.next = foo
   2428 
   2429             class A:
   2430                 def next(self, a, b):
   2431                     pass
   2432             """
   2433         self.unchanged(s)
   2434 
   2435     def test_assign_to_next_in_tuple(self):
   2436         s = """
   2437             def foo():
   2438                 (a, A.next) = foo
   2439 
   2440             class A:
   2441                 def next(self, a, b):
   2442                     pass
   2443             """
   2444         self.unchanged(s)
   2445 
   2446     def test_assign_to_next_in_list(self):
   2447         s = """
   2448             def foo():
   2449                 [a, A.next] = foo
   2450 
   2451             class A:
   2452                 def next(self, a, b):
   2453                     pass
   2454             """
   2455         self.unchanged(s)
   2456 
   2457     def test_shadowing_import_1(self):
   2458         s = """
   2459             import foo.bar as next
   2460 
   2461             class A:
   2462                 def next(self, a, b):
   2463                     pass
   2464             """
   2465         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2466 
   2467     def test_shadowing_import_2(self):
   2468         s = """
   2469             import bar, bar.foo as next
   2470 
   2471             class A:
   2472                 def next(self, a, b):
   2473                     pass
   2474             """
   2475         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2476 
   2477     def test_shadowing_import_3(self):
   2478         s = """
   2479             import bar, bar.foo as next, baz
   2480 
   2481             class A:
   2482                 def next(self, a, b):
   2483                     pass
   2484             """
   2485         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2486 
   2487     def test_shadowing_import_from_1(self):
   2488         s = """
   2489             from x import next
   2490 
   2491             class A:
   2492                 def next(self, a, b):
   2493                     pass
   2494             """
   2495         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2496 
   2497     def test_shadowing_import_from_2(self):
   2498         s = """
   2499             from x.a import next
   2500 
   2501             class A:
   2502                 def next(self, a, b):
   2503                     pass
   2504             """
   2505         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2506 
   2507     def test_shadowing_import_from_3(self):
   2508         s = """
   2509             from x import a, next, b
   2510 
   2511             class A:
   2512                 def next(self, a, b):
   2513                     pass
   2514             """
   2515         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2516 
   2517     def test_shadowing_import_from_4(self):
   2518         s = """
   2519             from x.a import a, next, b
   2520 
   2521             class A:
   2522                 def next(self, a, b):
   2523                     pass
   2524             """
   2525         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2526 
   2527     def test_shadowing_funcdef_1(self):
   2528         s = """
   2529             def next(a):
   2530                 pass
   2531 
   2532             class A:
   2533                 def next(self, a, b):
   2534                     pass
   2535             """
   2536         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2537 
   2538     def test_shadowing_funcdef_2(self):
   2539         b = """
   2540             def next(a):
   2541                 pass
   2542 
   2543             class A:
   2544                 def next(self):
   2545                     pass
   2546 
   2547             it.next()
   2548             """
   2549         a = """
   2550             def next(a):
   2551                 pass
   2552 
   2553             class A:
   2554                 def __next__(self):
   2555                     pass
   2556 
   2557             it.__next__()
   2558             """
   2559         self.warns(b, a, "Calls to builtin next() possibly shadowed")
   2560 
   2561     def test_shadowing_global_1(self):
   2562         s = """
   2563             def f():
   2564                 global next
   2565                 next = 5
   2566             """
   2567         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2568 
   2569     def test_shadowing_global_2(self):
   2570         s = """
   2571             def f():
   2572                 global a, next, b
   2573                 next = 5
   2574             """
   2575         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2576 
   2577     def test_shadowing_for_simple(self):
   2578         s = """
   2579             for next in it():
   2580                 pass
   2581 
   2582             b = 5
   2583             c = 6
   2584             """
   2585         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2586 
   2587     def test_shadowing_for_tuple_1(self):
   2588         s = """
   2589             for next, b in it():
   2590                 pass
   2591 
   2592             b = 5
   2593             c = 6
   2594             """
   2595         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2596 
   2597     def test_shadowing_for_tuple_2(self):
   2598         s = """
   2599             for a, (next, c), b in it():
   2600                 pass
   2601 
   2602             b = 5
   2603             c = 6
   2604             """
   2605         self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
   2606 
   2607     def test_noncall_access_1(self):
   2608         b = """gnext = g.next"""
   2609         a = """gnext = g.__next__"""
   2610         self.check(b, a)
   2611 
   2612     def test_noncall_access_2(self):
   2613         b = """f(g.next + 5)"""
   2614         a = """f(g.__next__ + 5)"""
   2615         self.check(b, a)
   2616 
   2617     def test_noncall_access_3(self):
   2618         b = """f(g().next + 5)"""
   2619         a = """f(g().__next__ + 5)"""
   2620         self.check(b, a)
   2621 
   2622 class Test_nonzero(FixerTestCase):
   2623     fixer = "nonzero"
   2624 
   2625     def test_1(self):
   2626         b = """
   2627             class A:
   2628                 def __nonzero__(self):
   2629                     pass
   2630             """
   2631         a = """
   2632             class A:
   2633                 def __bool__(self):
   2634                     pass
   2635             """
   2636         self.check(b, a)
   2637 
   2638     def test_2(self):
   2639         b = """
   2640             class A(object):
   2641                 def __nonzero__(self):
   2642                     pass
   2643             """
   2644         a = """
   2645             class A(object):
   2646                 def __bool__(self):
   2647                     pass
   2648             """
   2649         self.check(b, a)
   2650 
   2651     def test_unchanged_1(self):
   2652         s = """
   2653             class A(object):
   2654                 def __bool__(self):
   2655                     pass
   2656             """
   2657         self.unchanged(s)
   2658 
   2659     def test_unchanged_2(self):
   2660         s = """
   2661             class A(object):
   2662                 def __nonzero__(self, a):
   2663                     pass
   2664             """
   2665         self.unchanged(s)
   2666 
   2667     def test_unchanged_func(self):
   2668         s = """
   2669             def __nonzero__(self):
   2670                 pass
   2671             """
   2672         self.unchanged(s)
   2673 
   2674 class Test_numliterals(FixerTestCase):
   2675     fixer = "numliterals"
   2676 
   2677     def test_octal_1(self):
   2678         b = """0755"""
   2679         a = """0o755"""
   2680         self.check(b, a)
   2681 
   2682     def test_long_int_1(self):
   2683         b = """a = 12L"""
   2684         a = """a = 12"""
   2685         self.check(b, a)
   2686 
   2687     def test_long_int_2(self):
   2688         b = """a = 12l"""
   2689         a = """a = 12"""
   2690         self.check(b, a)
   2691 
   2692     def test_long_hex(self):
   2693         b = """b = 0x12l"""
   2694         a = """b = 0x12"""
   2695         self.check(b, a)
   2696 
   2697     def test_comments_and_spacing(self):
   2698         b = """b =   0x12L"""
   2699         a = """b =   0x12"""
   2700         self.check(b, a)
   2701 
   2702         b = """b = 0755 # spam"""
   2703         a = """b = 0o755 # spam"""
   2704         self.check(b, a)
   2705 
   2706     def test_unchanged_int(self):
   2707         s = """5"""
   2708         self.unchanged(s)
   2709 
   2710     def test_unchanged_float(self):
   2711         s = """5.0"""
   2712         self.unchanged(s)
   2713 
   2714     def test_unchanged_octal(self):
   2715         s = """0o755"""
   2716         self.unchanged(s)
   2717 
   2718     def test_unchanged_hex(self):
   2719         s = """0xABC"""
   2720         self.unchanged(s)
   2721 
   2722     def test_unchanged_exp(self):
   2723         s = """5.0e10"""
   2724         self.unchanged(s)
   2725 
   2726     def test_unchanged_complex_int(self):
   2727         s = """5 + 4j"""
   2728         self.unchanged(s)
   2729 
   2730     def test_unchanged_complex_float(self):
   2731         s = """5.4 + 4.9j"""
   2732         self.unchanged(s)
   2733 
   2734     def test_unchanged_complex_bare(self):
   2735         s = """4j"""
   2736         self.unchanged(s)
   2737         s = """4.4j"""
   2738         self.unchanged(s)
   2739 
   2740 class Test_renames(FixerTestCase):
   2741     fixer = "renames"
   2742 
   2743     modules = {"sys":  ("maxint", "maxsize"),
   2744               }
   2745 
   2746     def test_import_from(self):
   2747         for mod, (old, new) in self.modules.items():
   2748             b = "from %s import %s" % (mod, old)
   2749             a = "from %s import %s" % (mod, new)
   2750             self.check(b, a)
   2751 
   2752             s = "from foo import %s" % old
   2753             self.unchanged(s)
   2754 
   2755     def test_import_from_as(self):
   2756         for mod, (old, new) in self.modules.items():
   2757             b = "from %s import %s as foo_bar" % (mod, old)
   2758             a = "from %s import %s as foo_bar" % (mod, new)
   2759             self.check(b, a)
   2760 
   2761     def test_import_module_usage(self):
   2762         for mod, (old, new) in self.modules.items():
   2763             b = """
   2764                 import %s
   2765                 foo(%s, %s.%s)
   2766                 """ % (mod, mod, mod, old)
   2767             a = """
   2768                 import %s
   2769                 foo(%s, %s.%s)
   2770                 """ % (mod, mod, mod, new)
   2771             self.check(b, a)
   2772 
   2773     def XXX_test_from_import_usage(self):
   2774         # not implemented yet
   2775         for mod, (old, new) in self.modules.items():
   2776             b = """
   2777                 from %s import %s
   2778                 foo(%s, %s)
   2779                 """ % (mod, old, mod, old)
   2780             a = """
   2781                 from %s import %s
   2782                 foo(%s, %s)
   2783                 """ % (mod, new, mod, new)
   2784             self.check(b, a)
   2785 
   2786 class Test_unicode(FixerTestCase):
   2787     fixer = "unicode"
   2788 
   2789     def test_whitespace(self):
   2790         b = """unicode( x)"""
   2791         a = """str( x)"""
   2792         self.check(b, a)
   2793 
   2794         b = """ unicode(x )"""
   2795         a = """ str(x )"""
   2796         self.check(b, a)
   2797 
   2798         b = """ u'h'"""
   2799         a = """ 'h'"""
   2800         self.check(b, a)
   2801 
   2802     def test_unicode_call(self):
   2803         b = """unicode(x, y, z)"""
   2804         a = """str(x, y, z)"""
   2805         self.check(b, a)
   2806 
   2807     def test_unichr(self):
   2808         b = """unichr(u'h')"""
   2809         a = """chr('h')"""
   2810         self.check(b, a)
   2811 
   2812     def test_unicode_literal_1(self):
   2813         b = '''u"x"'''
   2814         a = '''"x"'''
   2815         self.check(b, a)
   2816 
   2817     def test_unicode_literal_2(self):
   2818         b = """ur'x'"""
   2819         a = """r'x'"""
   2820         self.check(b, a)
   2821 
   2822     def test_unicode_literal_3(self):
   2823         b = """UR'''x''' """
   2824         a = """R'''x''' """
   2825         self.check(b, a)
   2826 
   2827 class Test_callable(FixerTestCase):
   2828     fixer = "callable"
   2829 
   2830     def test_prefix_preservation(self):
   2831         b = """callable(    x)"""
   2832         a = """import collections\nisinstance(    x, collections.Callable)"""
   2833         self.check(b, a)
   2834 
   2835         b = """if     callable(x): pass"""
   2836         a = """import collections
   2837 if     isinstance(x, collections.Callable): pass"""
   2838         self.check(b, a)
   2839 
   2840     def test_callable_call(self):
   2841         b = """callable(x)"""
   2842         a = """import collections\nisinstance(x, collections.Callable)"""
   2843         self.check(b, a)
   2844 
   2845     def test_global_import(self):
   2846         b = """
   2847 def spam(foo):
   2848     callable(foo)"""[1:]
   2849         a = """
   2850 import collections
   2851 def spam(foo):
   2852     isinstance(foo, collections.Callable)"""[1:]
   2853         self.check(b, a)
   2854 
   2855         b = """
   2856 import collections
   2857 def spam(foo):
   2858     callable(foo)"""[1:]
   2859         # same output if it was already imported
   2860         self.check(b, a)
   2861 
   2862         b = """
   2863 from collections import *
   2864 def spam(foo):
   2865     callable(foo)"""[1:]
   2866         a = """
   2867 from collections import *
   2868 import collections
   2869 def spam(foo):
   2870     isinstance(foo, collections.Callable)"""[1:]
   2871         self.check(b, a)
   2872 
   2873         b = """
   2874 do_stuff()
   2875 do_some_other_stuff()
   2876 assert callable(do_stuff)"""[1:]
   2877         a = """
   2878 import collections
   2879 do_stuff()
   2880 do_some_other_stuff()
   2881 assert isinstance(do_stuff, collections.Callable)"""[1:]
   2882         self.check(b, a)
   2883 
   2884         b = """
   2885 if isinstance(do_stuff, Callable):
   2886     assert callable(do_stuff)
   2887     do_stuff(do_stuff)
   2888     if not callable(do_stuff):
   2889         exit(1)
   2890     else:
   2891         assert callable(do_stuff)
   2892 else:
   2893     assert not callable(do_stuff)"""[1:]
   2894         a = """
   2895 import collections
   2896 if isinstance(do_stuff, Callable):
   2897     assert isinstance(do_stuff, collections.Callable)
   2898     do_stuff(do_stuff)
   2899     if not isinstance(do_stuff, collections.Callable):
   2900         exit(1)
   2901     else:
   2902         assert isinstance(do_stuff, collections.Callable)
   2903 else:
   2904     assert not isinstance(do_stuff, collections.Callable)"""[1:]
   2905         self.check(b, a)
   2906 
   2907     def test_callable_should_not_change(self):
   2908         a = """callable(*x)"""
   2909         self.unchanged(a)
   2910 
   2911         a = """callable(x, y)"""
   2912         self.unchanged(a)
   2913 
   2914         a = """callable(x, kw=y)"""
   2915         self.unchanged(a)
   2916 
   2917         a = """callable()"""
   2918         self.unchanged(a)
   2919 
   2920 class Test_filter(FixerTestCase):
   2921     fixer = "filter"
   2922 
   2923     def test_prefix_preservation(self):
   2924         b = """x =   filter(    foo,     'abc'   )"""
   2925         a = """x =   list(filter(    foo,     'abc'   ))"""
   2926         self.check(b, a)
   2927 
   2928         b = """x =   filter(  None , 'abc'  )"""
   2929         a = """x =   [_f for _f in 'abc' if _f]"""
   2930         self.check(b, a)
   2931 
   2932     def test_filter_basic(self):
   2933         b = """x = filter(None, 'abc')"""
   2934         a = """x = [_f for _f in 'abc' if _f]"""
   2935         self.check(b, a)
   2936 
   2937         b = """x = len(filter(f, 'abc'))"""
   2938         a = """x = len(list(filter(f, 'abc')))"""
   2939         self.check(b, a)
   2940 
   2941         b = """x = filter(lambda x: x%2 == 0, range(10))"""
   2942         a = """x = [x for x in range(10) if x%2 == 0]"""
   2943         self.check(b, a)
   2944 
   2945         # Note the parens around x
   2946         b = """x = filter(lambda (x): x%2 == 0, range(10))"""
   2947         a = """x = [x for x in range(10) if x%2 == 0]"""
   2948         self.check(b, a)
   2949 
   2950         # XXX This (rare) case is not supported
   2951 ##         b = """x = filter(f, 'abc')[0]"""
   2952 ##         a = """x = list(filter(f, 'abc'))[0]"""
   2953 ##         self.check(b, a)
   2954 
   2955     def test_filter_nochange(self):
   2956         a = """b.join(filter(f, 'abc'))"""
   2957         self.unchanged(a)
   2958         a = """(a + foo(5)).join(filter(f, 'abc'))"""
   2959         self.unchanged(a)
   2960         a = """iter(filter(f, 'abc'))"""
   2961         self.unchanged(a)
   2962         a = """list(filter(f, 'abc'))"""
   2963         self.unchanged(a)
   2964         a = """list(filter(f, 'abc'))[0]"""
   2965         self.unchanged(a)
   2966         a = """set(filter(f, 'abc'))"""
   2967         self.unchanged(a)
   2968         a = """set(filter(f, 'abc')).pop()"""
   2969         self.unchanged(a)
   2970         a = """tuple(filter(f, 'abc'))"""
   2971         self.unchanged(a)
   2972         a = """any(filter(f, 'abc'))"""
   2973         self.unchanged(a)
   2974         a = """all(filter(f, 'abc'))"""
   2975         self.unchanged(a)
   2976         a = """sum(filter(f, 'abc'))"""
   2977         self.unchanged(a)
   2978         a = """sorted(filter(f, 'abc'))"""
   2979         self.unchanged(a)
   2980         a = """sorted(filter(f, 'abc'), key=blah)"""
   2981         self.unchanged(a)
   2982         a = """sorted(filter(f, 'abc'), key=blah)[0]"""
   2983         self.unchanged(a)
   2984         a = """enumerate(filter(f, 'abc'))"""
   2985         self.unchanged(a)
   2986         a = """enumerate(filter(f, 'abc'), start=1)"""
   2987         self.unchanged(a)
   2988         a = """for i in filter(f, 'abc'): pass"""
   2989         self.unchanged(a)
   2990         a = """[x for x in filter(f, 'abc')]"""
   2991         self.unchanged(a)
   2992         a = """(x for x in filter(f, 'abc'))"""
   2993         self.unchanged(a)
   2994 
   2995     def test_future_builtins(self):
   2996         a = "from future_builtins import spam, filter; filter(f, 'ham')"
   2997         self.unchanged(a)
   2998 
   2999         b = """from future_builtins import spam; x = filter(f, 'abc')"""
   3000         a = """from future_builtins import spam; x = list(filter(f, 'abc'))"""
   3001         self.check(b, a)
   3002 
   3003         a = "from future_builtins import *; filter(f, 'ham')"
   3004         self.unchanged(a)
   3005 
   3006 class Test_map(FixerTestCase):
   3007     fixer = "map"
   3008 
   3009     def check(self, b, a):
   3010         self.unchanged("from future_builtins import map; " + b, a)
   3011         super(Test_map, self).check(b, a)
   3012 
   3013     def test_prefix_preservation(self):
   3014         b = """x =    map(   f,    'abc'   )"""
   3015         a = """x =    list(map(   f,    'abc'   ))"""
   3016         self.check(b, a)
   3017 
   3018     def test_trailing_comment(self):
   3019         b = """x = map(f, 'abc')   #   foo"""
   3020         a = """x = list(map(f, 'abc'))   #   foo"""
   3021         self.check(b, a)
   3022 
   3023     def test_None_with_multiple_arguments(self):
   3024         s = """x = map(None, a, b, c)"""
   3025         self.warns_unchanged(s, "cannot convert map(None, ...) with "
   3026                              "multiple arguments")
   3027 
   3028     def test_map_basic(self):
   3029         b = """x = map(f, 'abc')"""
   3030         a = """x = list(map(f, 'abc'))"""
   3031         self.check(b, a)
   3032 
   3033         b = """x = len(map(f, 'abc', 'def'))"""
   3034         a = """x = len(list(map(f, 'abc', 'def')))"""
   3035         self.check(b, a)
   3036 
   3037         b = """x = map(None, 'abc')"""
   3038         a = """x = list('abc')"""
   3039         self.check(b, a)
   3040 
   3041         b = """x = map(lambda x: x+1, range(4))"""
   3042         a = """x = [x+1 for x in range(4)]"""
   3043         self.check(b, a)
   3044 
   3045         # Note the parens around x
   3046         b = """x = map(lambda (x): x+1, range(4))"""
   3047         a = """x = [x+1 for x in range(4)]"""
   3048         self.check(b, a)
   3049 
   3050         b = """
   3051             foo()
   3052             # foo
   3053             map(f, x)
   3054             """
   3055         a = """
   3056             foo()
   3057             # foo
   3058             list(map(f, x))
   3059             """
   3060         self.warns(b, a, "You should use a for loop here")
   3061 
   3062         # XXX This (rare) case is not supported
   3063 ##         b = """x = map(f, 'abc')[0]"""
   3064 ##         a = """x = list(map(f, 'abc'))[0]"""
   3065 ##         self.check(b, a)
   3066 
   3067     def test_map_nochange(self):
   3068         a = """b.join(map(f, 'abc'))"""
   3069         self.unchanged(a)
   3070         a = """(a + foo(5)).join(map(f, 'abc'))"""
   3071         self.unchanged(a)
   3072         a = """iter(map(f, 'abc'))"""
   3073         self.unchanged(a)
   3074         a = """list(map(f, 'abc'))"""
   3075         self.unchanged(a)
   3076         a = """list(map(f, 'abc'))[0]"""
   3077         self.unchanged(a)
   3078         a = """set(map(f, 'abc'))"""
   3079         self.unchanged(a)
   3080         a = """set(map(f, 'abc')).pop()"""
   3081         self.unchanged(a)
   3082         a = """tuple(map(f, 'abc'))"""
   3083         self.unchanged(a)
   3084         a = """any(map(f, 'abc'))"""
   3085         self.unchanged(a)
   3086         a = """all(map(f, 'abc'))"""
   3087         self.unchanged(a)
   3088         a = """sum(map(f, 'abc'))"""
   3089         self.unchanged(a)
   3090         a = """sorted(map(f, 'abc'))"""
   3091         self.unchanged(a)
   3092         a = """sorted(map(f, 'abc'), key=blah)"""
   3093         self.unchanged(a)
   3094         a = """sorted(map(f, 'abc'), key=blah)[0]"""
   3095         self.unchanged(a)
   3096         a = """enumerate(map(f, 'abc'))"""
   3097         self.unchanged(a)
   3098         a = """enumerate(map(f, 'abc'), start=1)"""
   3099         self.unchanged(a)
   3100         a = """for i in map(f, 'abc'): pass"""
   3101         self.unchanged(a)
   3102         a = """[x for x in map(f, 'abc')]"""
   3103         self.unchanged(a)
   3104         a = """(x for x in map(f, 'abc'))"""
   3105         self.unchanged(a)
   3106 
   3107     def test_future_builtins(self):
   3108         a = "from future_builtins import spam, map, eggs; map(f, 'ham')"
   3109         self.unchanged(a)
   3110 
   3111         b = """from future_builtins import spam, eggs; x = map(f, 'abc')"""
   3112         a = """from future_builtins import spam, eggs; x = list(map(f, 'abc'))"""
   3113         self.check(b, a)
   3114 
   3115         a = "from future_builtins import *; map(f, 'ham')"
   3116         self.unchanged(a)
   3117 
   3118 class Test_zip(FixerTestCase):
   3119     fixer = "zip"
   3120 
   3121     def check(self, b, a):
   3122         self.unchanged("from future_builtins import zip; " + b, a)
   3123         super(Test_zip, self).check(b, a)
   3124 
   3125     def test_zip_basic(self):
   3126         b = """x = zip(a, b, c)"""
   3127         a = """x = list(zip(a, b, c))"""
   3128         self.check(b, a)
   3129 
   3130         b = """x = len(zip(a, b))"""
   3131         a = """x = len(list(zip(a, b)))"""
   3132         self.check(b, a)
   3133 
   3134     def test_zip_nochange(self):
   3135         a = """b.join(zip(a, b))"""
   3136         self.unchanged(a)
   3137         a = """(a + foo(5)).join(zip(a, b))"""
   3138         self.unchanged(a)
   3139         a = """iter(zip(a, b))"""
   3140         self.unchanged(a)
   3141         a = """list(zip(a, b))"""
   3142         self.unchanged(a)
   3143         a = """list(zip(a, b))[0]"""
   3144         self.unchanged(a)
   3145         a = """set(zip(a, b))"""
   3146         self.unchanged(a)
   3147         a = """set(zip(a, b)).pop()"""
   3148         self.unchanged(a)
   3149         a = """tuple(zip(a, b))"""
   3150         self.unchanged(a)
   3151         a = """any(zip(a, b))"""
   3152         self.unchanged(a)
   3153         a = """all(zip(a, b))"""
   3154         self.unchanged(a)
   3155         a = """sum(zip(a, b))"""
   3156         self.unchanged(a)
   3157         a = """sorted(zip(a, b))"""
   3158         self.unchanged(a)
   3159         a = """sorted(zip(a, b), key=blah)"""
   3160         self.unchanged(a)
   3161         a = """sorted(zip(a, b), key=blah)[0]"""
   3162         self.unchanged(a)
   3163         a = """enumerate(zip(a, b))"""
   3164         self.unchanged(a)
   3165         a = """enumerate(zip(a, b), start=1)"""
   3166         self.unchanged(a)
   3167         a = """for i in zip(a, b): pass"""
   3168         self.unchanged(a)
   3169         a = """[x for x in zip(a, b)]"""
   3170         self.unchanged(a)
   3171         a = """(x for x in zip(a, b))"""
   3172         self.unchanged(a)
   3173 
   3174     def test_future_builtins(self):
   3175         a = "from future_builtins import spam, zip, eggs; zip(a, b)"
   3176         self.unchanged(a)
   3177 
   3178         b = """from future_builtins import spam, eggs; x = zip(a, b)"""
   3179         a = """from future_builtins import spam, eggs; x = list(zip(a, b))"""
   3180         self.check(b, a)
   3181 
   3182         a = "from future_builtins import *; zip(a, b)"
   3183         self.unchanged(a)
   3184 
   3185 class Test_standarderror(FixerTestCase):
   3186     fixer = "standarderror"
   3187 
   3188     def test(self):
   3189         b = """x =    StandardError()"""
   3190         a = """x =    Exception()"""
   3191         self.check(b, a)
   3192 
   3193         b = """x = StandardError(a, b, c)"""
   3194         a = """x = Exception(a, b, c)"""
   3195         self.check(b, a)
   3196 
   3197         b = """f(2 + StandardError(a, b, c))"""
   3198         a = """f(2 + Exception(a, b, c))"""
   3199         self.check(b, a)
   3200 
   3201 class Test_types(FixerTestCase):
   3202     fixer = "types"
   3203 
   3204     def test_basic_types_convert(self):
   3205         b = """types.StringType"""
   3206         a = """bytes"""
   3207         self.check(b, a)
   3208 
   3209         b = """types.DictType"""
   3210         a = """dict"""
   3211         self.check(b, a)
   3212 
   3213         b = """types . IntType"""
   3214         a = """int"""
   3215         self.check(b, a)
   3216 
   3217         b = """types.ListType"""
   3218         a = """list"""
   3219         self.check(b, a)
   3220 
   3221         b = """types.LongType"""
   3222         a = """int"""
   3223         self.check(b, a)
   3224 
   3225         b = """types.NoneType"""
   3226         a = """type(None)"""
   3227         self.check(b, a)
   3228 
   3229 class Test_idioms(FixerTestCase):
   3230     fixer = "idioms"
   3231 
   3232     def test_while(self):
   3233         b = """while 1: foo()"""
   3234         a = """while True: foo()"""
   3235         self.check(b, a)
   3236 
   3237         b = """while   1: foo()"""
   3238         a = """while   True: foo()"""
   3239         self.check(b, a)
   3240 
   3241         b = """
   3242             while 1:
   3243                 foo()
   3244             """
   3245         a = """
   3246             while True:
   3247                 foo()
   3248             """
   3249         self.check(b, a)
   3250 
   3251     def test_while_unchanged(self):
   3252         s = """while 11: foo()"""
   3253         self.unchanged(s)
   3254 
   3255         s = """while 0: foo()"""
   3256         self.unchanged(s)
   3257 
   3258         s = """while foo(): foo()"""
   3259         self.unchanged(s)
   3260 
   3261         s = """while []: foo()"""
   3262         self.unchanged(s)
   3263 
   3264     def test_eq_simple(self):
   3265         b = """type(x) == T"""
   3266         a = """isinstance(x, T)"""
   3267         self.check(b, a)
   3268 
   3269         b = """if   type(x) == T: pass"""
   3270         a = """if   isinstance(x, T): pass"""
   3271         self.check(b, a)
   3272 
   3273     def test_eq_reverse(self):
   3274         b = """T == type(x)"""
   3275         a = """isinstance(x, T)"""
   3276         self.check(b, a)
   3277 
   3278         b = """if   T == type(x): pass"""
   3279         a = """if   isinstance(x, T): pass"""
   3280         self.check(b, a)
   3281 
   3282     def test_eq_expression(self):
   3283         b = """type(x+y) == d.get('T')"""
   3284         a = """isinstance(x+y, d.get('T'))"""
   3285         self.check(b, a)
   3286 
   3287         b = """type(   x  +  y) == d.get('T')"""
   3288         a = """isinstance(x  +  y, d.get('T'))"""
   3289         self.check(b, a)
   3290 
   3291     def test_is_simple(self):
   3292         b = """type(x) is T"""
   3293         a = """isinstance(x, T)"""
   3294         self.check(b, a)
   3295 
   3296         b = """if   type(x) is T: pass"""
   3297         a = """if   isinstance(x, T): pass"""
   3298         self.check(b, a)
   3299 
   3300     def test_is_reverse(self):
   3301         b = """T is type(x)"""
   3302         a = """isinstance(x, T)"""
   3303         self.check(b, a)
   3304 
   3305         b = """if   T is type(x): pass"""
   3306         a = """if   isinstance(x, T): pass"""
   3307         self.check(b, a)
   3308 
   3309     def test_is_expression(self):
   3310         b = """type(x+y) is d.get('T')"""
   3311         a = """isinstance(x+y, d.get('T'))"""
   3312         self.check(b, a)
   3313 
   3314         b = """type(   x  +  y) is d.get('T')"""
   3315         a = """isinstance(x  +  y, d.get('T'))"""
   3316         self.check(b, a)
   3317 
   3318     def test_is_not_simple(self):
   3319         b = """type(x) is not T"""
   3320         a = """not isinstance(x, T)"""
   3321         self.check(b, a)
   3322 
   3323         b = """if   type(x) is not T: pass"""
   3324         a = """if   not isinstance(x, T): pass"""
   3325         self.check(b, a)
   3326 
   3327     def test_is_not_reverse(self):
   3328         b = """T is not type(x)"""
   3329         a = """not isinstance(x, T)"""
   3330         self.check(b, a)
   3331 
   3332         b = """if   T is not type(x): pass"""
   3333         a = """if   not isinstance(x, T): pass"""
   3334         self.check(b, a)
   3335 
   3336     def test_is_not_expression(self):
   3337         b = """type(x+y) is not d.get('T')"""
   3338         a = """not isinstance(x+y, d.get('T'))"""
   3339         self.check(b, a)
   3340 
   3341         b = """type(   x  +  y) is not d.get('T')"""
   3342         a = """not isinstance(x  +  y, d.get('T'))"""
   3343         self.check(b, a)
   3344 
   3345     def test_ne_simple(self):
   3346         b = """type(x) != T"""
   3347         a = """not isinstance(x, T)"""
   3348         self.check(b, a)
   3349 
   3350         b = """if   type(x) != T: pass"""
   3351         a = """if   not isinstance(x, T): pass"""
   3352         self.check(b, a)
   3353 
   3354     def test_ne_reverse(self):
   3355         b = """T != type(x)"""
   3356         a = """not isinstance(x, T)"""
   3357         self.check(b, a)
   3358 
   3359         b = """if   T != type(x): pass"""
   3360         a = """if   not isinstance(x, T): pass"""
   3361         self.check(b, a)
   3362 
   3363     def test_ne_expression(self):
   3364         b = """type(x+y) != d.get('T')"""
   3365         a = """not isinstance(x+y, d.get('T'))"""
   3366         self.check(b, a)
   3367 
   3368         b = """type(   x  +  y) != d.get('T')"""
   3369         a = """not isinstance(x  +  y, d.get('T'))"""
   3370         self.check(b, a)
   3371 
   3372     def test_type_unchanged(self):
   3373         a = """type(x).__name__"""
   3374         self.unchanged(a)
   3375 
   3376     def test_sort_list_call(self):
   3377         b = """
   3378             v = list(t)
   3379             v.sort()
   3380             foo(v)
   3381             """
   3382         a = """
   3383             v = sorted(t)
   3384             foo(v)
   3385             """
   3386         self.check(b, a)
   3387 
   3388         b = """
   3389             v = list(foo(b) + d)
   3390             v.sort()
   3391             foo(v)
   3392             """
   3393         a = """
   3394             v = sorted(foo(b) + d)
   3395             foo(v)
   3396             """
   3397         self.check(b, a)
   3398 
   3399         b = """
   3400             while x:
   3401                 v = list(t)
   3402                 v.sort()
   3403                 foo(v)
   3404             """
   3405         a = """
   3406             while x:
   3407                 v = sorted(t)
   3408                 foo(v)
   3409             """
   3410         self.check(b, a)
   3411 
   3412         b = """
   3413             v = list(t)
   3414             # foo
   3415             v.sort()
   3416             foo(v)
   3417             """
   3418         a = """
   3419             v = sorted(t)
   3420             # foo
   3421             foo(v)
   3422             """
   3423         self.check(b, a)
   3424 
   3425         b = r"""
   3426             v = list(   t)
   3427             v.sort()
   3428             foo(v)
   3429             """
   3430         a = r"""
   3431             v = sorted(   t)
   3432             foo(v)
   3433             """
   3434         self.check(b, a)
   3435 
   3436         b = r"""
   3437             try:
   3438                 m = list(s)
   3439                 m.sort()
   3440             except: pass
   3441             """
   3442 
   3443         a = r"""
   3444             try:
   3445                 m = sorted(s)
   3446             except: pass
   3447             """
   3448         self.check(b, a)
   3449 
   3450         b = r"""
   3451             try:
   3452                 m = list(s)
   3453                 # foo
   3454                 m.sort()
   3455             except: pass
   3456             """
   3457 
   3458         a = r"""
   3459             try:
   3460                 m = sorted(s)
   3461                 # foo
   3462             except: pass
   3463             """
   3464         self.check(b, a)
   3465 
   3466         b = r"""
   3467             m = list(s)
   3468             # more comments
   3469             m.sort()"""
   3470 
   3471         a = r"""
   3472             m = sorted(s)
   3473             # more comments"""
   3474         self.check(b, a)
   3475 
   3476     def test_sort_simple_expr(self):
   3477         b = """
   3478             v = t
   3479             v.sort()
   3480             foo(v)
   3481             """
   3482         a = """
   3483             v = sorted(t)
   3484             foo(v)
   3485             """
   3486         self.check(b, a)
   3487 
   3488         b = """
   3489             v = foo(b)
   3490             v.sort()
   3491             foo(v)
   3492             """
   3493         a = """
   3494             v = sorted(foo(b))
   3495             foo(v)
   3496             """
   3497         self.check(b, a)
   3498 
   3499         b = """
   3500             v = b.keys()
   3501             v.sort()
   3502             foo(v)
   3503             """
   3504         a = """
   3505             v = sorted(b.keys())
   3506             foo(v)
   3507             """
   3508         self.check(b, a)
   3509 
   3510         b = """
   3511             v = foo(b) + d
   3512             v.sort()
   3513             foo(v)
   3514             """
   3515         a = """
   3516             v = sorted(foo(b) + d)
   3517             foo(v)
   3518             """
   3519         self.check(b, a)
   3520 
   3521         b = """
   3522             while x:
   3523                 v = t
   3524                 v.sort()
   3525                 foo(v)
   3526             """
   3527         a = """
   3528             while x:
   3529                 v = sorted(t)
   3530                 foo(v)
   3531             """
   3532         self.check(b, a)
   3533 
   3534         b = """
   3535             v = t
   3536             # foo
   3537             v.sort()
   3538             foo(v)
   3539             """
   3540         a = """
   3541             v = sorted(t)
   3542             # foo
   3543             foo(v)
   3544             """
   3545         self.check(b, a)
   3546 
   3547         b = r"""
   3548             v =   t
   3549             v.sort()
   3550             foo(v)
   3551             """
   3552         a = r"""
   3553             v =   sorted(t)
   3554             foo(v)
   3555             """
   3556         self.check(b, a)
   3557 
   3558     def test_sort_unchanged(self):
   3559         s = """
   3560             v = list(t)
   3561             w.sort()
   3562             foo(w)
   3563             """
   3564         self.unchanged(s)
   3565 
   3566         s = """
   3567             v = list(t)
   3568             v.sort(u)
   3569             foo(v)
   3570             """
   3571         self.unchanged(s)
   3572 
   3573 class Test_basestring(FixerTestCase):
   3574     fixer = "basestring"
   3575 
   3576     def test_basestring(self):
   3577         b = """isinstance(x, basestring)"""
   3578         a = """isinstance(x, str)"""
   3579         self.check(b, a)
   3580 
   3581 class Test_buffer(FixerTestCase):
   3582     fixer = "buffer"
   3583 
   3584     def test_buffer(self):
   3585         b = """x = buffer(y)"""
   3586         a = """x = memoryview(y)"""
   3587         self.check(b, a)
   3588 
   3589     def test_slicing(self):
   3590         b = """buffer(y)[4:5]"""
   3591         a = """memoryview(y)[4:5]"""
   3592         self.check(b, a)
   3593 
   3594 class Test_future(FixerTestCase):
   3595     fixer = "future"
   3596 
   3597     def test_future(self):
   3598         b = """from __future__ import braces"""
   3599         a = """"""
   3600         self.check(b, a)
   3601 
   3602         b = """# comment\nfrom __future__ import braces"""
   3603         a = """# comment\n"""
   3604         self.check(b, a)
   3605 
   3606         b = """from __future__ import braces\n# comment"""
   3607         a = """\n# comment"""
   3608         self.check(b, a)
   3609 
   3610     def test_run_order(self):
   3611         self.assert_runs_after('print')
   3612 
   3613 class Test_itertools(FixerTestCase):
   3614     fixer = "itertools"
   3615 
   3616     def checkall(self, before, after):
   3617         # Because we need to check with and without the itertools prefix
   3618         # and on each of the three functions, these loops make it all
   3619         # much easier
   3620         for i in ('itertools.', ''):
   3621             for f in ('map', 'filter', 'zip'):
   3622                 b = before %(i+'i'+f)
   3623                 a = after %(f)
   3624                 self.check(b, a)
   3625 
   3626     def test_0(self):
   3627         # A simple example -- test_1 covers exactly the same thing,
   3628         # but it's not quite as clear.
   3629         b = "itertools.izip(a, b)"
   3630         a = "zip(a, b)"
   3631         self.check(b, a)
   3632 
   3633     def test_1(self):
   3634         b = """%s(f, a)"""
   3635         a = """%s(f, a)"""
   3636         self.checkall(b, a)
   3637 
   3638     def test_qualified(self):
   3639         b = """itertools.ifilterfalse(a, b)"""
   3640         a = """itertools.filterfalse(a, b)"""
   3641         self.check(b, a)
   3642 
   3643         b = """itertools.izip_longest(a, b)"""
   3644         a = """itertools.zip_longest(a, b)"""
   3645         self.check(b, a)
   3646 
   3647     def test_2(self):
   3648         b = """ifilterfalse(a, b)"""
   3649         a = """filterfalse(a, b)"""
   3650         self.check(b, a)
   3651 
   3652         b = """izip_longest(a, b)"""
   3653         a = """zip_longest(a, b)"""
   3654         self.check(b, a)
   3655 
   3656     def test_space_1(self):
   3657         b = """    %s(f, a)"""
   3658         a = """    %s(f, a)"""
   3659         self.checkall(b, a)
   3660 
   3661     def test_space_2(self):
   3662         b = """    itertools.ifilterfalse(a, b)"""
   3663         a = """    itertools.filterfalse(a, b)"""
   3664         self.check(b, a)
   3665 
   3666         b = """    itertools.izip_longest(a, b)"""
   3667         a = """    itertools.zip_longest(a, b)"""
   3668         self.check(b, a)
   3669 
   3670     def test_run_order(self):
   3671         self.assert_runs_after('map', 'zip', 'filter')
   3672 
   3673 
   3674 class Test_itertools_imports(FixerTestCase):
   3675     fixer = 'itertools_imports'
   3676 
   3677     def test_reduced(self):
   3678         b = "from itertools import imap, izip, foo"
   3679         a = "from itertools import foo"
   3680         self.check(b, a)
   3681 
   3682         b = "from itertools import bar, imap, izip, foo"
   3683         a = "from itertools import bar, foo"
   3684         self.check(b, a)
   3685 
   3686         b = "from itertools import chain, imap, izip"
   3687         a = "from itertools import chain"
   3688         self.check(b, a)
   3689 
   3690     def test_comments(self):
   3691         b = "#foo\nfrom itertools import imap, izip"
   3692         a = "#foo\n"
   3693         self.check(b, a)
   3694 
   3695     def test_none(self):
   3696         b = "from itertools import imap, izip"
   3697         a = ""
   3698         self.check(b, a)
   3699 
   3700         b = "from itertools import izip"
   3701         a = ""
   3702         self.check(b, a)
   3703 
   3704     def test_import_as(self):
   3705         b = "from itertools import izip, bar as bang, imap"
   3706         a = "from itertools import bar as bang"
   3707         self.check(b, a)
   3708 
   3709         b = "from itertools import izip as _zip, imap, bar"
   3710         a = "from itertools import bar"
   3711         self.check(b, a)
   3712 
   3713         b = "from itertools import imap as _map"
   3714         a = ""
   3715         self.check(b, a)
   3716 
   3717         b = "from itertools import imap as _map, izip as _zip"
   3718         a = ""
   3719         self.check(b, a)
   3720 
   3721         s = "from itertools import bar as bang"
   3722         self.unchanged(s)
   3723 
   3724     def test_ifilter_and_zip_longest(self):
   3725         for name in "filterfalse", "zip_longest":
   3726             b = "from itertools import i%s" % (name,)
   3727             a = "from itertools import %s" % (name,)
   3728             self.check(b, a)
   3729 
   3730             b = "from itertools import imap, i%s, foo" % (name,)
   3731             a = "from itertools import %s, foo" % (name,)
   3732             self.check(b, a)
   3733 
   3734             b = "from itertools import bar, i%s, foo" % (name,)
   3735             a = "from itertools import bar, %s, foo" % (name,)
   3736             self.check(b, a)
   3737 
   3738     def test_import_star(self):
   3739         s = "from itertools import *"
   3740         self.unchanged(s)
   3741 
   3742 
   3743     def test_unchanged(self):
   3744         s = "from itertools import foo"
   3745         self.unchanged(s)
   3746 
   3747 
   3748 class Test_import(FixerTestCase):
   3749     fixer = "import"
   3750 
   3751     def setUp(self):
   3752         super(Test_import, self).setUp()
   3753         # Need to replace fix_import's exists method
   3754         # so we can check that it's doing the right thing
   3755         self.files_checked = []
   3756         self.present_files = set()
   3757         self.always_exists = True
   3758         def fake_exists(name):
   3759             self.files_checked.append(name)
   3760             return self.always_exists or (name in self.present_files)
   3761 
   3762         from lib2to3.fixes import fix_import
   3763         fix_import.exists = fake_exists
   3764 
   3765     def tearDown(self):
   3766         from lib2to3.fixes import fix_import
   3767         fix_import.exists = os.path.exists
   3768 
   3769     def check_both(self, b, a):
   3770         self.always_exists = True
   3771         super(Test_import, self).check(b, a)
   3772         self.always_exists = False
   3773         super(Test_import, self).unchanged(b)
   3774 
   3775     def test_files_checked(self):
   3776         def p(path):
   3777             # Takes a unix path and returns a path with correct separators
   3778             return os.path.pathsep.join(path.split("/"))
   3779 
   3780         self.always_exists = False
   3781         self.present_files = set(['__init__.py'])
   3782         expected_extensions = ('.py', os.path.sep, '.pyc', '.so', '.sl', '.pyd')
   3783         names_to_test = (p("/spam/eggs.py"), "ni.py", p("../../shrubbery.py"))
   3784 
   3785         for name in names_to_test:
   3786             self.files_checked = []
   3787             self.filename = name
   3788             self.unchanged("import jam")
   3789 
   3790             if os.path.dirname(name):
   3791                 name = os.path.dirname(name) + '/jam'
   3792             else:
   3793                 name = 'jam'
   3794             expected_checks = set(name + ext for ext in expected_extensions)
   3795             expected_checks.add("__init__.py")
   3796 
   3797             self.assertEqual(set(self.files_checked), expected_checks)
   3798 
   3799     def test_not_in_package(self):
   3800         s = "import bar"
   3801         self.always_exists = False
   3802         self.present_files = set(["bar.py"])
   3803         self.unchanged(s)
   3804 
   3805     def test_with_absolute_import_enabled(self):
   3806         s = "from __future__ import absolute_import\nimport bar"
   3807         self.always_exists = False
   3808         self.present_files = set(["__init__.py", "bar.py"])
   3809         self.unchanged(s)
   3810 
   3811     def test_in_package(self):
   3812         b = "import bar"
   3813         a = "from . import bar"
   3814         self.always_exists = False
   3815         self.present_files = set(["__init__.py", "bar.py"])
   3816         self.check(b, a)
   3817 
   3818     def test_import_from_package(self):
   3819         b = "import bar"
   3820         a = "from . import bar"
   3821         self.always_exists = False
   3822         self.present_files = set(["__init__.py", "bar" + os.path.sep])
   3823         self.check(b, a)
   3824 
   3825     def test_already_relative_import(self):
   3826         s = "from . import bar"
   3827         self.unchanged(s)
   3828 
   3829     def test_comments_and_indent(self):
   3830         b = "import bar # Foo"
   3831         a = "from . import bar # Foo"
   3832         self.check(b, a)
   3833 
   3834     def test_from(self):
   3835         b = "from foo import bar, baz"
   3836         a = "from .foo import bar, baz"
   3837         self.check_both(b, a)
   3838 
   3839         b = "from foo import bar"
   3840         a = "from .foo import bar"
   3841         self.check_both(b, a)
   3842 
   3843         b = "from foo import (bar, baz)"
   3844         a = "from .foo import (bar, baz)"
   3845         self.check_both(b, a)
   3846 
   3847     def test_dotted_from(self):
   3848         b = "from green.eggs import ham"
   3849         a = "from .green.eggs import ham"
   3850         self.check_both(b, a)
   3851 
   3852     def test_from_as(self):
   3853         b = "from green.eggs import ham as spam"
   3854         a = "from .green.eggs import ham as spam"
   3855         self.check_both(b, a)
   3856 
   3857     def test_import(self):
   3858         b = "import foo"
   3859         a = "from . import foo"
   3860         self.check_both(b, a)
   3861 
   3862         b = "import foo, bar"
   3863         a = "from . import foo, bar"
   3864         self.check_both(b, a)
   3865 
   3866         b = "import foo, bar, x"
   3867         a = "from . import foo, bar, x"
   3868         self.check_both(b, a)
   3869 
   3870         b = "import x, y, z"
   3871         a = "from . import x, y, z"
   3872         self.check_both(b, a)
   3873 
   3874     def test_import_as(self):
   3875         b = "import foo as x"
   3876         a = "from . import foo as x"
   3877         self.check_both(b, a)
   3878 
   3879         b = "import a as b, b as c, c as d"
   3880         a = "from . import a as b, b as c, c as d"
   3881         self.check_both(b, a)
   3882 
   3883     def test_local_and_absolute(self):
   3884         self.always_exists = False
   3885         self.present_files = set(["foo.py", "__init__.py"])
   3886 
   3887         s = "import foo, bar"
   3888         self.warns_unchanged(s, "absolute and local imports together")
   3889 
   3890     def test_dotted_import(self):
   3891         b = "import foo.bar"
   3892         a = "from . import foo.bar"
   3893         self.check_both(b, a)
   3894 
   3895     def test_dotted_import_as(self):
   3896         b = "import foo.bar as bang"
   3897         a = "from . import foo.bar as bang"
   3898         self.check_both(b, a)
   3899 
   3900     def test_prefix(self):
   3901         b = """
   3902         # prefix
   3903         import foo.bar
   3904         """
   3905         a = """
   3906         # prefix
   3907         from . import foo.bar
   3908         """
   3909         self.check_both(b, a)
   3910 
   3911 
   3912 class Test_set_literal(FixerTestCase):
   3913 
   3914     fixer = "set_literal"
   3915 
   3916     def test_basic(self):
   3917         b = """set([1, 2, 3])"""
   3918         a = """{1, 2, 3}"""
   3919         self.check(b, a)
   3920 
   3921         b = """set((1, 2, 3))"""
   3922         a = """{1, 2, 3}"""
   3923         self.check(b, a)
   3924 
   3925         b = """set((1,))"""
   3926         a = """{1}"""
   3927         self.check(b, a)
   3928 
   3929         b = """set([1])"""
   3930         self.check(b, a)
   3931 
   3932         b = """set((a, b))"""
   3933         a = """{a, b}"""
   3934         self.check(b, a)
   3935 
   3936         b = """set([a, b])"""
   3937         self.check(b, a)
   3938 
   3939         b = """set((a*234, f(args=23)))"""
   3940         a = """{a*234, f(args=23)}"""
   3941         self.check(b, a)
   3942 
   3943         b = """set([a*23, f(23)])"""
   3944         a = """{a*23, f(23)}"""
   3945         self.check(b, a)
   3946 
   3947         b = """set([a-234**23])"""
   3948         a = """{a-234**23}"""
   3949         self.check(b, a)
   3950 
   3951     def test_listcomps(self):
   3952         b = """set([x for x in y])"""
   3953         a = """{x for x in y}"""
   3954         self.check(b, a)
   3955 
   3956         b = """set([x for x in y if x == m])"""
   3957         a = """{x for x in y if x == m}"""
   3958         self.check(b, a)
   3959 
   3960         b = """set([x for x in y for a in b])"""
   3961         a = """{x for x in y for a in b}"""
   3962         self.check(b, a)
   3963 
   3964         b = """set([f(x) - 23 for x in y])"""
   3965         a = """{f(x) - 23 for x in y}"""
   3966         self.check(b, a)
   3967 
   3968     def test_whitespace(self):
   3969         b = """set( [1, 2])"""
   3970         a = """{1, 2}"""
   3971         self.check(b, a)
   3972 
   3973         b = """set([1 ,  2])"""
   3974         a = """{1 ,  2}"""
   3975         self.check(b, a)
   3976 
   3977         b = """set([ 1 ])"""
   3978         a = """{ 1 }"""
   3979         self.check(b, a)
   3980 
   3981         b = """set( [1] )"""
   3982         a = """{1}"""
   3983         self.check(b, a)
   3984 
   3985         b = """set([  1,  2  ])"""
   3986         a = """{  1,  2  }"""
   3987         self.check(b, a)
   3988 
   3989         b = """set([x  for x in y ])"""
   3990         a = """{x  for x in y }"""
   3991         self.check(b, a)
   3992 
   3993         b = """set(
   3994                    [1, 2]
   3995                )
   3996             """
   3997         a = """{1, 2}\n"""
   3998         self.check(b, a)
   3999 
   4000     def test_comments(self):
   4001         b = """set((1, 2)) # Hi"""
   4002         a = """{1, 2} # Hi"""
   4003         self.check(b, a)
   4004 
   4005         # This isn't optimal behavior, but the fixer is optional.
   4006         b = """
   4007             # Foo
   4008             set( # Bar
   4009                (1, 2)
   4010             )
   4011             """
   4012         a = """
   4013             # Foo
   4014             {1, 2}
   4015             """
   4016         self.check(b, a)
   4017 
   4018     def test_unchanged(self):
   4019         s = """set()"""
   4020         self.unchanged(s)
   4021 
   4022         s = """set(a)"""
   4023         self.unchanged(s)
   4024 
   4025         s = """set(a, b, c)"""
   4026         self.unchanged(s)
   4027 
   4028         # Don't transform generators because they might have to be lazy.
   4029         s = """set(x for x in y)"""
   4030         self.unchanged(s)
   4031 
   4032         s = """set(x for x in y if z)"""
   4033         self.unchanged(s)
   4034 
   4035         s = """set(a*823-23**2 + f(23))"""
   4036         self.unchanged(s)
   4037 
   4038 
   4039 class Test_sys_exc(FixerTestCase):
   4040     fixer = "sys_exc"
   4041 
   4042     def test_0(self):
   4043         b = "sys.exc_type"
   4044         a = "sys.exc_info()[0]"
   4045         self.check(b, a)
   4046 
   4047     def test_1(self):
   4048         b = "sys.exc_value"
   4049         a = "sys.exc_info()[1]"
   4050         self.check(b, a)
   4051 
   4052     def test_2(self):
   4053         b = "sys.exc_traceback"
   4054         a = "sys.exc_info()[2]"
   4055         self.check(b, a)
   4056 
   4057     def test_3(self):
   4058         b = "sys.exc_type # Foo"
   4059         a = "sys.exc_info()[0] # Foo"
   4060         self.check(b, a)
   4061 
   4062     def test_4(self):
   4063         b = "sys.  exc_type"
   4064         a = "sys.  exc_info()[0]"
   4065         self.check(b, a)
   4066 
   4067     def test_5(self):
   4068         b = "sys  .exc_type"
   4069         a = "sys  .exc_info()[0]"
   4070         self.check(b, a)
   4071 
   4072 
   4073 class Test_paren(FixerTestCase):
   4074     fixer = "paren"
   4075 
   4076     def test_0(self):
   4077         b = """[i for i in 1, 2 ]"""
   4078         a = """[i for i in (1, 2) ]"""
   4079         self.check(b, a)
   4080 
   4081     def test_1(self):
   4082         b = """[i for i in 1, 2, ]"""
   4083         a = """[i for i in (1, 2,) ]"""
   4084         self.check(b, a)
   4085 
   4086     def test_2(self):
   4087         b = """[i for i  in     1, 2 ]"""
   4088         a = """[i for i  in     (1, 2) ]"""
   4089         self.check(b, a)
   4090 
   4091     def test_3(self):
   4092         b = """[i for i in 1, 2 if i]"""
   4093         a = """[i for i in (1, 2) if i]"""
   4094         self.check(b, a)
   4095 
   4096     def test_4(self):
   4097         b = """[i for i in 1,    2    ]"""
   4098         a = """[i for i in (1,    2)    ]"""
   4099         self.check(b, a)
   4100 
   4101     def test_5(self):
   4102         b = """(i for i in 1, 2)"""
   4103         a = """(i for i in (1, 2))"""
   4104         self.check(b, a)
   4105 
   4106     def test_6(self):
   4107         b = """(i for i in 1   ,2   if i)"""
   4108         a = """(i for i in (1   ,2)   if i)"""
   4109         self.check(b, a)
   4110 
   4111     def test_unchanged_0(self):
   4112         s = """[i for i in (1, 2)]"""
   4113         self.unchanged(s)
   4114 
   4115     def test_unchanged_1(self):
   4116         s = """[i for i in foo()]"""
   4117         self.unchanged(s)
   4118 
   4119     def test_unchanged_2(self):
   4120         s = """[i for i in (1, 2) if nothing]"""
   4121         self.unchanged(s)
   4122 
   4123     def test_unchanged_3(self):
   4124         s = """(i for i in (1, 2))"""
   4125         self.unchanged(s)
   4126 
   4127     def test_unchanged_4(self):
   4128         s = """[i for i in m]"""
   4129         self.unchanged(s)
   4130 
   4131 class Test_metaclass(FixerTestCase):
   4132 
   4133     fixer = 'metaclass'
   4134 
   4135     def test_unchanged(self):
   4136         self.unchanged("class X(): pass")
   4137         self.unchanged("class X(object): pass")
   4138         self.unchanged("class X(object1, object2): pass")
   4139         self.unchanged("class X(object1, object2, object3): pass")
   4140         self.unchanged("class X(metaclass=Meta): pass")
   4141         self.unchanged("class X(b, arg=23, metclass=Meta): pass")
   4142         self.unchanged("class X(b, arg=23, metaclass=Meta, other=42): pass")
   4143 
   4144         s = """
   4145         class X:
   4146             def __metaclass__(self): pass
   4147         """
   4148         self.unchanged(s)
   4149 
   4150         s = """
   4151         class X:
   4152             a[23] = 74
   4153         """
   4154         self.unchanged(s)
   4155 
   4156     def test_comments(self):
   4157         b = """
   4158         class X:
   4159             # hi
   4160             __metaclass__ = AppleMeta
   4161         """
   4162         a = """
   4163         class X(metaclass=AppleMeta):
   4164             # hi
   4165             pass
   4166         """
   4167         self.check(b, a)
   4168 
   4169         b = """
   4170         class X:
   4171             __metaclass__ = Meta
   4172             # Bedtime!
   4173         """
   4174         a = """
   4175         class X(metaclass=Meta):
   4176             pass
   4177             # Bedtime!
   4178         """
   4179         self.check(b, a)
   4180 
   4181     def test_meta(self):
   4182         # no-parent class, odd body
   4183         b = """
   4184         class X():
   4185             __metaclass__ = Q
   4186             pass
   4187         """
   4188         a = """
   4189         class X(metaclass=Q):
   4190             pass
   4191         """
   4192         self.check(b, a)
   4193 
   4194         # one parent class, no body
   4195         b = """class X(object): __metaclass__ = Q"""
   4196         a = """class X(object, metaclass=Q): pass"""
   4197         self.check(b, a)
   4198 
   4199 
   4200         # one parent, simple body
   4201         b = """
   4202         class X(object):
   4203             __metaclass__ = Meta
   4204             bar = 7
   4205         """
   4206         a = """
   4207         class X(object, metaclass=Meta):
   4208             bar = 7
   4209         """
   4210         self.check(b, a)
   4211 
   4212         b = """
   4213         class X:
   4214             __metaclass__ = Meta; x = 4; g = 23
   4215         """
   4216         a = """
   4217         class X(metaclass=Meta):
   4218             x = 4; g = 23
   4219         """
   4220         self.check(b, a)
   4221 
   4222         # one parent, simple body, __metaclass__ last
   4223         b = """
   4224         class X(object):
   4225             bar = 7
   4226             __metaclass__ = Meta
   4227         """
   4228         a = """
   4229         class X(object, metaclass=Meta):
   4230             bar = 7
   4231         """
   4232         self.check(b, a)
   4233 
   4234         # redefining __metaclass__
   4235         b = """
   4236         class X():
   4237             __metaclass__ = A
   4238             __metaclass__ = B
   4239             bar = 7
   4240         """
   4241         a = """
   4242         class X(metaclass=B):
   4243             bar = 7
   4244         """
   4245         self.check(b, a)
   4246 
   4247         # multiple inheritance, simple body
   4248         b = """
   4249         class X(clsA, clsB):
   4250             __metaclass__ = Meta
   4251             bar = 7
   4252         """
   4253         a = """
   4254         class X(clsA, clsB, metaclass=Meta):
   4255             bar = 7
   4256         """
   4257         self.check(b, a)
   4258 
   4259         # keywords in the class statement
   4260         b = """class m(a, arg=23): __metaclass__ = Meta"""
   4261         a = """class m(a, arg=23, metaclass=Meta): pass"""
   4262         self.check(b, a)
   4263 
   4264         b = """
   4265         class X(expression(2 + 4)):
   4266             __metaclass__ = Meta
   4267         """
   4268         a = """
   4269         class X(expression(2 + 4), metaclass=Meta):
   4270             pass
   4271         """
   4272         self.check(b, a)
   4273 
   4274         b = """
   4275         class X(expression(2 + 4), x**4):
   4276             __metaclass__ = Meta
   4277         """
   4278         a = """
   4279         class X(expression(2 + 4), x**4, metaclass=Meta):
   4280             pass
   4281         """
   4282         self.check(b, a)
   4283 
   4284         b = """
   4285         class X:
   4286             __metaclass__ = Meta
   4287             save.py = 23
   4288         """
   4289         a = """
   4290         class X(metaclass=Meta):
   4291             save.py = 23
   4292         """
   4293         self.check(b, a)
   4294 
   4295 
   4296 class Test_getcwdu(FixerTestCase):
   4297 
   4298     fixer = 'getcwdu'
   4299 
   4300     def test_basic(self):
   4301         b = """os.getcwdu"""
   4302         a = """os.getcwd"""
   4303         self.check(b, a)
   4304 
   4305         b = """os.getcwdu()"""
   4306         a = """os.getcwd()"""
   4307         self.check(b, a)
   4308 
   4309         b = """meth = os.getcwdu"""
   4310         a = """meth = os.getcwd"""
   4311         self.check(b, a)
   4312 
   4313         b = """os.getcwdu(args)"""
   4314         a = """os.getcwd(args)"""
   4315         self.check(b, a)
   4316 
   4317     def test_comment(self):
   4318         b = """os.getcwdu() # Foo"""
   4319         a = """os.getcwd() # Foo"""
   4320         self.check(b, a)
   4321 
   4322     def test_unchanged(self):
   4323         s = """os.getcwd()"""
   4324         self.unchanged(s)
   4325 
   4326         s = """getcwdu()"""
   4327         self.unchanged(s)
   4328 
   4329         s = """os.getcwdb()"""
   4330         self.unchanged(s)
   4331 
   4332     def test_indentation(self):
   4333         b = """
   4334             if 1:
   4335                 os.getcwdu()
   4336             """
   4337         a = """
   4338             if 1:
   4339                 os.getcwd()
   4340             """
   4341         self.check(b, a)
   4342 
   4343     def test_multilation(self):
   4344         b = """os .getcwdu()"""
   4345         a = """os .getcwd()"""
   4346         self.check(b, a)
   4347 
   4348         b = """os.  getcwdu"""
   4349         a = """os.  getcwd"""
   4350         self.check(b, a)
   4351 
   4352         b = """os.getcwdu (  )"""
   4353         a = """os.getcwd (  )"""
   4354         self.check(b, a)
   4355 
   4356 
   4357 class Test_operator(FixerTestCase):
   4358 
   4359     fixer = "operator"
   4360 
   4361     def test_operator_isCallable(self):
   4362         b = "operator.isCallable(x)"
   4363         a = "hasattr(x, '__call__')"
   4364         self.check(b, a)
   4365 
   4366     def test_operator_sequenceIncludes(self):
   4367         b = "operator.sequenceIncludes(x, y)"
   4368         a = "operator.contains(x, y)"
   4369         self.check(b, a)
   4370 
   4371         b = "operator .sequenceIncludes(x, y)"
   4372         a = "operator .contains(x, y)"
   4373         self.check(b, a)
   4374 
   4375         b = "operator.  sequenceIncludes(x, y)"
   4376         a = "operator.  contains(x, y)"
   4377         self.check(b, a)
   4378 
   4379     def test_operator_isSequenceType(self):
   4380         b = "operator.isSequenceType(x)"
   4381         a = "import collections\nisinstance(x, collections.Sequence)"
   4382         self.check(b, a)
   4383 
   4384     def test_operator_isMappingType(self):
   4385         b = "operator.isMappingType(x)"
   4386         a = "import collections\nisinstance(x, collections.Mapping)"
   4387         self.check(b, a)
   4388 
   4389     def test_operator_isNumberType(self):
   4390         b = "operator.isNumberType(x)"
   4391         a = "import numbers\nisinstance(x, numbers.Number)"
   4392         self.check(b, a)
   4393 
   4394     def test_operator_repeat(self):
   4395         b = "operator.repeat(x, n)"
   4396         a = "operator.mul(x, n)"
   4397         self.check(b, a)
   4398 
   4399         b = "operator .repeat(x, n)"
   4400         a = "operator .mul(x, n)"
   4401         self.check(b, a)
   4402 
   4403         b = "operator.  repeat(x, n)"
   4404         a = "operator.  mul(x, n)"
   4405         self.check(b, a)
   4406 
   4407     def test_operator_irepeat(self):
   4408         b = "operator.irepeat(x, n)"
   4409         a = "operator.imul(x, n)"
   4410         self.check(b, a)
   4411 
   4412         b = "operator .irepeat(x, n)"
   4413         a = "operator .imul(x, n)"
   4414         self.check(b, a)
   4415 
   4416         b = "operator.  irepeat(x, n)"
   4417         a = "operator.  imul(x, n)"
   4418         self.check(b, a)
   4419 
   4420     def test_bare_isCallable(self):
   4421         s = "isCallable(x)"
   4422         t = "You should use 'hasattr(x, '__call__')' here."
   4423         self.warns_unchanged(s, t)
   4424 
   4425     def test_bare_sequenceIncludes(self):
   4426         s = "sequenceIncludes(x, y)"
   4427         t = "You should use 'operator.contains(x, y)' here."
   4428         self.warns_unchanged(s, t)
   4429 
   4430     def test_bare_operator_isSequenceType(self):
   4431         s = "isSequenceType(z)"
   4432         t = "You should use 'isinstance(z, collections.Sequence)' here."
   4433         self.warns_unchanged(s, t)
   4434 
   4435     def test_bare_operator_isMappingType(self):
   4436         s = "isMappingType(x)"
   4437         t = "You should use 'isinstance(x, collections.Mapping)' here."
   4438         self.warns_unchanged(s, t)
   4439 
   4440     def test_bare_operator_isNumberType(self):
   4441         s = "isNumberType(y)"
   4442         t = "You should use 'isinstance(y, numbers.Number)' here."
   4443         self.warns_unchanged(s, t)
   4444 
   4445     def test_bare_operator_repeat(self):
   4446         s = "repeat(x, n)"
   4447         t = "You should use 'operator.mul(x, n)' here."
   4448         self.warns_unchanged(s, t)
   4449 
   4450     def test_bare_operator_irepeat(self):
   4451         s = "irepeat(y, 187)"
   4452         t = "You should use 'operator.imul(y, 187)' here."
   4453         self.warns_unchanged(s, t)
   4454 
   4455 
   4456 class Test_exitfunc(FixerTestCase):
   4457 
   4458     fixer = "exitfunc"
   4459 
   4460     def test_simple(self):
   4461         b = """
   4462             import sys
   4463             sys.exitfunc = my_atexit
   4464             """
   4465         a = """
   4466             import sys
   4467             import atexit
   4468             atexit.register(my_atexit)
   4469             """
   4470         self.check(b, a)
   4471 
   4472     def test_names_import(self):
   4473         b = """
   4474             import sys, crumbs
   4475             sys.exitfunc = my_func
   4476             """
   4477         a = """
   4478             import sys, crumbs, atexit
   4479             atexit.register(my_func)
   4480             """
   4481         self.check(b, a)
   4482 
   4483     def test_complex_expression(self):
   4484         b = """
   4485             import sys
   4486             sys.exitfunc = do(d)/a()+complex(f=23, g=23)*expression
   4487             """
   4488         a = """
   4489             import sys
   4490             import atexit
   4491             atexit.register(do(d)/a()+complex(f=23, g=23)*expression)
   4492             """
   4493         self.check(b, a)
   4494 
   4495     def test_comments(self):
   4496         b = """
   4497             import sys # Foo
   4498             sys.exitfunc = f # Blah
   4499             """
   4500         a = """
   4501             import sys
   4502             import atexit # Foo
   4503             atexit.register(f) # Blah
   4504             """
   4505         self.check(b, a)
   4506 
   4507         b = """
   4508             import apples, sys, crumbs, larry # Pleasant comments
   4509             sys.exitfunc = func
   4510             """
   4511         a = """
   4512             import apples, sys, crumbs, larry, atexit # Pleasant comments
   4513             atexit.register(func)
   4514             """
   4515         self.check(b, a)
   4516 
   4517     def test_in_a_function(self):
   4518         b = """
   4519             import sys
   4520             def f():
   4521                 sys.exitfunc = func
   4522             """
   4523         a = """
   4524             import sys
   4525             import atexit
   4526             def f():
   4527                 atexit.register(func)
   4528              """
   4529         self.check(b, a)
   4530 
   4531     def test_no_sys_import(self):
   4532         b = """sys.exitfunc = f"""
   4533         a = """atexit.register(f)"""
   4534         msg = ("Can't find sys import; Please add an atexit import at the "
   4535             "top of your file.")
   4536         self.warns(b, a, msg)
   4537 
   4538 
   4539     def test_unchanged(self):
   4540         s = """f(sys.exitfunc)"""
   4541         self.unchanged(s)
   4542