1""" Test suite for the fixer modules """
2
3# Python imports
4import os
5import unittest
6from itertools import chain
7from operator import itemgetter
8
9# Local imports
10from lib2to3 import pygram, pytree, refactor, fixer_util
11from lib2to3.tests import support
12
13
14class 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
71class 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
98class 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
156class 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
285class 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
334class 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
369class 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
475class 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
526class 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
564class 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
839class 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
966class 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
1091class 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
1137class 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
1175class 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
1203class 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
1433class 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
1505class 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
1513reduce(x, range(5))"""
1514        self.check(b, a)
1515
1516class 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
1563class 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
1589class 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
1632class 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
1730class 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
1751class Test_imports2(FixerTestCase, ImportsFixerTests):
1752    fixer = "imports2"
1753    from ..fixes.fix_imports2 import MAPPING as modules
1754
1755
1756class 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
1772class 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 = """
1831def foo():
1832    from urllib import urlencode, urlopen
1833"""
1834        a = """
1835def foo():
1836    from urllib.parse import urlencode
1837    from urllib.request import urlopen
1838"""
1839        self.check(b, a)
1840
1841        b = """
1842def foo():
1843    other()
1844    from urllib import urlencode, urlopen
1845"""
1846        a = """
1847def 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
1882class 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
1929class 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
2116class 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
2148class 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
2622class 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
2674class 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
2740class 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
2786class 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
2827class 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
2837if     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 = """
2847def spam(foo):
2848    callable(foo)"""[1:]
2849        a = """
2850import collections
2851def spam(foo):
2852    isinstance(foo, collections.Callable)"""[1:]
2853        self.check(b, a)
2854
2855        b = """
2856import collections
2857def spam(foo):
2858    callable(foo)"""[1:]
2859        # same output if it was already imported
2860        self.check(b, a)
2861
2862        b = """
2863from collections import *
2864def spam(foo):
2865    callable(foo)"""[1:]
2866        a = """
2867from collections import *
2868import collections
2869def spam(foo):
2870    isinstance(foo, collections.Callable)"""[1:]
2871        self.check(b, a)
2872
2873        b = """
2874do_stuff()
2875do_some_other_stuff()
2876assert callable(do_stuff)"""[1:]
2877        a = """
2878import collections
2879do_stuff()
2880do_some_other_stuff()
2881assert isinstance(do_stuff, collections.Callable)"""[1:]
2882        self.check(b, a)
2883
2884        b = """
2885if 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)
2892else:
2893    assert not callable(do_stuff)"""[1:]
2894        a = """
2895import collections
2896if 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)
2903else:
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
2920class 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 = """for i in filter(f, 'abc'): pass"""
2985        self.unchanged(a)
2986        a = """[x for x in filter(f, 'abc')]"""
2987        self.unchanged(a)
2988        a = """(x for x in filter(f, 'abc'))"""
2989        self.unchanged(a)
2990
2991    def test_future_builtins(self):
2992        a = "from future_builtins import spam, filter; filter(f, 'ham')"
2993        self.unchanged(a)
2994
2995        b = """from future_builtins import spam; x = filter(f, 'abc')"""
2996        a = """from future_builtins import spam; x = list(filter(f, 'abc'))"""
2997        self.check(b, a)
2998
2999        a = "from future_builtins import *; filter(f, 'ham')"
3000        self.unchanged(a)
3001
3002class Test_map(FixerTestCase):
3003    fixer = "map"
3004
3005    def check(self, b, a):
3006        self.unchanged("from future_builtins import map; " + b, a)
3007        super(Test_map, self).check(b, a)
3008
3009    def test_prefix_preservation(self):
3010        b = """x =    map(   f,    'abc'   )"""
3011        a = """x =    list(map(   f,    'abc'   ))"""
3012        self.check(b, a)
3013
3014    def test_trailing_comment(self):
3015        b = """x = map(f, 'abc')   #   foo"""
3016        a = """x = list(map(f, 'abc'))   #   foo"""
3017        self.check(b, a)
3018
3019    def test_None_with_multiple_arguments(self):
3020        s = """x = map(None, a, b, c)"""
3021        self.warns_unchanged(s, "cannot convert map(None, ...) with "
3022                             "multiple arguments")
3023
3024    def test_map_basic(self):
3025        b = """x = map(f, 'abc')"""
3026        a = """x = list(map(f, 'abc'))"""
3027        self.check(b, a)
3028
3029        b = """x = len(map(f, 'abc', 'def'))"""
3030        a = """x = len(list(map(f, 'abc', 'def')))"""
3031        self.check(b, a)
3032
3033        b = """x = map(None, 'abc')"""
3034        a = """x = list('abc')"""
3035        self.check(b, a)
3036
3037        b = """x = map(lambda x: x+1, range(4))"""
3038        a = """x = [x+1 for x in range(4)]"""
3039        self.check(b, a)
3040
3041        # Note the parens around x
3042        b = """x = map(lambda (x): x+1, range(4))"""
3043        a = """x = [x+1 for x in range(4)]"""
3044        self.check(b, a)
3045
3046        b = """
3047            foo()
3048            # foo
3049            map(f, x)
3050            """
3051        a = """
3052            foo()
3053            # foo
3054            list(map(f, x))
3055            """
3056        self.warns(b, a, "You should use a for loop here")
3057
3058        # XXX This (rare) case is not supported
3059##         b = """x = map(f, 'abc')[0]"""
3060##         a = """x = list(map(f, 'abc'))[0]"""
3061##         self.check(b, a)
3062
3063    def test_map_nochange(self):
3064        a = """b.join(map(f, 'abc'))"""
3065        self.unchanged(a)
3066        a = """(a + foo(5)).join(map(f, 'abc'))"""
3067        self.unchanged(a)
3068        a = """iter(map(f, 'abc'))"""
3069        self.unchanged(a)
3070        a = """list(map(f, 'abc'))"""
3071        self.unchanged(a)
3072        a = """list(map(f, 'abc'))[0]"""
3073        self.unchanged(a)
3074        a = """set(map(f, 'abc'))"""
3075        self.unchanged(a)
3076        a = """set(map(f, 'abc')).pop()"""
3077        self.unchanged(a)
3078        a = """tuple(map(f, 'abc'))"""
3079        self.unchanged(a)
3080        a = """any(map(f, 'abc'))"""
3081        self.unchanged(a)
3082        a = """all(map(f, 'abc'))"""
3083        self.unchanged(a)
3084        a = """sum(map(f, 'abc'))"""
3085        self.unchanged(a)
3086        a = """sorted(map(f, 'abc'))"""
3087        self.unchanged(a)
3088        a = """sorted(map(f, 'abc'), key=blah)"""
3089        self.unchanged(a)
3090        a = """sorted(map(f, 'abc'), key=blah)[0]"""
3091        self.unchanged(a)
3092        a = """for i in map(f, 'abc'): pass"""
3093        self.unchanged(a)
3094        a = """[x for x in map(f, 'abc')]"""
3095        self.unchanged(a)
3096        a = """(x for x in map(f, 'abc'))"""
3097        self.unchanged(a)
3098
3099    def test_future_builtins(self):
3100        a = "from future_builtins import spam, map, eggs; map(f, 'ham')"
3101        self.unchanged(a)
3102
3103        b = """from future_builtins import spam, eggs; x = map(f, 'abc')"""
3104        a = """from future_builtins import spam, eggs; x = list(map(f, 'abc'))"""
3105        self.check(b, a)
3106
3107        a = "from future_builtins import *; map(f, 'ham')"
3108        self.unchanged(a)
3109
3110class Test_zip(FixerTestCase):
3111    fixer = "zip"
3112
3113    def check(self, b, a):
3114        self.unchanged("from future_builtins import zip; " + b, a)
3115        super(Test_zip, self).check(b, a)
3116
3117    def test_zip_basic(self):
3118        b = """x = zip(a, b, c)"""
3119        a = """x = list(zip(a, b, c))"""
3120        self.check(b, a)
3121
3122        b = """x = len(zip(a, b))"""
3123        a = """x = len(list(zip(a, b)))"""
3124        self.check(b, a)
3125
3126    def test_zip_nochange(self):
3127        a = """b.join(zip(a, b))"""
3128        self.unchanged(a)
3129        a = """(a + foo(5)).join(zip(a, b))"""
3130        self.unchanged(a)
3131        a = """iter(zip(a, b))"""
3132        self.unchanged(a)
3133        a = """list(zip(a, b))"""
3134        self.unchanged(a)
3135        a = """list(zip(a, b))[0]"""
3136        self.unchanged(a)
3137        a = """set(zip(a, b))"""
3138        self.unchanged(a)
3139        a = """set(zip(a, b)).pop()"""
3140        self.unchanged(a)
3141        a = """tuple(zip(a, b))"""
3142        self.unchanged(a)
3143        a = """any(zip(a, b))"""
3144        self.unchanged(a)
3145        a = """all(zip(a, b))"""
3146        self.unchanged(a)
3147        a = """sum(zip(a, b))"""
3148        self.unchanged(a)
3149        a = """sorted(zip(a, b))"""
3150        self.unchanged(a)
3151        a = """sorted(zip(a, b), key=blah)"""
3152        self.unchanged(a)
3153        a = """sorted(zip(a, b), key=blah)[0]"""
3154        self.unchanged(a)
3155        a = """for i in zip(a, b): pass"""
3156        self.unchanged(a)
3157        a = """[x for x in zip(a, b)]"""
3158        self.unchanged(a)
3159        a = """(x for x in zip(a, b))"""
3160        self.unchanged(a)
3161
3162    def test_future_builtins(self):
3163        a = "from future_builtins import spam, zip, eggs; zip(a, b)"
3164        self.unchanged(a)
3165
3166        b = """from future_builtins import spam, eggs; x = zip(a, b)"""
3167        a = """from future_builtins import spam, eggs; x = list(zip(a, b))"""
3168        self.check(b, a)
3169
3170        a = "from future_builtins import *; zip(a, b)"
3171        self.unchanged(a)
3172
3173class Test_standarderror(FixerTestCase):
3174    fixer = "standarderror"
3175
3176    def test(self):
3177        b = """x =    StandardError()"""
3178        a = """x =    Exception()"""
3179        self.check(b, a)
3180
3181        b = """x = StandardError(a, b, c)"""
3182        a = """x = Exception(a, b, c)"""
3183        self.check(b, a)
3184
3185        b = """f(2 + StandardError(a, b, c))"""
3186        a = """f(2 + Exception(a, b, c))"""
3187        self.check(b, a)
3188
3189class Test_types(FixerTestCase):
3190    fixer = "types"
3191
3192    def test_basic_types_convert(self):
3193        b = """types.StringType"""
3194        a = """bytes"""
3195        self.check(b, a)
3196
3197        b = """types.DictType"""
3198        a = """dict"""
3199        self.check(b, a)
3200
3201        b = """types . IntType"""
3202        a = """int"""
3203        self.check(b, a)
3204
3205        b = """types.ListType"""
3206        a = """list"""
3207        self.check(b, a)
3208
3209        b = """types.LongType"""
3210        a = """int"""
3211        self.check(b, a)
3212
3213        b = """types.NoneType"""
3214        a = """type(None)"""
3215        self.check(b, a)
3216
3217class Test_idioms(FixerTestCase):
3218    fixer = "idioms"
3219
3220    def test_while(self):
3221        b = """while 1: foo()"""
3222        a = """while True: foo()"""
3223        self.check(b, a)
3224
3225        b = """while   1: foo()"""
3226        a = """while   True: foo()"""
3227        self.check(b, a)
3228
3229        b = """
3230            while 1:
3231                foo()
3232            """
3233        a = """
3234            while True:
3235                foo()
3236            """
3237        self.check(b, a)
3238
3239    def test_while_unchanged(self):
3240        s = """while 11: foo()"""
3241        self.unchanged(s)
3242
3243        s = """while 0: foo()"""
3244        self.unchanged(s)
3245
3246        s = """while foo(): foo()"""
3247        self.unchanged(s)
3248
3249        s = """while []: foo()"""
3250        self.unchanged(s)
3251
3252    def test_eq_simple(self):
3253        b = """type(x) == T"""
3254        a = """isinstance(x, T)"""
3255        self.check(b, a)
3256
3257        b = """if   type(x) == T: pass"""
3258        a = """if   isinstance(x, T): pass"""
3259        self.check(b, a)
3260
3261    def test_eq_reverse(self):
3262        b = """T == type(x)"""
3263        a = """isinstance(x, T)"""
3264        self.check(b, a)
3265
3266        b = """if   T == type(x): pass"""
3267        a = """if   isinstance(x, T): pass"""
3268        self.check(b, a)
3269
3270    def test_eq_expression(self):
3271        b = """type(x+y) == d.get('T')"""
3272        a = """isinstance(x+y, d.get('T'))"""
3273        self.check(b, a)
3274
3275        b = """type(   x  +  y) == d.get('T')"""
3276        a = """isinstance(x  +  y, d.get('T'))"""
3277        self.check(b, a)
3278
3279    def test_is_simple(self):
3280        b = """type(x) is T"""
3281        a = """isinstance(x, T)"""
3282        self.check(b, a)
3283
3284        b = """if   type(x) is T: pass"""
3285        a = """if   isinstance(x, T): pass"""
3286        self.check(b, a)
3287
3288    def test_is_reverse(self):
3289        b = """T is type(x)"""
3290        a = """isinstance(x, T)"""
3291        self.check(b, a)
3292
3293        b = """if   T is type(x): pass"""
3294        a = """if   isinstance(x, T): pass"""
3295        self.check(b, a)
3296
3297    def test_is_expression(self):
3298        b = """type(x+y) is d.get('T')"""
3299        a = """isinstance(x+y, d.get('T'))"""
3300        self.check(b, a)
3301
3302        b = """type(   x  +  y) is d.get('T')"""
3303        a = """isinstance(x  +  y, d.get('T'))"""
3304        self.check(b, a)
3305
3306    def test_is_not_simple(self):
3307        b = """type(x) is not T"""
3308        a = """not isinstance(x, T)"""
3309        self.check(b, a)
3310
3311        b = """if   type(x) is not T: pass"""
3312        a = """if   not isinstance(x, T): pass"""
3313        self.check(b, a)
3314
3315    def test_is_not_reverse(self):
3316        b = """T is not type(x)"""
3317        a = """not isinstance(x, T)"""
3318        self.check(b, a)
3319
3320        b = """if   T is not type(x): pass"""
3321        a = """if   not isinstance(x, T): pass"""
3322        self.check(b, a)
3323
3324    def test_is_not_expression(self):
3325        b = """type(x+y) is not d.get('T')"""
3326        a = """not isinstance(x+y, d.get('T'))"""
3327        self.check(b, a)
3328
3329        b = """type(   x  +  y) is not d.get('T')"""
3330        a = """not isinstance(x  +  y, d.get('T'))"""
3331        self.check(b, a)
3332
3333    def test_ne_simple(self):
3334        b = """type(x) != T"""
3335        a = """not isinstance(x, T)"""
3336        self.check(b, a)
3337
3338        b = """if   type(x) != T: pass"""
3339        a = """if   not isinstance(x, T): pass"""
3340        self.check(b, a)
3341
3342    def test_ne_reverse(self):
3343        b = """T != type(x)"""
3344        a = """not isinstance(x, T)"""
3345        self.check(b, a)
3346
3347        b = """if   T != type(x): pass"""
3348        a = """if   not isinstance(x, T): pass"""
3349        self.check(b, a)
3350
3351    def test_ne_expression(self):
3352        b = """type(x+y) != d.get('T')"""
3353        a = """not isinstance(x+y, d.get('T'))"""
3354        self.check(b, a)
3355
3356        b = """type(   x  +  y) != d.get('T')"""
3357        a = """not isinstance(x  +  y, d.get('T'))"""
3358        self.check(b, a)
3359
3360    def test_type_unchanged(self):
3361        a = """type(x).__name__"""
3362        self.unchanged(a)
3363
3364    def test_sort_list_call(self):
3365        b = """
3366            v = list(t)
3367            v.sort()
3368            foo(v)
3369            """
3370        a = """
3371            v = sorted(t)
3372            foo(v)
3373            """
3374        self.check(b, a)
3375
3376        b = """
3377            v = list(foo(b) + d)
3378            v.sort()
3379            foo(v)
3380            """
3381        a = """
3382            v = sorted(foo(b) + d)
3383            foo(v)
3384            """
3385        self.check(b, a)
3386
3387        b = """
3388            while x:
3389                v = list(t)
3390                v.sort()
3391                foo(v)
3392            """
3393        a = """
3394            while x:
3395                v = sorted(t)
3396                foo(v)
3397            """
3398        self.check(b, a)
3399
3400        b = """
3401            v = list(t)
3402            # foo
3403            v.sort()
3404            foo(v)
3405            """
3406        a = """
3407            v = sorted(t)
3408            # foo
3409            foo(v)
3410            """
3411        self.check(b, a)
3412
3413        b = r"""
3414            v = list(   t)
3415            v.sort()
3416            foo(v)
3417            """
3418        a = r"""
3419            v = sorted(   t)
3420            foo(v)
3421            """
3422        self.check(b, a)
3423
3424        b = r"""
3425            try:
3426                m = list(s)
3427                m.sort()
3428            except: pass
3429            """
3430
3431        a = r"""
3432            try:
3433                m = sorted(s)
3434            except: pass
3435            """
3436        self.check(b, a)
3437
3438        b = r"""
3439            try:
3440                m = list(s)
3441                # foo
3442                m.sort()
3443            except: pass
3444            """
3445
3446        a = r"""
3447            try:
3448                m = sorted(s)
3449                # foo
3450            except: pass
3451            """
3452        self.check(b, a)
3453
3454        b = r"""
3455            m = list(s)
3456            # more comments
3457            m.sort()"""
3458
3459        a = r"""
3460            m = sorted(s)
3461            # more comments"""
3462        self.check(b, a)
3463
3464    def test_sort_simple_expr(self):
3465        b = """
3466            v = t
3467            v.sort()
3468            foo(v)
3469            """
3470        a = """
3471            v = sorted(t)
3472            foo(v)
3473            """
3474        self.check(b, a)
3475
3476        b = """
3477            v = foo(b)
3478            v.sort()
3479            foo(v)
3480            """
3481        a = """
3482            v = sorted(foo(b))
3483            foo(v)
3484            """
3485        self.check(b, a)
3486
3487        b = """
3488            v = b.keys()
3489            v.sort()
3490            foo(v)
3491            """
3492        a = """
3493            v = sorted(b.keys())
3494            foo(v)
3495            """
3496        self.check(b, a)
3497
3498        b = """
3499            v = foo(b) + d
3500            v.sort()
3501            foo(v)
3502            """
3503        a = """
3504            v = sorted(foo(b) + d)
3505            foo(v)
3506            """
3507        self.check(b, a)
3508
3509        b = """
3510            while x:
3511                v = t
3512                v.sort()
3513                foo(v)
3514            """
3515        a = """
3516            while x:
3517                v = sorted(t)
3518                foo(v)
3519            """
3520        self.check(b, a)
3521
3522        b = """
3523            v = t
3524            # foo
3525            v.sort()
3526            foo(v)
3527            """
3528        a = """
3529            v = sorted(t)
3530            # foo
3531            foo(v)
3532            """
3533        self.check(b, a)
3534
3535        b = r"""
3536            v =   t
3537            v.sort()
3538            foo(v)
3539            """
3540        a = r"""
3541            v =   sorted(t)
3542            foo(v)
3543            """
3544        self.check(b, a)
3545
3546    def test_sort_unchanged(self):
3547        s = """
3548            v = list(t)
3549            w.sort()
3550            foo(w)
3551            """
3552        self.unchanged(s)
3553
3554        s = """
3555            v = list(t)
3556            v.sort(u)
3557            foo(v)
3558            """
3559        self.unchanged(s)
3560
3561class Test_basestring(FixerTestCase):
3562    fixer = "basestring"
3563
3564    def test_basestring(self):
3565        b = """isinstance(x, basestring)"""
3566        a = """isinstance(x, str)"""
3567        self.check(b, a)
3568
3569class Test_buffer(FixerTestCase):
3570    fixer = "buffer"
3571
3572    def test_buffer(self):
3573        b = """x = buffer(y)"""
3574        a = """x = memoryview(y)"""
3575        self.check(b, a)
3576
3577    def test_slicing(self):
3578        b = """buffer(y)[4:5]"""
3579        a = """memoryview(y)[4:5]"""
3580        self.check(b, a)
3581
3582class Test_future(FixerTestCase):
3583    fixer = "future"
3584
3585    def test_future(self):
3586        b = """from __future__ import braces"""
3587        a = """"""
3588        self.check(b, a)
3589
3590        b = """# comment\nfrom __future__ import braces"""
3591        a = """# comment\n"""
3592        self.check(b, a)
3593
3594        b = """from __future__ import braces\n# comment"""
3595        a = """\n# comment"""
3596        self.check(b, a)
3597
3598    def test_run_order(self):
3599        self.assert_runs_after('print')
3600
3601class Test_itertools(FixerTestCase):
3602    fixer = "itertools"
3603
3604    def checkall(self, before, after):
3605        # Because we need to check with and without the itertools prefix
3606        # and on each of the three functions, these loops make it all
3607        # much easier
3608        for i in ('itertools.', ''):
3609            for f in ('map', 'filter', 'zip'):
3610                b = before %(i+'i'+f)
3611                a = after %(f)
3612                self.check(b, a)
3613
3614    def test_0(self):
3615        # A simple example -- test_1 covers exactly the same thing,
3616        # but it's not quite as clear.
3617        b = "itertools.izip(a, b)"
3618        a = "zip(a, b)"
3619        self.check(b, a)
3620
3621    def test_1(self):
3622        b = """%s(f, a)"""
3623        a = """%s(f, a)"""
3624        self.checkall(b, a)
3625
3626    def test_qualified(self):
3627        b = """itertools.ifilterfalse(a, b)"""
3628        a = """itertools.filterfalse(a, b)"""
3629        self.check(b, a)
3630
3631        b = """itertools.izip_longest(a, b)"""
3632        a = """itertools.zip_longest(a, b)"""
3633        self.check(b, a)
3634
3635    def test_2(self):
3636        b = """ifilterfalse(a, b)"""
3637        a = """filterfalse(a, b)"""
3638        self.check(b, a)
3639
3640        b = """izip_longest(a, b)"""
3641        a = """zip_longest(a, b)"""
3642        self.check(b, a)
3643
3644    def test_space_1(self):
3645        b = """    %s(f, a)"""
3646        a = """    %s(f, a)"""
3647        self.checkall(b, a)
3648
3649    def test_space_2(self):
3650        b = """    itertools.ifilterfalse(a, b)"""
3651        a = """    itertools.filterfalse(a, b)"""
3652        self.check(b, a)
3653
3654        b = """    itertools.izip_longest(a, b)"""
3655        a = """    itertools.zip_longest(a, b)"""
3656        self.check(b, a)
3657
3658    def test_run_order(self):
3659        self.assert_runs_after('map', 'zip', 'filter')
3660
3661
3662class Test_itertools_imports(FixerTestCase):
3663    fixer = 'itertools_imports'
3664
3665    def test_reduced(self):
3666        b = "from itertools import imap, izip, foo"
3667        a = "from itertools import foo"
3668        self.check(b, a)
3669
3670        b = "from itertools import bar, imap, izip, foo"
3671        a = "from itertools import bar, foo"
3672        self.check(b, a)
3673
3674        b = "from itertools import chain, imap, izip"
3675        a = "from itertools import chain"
3676        self.check(b, a)
3677
3678    def test_comments(self):
3679        b = "#foo\nfrom itertools import imap, izip"
3680        a = "#foo\n"
3681        self.check(b, a)
3682
3683    def test_none(self):
3684        b = "from itertools import imap, izip"
3685        a = ""
3686        self.check(b, a)
3687
3688        b = "from itertools import izip"
3689        a = ""
3690        self.check(b, a)
3691
3692    def test_import_as(self):
3693        b = "from itertools import izip, bar as bang, imap"
3694        a = "from itertools import bar as bang"
3695        self.check(b, a)
3696
3697        b = "from itertools import izip as _zip, imap, bar"
3698        a = "from itertools import bar"
3699        self.check(b, a)
3700
3701        b = "from itertools import imap as _map"
3702        a = ""
3703        self.check(b, a)
3704
3705        b = "from itertools import imap as _map, izip as _zip"
3706        a = ""
3707        self.check(b, a)
3708
3709        s = "from itertools import bar as bang"
3710        self.unchanged(s)
3711
3712    def test_ifilter_and_zip_longest(self):
3713        for name in "filterfalse", "zip_longest":
3714            b = "from itertools import i%s" % (name,)
3715            a = "from itertools import %s" % (name,)
3716            self.check(b, a)
3717
3718            b = "from itertools import imap, i%s, foo" % (name,)
3719            a = "from itertools import %s, foo" % (name,)
3720            self.check(b, a)
3721
3722            b = "from itertools import bar, i%s, foo" % (name,)
3723            a = "from itertools import bar, %s, foo" % (name,)
3724            self.check(b, a)
3725
3726    def test_import_star(self):
3727        s = "from itertools import *"
3728        self.unchanged(s)
3729
3730
3731    def test_unchanged(self):
3732        s = "from itertools import foo"
3733        self.unchanged(s)
3734
3735
3736class Test_import(FixerTestCase):
3737    fixer = "import"
3738
3739    def setUp(self):
3740        super(Test_import, self).setUp()
3741        # Need to replace fix_import's exists method
3742        # so we can check that it's doing the right thing
3743        self.files_checked = []
3744        self.present_files = set()
3745        self.always_exists = True
3746        def fake_exists(name):
3747            self.files_checked.append(name)
3748            return self.always_exists or (name in self.present_files)
3749
3750        from lib2to3.fixes import fix_import
3751        fix_import.exists = fake_exists
3752
3753    def tearDown(self):
3754        from lib2to3.fixes import fix_import
3755        fix_import.exists = os.path.exists
3756
3757    def check_both(self, b, a):
3758        self.always_exists = True
3759        super(Test_import, self).check(b, a)
3760        self.always_exists = False
3761        super(Test_import, self).unchanged(b)
3762
3763    def test_files_checked(self):
3764        def p(path):
3765            # Takes a unix path and returns a path with correct separators
3766            return os.path.pathsep.join(path.split("/"))
3767
3768        self.always_exists = False
3769        self.present_files = set(['__init__.py'])
3770        expected_extensions = ('.py', os.path.sep, '.pyc', '.so', '.sl', '.pyd')
3771        names_to_test = (p("/spam/eggs.py"), "ni.py", p("../../shrubbery.py"))
3772
3773        for name in names_to_test:
3774            self.files_checked = []
3775            self.filename = name
3776            self.unchanged("import jam")
3777
3778            if os.path.dirname(name):
3779                name = os.path.dirname(name) + '/jam'
3780            else:
3781                name = 'jam'
3782            expected_checks = set(name + ext for ext in expected_extensions)
3783            expected_checks.add("__init__.py")
3784
3785            self.assertEqual(set(self.files_checked), expected_checks)
3786
3787    def test_not_in_package(self):
3788        s = "import bar"
3789        self.always_exists = False
3790        self.present_files = set(["bar.py"])
3791        self.unchanged(s)
3792
3793    def test_with_absolute_import_enabled(self):
3794        s = "from __future__ import absolute_import\nimport bar"
3795        self.always_exists = False
3796        self.present_files = set(["__init__.py", "bar.py"])
3797        self.unchanged(s)
3798
3799    def test_in_package(self):
3800        b = "import bar"
3801        a = "from . import bar"
3802        self.always_exists = False
3803        self.present_files = set(["__init__.py", "bar.py"])
3804        self.check(b, a)
3805
3806    def test_import_from_package(self):
3807        b = "import bar"
3808        a = "from . import bar"
3809        self.always_exists = False
3810        self.present_files = set(["__init__.py", "bar" + os.path.sep])
3811        self.check(b, a)
3812
3813    def test_already_relative_import(self):
3814        s = "from . import bar"
3815        self.unchanged(s)
3816
3817    def test_comments_and_indent(self):
3818        b = "import bar # Foo"
3819        a = "from . import bar # Foo"
3820        self.check(b, a)
3821
3822    def test_from(self):
3823        b = "from foo import bar, baz"
3824        a = "from .foo import bar, baz"
3825        self.check_both(b, a)
3826
3827        b = "from foo import bar"
3828        a = "from .foo import bar"
3829        self.check_both(b, a)
3830
3831        b = "from foo import (bar, baz)"
3832        a = "from .foo import (bar, baz)"
3833        self.check_both(b, a)
3834
3835    def test_dotted_from(self):
3836        b = "from green.eggs import ham"
3837        a = "from .green.eggs import ham"
3838        self.check_both(b, a)
3839
3840    def test_from_as(self):
3841        b = "from green.eggs import ham as spam"
3842        a = "from .green.eggs import ham as spam"
3843        self.check_both(b, a)
3844
3845    def test_import(self):
3846        b = "import foo"
3847        a = "from . import foo"
3848        self.check_both(b, a)
3849
3850        b = "import foo, bar"
3851        a = "from . import foo, bar"
3852        self.check_both(b, a)
3853
3854        b = "import foo, bar, x"
3855        a = "from . import foo, bar, x"
3856        self.check_both(b, a)
3857
3858        b = "import x, y, z"
3859        a = "from . import x, y, z"
3860        self.check_both(b, a)
3861
3862    def test_import_as(self):
3863        b = "import foo as x"
3864        a = "from . import foo as x"
3865        self.check_both(b, a)
3866
3867        b = "import a as b, b as c, c as d"
3868        a = "from . import a as b, b as c, c as d"
3869        self.check_both(b, a)
3870
3871    def test_local_and_absolute(self):
3872        self.always_exists = False
3873        self.present_files = set(["foo.py", "__init__.py"])
3874
3875        s = "import foo, bar"
3876        self.warns_unchanged(s, "absolute and local imports together")
3877
3878    def test_dotted_import(self):
3879        b = "import foo.bar"
3880        a = "from . import foo.bar"
3881        self.check_both(b, a)
3882
3883    def test_dotted_import_as(self):
3884        b = "import foo.bar as bang"
3885        a = "from . import foo.bar as bang"
3886        self.check_both(b, a)
3887
3888    def test_prefix(self):
3889        b = """
3890        # prefix
3891        import foo.bar
3892        """
3893        a = """
3894        # prefix
3895        from . import foo.bar
3896        """
3897        self.check_both(b, a)
3898
3899
3900class Test_set_literal(FixerTestCase):
3901
3902    fixer = "set_literal"
3903
3904    def test_basic(self):
3905        b = """set([1, 2, 3])"""
3906        a = """{1, 2, 3}"""
3907        self.check(b, a)
3908
3909        b = """set((1, 2, 3))"""
3910        a = """{1, 2, 3}"""
3911        self.check(b, a)
3912
3913        b = """set((1,))"""
3914        a = """{1}"""
3915        self.check(b, a)
3916
3917        b = """set([1])"""
3918        self.check(b, a)
3919
3920        b = """set((a, b))"""
3921        a = """{a, b}"""
3922        self.check(b, a)
3923
3924        b = """set([a, b])"""
3925        self.check(b, a)
3926
3927        b = """set((a*234, f(args=23)))"""
3928        a = """{a*234, f(args=23)}"""
3929        self.check(b, a)
3930
3931        b = """set([a*23, f(23)])"""
3932        a = """{a*23, f(23)}"""
3933        self.check(b, a)
3934
3935        b = """set([a-234**23])"""
3936        a = """{a-234**23}"""
3937        self.check(b, a)
3938
3939    def test_listcomps(self):
3940        b = """set([x for x in y])"""
3941        a = """{x for x in y}"""
3942        self.check(b, a)
3943
3944        b = """set([x for x in y if x == m])"""
3945        a = """{x for x in y if x == m}"""
3946        self.check(b, a)
3947
3948        b = """set([x for x in y for a in b])"""
3949        a = """{x for x in y for a in b}"""
3950        self.check(b, a)
3951
3952        b = """set([f(x) - 23 for x in y])"""
3953        a = """{f(x) - 23 for x in y}"""
3954        self.check(b, a)
3955
3956    def test_whitespace(self):
3957        b = """set( [1, 2])"""
3958        a = """{1, 2}"""
3959        self.check(b, a)
3960
3961        b = """set([1 ,  2])"""
3962        a = """{1 ,  2}"""
3963        self.check(b, a)
3964
3965        b = """set([ 1 ])"""
3966        a = """{ 1 }"""
3967        self.check(b, a)
3968
3969        b = """set( [1] )"""
3970        a = """{1}"""
3971        self.check(b, a)
3972
3973        b = """set([  1,  2  ])"""
3974        a = """{  1,  2  }"""
3975        self.check(b, a)
3976
3977        b = """set([x  for x in y ])"""
3978        a = """{x  for x in y }"""
3979        self.check(b, a)
3980
3981        b = """set(
3982                   [1, 2]
3983               )
3984            """
3985        a = """{1, 2}\n"""
3986        self.check(b, a)
3987
3988    def test_comments(self):
3989        b = """set((1, 2)) # Hi"""
3990        a = """{1, 2} # Hi"""
3991        self.check(b, a)
3992
3993        # This isn't optimal behavior, but the fixer is optional.
3994        b = """
3995            # Foo
3996            set( # Bar
3997               (1, 2)
3998            )
3999            """
4000        a = """
4001            # Foo
4002            {1, 2}
4003            """
4004        self.check(b, a)
4005
4006    def test_unchanged(self):
4007        s = """set()"""
4008        self.unchanged(s)
4009
4010        s = """set(a)"""
4011        self.unchanged(s)
4012
4013        s = """set(a, b, c)"""
4014        self.unchanged(s)
4015
4016        # Don't transform generators because they might have to be lazy.
4017        s = """set(x for x in y)"""
4018        self.unchanged(s)
4019
4020        s = """set(x for x in y if z)"""
4021        self.unchanged(s)
4022
4023        s = """set(a*823-23**2 + f(23))"""
4024        self.unchanged(s)
4025
4026
4027class Test_sys_exc(FixerTestCase):
4028    fixer = "sys_exc"
4029
4030    def test_0(self):
4031        b = "sys.exc_type"
4032        a = "sys.exc_info()[0]"
4033        self.check(b, a)
4034
4035    def test_1(self):
4036        b = "sys.exc_value"
4037        a = "sys.exc_info()[1]"
4038        self.check(b, a)
4039
4040    def test_2(self):
4041        b = "sys.exc_traceback"
4042        a = "sys.exc_info()[2]"
4043        self.check(b, a)
4044
4045    def test_3(self):
4046        b = "sys.exc_type # Foo"
4047        a = "sys.exc_info()[0] # Foo"
4048        self.check(b, a)
4049
4050    def test_4(self):
4051        b = "sys.  exc_type"
4052        a = "sys.  exc_info()[0]"
4053        self.check(b, a)
4054
4055    def test_5(self):
4056        b = "sys  .exc_type"
4057        a = "sys  .exc_info()[0]"
4058        self.check(b, a)
4059
4060
4061class Test_paren(FixerTestCase):
4062    fixer = "paren"
4063
4064    def test_0(self):
4065        b = """[i for i in 1, 2 ]"""
4066        a = """[i for i in (1, 2) ]"""
4067        self.check(b, a)
4068
4069    def test_1(self):
4070        b = """[i for i in 1, 2, ]"""
4071        a = """[i for i in (1, 2,) ]"""
4072        self.check(b, a)
4073
4074    def test_2(self):
4075        b = """[i for i  in     1, 2 ]"""
4076        a = """[i for i  in     (1, 2) ]"""
4077        self.check(b, a)
4078
4079    def test_3(self):
4080        b = """[i for i in 1, 2 if i]"""
4081        a = """[i for i in (1, 2) if i]"""
4082        self.check(b, a)
4083
4084    def test_4(self):
4085        b = """[i for i in 1,    2    ]"""
4086        a = """[i for i in (1,    2)    ]"""
4087        self.check(b, a)
4088
4089    def test_5(self):
4090        b = """(i for i in 1, 2)"""
4091        a = """(i for i in (1, 2))"""
4092        self.check(b, a)
4093
4094    def test_6(self):
4095        b = """(i for i in 1   ,2   if i)"""
4096        a = """(i for i in (1   ,2)   if i)"""
4097        self.check(b, a)
4098
4099    def test_unchanged_0(self):
4100        s = """[i for i in (1, 2)]"""
4101        self.unchanged(s)
4102
4103    def test_unchanged_1(self):
4104        s = """[i for i in foo()]"""
4105        self.unchanged(s)
4106
4107    def test_unchanged_2(self):
4108        s = """[i for i in (1, 2) if nothing]"""
4109        self.unchanged(s)
4110
4111    def test_unchanged_3(self):
4112        s = """(i for i in (1, 2))"""
4113        self.unchanged(s)
4114
4115    def test_unchanged_4(self):
4116        s = """[i for i in m]"""
4117        self.unchanged(s)
4118
4119class Test_metaclass(FixerTestCase):
4120
4121    fixer = 'metaclass'
4122
4123    def test_unchanged(self):
4124        self.unchanged("class X(): pass")
4125        self.unchanged("class X(object): pass")
4126        self.unchanged("class X(object1, object2): pass")
4127        self.unchanged("class X(object1, object2, object3): pass")
4128        self.unchanged("class X(metaclass=Meta): pass")
4129        self.unchanged("class X(b, arg=23, metclass=Meta): pass")
4130        self.unchanged("class X(b, arg=23, metaclass=Meta, other=42): pass")
4131
4132        s = """
4133        class X:
4134            def __metaclass__(self): pass
4135        """
4136        self.unchanged(s)
4137
4138        s = """
4139        class X:
4140            a[23] = 74
4141        """
4142        self.unchanged(s)
4143
4144    def test_comments(self):
4145        b = """
4146        class X:
4147            # hi
4148            __metaclass__ = AppleMeta
4149        """
4150        a = """
4151        class X(metaclass=AppleMeta):
4152            # hi
4153            pass
4154        """
4155        self.check(b, a)
4156
4157        b = """
4158        class X:
4159            __metaclass__ = Meta
4160            # Bedtime!
4161        """
4162        a = """
4163        class X(metaclass=Meta):
4164            pass
4165            # Bedtime!
4166        """
4167        self.check(b, a)
4168
4169    def test_meta(self):
4170        # no-parent class, odd body
4171        b = """
4172        class X():
4173            __metaclass__ = Q
4174            pass
4175        """
4176        a = """
4177        class X(metaclass=Q):
4178            pass
4179        """
4180        self.check(b, a)
4181
4182        # one parent class, no body
4183        b = """class X(object): __metaclass__ = Q"""
4184        a = """class X(object, metaclass=Q): pass"""
4185        self.check(b, a)
4186
4187
4188        # one parent, simple body
4189        b = """
4190        class X(object):
4191            __metaclass__ = Meta
4192            bar = 7
4193        """
4194        a = """
4195        class X(object, metaclass=Meta):
4196            bar = 7
4197        """
4198        self.check(b, a)
4199
4200        b = """
4201        class X:
4202            __metaclass__ = Meta; x = 4; g = 23
4203        """
4204        a = """
4205        class X(metaclass=Meta):
4206            x = 4; g = 23
4207        """
4208        self.check(b, a)
4209
4210        # one parent, simple body, __metaclass__ last
4211        b = """
4212        class X(object):
4213            bar = 7
4214            __metaclass__ = Meta
4215        """
4216        a = """
4217        class X(object, metaclass=Meta):
4218            bar = 7
4219        """
4220        self.check(b, a)
4221
4222        # redefining __metaclass__
4223        b = """
4224        class X():
4225            __metaclass__ = A
4226            __metaclass__ = B
4227            bar = 7
4228        """
4229        a = """
4230        class X(metaclass=B):
4231            bar = 7
4232        """
4233        self.check(b, a)
4234
4235        # multiple inheritance, simple body
4236        b = """
4237        class X(clsA, clsB):
4238            __metaclass__ = Meta
4239            bar = 7
4240        """
4241        a = """
4242        class X(clsA, clsB, metaclass=Meta):
4243            bar = 7
4244        """
4245        self.check(b, a)
4246
4247        # keywords in the class statement
4248        b = """class m(a, arg=23): __metaclass__ = Meta"""
4249        a = """class m(a, arg=23, metaclass=Meta): pass"""
4250        self.check(b, a)
4251
4252        b = """
4253        class X(expression(2 + 4)):
4254            __metaclass__ = Meta
4255        """
4256        a = """
4257        class X(expression(2 + 4), metaclass=Meta):
4258            pass
4259        """
4260        self.check(b, a)
4261
4262        b = """
4263        class X(expression(2 + 4), x**4):
4264            __metaclass__ = Meta
4265        """
4266        a = """
4267        class X(expression(2 + 4), x**4, metaclass=Meta):
4268            pass
4269        """
4270        self.check(b, a)
4271
4272        b = """
4273        class X:
4274            __metaclass__ = Meta
4275            save.py = 23
4276        """
4277        a = """
4278        class X(metaclass=Meta):
4279            save.py = 23
4280        """
4281        self.check(b, a)
4282
4283
4284class Test_getcwdu(FixerTestCase):
4285
4286    fixer = 'getcwdu'
4287
4288    def test_basic(self):
4289        b = """os.getcwdu"""
4290        a = """os.getcwd"""
4291        self.check(b, a)
4292
4293        b = """os.getcwdu()"""
4294        a = """os.getcwd()"""
4295        self.check(b, a)
4296
4297        b = """meth = os.getcwdu"""
4298        a = """meth = os.getcwd"""
4299        self.check(b, a)
4300
4301        b = """os.getcwdu(args)"""
4302        a = """os.getcwd(args)"""
4303        self.check(b, a)
4304
4305    def test_comment(self):
4306        b = """os.getcwdu() # Foo"""
4307        a = """os.getcwd() # Foo"""
4308        self.check(b, a)
4309
4310    def test_unchanged(self):
4311        s = """os.getcwd()"""
4312        self.unchanged(s)
4313
4314        s = """getcwdu()"""
4315        self.unchanged(s)
4316
4317        s = """os.getcwdb()"""
4318        self.unchanged(s)
4319
4320    def test_indentation(self):
4321        b = """
4322            if 1:
4323                os.getcwdu()
4324            """
4325        a = """
4326            if 1:
4327                os.getcwd()
4328            """
4329        self.check(b, a)
4330
4331    def test_multilation(self):
4332        b = """os .getcwdu()"""
4333        a = """os .getcwd()"""
4334        self.check(b, a)
4335
4336        b = """os.  getcwdu"""
4337        a = """os.  getcwd"""
4338        self.check(b, a)
4339
4340        b = """os.getcwdu (  )"""
4341        a = """os.getcwd (  )"""
4342        self.check(b, a)
4343
4344
4345class Test_operator(FixerTestCase):
4346
4347    fixer = "operator"
4348
4349    def test_operator_isCallable(self):
4350        b = "operator.isCallable(x)"
4351        a = "hasattr(x, '__call__')"
4352        self.check(b, a)
4353
4354    def test_operator_sequenceIncludes(self):
4355        b = "operator.sequenceIncludes(x, y)"
4356        a = "operator.contains(x, y)"
4357        self.check(b, a)
4358
4359        b = "operator .sequenceIncludes(x, y)"
4360        a = "operator .contains(x, y)"
4361        self.check(b, a)
4362
4363        b = "operator.  sequenceIncludes(x, y)"
4364        a = "operator.  contains(x, y)"
4365        self.check(b, a)
4366
4367    def test_operator_isSequenceType(self):
4368        b = "operator.isSequenceType(x)"
4369        a = "import collections\nisinstance(x, collections.Sequence)"
4370        self.check(b, a)
4371
4372    def test_operator_isMappingType(self):
4373        b = "operator.isMappingType(x)"
4374        a = "import collections\nisinstance(x, collections.Mapping)"
4375        self.check(b, a)
4376
4377    def test_operator_isNumberType(self):
4378        b = "operator.isNumberType(x)"
4379        a = "import numbers\nisinstance(x, numbers.Number)"
4380        self.check(b, a)
4381
4382    def test_operator_repeat(self):
4383        b = "operator.repeat(x, n)"
4384        a = "operator.mul(x, n)"
4385        self.check(b, a)
4386
4387        b = "operator .repeat(x, n)"
4388        a = "operator .mul(x, n)"
4389        self.check(b, a)
4390
4391        b = "operator.  repeat(x, n)"
4392        a = "operator.  mul(x, n)"
4393        self.check(b, a)
4394
4395    def test_operator_irepeat(self):
4396        b = "operator.irepeat(x, n)"
4397        a = "operator.imul(x, n)"
4398        self.check(b, a)
4399
4400        b = "operator .irepeat(x, n)"
4401        a = "operator .imul(x, n)"
4402        self.check(b, a)
4403
4404        b = "operator.  irepeat(x, n)"
4405        a = "operator.  imul(x, n)"
4406        self.check(b, a)
4407
4408    def test_bare_isCallable(self):
4409        s = "isCallable(x)"
4410        t = "You should use 'hasattr(x, '__call__')' here."
4411        self.warns_unchanged(s, t)
4412
4413    def test_bare_sequenceIncludes(self):
4414        s = "sequenceIncludes(x, y)"
4415        t = "You should use 'operator.contains(x, y)' here."
4416        self.warns_unchanged(s, t)
4417
4418    def test_bare_operator_isSequenceType(self):
4419        s = "isSequenceType(z)"
4420        t = "You should use 'isinstance(z, collections.Sequence)' here."
4421        self.warns_unchanged(s, t)
4422
4423    def test_bare_operator_isMappingType(self):
4424        s = "isMappingType(x)"
4425        t = "You should use 'isinstance(x, collections.Mapping)' here."
4426        self.warns_unchanged(s, t)
4427
4428    def test_bare_operator_isNumberType(self):
4429        s = "isNumberType(y)"
4430        t = "You should use 'isinstance(y, numbers.Number)' here."
4431        self.warns_unchanged(s, t)
4432
4433    def test_bare_operator_repeat(self):
4434        s = "repeat(x, n)"
4435        t = "You should use 'operator.mul(x, n)' here."
4436        self.warns_unchanged(s, t)
4437
4438    def test_bare_operator_irepeat(self):
4439        s = "irepeat(y, 187)"
4440        t = "You should use 'operator.imul(y, 187)' here."
4441        self.warns_unchanged(s, t)
4442
4443
4444class Test_exitfunc(FixerTestCase):
4445
4446    fixer = "exitfunc"
4447
4448    def test_simple(self):
4449        b = """
4450            import sys
4451            sys.exitfunc = my_atexit
4452            """
4453        a = """
4454            import sys
4455            import atexit
4456            atexit.register(my_atexit)
4457            """
4458        self.check(b, a)
4459
4460    def test_names_import(self):
4461        b = """
4462            import sys, crumbs
4463            sys.exitfunc = my_func
4464            """
4465        a = """
4466            import sys, crumbs, atexit
4467            atexit.register(my_func)
4468            """
4469        self.check(b, a)
4470
4471    def test_complex_expression(self):
4472        b = """
4473            import sys
4474            sys.exitfunc = do(d)/a()+complex(f=23, g=23)*expression
4475            """
4476        a = """
4477            import sys
4478            import atexit
4479            atexit.register(do(d)/a()+complex(f=23, g=23)*expression)
4480            """
4481        self.check(b, a)
4482
4483    def test_comments(self):
4484        b = """
4485            import sys # Foo
4486            sys.exitfunc = f # Blah
4487            """
4488        a = """
4489            import sys
4490            import atexit # Foo
4491            atexit.register(f) # Blah
4492            """
4493        self.check(b, a)
4494
4495        b = """
4496            import apples, sys, crumbs, larry # Pleasant comments
4497            sys.exitfunc = func
4498            """
4499        a = """
4500            import apples, sys, crumbs, larry, atexit # Pleasant comments
4501            atexit.register(func)
4502            """
4503        self.check(b, a)
4504
4505    def test_in_a_function(self):
4506        b = """
4507            import sys
4508            def f():
4509                sys.exitfunc = func
4510            """
4511        a = """
4512            import sys
4513            import atexit
4514            def f():
4515                atexit.register(func)
4516             """
4517        self.check(b, a)
4518
4519    def test_no_sys_import(self):
4520        b = """sys.exitfunc = f"""
4521        a = """atexit.register(f)"""
4522        msg = ("Can't find sys import; Please add an atexit import at the "
4523            "top of your file.")
4524        self.warns(b, a, msg)
4525
4526
4527    def test_unchanged(self):
4528        s = """f(sys.exitfunc)"""
4529        self.unchanged(s)
4530