PageRenderTime 106ms CodeModel.GetById 15ms app.highlight 79ms RepoModel.GetById 1ms app.codeStats 1ms

/Lib/lib2to3/tests/test_fixers.py

http://unladen-swallow.googlecode.com/
Python | 4023 lines | 3981 code | 36 blank | 6 comment | 52 complexity | 8a68ab4c442c928a298d8ab47967b6e0 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1#!/usr/bin/env python2.5
   2""" Test suite for the fixer modules """
   3# Author: Collin Winter
   4
   5# Testing imports
   6try:
   7    from tests import support
   8except ImportError:
   9    import support
  10
  11# Python imports
  12import os
  13import unittest
  14from itertools import chain
  15from operator import itemgetter
  16
  17# Local imports
  18from lib2to3 import pygram, pytree, refactor, fixer_util
  19
  20
  21class FixerTestCase(support.TestCase):
  22    def setUp(self, fix_list=None):
  23        if fix_list is None:
  24            fix_list = [self.fixer]
  25        options = {"print_function" : False}
  26        self.refactor = support.get_refactorer(fix_list, options)
  27        self.fixer_log = []
  28        self.filename = "<string>"
  29
  30        for fixer in chain(self.refactor.pre_order,
  31                           self.refactor.post_order):
  32            fixer.log = self.fixer_log
  33
  34    def _check(self, before, after):
  35        before = support.reformat(before)
  36        after = support.reformat(after)
  37        tree = self.refactor.refactor_string(before, self.filename)
  38        self.failUnlessEqual(after, str(tree))
  39        return tree
  40
  41    def check(self, before, after, ignore_warnings=False):
  42        tree = self._check(before, after)
  43        self.failUnless(tree.was_changed)
  44        if not ignore_warnings:
  45            self.failUnlessEqual(self.fixer_log, [])
  46
  47    def warns(self, before, after, message, unchanged=False):
  48        tree = self._check(before, after)
  49        self.failUnless(message in "".join(self.fixer_log))
  50        if not unchanged:
  51            self.failUnless(tree.was_changed)
  52
  53    def warns_unchanged(self, before, message):
  54        self.warns(before, before, message, unchanged=True)
  55
  56    def unchanged(self, before, ignore_warnings=False):
  57        self._check(before, before)
  58        if not ignore_warnings:
  59            self.failUnlessEqual(self.fixer_log, [])
  60
  61    def assert_runs_after(self, *names):
  62        fixes = [self.fixer]
  63        fixes.extend(names)
  64        options = {"print_function" : False}
  65        r = support.get_refactorer(fixes, options)
  66        (pre, post) = r.get_fixers()
  67        n = "fix_" + self.fixer
  68        if post and post[-1].__class__.__module__.endswith(n):
  69            # We're the last fixer to run
  70            return
  71        if pre and pre[-1].__class__.__module__.endswith(n) and not post:
  72            # We're the last in pre and post is empty
  73            return
  74        self.fail("Fixer run order (%s) is incorrect; %s should be last."\
  75               %(", ".join([x.__class__.__module__ for x in (pre+post)]), n))
  76
  77class Test_ne(FixerTestCase):
  78    fixer = "ne"
  79
  80    def test_basic(self):
  81        b = """if x <> y:
  82            pass"""
  83
  84        a = """if x != y:
  85            pass"""
  86        self.check(b, a)
  87
  88    def test_no_spaces(self):
  89        b = """if x<>y:
  90            pass"""
  91
  92        a = """if x!=y:
  93            pass"""
  94        self.check(b, a)
  95
  96    def test_chained(self):
  97        b = """if x<>y<>z:
  98            pass"""
  99
 100        a = """if x!=y!=z:
 101            pass"""
 102        self.check(b, a)
 103
 104class Test_has_key(FixerTestCase):
 105    fixer = "has_key"
 106
 107    def test_1(self):
 108        b = """x = d.has_key("x") or d.has_key("y")"""
 109        a = """x = "x" in d or "y" in d"""
 110        self.check(b, a)
 111
 112    def test_2(self):
 113        b = """x = a.b.c.d.has_key("x") ** 3"""
 114        a = """x = ("x" in a.b.c.d) ** 3"""
 115        self.check(b, a)
 116
 117    def test_3(self):
 118        b = """x = a.b.has_key(1 + 2).__repr__()"""
 119        a = """x = (1 + 2 in a.b).__repr__()"""
 120        self.check(b, a)
 121
 122    def test_4(self):
 123        b = """x = a.b.has_key(1 + 2).__repr__() ** -3 ** 4"""
 124        a = """x = (1 + 2 in a.b).__repr__() ** -3 ** 4"""
 125        self.check(b, a)
 126
 127    def test_5(self):
 128        b = """x = a.has_key(f or g)"""
 129        a = """x = (f or g) in a"""
 130        self.check(b, a)
 131
 132    def test_6(self):
 133        b = """x = a + b.has_key(c)"""
 134        a = """x = a + (c in b)"""
 135        self.check(b, a)
 136
 137    def test_7(self):
 138        b = """x = a.has_key(lambda: 12)"""
 139        a = """x = (lambda: 12) in a"""
 140        self.check(b, a)
 141
 142    def test_8(self):
 143        b = """x = a.has_key(a for a in b)"""
 144        a = """x = (a for a in b) in a"""
 145        self.check(b, a)
 146
 147    def test_9(self):
 148        b = """if not a.has_key(b): pass"""
 149        a = """if b not in a: pass"""
 150        self.check(b, a)
 151
 152    def test_10(self):
 153        b = """if not a.has_key(b).__repr__(): pass"""
 154        a = """if not (b in a).__repr__(): pass"""
 155        self.check(b, a)
 156
 157    def test_11(self):
 158        b = """if not a.has_key(b) ** 2: pass"""
 159        a = """if not (b in a) ** 2: pass"""
 160        self.check(b, a)
 161
 162class Test_apply(FixerTestCase):
 163    fixer = "apply"
 164
 165    def test_1(self):
 166        b = """x = apply(f, g + h)"""
 167        a = """x = f(*g + h)"""
 168        self.check(b, a)
 169
 170    def test_2(self):
 171        b = """y = apply(f, g, h)"""
 172        a = """y = f(*g, **h)"""
 173        self.check(b, a)
 174
 175    def test_3(self):
 176        b = """z = apply(fs[0], g or h, h or g)"""
 177        a = """z = fs[0](*g or h, **h or g)"""
 178        self.check(b, a)
 179
 180    def test_4(self):
 181        b = """apply(f, (x, y) + t)"""
 182        a = """f(*(x, y) + t)"""
 183        self.check(b, a)
 184
 185    def test_5(self):
 186        b = """apply(f, args,)"""
 187        a = """f(*args)"""
 188        self.check(b, a)
 189
 190    def test_6(self):
 191        b = """apply(f, args, kwds,)"""
 192        a = """f(*args, **kwds)"""
 193        self.check(b, a)
 194
 195    # Test that complex functions are parenthesized
 196
 197    def test_complex_1(self):
 198        b = """x = apply(f+g, args)"""
 199        a = """x = (f+g)(*args)"""
 200        self.check(b, a)
 201
 202    def test_complex_2(self):
 203        b = """x = apply(f*g, args)"""
 204        a = """x = (f*g)(*args)"""
 205        self.check(b, a)
 206
 207    def test_complex_3(self):
 208        b = """x = apply(f**g, args)"""
 209        a = """x = (f**g)(*args)"""
 210        self.check(b, a)
 211
 212    # But dotted names etc. not
 213
 214    def test_dotted_name(self):
 215        b = """x = apply(f.g, args)"""
 216        a = """x = f.g(*args)"""
 217        self.check(b, a)
 218
 219    def test_subscript(self):
 220        b = """x = apply(f[x], args)"""
 221        a = """x = f[x](*args)"""
 222        self.check(b, a)
 223
 224    def test_call(self):
 225        b = """x = apply(f(), args)"""
 226        a = """x = f()(*args)"""
 227        self.check(b, a)
 228
 229    # Extreme case
 230    def test_extreme(self):
 231        b = """x = apply(a.b.c.d.e.f, args, kwds)"""
 232        a = """x = a.b.c.d.e.f(*args, **kwds)"""
 233        self.check(b, a)
 234
 235    # XXX Comments in weird places still get lost
 236    def test_weird_comments(self):
 237        b = """apply(   # foo
 238          f, # bar
 239          args)"""
 240        a = """f(*args)"""
 241        self.check(b, a)
 242
 243    # These should *not* be touched
 244
 245    def test_unchanged_1(self):
 246        s = """apply()"""
 247        self.unchanged(s)
 248
 249    def test_unchanged_2(self):
 250        s = """apply(f)"""
 251        self.unchanged(s)
 252
 253    def test_unchanged_3(self):
 254        s = """apply(f,)"""
 255        self.unchanged(s)
 256
 257    def test_unchanged_4(self):
 258        s = """apply(f, args, kwds, extras)"""
 259        self.unchanged(s)
 260
 261    def test_unchanged_5(self):
 262        s = """apply(f, *args, **kwds)"""
 263        self.unchanged(s)
 264
 265    def test_unchanged_6(self):
 266        s = """apply(f, *args)"""
 267        self.unchanged(s)
 268
 269    def test_unchanged_7(self):
 270        s = """apply(func=f, args=args, kwds=kwds)"""
 271        self.unchanged(s)
 272
 273    def test_unchanged_8(self):
 274        s = """apply(f, args=args, kwds=kwds)"""
 275        self.unchanged(s)
 276
 277    def test_unchanged_9(self):
 278        s = """apply(f, args, kwds=kwds)"""
 279        self.unchanged(s)
 280
 281    def test_space_1(self):
 282        a = """apply(  f,  args,   kwds)"""
 283        b = """f(*args, **kwds)"""
 284        self.check(a, b)
 285
 286    def test_space_2(self):
 287        a = """apply(  f  ,args,kwds   )"""
 288        b = """f(*args, **kwds)"""
 289        self.check(a, b)
 290
 291class Test_intern(FixerTestCase):
 292    fixer = "intern"
 293
 294    def test_prefix_preservation(self):
 295        b = """x =   intern(  a  )"""
 296        a = """import sys\nx =   sys.intern(  a  )"""
 297        self.check(b, a)
 298
 299        b = """y = intern("b" # test
 300              )"""
 301        a = """import sys\ny = sys.intern("b" # test
 302              )"""
 303        self.check(b, a)
 304
 305        b = """z = intern(a+b+c.d,   )"""
 306        a = """import sys\nz = sys.intern(a+b+c.d,   )"""
 307        self.check(b, a)
 308
 309    def test(self):
 310        b = """x = intern(a)"""
 311        a = """import sys\nx = sys.intern(a)"""
 312        self.check(b, a)
 313
 314        b = """z = intern(a+b+c.d,)"""
 315        a = """import sys\nz = sys.intern(a+b+c.d,)"""
 316        self.check(b, a)
 317
 318        b = """intern("y%s" % 5).replace("y", "")"""
 319        a = """import sys\nsys.intern("y%s" % 5).replace("y", "")"""
 320        self.check(b, a)
 321
 322    # These should not be refactored
 323
 324    def test_unchanged(self):
 325        s = """intern(a=1)"""
 326        self.unchanged(s)
 327
 328        s = """intern(f, g)"""
 329        self.unchanged(s)
 330
 331        s = """intern(*h)"""
 332        self.unchanged(s)
 333
 334        s = """intern(**i)"""
 335        self.unchanged(s)
 336
 337        s = """intern()"""
 338        self.unchanged(s)
 339
 340class Test_reduce(FixerTestCase):
 341    fixer = "reduce"
 342
 343    def test_simple_call(self):
 344        b = "reduce(a, b, c)"
 345        a = "from functools import reduce\nreduce(a, b, c)"
 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        print_stmt = pygram.python_grammar.keywords.pop("print")
 386        try:
 387            s = """print(1, 1+1, 1+1+1)"""
 388            self.unchanged(s)
 389
 390            s = """print()"""
 391            self.unchanged(s)
 392
 393            s = """print('')"""
 394            self.unchanged(s)
 395        finally:
 396            pygram.python_grammar.keywords["print"] = print_stmt
 397
 398    def test_1(self):
 399        b = """print 1, 1+1, 1+1+1"""
 400        a = """print(1, 1+1, 1+1+1)"""
 401        self.check(b, a)
 402
 403    def test_2(self):
 404        b = """print 1, 2"""
 405        a = """print(1, 2)"""
 406        self.check(b, a)
 407
 408    def test_3(self):
 409        b = """print"""
 410        a = """print()"""
 411        self.check(b, a)
 412
 413    def test_4(self):
 414        # from bug 3000
 415        b = """print whatever; print"""
 416        a = """print(whatever); print()"""
 417        self.check(b, a)
 418
 419    def test_5(self):
 420        b = """print; print whatever;"""
 421        a = """print(); print(whatever);"""
 422
 423    def test_tuple(self):
 424        b = """print (a, b, c)"""
 425        a = """print((a, b, c))"""
 426        self.check(b, a)
 427
 428    # trailing commas
 429
 430    def test_trailing_comma_1(self):
 431        b = """print 1, 2, 3,"""
 432        a = """print(1, 2, 3, end=' ')"""
 433        self.check(b, a)
 434
 435    def test_trailing_comma_2(self):
 436        b = """print 1, 2,"""
 437        a = """print(1, 2, end=' ')"""
 438        self.check(b, a)
 439
 440    def test_trailing_comma_3(self):
 441        b = """print 1,"""
 442        a = """print(1, end=' ')"""
 443        self.check(b, a)
 444
 445    # >> stuff
 446
 447    def test_vargs_without_trailing_comma(self):
 448        b = """print >>sys.stderr, 1, 2, 3"""
 449        a = """print(1, 2, 3, file=sys.stderr)"""
 450        self.check(b, a)
 451
 452    def test_with_trailing_comma(self):
 453        b = """print >>sys.stderr, 1, 2,"""
 454        a = """print(1, 2, end=' ', file=sys.stderr)"""
 455        self.check(b, a)
 456
 457    def test_no_trailing_comma(self):
 458        b = """print >>sys.stderr, 1+1"""
 459        a = """print(1+1, file=sys.stderr)"""
 460        self.check(b, a)
 461
 462    def test_spaces_before_file(self):
 463        b = """print >>  sys.stderr"""
 464        a = """print(file=sys.stderr)"""
 465        self.check(b, a)
 466
 467    # With from __future__ import print_function
 468    def test_with_future_print_function(self):
 469        # XXX: These tests won't actually do anything until the parser
 470        #      is fixed so it won't crash when it sees print(x=y).
 471        #      When #2412 is fixed, the try/except block can be taken
 472        #      out and the tests can be run like normal.
 473        # MvL: disable entirely for now, so that it doesn't print to stdout
 474        return
 475        try:
 476            s = "from __future__ import print_function\n"\
 477                "print('Hai!', end=' ')"
 478            self.unchanged(s)
 479
 480            b = "print 'Hello, world!'"
 481            a = "print('Hello, world!')"
 482            self.check(b, a)
 483
 484            s = "from __future__ import *\n"\
 485                "print('Hai!', end=' ')"
 486            self.unchanged(s)
 487        except:
 488            return
 489        else:
 490            self.assertFalse(True, "#2421 has been fixed -- printing tests "\
 491                                   "need to be updated!")
 492
 493class Test_exec(FixerTestCase):
 494    fixer = "exec"
 495
 496    def test_prefix_preservation(self):
 497        b = """  exec code in ns1,   ns2"""
 498        a = """  exec(code, ns1,   ns2)"""
 499        self.check(b, a)
 500
 501    def test_basic(self):
 502        b = """exec code"""
 503        a = """exec(code)"""
 504        self.check(b, a)
 505
 506    def test_with_globals(self):
 507        b = """exec code in ns"""
 508        a = """exec(code, ns)"""
 509        self.check(b, a)
 510
 511    def test_with_globals_locals(self):
 512        b = """exec code in ns1, ns2"""
 513        a = """exec(code, ns1, ns2)"""
 514        self.check(b, a)
 515
 516    def test_complex_1(self):
 517        b = """exec (a.b()) in ns"""
 518        a = """exec((a.b()), ns)"""
 519        self.check(b, a)
 520
 521    def test_complex_2(self):
 522        b = """exec a.b() + c in ns"""
 523        a = """exec(a.b() + c, ns)"""
 524        self.check(b, a)
 525
 526    # These should not be touched
 527
 528    def test_unchanged_1(self):
 529        s = """exec(code)"""
 530        self.unchanged(s)
 531
 532    def test_unchanged_2(self):
 533        s = """exec (code)"""
 534        self.unchanged(s)
 535
 536    def test_unchanged_3(self):
 537        s = """exec(code, ns)"""
 538        self.unchanged(s)
 539
 540    def test_unchanged_4(self):
 541        s = """exec(code, ns1, ns2)"""
 542        self.unchanged(s)
 543
 544class Test_repr(FixerTestCase):
 545    fixer = "repr"
 546
 547    def test_prefix_preservation(self):
 548        b = """x =   `1 + 2`"""
 549        a = """x =   repr(1 + 2)"""
 550        self.check(b, a)
 551
 552    def test_simple_1(self):
 553        b = """x = `1 + 2`"""
 554        a = """x = repr(1 + 2)"""
 555        self.check(b, a)
 556
 557    def test_simple_2(self):
 558        b = """y = `x`"""
 559        a = """y = repr(x)"""
 560        self.check(b, a)
 561
 562    def test_complex(self):
 563        b = """z = `y`.__repr__()"""
 564        a = """z = repr(y).__repr__()"""
 565        self.check(b, a)
 566
 567    def test_tuple(self):
 568        b = """x = `1, 2, 3`"""
 569        a = """x = repr((1, 2, 3))"""
 570        self.check(b, a)
 571
 572    def test_nested(self):
 573        b = """x = `1 + `2``"""
 574        a = """x = repr(1 + repr(2))"""
 575        self.check(b, a)
 576
 577    def test_nested_tuples(self):
 578        b = """x = `1, 2 + `3, 4``"""
 579        a = """x = repr((1, 2 + repr((3, 4))))"""
 580        self.check(b, a)
 581
 582class Test_except(FixerTestCase):
 583    fixer = "except"
 584
 585    def test_prefix_preservation(self):
 586        b = """
 587            try:
 588                pass
 589            except (RuntimeError, ImportError),    e:
 590                pass"""
 591        a = """
 592            try:
 593                pass
 594            except (RuntimeError, ImportError) as    e:
 595                pass"""
 596        self.check(b, a)
 597
 598    def test_simple(self):
 599        b = """
 600            try:
 601                pass
 602            except Foo, e:
 603                pass"""
 604        a = """
 605            try:
 606                pass
 607            except Foo as e:
 608                pass"""
 609        self.check(b, a)
 610
 611    def test_simple_no_space_before_target(self):
 612        b = """
 613            try:
 614                pass
 615            except Foo,e:
 616                pass"""
 617        a = """
 618            try:
 619                pass
 620            except Foo as e:
 621                pass"""
 622        self.check(b, a)
 623
 624    def test_tuple_unpack(self):
 625        b = """
 626            def foo():
 627                try:
 628                    pass
 629                except Exception, (f, e):
 630                    pass
 631                except ImportError, e:
 632                    pass"""
 633
 634        a = """
 635            def foo():
 636                try:
 637                    pass
 638                except Exception as xxx_todo_changeme:
 639                    (f, e) = xxx_todo_changeme.args
 640                    pass
 641                except ImportError as e:
 642                    pass"""
 643        self.check(b, a)
 644
 645    def test_multi_class(self):
 646        b = """
 647            try:
 648                pass
 649            except (RuntimeError, ImportError), e:
 650                pass"""
 651
 652        a = """
 653            try:
 654                pass
 655            except (RuntimeError, ImportError) as e:
 656                pass"""
 657        self.check(b, a)
 658
 659    def test_list_unpack(self):
 660        b = """
 661            try:
 662                pass
 663            except Exception, [a, b]:
 664                pass"""
 665
 666        a = """
 667            try:
 668                pass
 669            except Exception as xxx_todo_changeme:
 670                [a, b] = xxx_todo_changeme.args
 671                pass"""
 672        self.check(b, a)
 673
 674    def test_weird_target_1(self):
 675        b = """
 676            try:
 677                pass
 678            except Exception, d[5]:
 679                pass"""
 680
 681        a = """
 682            try:
 683                pass
 684            except Exception as xxx_todo_changeme:
 685                d[5] = xxx_todo_changeme
 686                pass"""
 687        self.check(b, a)
 688
 689    def test_weird_target_2(self):
 690        b = """
 691            try:
 692                pass
 693            except Exception, a.foo:
 694                pass"""
 695
 696        a = """
 697            try:
 698                pass
 699            except Exception as xxx_todo_changeme:
 700                a.foo = xxx_todo_changeme
 701                pass"""
 702        self.check(b, a)
 703
 704    def test_weird_target_3(self):
 705        b = """
 706            try:
 707                pass
 708            except Exception, a().foo:
 709                pass"""
 710
 711        a = """
 712            try:
 713                pass
 714            except Exception as xxx_todo_changeme:
 715                a().foo = xxx_todo_changeme
 716                pass"""
 717        self.check(b, a)
 718
 719    def test_bare_except(self):
 720        b = """
 721            try:
 722                pass
 723            except Exception, a:
 724                pass
 725            except:
 726                pass"""
 727
 728        a = """
 729            try:
 730                pass
 731            except Exception as a:
 732                pass
 733            except:
 734                pass"""
 735        self.check(b, a)
 736
 737    def test_bare_except_and_else_finally(self):
 738        b = """
 739            try:
 740                pass
 741            except Exception, a:
 742                pass
 743            except:
 744                pass
 745            else:
 746                pass
 747            finally:
 748                pass"""
 749
 750        a = """
 751            try:
 752                pass
 753            except Exception as a:
 754                pass
 755            except:
 756                pass
 757            else:
 758                pass
 759            finally:
 760                pass"""
 761        self.check(b, a)
 762
 763    def test_multi_fixed_excepts_before_bare_except(self):
 764        b = """
 765            try:
 766                pass
 767            except TypeError, b:
 768                pass
 769            except Exception, a:
 770                pass
 771            except:
 772                pass"""
 773
 774        a = """
 775            try:
 776                pass
 777            except TypeError as b:
 778                pass
 779            except Exception as a:
 780                pass
 781            except:
 782                pass"""
 783        self.check(b, a)
 784
 785    # These should not be touched:
 786
 787    def test_unchanged_1(self):
 788        s = """
 789            try:
 790                pass
 791            except:
 792                pass"""
 793        self.unchanged(s)
 794
 795    def test_unchanged_2(self):
 796        s = """
 797            try:
 798                pass
 799            except Exception:
 800                pass"""
 801        self.unchanged(s)
 802
 803    def test_unchanged_3(self):
 804        s = """
 805            try:
 806                pass
 807            except (Exception, SystemExit):
 808                pass"""
 809        self.unchanged(s)
 810
 811class Test_raise(FixerTestCase):
 812    fixer = "raise"
 813
 814    def test_basic(self):
 815        b = """raise Exception, 5"""
 816        a = """raise Exception(5)"""
 817        self.check(b, a)
 818
 819    def test_prefix_preservation(self):
 820        b = """raise Exception,5"""
 821        a = """raise Exception(5)"""
 822        self.check(b, a)
 823
 824        b = """raise   Exception,    5"""
 825        a = """raise   Exception(5)"""
 826        self.check(b, a)
 827
 828    def test_with_comments(self):
 829        b = """raise Exception, 5 # foo"""
 830        a = """raise Exception(5) # foo"""
 831        self.check(b, a)
 832
 833        b = """raise E, (5, 6) % (a, b) # foo"""
 834        a = """raise E((5, 6) % (a, b)) # foo"""
 835        self.check(b, a)
 836
 837        b = """def foo():
 838                    raise Exception, 5, 6 # foo"""
 839        a = """def foo():
 840                    raise Exception(5).with_traceback(6) # foo"""
 841        self.check(b, a)
 842
 843    def test_tuple_value(self):
 844        b = """raise Exception, (5, 6, 7)"""
 845        a = """raise Exception(5, 6, 7)"""
 846        self.check(b, a)
 847
 848    def test_tuple_detection(self):
 849        b = """raise E, (5, 6) % (a, b)"""
 850        a = """raise E((5, 6) % (a, b))"""
 851        self.check(b, a)
 852
 853    def test_tuple_exc_1(self):
 854        b = """raise (((E1, E2), E3), E4), V"""
 855        a = """raise E1(V)"""
 856        self.check(b, a)
 857
 858    def test_tuple_exc_2(self):
 859        b = """raise (E1, (E2, E3), E4), V"""
 860        a = """raise E1(V)"""
 861        self.check(b, a)
 862
 863    # These should produce a warning
 864
 865    def test_string_exc(self):
 866        s = """raise 'foo'"""
 867        self.warns_unchanged(s, "Python 3 does not support string exceptions")
 868
 869    def test_string_exc_val(self):
 870        s = """raise "foo", 5"""
 871        self.warns_unchanged(s, "Python 3 does not support string exceptions")
 872
 873    def test_string_exc_val_tb(self):
 874        s = """raise "foo", 5, 6"""
 875        self.warns_unchanged(s, "Python 3 does not support string exceptions")
 876
 877    # These should result in traceback-assignment
 878
 879    def test_tb_1(self):
 880        b = """def foo():
 881                    raise Exception, 5, 6"""
 882        a = """def foo():
 883                    raise Exception(5).with_traceback(6)"""
 884        self.check(b, a)
 885
 886    def test_tb_2(self):
 887        b = """def foo():
 888                    a = 5
 889                    raise Exception, 5, 6
 890                    b = 6"""
 891        a = """def foo():
 892                    a = 5
 893                    raise Exception(5).with_traceback(6)
 894                    b = 6"""
 895        self.check(b, a)
 896
 897    def test_tb_3(self):
 898        b = """def foo():
 899                    raise Exception,5,6"""
 900        a = """def foo():
 901                    raise Exception(5).with_traceback(6)"""
 902        self.check(b, a)
 903
 904    def test_tb_4(self):
 905        b = """def foo():
 906                    a = 5
 907                    raise Exception,5,6
 908                    b = 6"""
 909        a = """def foo():
 910                    a = 5
 911                    raise Exception(5).with_traceback(6)
 912                    b = 6"""
 913        self.check(b, a)
 914
 915    def test_tb_5(self):
 916        b = """def foo():
 917                    raise Exception, (5, 6, 7), 6"""
 918        a = """def foo():
 919                    raise Exception(5, 6, 7).with_traceback(6)"""
 920        self.check(b, a)
 921
 922    def test_tb_6(self):
 923        b = """def foo():
 924                    a = 5
 925                    raise Exception, (5, 6, 7), 6
 926                    b = 6"""
 927        a = """def foo():
 928                    a = 5
 929                    raise Exception(5, 6, 7).with_traceback(6)
 930                    b = 6"""
 931        self.check(b, a)
 932
 933class Test_throw(FixerTestCase):
 934    fixer = "throw"
 935
 936    def test_1(self):
 937        b = """g.throw(Exception, 5)"""
 938        a = """g.throw(Exception(5))"""
 939        self.check(b, a)
 940
 941    def test_2(self):
 942        b = """g.throw(Exception,5)"""
 943        a = """g.throw(Exception(5))"""
 944        self.check(b, a)
 945
 946    def test_3(self):
 947        b = """g.throw(Exception, (5, 6, 7))"""
 948        a = """g.throw(Exception(5, 6, 7))"""
 949        self.check(b, a)
 950
 951    def test_4(self):
 952        b = """5 + g.throw(Exception, 5)"""
 953        a = """5 + g.throw(Exception(5))"""
 954        self.check(b, a)
 955
 956    # These should produce warnings
 957
 958    def test_warn_1(self):
 959        s = """g.throw("foo")"""
 960        self.warns_unchanged(s, "Python 3 does not support string exceptions")
 961
 962    def test_warn_2(self):
 963        s = """g.throw("foo", 5)"""
 964        self.warns_unchanged(s, "Python 3 does not support string exceptions")
 965
 966    def test_warn_3(self):
 967        s = """g.throw("foo", 5, 6)"""
 968        self.warns_unchanged(s, "Python 3 does not support string exceptions")
 969
 970    # These should not be touched
 971
 972    def test_untouched_1(self):
 973        s = """g.throw(Exception)"""
 974        self.unchanged(s)
 975
 976    def test_untouched_2(self):
 977        s = """g.throw(Exception(5, 6))"""
 978        self.unchanged(s)
 979
 980    def test_untouched_3(self):
 981        s = """5 + g.throw(Exception(5, 6))"""
 982        self.unchanged(s)
 983
 984    # These should result in traceback-assignment
 985
 986    def test_tb_1(self):
 987        b = """def foo():
 988                    g.throw(Exception, 5, 6)"""
 989        a = """def foo():
 990                    g.throw(Exception(5).with_traceback(6))"""
 991        self.check(b, a)
 992
 993    def test_tb_2(self):
 994        b = """def foo():
 995                    a = 5
 996                    g.throw(Exception, 5, 6)
 997                    b = 6"""
 998        a = """def foo():
 999                    a = 5
1000                    g.throw(Exception(5).with_traceback(6))
1001                    b = 6"""
1002        self.check(b, a)
1003
1004    def test_tb_3(self):
1005        b = """def foo():
1006                    g.throw(Exception,5,6)"""
1007        a = """def foo():
1008                    g.throw(Exception(5).with_traceback(6))"""
1009        self.check(b, a)
1010
1011    def test_tb_4(self):
1012        b = """def foo():
1013                    a = 5
1014                    g.throw(Exception,5,6)
1015                    b = 6"""
1016        a = """def foo():
1017                    a = 5
1018                    g.throw(Exception(5).with_traceback(6))
1019                    b = 6"""
1020        self.check(b, a)
1021
1022    def test_tb_5(self):
1023        b = """def foo():
1024                    g.throw(Exception, (5, 6, 7), 6)"""
1025        a = """def foo():
1026                    g.throw(Exception(5, 6, 7).with_traceback(6))"""
1027        self.check(b, a)
1028
1029    def test_tb_6(self):
1030        b = """def foo():
1031                    a = 5
1032                    g.throw(Exception, (5, 6, 7), 6)
1033                    b = 6"""
1034        a = """def foo():
1035                    a = 5
1036                    g.throw(Exception(5, 6, 7).with_traceback(6))
1037                    b = 6"""
1038        self.check(b, a)
1039
1040    def test_tb_7(self):
1041        b = """def foo():
1042                    a + g.throw(Exception, 5, 6)"""
1043        a = """def foo():
1044                    a + g.throw(Exception(5).with_traceback(6))"""
1045        self.check(b, a)
1046
1047    def test_tb_8(self):
1048        b = """def foo():
1049                    a = 5
1050                    a + g.throw(Exception, 5, 6)
1051                    b = 6"""
1052        a = """def foo():
1053                    a = 5
1054                    a + g.throw(Exception(5).with_traceback(6))
1055                    b = 6"""
1056        self.check(b, a)
1057
1058class Test_long(FixerTestCase):
1059    fixer = "long"
1060
1061    def test_1(self):
1062        b = """x = long(x)"""
1063        a = """x = int(x)"""
1064        self.check(b, a)
1065
1066    def test_2(self):
1067        b = """y = isinstance(x, long)"""
1068        a = """y = isinstance(x, int)"""
1069        self.check(b, a)
1070
1071    def test_3(self):
1072        b = """z = type(x) in (int, long)"""
1073        a = """z = type(x) in (int, int)"""
1074        self.check(b, a)
1075
1076    def test_unchanged(self):
1077        s = """long = True"""
1078        self.unchanged(s)
1079
1080        s = """s.long = True"""
1081        self.unchanged(s)
1082
1083        s = """def long(): pass"""
1084        self.unchanged(s)
1085
1086        s = """class long(): pass"""
1087        self.unchanged(s)
1088
1089        s = """def f(long): pass"""
1090        self.unchanged(s)
1091
1092        s = """def f(g, long): pass"""
1093        self.unchanged(s)
1094
1095        s = """def f(x, long=True): pass"""
1096        self.unchanged(s)
1097
1098    def test_prefix_preservation(self):
1099        b = """x =   long(  x  )"""
1100        a = """x =   int(  x  )"""
1101        self.check(b, a)
1102
1103
1104class Test_execfile(FixerTestCase):
1105    fixer = "execfile"
1106
1107    def test_conversion(self):
1108        b = """execfile("fn")"""
1109        a = """exec(compile(open("fn").read(), "fn", 'exec'))"""
1110        self.check(b, a)
1111
1112        b = """execfile("fn", glob)"""
1113        a = """exec(compile(open("fn").read(), "fn", 'exec'), glob)"""
1114        self.check(b, a)
1115
1116        b = """execfile("fn", glob, loc)"""
1117        a = """exec(compile(open("fn").read(), "fn", 'exec'), glob, loc)"""
1118        self.check(b, a)
1119
1120        b = """execfile("fn", globals=glob)"""
1121        a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob)"""
1122        self.check(b, a)
1123
1124        b = """execfile("fn", locals=loc)"""
1125        a = """exec(compile(open("fn").read(), "fn", 'exec'), locals=loc)"""
1126        self.check(b, a)
1127
1128        b = """execfile("fn", globals=glob, locals=loc)"""
1129        a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob, locals=loc)"""
1130        self.check(b, a)
1131
1132    def test_spacing(self):
1133        b = """execfile( "fn" )"""
1134        a = """exec(compile(open( "fn" ).read(), "fn", 'exec'))"""
1135        self.check(b, a)
1136
1137        b = """execfile("fn",  globals = glob)"""
1138        a = """exec(compile(open("fn").read(), "fn", 'exec'),  globals = glob)"""
1139        self.check(b, a)
1140
1141
1142class Test_isinstance(FixerTestCase):
1143    fixer = "isinstance"
1144
1145    def test_remove_multiple_items(self):
1146        b = """isinstance(x, (int, int, int))"""
1147        a = """isinstance(x, int)"""
1148        self.check(b, a)
1149
1150        b = """isinstance(x, (int, float, int, int, float))"""
1151        a = """isinstance(x, (int, float))"""
1152        self.check(b, a)
1153
1154        b = """isinstance(x, (int, float, int, int, float, str))"""
1155        a = """isinstance(x, (int, float, str))"""
1156        self.check(b, a)
1157
1158        b = """isinstance(foo() + bar(), (x(), y(), x(), int, int))"""
1159        a = """isinstance(foo() + bar(), (x(), y(), x(), int))"""
1160        self.check(b, a)
1161
1162    def test_prefix_preservation(self):
1163        b = """if    isinstance(  foo(), (  bar, bar, baz )) : pass"""
1164        a = """if    isinstance(  foo(), (  bar, baz )) : pass"""
1165        self.check(b, a)
1166
1167    def test_unchanged(self):
1168        self.unchanged("isinstance(x, (str, int))")
1169
1170class Test_dict(FixerTestCase):
1171    fixer = "dict"
1172
1173    def test_prefix_preservation(self):
1174        b = "if   d. keys  (  )  : pass"
1175        a = "if   list(d. keys  (  ))  : pass"
1176        self.check(b, a)
1177
1178        b = "if   d. items  (  )  : pass"
1179        a = "if   list(d. items  (  ))  : pass"
1180        self.check(b, a)
1181
1182        b = "if   d. iterkeys  ( )  : pass"
1183        a = "if   iter(d. keys  ( ))  : pass"
1184        self.check(b, a)
1185
1186        b = "[i for i in    d.  iterkeys(  )  ]"
1187        a = "[i for i in    d.  keys(  )  ]"
1188        self.check(b, a)
1189
1190    def test_trailing_comment(self):
1191        b = "d.keys() # foo"
1192        a = "list(d.keys()) # foo"
1193        self.check(b, a)
1194
1195        b = "d.items()  # foo"
1196        a = "list(d.items())  # foo"
1197        self.check(b, a)
1198
1199        b = "d.iterkeys()  # foo"
1200        a = "iter(d.keys())  # foo"
1201        self.check(b, a)
1202
1203        b = """[i for i in d.iterkeys() # foo
1204               ]"""
1205        a = """[i for i in d.keys() # foo
1206               ]"""
1207        self.check(b, a)
1208
1209    def test_unchanged(self):
1210        for wrapper in fixer_util.consuming_calls:
1211            s = "s = %s(d.keys())" % wrapper
1212            self.unchanged(s)
1213
1214            s = "s = %s(d.values())" % wrapper
1215            self.unchanged(s)
1216
1217            s = "s = %s(d.items())" % wrapper
1218            self.unchanged(s)
1219
1220    def test_01(self):
1221        b = "d.keys()"
1222        a = "list(d.keys())"
1223        self.check(b, a)
1224
1225        b = "a[0].foo().keys()"
1226        a = "list(a[0].foo().keys())"
1227        self.check(b, a)
1228
1229    def test_02(self):
1230        b = "d.items()"
1231        a = "list(d.items())"
1232        self.check(b, a)
1233
1234    def test_03(self):
1235        b = "d.values()"
1236        a = "list(d.values())"
1237        self.check(b, a)
1238
1239    def test_04(self):
1240        b = "d.iterkeys()"
1241        a = "iter(d.keys())"
1242        self.check(b, a)
1243
1244    def test_05(self):
1245        b = "d.iteritems()"
1246        a = "iter(d.items())"
1247        self.check(b, a)
1248
1249    def test_06(self):
1250        b = "d.itervalues()"
1251        a = "iter(d.values())"
1252        self.check(b, a)
1253
1254    def test_07(self):
1255        s = "list(d.keys())"
1256        self.unchanged(s)
1257
1258    def test_08(self):
1259        s = "sorted(d.keys())"
1260        self.unchanged(s)
1261
1262    def test_09(self):
1263        b = "iter(d.keys())"
1264        a = "iter(list(d.keys()))"
1265        self.check(b, a)
1266
1267    def test_10(self):
1268        b = "foo(d.keys())"
1269        a = "foo(list(d.keys()))"
1270        self.check(b, a)
1271
1272    def test_11(self):
1273        b = "for i in d.keys(): print i"
1274        a = "for i in list(d.keys()): print i"
1275        self.check(b, a)
1276
1277    def test_12(self):
1278        b = "for i in d.iterkeys(): print i"
1279        a = "for i in d.keys(): print i"
1280        self.check(b, a)
1281
1282    def test_13(self):
1283        b = "[i for i in d.keys()]"
1284        a = "[i for i in list(d.keys())]"
1285        self.check(b, a)
1286
1287    def test_14(self):
1288        b = "[i for i in d.iterkeys()]"
1289        a = "[i for i in d.keys()]"
1290        self.check(b, a)
1291
1292    def test_15(self):
1293        b = "(i for i in d.keys())"
1294        a = "(i for i in list(d.keys()))"
1295        self.check(b, a)
1296
1297    def test_16(self):
1298        b = "(i for i in d.iterkeys())"
1299        a = "(i for i in d.keys())"
1300        self.check(b, a)
1301
1302    def test_17(self):
1303        b = "iter(d.iterkeys())"
1304        a = "iter(d.keys())"
1305        self.check(b, a)
1306
1307    def test_18(self):
1308        b = "list(d.iterkeys())"
1309        a = "list(d.keys())"
1310        self.check(b, a)
1311
1312    def test_19(self):
1313        b = "sorted(d.iterkeys())"
1314        a = "sorted(d.keys())"
1315        self.check(b, a)
1316
1317    def test_20(self):
1318        b = "foo(d.iterkeys())"
1319        a = "foo(iter(d.keys()))"
1320        self.check(b, a)
1321
1322    def test_21(self):
1323        b = "print h.iterkeys().next()"
1324        a = "print iter(h.keys()).next()"
1325        self.check(b, a)
1326
1327    def test_22(self):
1328        b = "print h.keys()[0]"
1329        a = "print list(h.keys())[0]"
1330        self.check(b, a)
1331
1332    def test_23(self):
1333        b = "print list(h.iterkeys().next())"
1334        a = "print list(iter(h.keys()).next())"
1335        self.check(b, a)
1336
1337    def test_24(self):
1338        b = "for x in h.keys()[0]: print x"
1339        a = "for x in list(h.keys())[0]: print x"
1340        self.check(b, a)
1341
1342class Test_xrange(FixerTestCase):
1343    fixer = "xrange"
1344
1345    def test_prefix_preservation(self):
1346        b = """x =    xrange(  10  )"""
1347        a = """x =    range(  10  )"""
1348        self.check(b, a)
1349
1350        b = """x = xrange(  1  ,  10   )"""
1351        a = """x = range(  1  ,  10   )"""
1352        self.check(b, a)
1353
1354        b = """x = xrange(  0  ,  10 ,  2 )"""
1355        a = """x = range(  0  ,  10 ,  2 )"""
1356        self.check(b, a)
1357
1358    def test_single_arg(self):
1359        b = """x = xrange(10)"""
1360        a = """x = range(10)"""
1361        self.check(b, a)
1362
1363    def test_two_args(self):
1364        b = """x = xrange(1, 10)"""
1365        a = """x = range(1, 10)"""
1366        self.check(b, a)
1367
1368    def test_three_args(self):
1369        b = """x = xrange(0, 10, 2)"""
1370        a = """x = range(0, 10, 2)"""
1371        self.check(b, a)
1372
1373    def test_wrap_in_list(self):
1374        b = """x = range(10, 3, 9)"""
1375        a = """x = list(range(10, 3, 9))"""
1376        self.check(b, a)
1377
1378        b = """x = foo(range(10, 3, 9))"""
1379        a = """x = foo(list(range(10, 3, 9)))"""
1380        self.check(b, a)
1381
1382        b = """x = range(10, 3, 9) + [4]"""
1383        a = """x = list(range(10, 3, 9)) + [4]"""
1384        self.check(b, a)
1385
1386        b = """x = range(10)[::-1]"""
1387        a = """x = list(range(10))[::-1]"""
1388        self.check(b, a)
1389
1390        b = """x = range(10)  [3]"""
1391        a = """x = list(range(10))  [3]"""
1392        self.check(b, a)
1393
1394    def test_xrange_in_for(self):
1395        b = """for i in xrange(10):\n    j=i"""
1396        a = """for i in range(10):\n    j=i"""
1397        self.check(b, a)
1398
1399        b = """[i for i in xrange(10)]"""
1400        a = """[i for i in range(10)]"""
1401        self.check(b, a)
1402
1403    def test_range_in_for(self):
1404        self.unchanged("for i in range(10): pass")
1405        self.unchanged("[i for i in range(10)]")
1406
1407    def test_in_contains_test(self):
1408        self.unchanged("x in range(10, 3, 9)")
1409
1410    def test_in_consuming_context(self):
1411        for call in fixer_util.consuming_calls:
1412            self.unchanged("a = %s(range(10))" % call)
1413
1414class Test_raw_input(FixerTestCase):
1415    fixer = "raw_input"
1416
1417    def test_prefix_preservation(self):
1418        b = """x =    raw_input(   )"""
1419        a = """x =    input(   )"""
1420        self.check(b, a)
1421
1422        b = """x = raw_input(   ''   )"""
1423        a = """x = input(   ''   )"""
1424        self.check(b, a)
1425
1426    def test_1(self):
1427        b = """x = raw_input()"""
1428        a = """x = input()"""
1429        self.check(b, a)
1430
1431    def test_2(self):
1432        b = """x = raw_input('')"""
1433        a = """x = input('')"""
1434        self.check(b, a)
1435
1436    def test_3(self):
1437        b = """x = raw_input('prompt')"""
1438        a = """x = input('prompt')"""
1439        self.check(b, a)
1440
1441    def test_4(self):
1442        b = """x = raw_input(foo(a) + 6)"""
1443        a = """x = input(foo(a) + 6)"""
1444        self.check(b, a)
1445
1446    def test_5(self):
1447        b = """x = raw_input(invite).split()"""
1448        a = """x = input(invite).split()"""
1449        self.check(b, a)
1450
1451    def test_6(self):
1452        b = """x = raw_input(invite) . split ()"""
1453        a = """x = input(invite) . split ()"""
1454        self.check(b, a)
1455
1456    def test_8(self):
1457        b = "x = int(raw_input())"
1458        a = "x = int(input())"
1459        self.check(b, a)
1460
1461class Test_funcattrs(FixerTestCase):
1462    fixer = "funcattrs"
1463
1464    attrs = ["closure", "doc", "name", "defaults", "code", "globals", "dict"]
1465
1466    def test(self):
1467        for attr in self.attrs:
1468            b = "a.func_%s" % attr
1469            a = "a.__%s__" % attr
1470            self.check(b, a)
1471
1472            b = "self.foo.func_%s.foo_bar" % attr
1473            a = "self.foo.__%s__.foo_bar" % attr
1474            self.check(b, a)
1475
1476    def test_unchanged(self):
1477        for attr in self.attrs:
1478            s = "foo(func_%s + 5)" % attr
1479            self.unchanged(s)
1480
1481            s = "f(foo.__%s__)" % attr
1482            self.unchanged(s)
1483
1484            s = "f(foo.__%s__.foo)" % attr
1485            self.unchanged(s)
1486
1487class Test_xreadlines(FixerTestCase):
1488    fixer = "xreadlines"
1489
1490    def test_call(self):
1491        b = "for x in f.xreadlines(): pass"
1492        a = "for x in f: pass"
1493        self.check(b, a)
1494
1495        b = "for x in foo().xreadlines(): pass"
1496        a = "for x in foo(): pass"
1497        self.check(b, a)
1498
1499        b = "for x in (5 + foo()).xreadlines(): pass"
1500        a = "for x in (5 + foo()): pass"
1501        self.check(b, a)
1502
1503    def test_attr_ref(self):
1504        b = "foo(f.xreadlines + 5)"
1505        a = "foo(f.__iter__ + 5)"
1506        self.check(b, a)
1507
1508        b = "foo(f().xreadlines + 5)"
1509        a = "foo(f().__iter__ + 5)"
1510        self.check(b, a)
1511
1512        b = "foo((5 + f()).xreadlines + 5)"
1513        a = "foo((5 + f()).__iter__ + 5)"
1514        self.check(b, a)
1515
1516    def test_unchanged(self):
1517        s = "for x in f.xreadlines(5): pass"
1518        self.unchanged(s)
1519
1520        s = "for x in f.xreadlines(k=5): pass"
1521        self.unchanged(s)
1522
1523        s = "for x in f.xreadlines(*k, **v): pass"
1524        self.unchanged(s)
1525
1526        s = "foo(xreadlines)"
1527        self.unchanged(s)
1528
1529
1530class ImportsFixerTests:
1531
1532    def test_import_module(self):
1533        for old, new in self.modules.items():
1534            b = "import %s" % old
1535            a = "import %s" % new
1536            self.check(b, a)
1537
1538            b = "import foo, %s, bar" % old
1539            a = "import foo, %s, bar" % new
1540            self.check(b, a)
1541
1542    def test_import_from(self):
1543        for old, new in self.modules.items():
1544            b = "from %s import foo" % old
1545            a = "from %s import foo" % new
1546            self.check(b, a)
1547
1548            b = "from %s import foo, bar" % old
1549            a = "from %s import foo, bar" % new
1550            self.check(b, a)
1551
1552            b = "from %s import (yes, no)" % old
1553            a = "from %s import (yes, no)" % new
1554            self.check(b, a)
1555
1556    def test_import_module_as(self):
1557        for old, new in self.modules.items():
1558            b = "import %s as foo_bar" % old
1559            a = "import %s as foo_bar" % new
1560            self.check(b, a)
1561
1562            b = "import %s as foo_bar" % old
1563            a = "import %s as foo_bar" % new
1564            self.check(b, a)
1565
1566    def test_import_from_as(self):
1567        for old, new in self.modules.items():
1568            b = "from %s import foo as bar" % old
1569            a = "from %s import foo as bar" % new
1570            self.check(b, a)
1571
1572    def test_star(self):
1573        for old, new in self.modules.items():
1574            b = "from %s import *" % old
1575            a = "from %s import *" % new
1576            self.check(b, a)
1577
1578    def test_import_module_usage(self):
1579        for old, new in self.modules.items():
1580            b = """
1581                import %s
1582                foo(%s.bar)
1583                """ % (old, old)
1584            a = """
1585                import %s
1586                foo(%s.bar)
1587                """ % (new, new)
1588            self.check(b, a)
1589
1590            b = """
1591                from %s import x
1592                %s = 23
1593                """ % (old, old)
1594            a = """
1595                from %s import x
1596                %s = 23
1597                """ % (new, old)
1598            self.check(b, a)
1599
1600            s = """
1601                def f():
1602                    %s.method()
1603                """ % (old,)
1604            self.unchanged(s)
1605
1606            # test nested usage
1607            b = """
1608                import %s
1609                %s.bar(%s.foo)
1610                """ % (old, old, old)
1611            a = """
1612                import %s
1613                %s.bar(%s.foo)
1614                """ % (new, new, new)
1615            self.check(b, a)
1616
1617            b = """
1618                import %s
1619                x.%s
1620                """ % (old, old)
1621            a = """
1622                import %s
1623                x.%s
1624                """ % (new, old)
1625            self.check(b, a)
1626
1627
1628class Test_imports(FixerTestCase, ImportsFixerTests):
1629    fixer = "imports"
1630    from ..fixes.fix_imports import MAPPING as modules
1631
1632    def test_multiple_imports(self):
1633        b = """import urlparse, cStringIO"""
1634        a = """import urllib.parse, io"""
1635        self.check(b, a)
1636
1637    def test_multiple_imports_as(self):
1638        b = """
1639            import copy_reg as bar, HTMLParser as foo, urlparse
1640            s = urlparse.spam(bar.foo())
1641            """
1642        a = """
1643            import copyreg as bar, html.parser as foo, urllib.parse
1644            s = urllib.parse.spam(bar.foo())
1645            """
1646        self.check(b, a)
1647
1648
1649class Test_imports2(FixerTestCase, ImportsFixerTests):
1650    fixer = "imports2"
1651    from ..fixes.fix_imports2 import MAPPING as modules
1652
1653
1654class Test_imports_fixer_order(FixerTestCase, ImportsFixerTests):
1655
1656    def setUp(self):
1657        super(Test_imports_fixer_order, self).setUp(['imports', 'imports2'])
1658        from ..fixes.fix_imports2 import MAPPING as mapping2
1659        self.modules = mapping2.copy()
1660        from ..fixes.fix_imports import MAPPING as mapping1
1661        for key in ('dbhash', 'dumbdbm', 'dbm', 'gdbm'):
1662            self.modules[key] = mapping1[key]
1663
1664
1665class Test_urllib(FixerTestCase):
1666    fixer = "urllib"
1667    from ..fixes.fix_urllib import MAPPING as modules
1668
1669    def test_import_module(self):
1670        for old, changes in self.modules.items():
1671            b = "import %s" % old
1672            a = "import %s" % ", ".join(map(itemgetter(0), changes))
1673            self.check(b, a)
1674
1675    def test_import_from(self):
1676        for old, changes in self.modules.items():
1677            all_members = []
1678            for new, members in changes:
1679                for member in members:
1680                    all_members.append(member)
1681                    b = "from %s import %s" % (old, member)
1682                    a = "from %s import %s" % (new, member)
1683                    self.check(b, a)
1684
1685                    s = "from foo import %s" % member
1686                    self.unchanged(s)
1687
1688                b = "from %s import %s" % (old, ", ".join(members))
1689                a = "from %s import %s" % (new, ", ".join(members))
1690                self.check(b, a)
1691
1692                s = "from foo import %s" % ", ".join(members)
1693                self.unchanged(s)
1694
1695            # test the breaking of a module into multiple replacements
1696            b = "from %s import %s" % (old, ", ".join(all_members))
1697            a = "\n".join(["from %s import %s" % (new, ", ".join(members))
1698                            for (new, members) in changes])
1699            self.check(b, a)
1700
1701    def test_import_module_as(self):
1702        for old in self.modules:
1703            s = "import %s as foo" % old
1704            self.warns_unchanged(s, "This module is now multiple modules")
1705
1706    def test_import_from_as(self):
1707        for old, changes in self.modules.items():
1708            for new, members in changes:
1709                for member in members:
1710                    b = "from %s import %s as foo_bar" % (old, member)
1711                    a = "from %s import %s as foo_bar" % (new, member)
1712                    self.check(b, a)
1713
1714    def test_star(self):
1715        for old in self.modules:
1716            s = "from %s import *" % old
1717            self.warns_unchanged(s, "Cannot handle star imports")
1718
1719    def test_import_module_usage(self):
1720        for old, changes in self.modules.items():
1721            for new, members in changes:
1722                for member in members:
1723                    b = """
1724                        import %s
1725                        foo(%s.%s)
1726                        """ % (old, old, member)
1727                    a = """
1728                        import %s
1729                        foo(%s.%s)
1730                        """ % (", ".join([n for (n, mems)
1731                                           in self.modules[old]]),
1732                                         new, member)
1733                    self.check(b, a)
1734
1735
1736class Test_input(FixerTestCase):
1737    fixer = "input"
1738
1739    def test_prefix_preservation(self):
1740        b = """x =   input(   )"""
1741        a = """x =   eval(input(   ))"""
1742        self.check(b, a)
1743
1744        b = """x = input(   ''   )"""
1745        a = """x = eval(input(   ''   ))"""
1746        self.check(b, a)
1747
1748    def test_trailing_comment(self):
1749        b = """x = input()  #  foo"""
1750        a = """x = eval(input())  #  foo"""
1751        self.check(b, a)
1752
1753    def test_idempotency(self):
1754        s = """x = eval(input())"""
1755        self.unchanged(s)
1756
1757        s = """x = eval(input(''))"""
1758        self.unchanged(s)
1759
1760        s = """x = eval(input(foo(5) + 9))"""
1761        self.unchanged(s)
1762
1763    def test_1(self):
1764        b = """x = input()"""
1765        a = """x = eval(input())"""
1766        self.check(b, a)
1767
1768    def test_2(self):
1769        b = """x = input('')"""
1770        a = """x = eval(input(''))"""
1771        self.check(b, a)
1772
1773    def test_3(self):
1774        b = """x = input('prompt')"""
1775        a = """x = eval(input('prompt'))"""
1776        self.check(b, a)
1777
1778    def test_4(self):
1779        b = """x = input(foo(5) + 9)"""
1780        a = """x = eval(input(foo(5) + 9))"""
1781        self.check(b, a)
1782
1783class Test_tuple_params(FixerTestCase):
1784    fixer = "tuple_params"
1785
1786    def test_unchanged_1(self):
1787        s = """def foo(): pass"""
1788        self.unchanged(s)
1789
1790    def test_unchanged_2(self):
1791        s = """def foo(a, b, c): pass"""
1792        self.unchanged(s)
1793
1794    def test_unchanged_3(self):
1795        s = """def foo(a=3, b=4, c=5): pass"""
1796        self.unchanged(s)
1797
1798    def test_1(self):
1799        b = """
1800            def foo(((a, b), c)):
1801                x = 5"""
1802
1803        a = """
1804            def foo(xxx_todo_changeme):
1805                ((a, b), c) = xxx_todo_changeme
1806                x = 5"""
1807        self.check(b, a)
1808
1809    def test_2(self):
1810        b = """
1811            def foo(((a, b), c), d):
1812                x = 5"""
1813
1814        a = """
1815            def foo(xxx_todo_changeme, d):
1816                ((a, b), c) = xxx_todo_changeme
1817                x = 5"""
1818        self.check(b, a)
1819
1820    def test_3(self):
1821        b = """
1822            def foo(((a, b), c), d) -> e:
1823                x = 5"""
1824
1825        a = """
1826            def foo(xxx_todo_changeme, d) -> e:
1827                ((a, b), c) = xxx_todo_changeme
1828                x = 5"""
1829        self.check(b, a)
1830
1831    def test_semicolon(self):
1832        b = """
1833            def foo(((a, b), c)): x = 5; y = 7"""
1834
1835        a = """
1836            def foo(xxx_todo_changeme): ((a, b), c) = xxx_todo_changeme; x = 5; y = 7"""
1837        self.check(b, a)
1838
1839    def test_keywords(self):
1840        b = """
1841            def foo(((a, b), c), d, e=5) -> z:
1842                x = 5"""
1843
1844        a = """
1845            def foo(xxx_todo_changeme, d, e=5) -> z:
1846                ((a, b), c) = xxx_todo_changeme
1847                x = 5"""
1848        self.check(b, a)
1849
1850    def test_varargs(self):
1851        b = """
1852            def foo(((a, b), c), d, *vargs, **kwargs) -> z:
1853                x = 5"""
1854
1855        a = """
1856            def foo(xxx_todo_changem…

Large files files are truncated, but you can click here to view the full file