~pythonregexp2.7/python/issue2636-09-01+10

« back to all changes in this revision

Viewing changes to Lib/lib2to3/tests/test_fixers.py

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-22 21:39:45 UTC
  • mfrom: (39055.1.33 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080922213945-23717m5eiqpamcyn
Merged in changes from the Single-Loop Engine branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 
11
11
# Python imports
12
12
import unittest
 
13
from itertools import chain
 
14
from operator import itemgetter
13
15
from os.path import dirname, pathsep
14
16
 
15
17
# Local imports
16
18
from .. import pygram
17
19
from .. import pytree
18
20
from .. import refactor
19
 
from ..fixes import util
20
 
 
21
 
 
22
 
class Options:
23
 
    def __init__(self, **kwargs):
24
 
        for k, v in kwargs.items():
25
 
            setattr(self, k, v)
26
 
 
27
 
        self.verbose = False
 
21
from .. import fixer_util
 
22
 
28
23
 
29
24
class FixerTestCase(support.TestCase):
30
 
    def setUp(self):
31
 
        options = Options(fix=[self.fixer], print_function=False)
32
 
        self.refactor = refactor.RefactoringTool(options)
 
25
    def setUp(self, fix_list=None):
 
26
        if fix_list is None:
 
27
            fix_list = [self.fixer]
 
28
        options = {"print_function" : False}
 
29
        self.refactor = support.get_refactorer(fix_list, options)
33
30
        self.fixer_log = []
34
31
        self.filename = "<string>"
35
32
 
36
 
        from itertools import chain
37
33
        for order in (self.refactor.pre_order.values(),\
38
34
                      self.refactor.post_order.values()):
39
35
            for fixer in chain(*order):
67
63
            self.failUnlessEqual(self.fixer_log, [])
68
64
 
69
65
    def assert_runs_after(self, *names):
70
 
        fix = [self.fixer]
71
 
        fix.extend(names)
72
 
        options = Options(fix=fix, print_function=False)
73
 
        r = refactor.RefactoringTool(options)
 
66
        fixes = [self.fixer]
 
67
        fixes.extend(names)
 
68
        options = {"print_function" : False}
 
69
        r = support.get_refactorer(fixes, options)
74
70
        (pre, post) = r.get_fixers()
75
71
        n = "fix_" + self.fixer
76
72
        if post and post[-1].__class__.__module__.endswith(n):
389
385
        a = """print()"""
390
386
        self.check(b, a)
391
387
 
 
388
    def test_4(self):
 
389
        # from bug 3000
 
390
        b = """print whatever; print"""
 
391
        a = """print(whatever); print()"""
 
392
        self.check(b, a)
 
393
 
 
394
    def test_5(self):
 
395
        b = """print; print whatever;"""
 
396
        a = """print(); print(whatever);"""
 
397
 
392
398
    def test_tuple(self):
393
399
        b = """print (a, b, c)"""
394
400
        a = """print((a, b, c))"""
1109
1115
        self.check(b, a)
1110
1116
 
1111
1117
    def test_unchanged(self):
1112
 
        for wrapper in util.consuming_calls:
 
1118
        for wrapper in fixer_util.consuming_calls:
1113
1119
            s = "s = %s(d.keys())" % wrapper
1114
1120
            self.unchanged(s)
1115
1121
 
1302
1308
        self.unchanged("x in range(10, 3, 9)")
1303
1309
 
1304
1310
    def test_in_consuming_context(self):
1305
 
        for call in util.consuming_calls:
 
1311
        for call in fixer_util.consuming_calls:
1306
1312
            self.unchanged("a = %s(range(10))" % call)
1307
1313
 
1308
1314
class Test_raw_input(FixerTestCase):
1337
1343
        a = """x = input(foo(a) + 6)"""
1338
1344
        self.check(b, a)
1339
1345
 
 
1346
    def test_5(self):
 
1347
        b = """x = raw_input(invite).split()"""
 
1348
        a = """x = input(invite).split()"""
 
1349
        self.check(b, a)
 
1350
 
 
1351
    def test_6(self):
 
1352
        b = """x = raw_input(invite) . split ()"""
 
1353
        a = """x = input(invite) . split ()"""
 
1354
        self.check(b, a)
 
1355
 
 
1356
    def test_8(self):
 
1357
        b = "x = int(raw_input())"
 
1358
        a = "x = int(input())"
 
1359
        self.check(b, a)
 
1360
 
1340
1361
class Test_funcattrs(FixerTestCase):
1341
1362
    fixer = "funcattrs"
1342
1363
 
1405
1426
        s = "foo(xreadlines)"
1406
1427
        self.unchanged(s)
1407
1428
 
1408
 
# Disable test, as it takes a too long time to run, and also
1409
 
# fails in 2.6.
1410
 
#class Test_imports(FixerTestCase):
1411
 
class Test_imports:
 
1429
class Test_imports(FixerTestCase):
1412
1430
    fixer = "imports"
1413
1431
    from ..fixes.fix_imports import MAPPING as modules
1414
1432
 
1415
1433
    def test_import_module(self):
1416
 
        for old, (new, members) in self.modules.items():
 
1434
        for old, new in self.modules.items():
1417
1435
            b = "import %s" % old
1418
1436
            a = "import %s" % new
1419
1437
            self.check(b, a)
1423
1441
            self.check(b, a)
1424
1442
 
1425
1443
    def test_import_from(self):
1426
 
        for old, (new, members) in self.modules.items():
1427
 
            for member in members:
1428
 
                b = "from %s import %s" % (old, member)
1429
 
                a = "from %s import %s" % (new, member)
 
1444
        for old, new in self.modules.items():
 
1445
            b = "from %s import foo" % old
 
1446
            a = "from %s import foo" % new
 
1447
            self.check(b, a)
 
1448
 
 
1449
            b = "from %s import foo, bar" % old
 
1450
            a = "from %s import foo, bar" % new
 
1451
            self.check(b, a)
 
1452
 
 
1453
    def test_import_module_as(self):
 
1454
        for old, new in self.modules.items():
 
1455
            b = "import %s as foo_bar" % old
 
1456
            a = "import %s as foo_bar" % new
 
1457
            self.check(b, a)
 
1458
 
 
1459
            b = "import %s as foo_bar" % old
 
1460
            a = "import %s as foo_bar" % new
 
1461
            self.check(b, a)
 
1462
 
 
1463
    def test_import_from_as(self):
 
1464
        for old, new in self.modules.items():
 
1465
            b = "from %s import foo as bar" % old
 
1466
            a = "from %s import foo as bar" % new
 
1467
            self.check(b, a)
 
1468
 
 
1469
    def test_star(self):
 
1470
        for old, new in self.modules.items():
 
1471
            b = "from %s import *" % old
 
1472
            a = "from %s import *" % new
 
1473
            self.check(b, a)
 
1474
 
 
1475
    def test_import_module_usage(self):
 
1476
        for old, new in self.modules.items():
 
1477
            b = """
 
1478
                import %s
 
1479
                foo(%s.bar)
 
1480
                """ % (old, old)
 
1481
            a = """
 
1482
                import %s
 
1483
                foo(%s.bar)
 
1484
                """ % (new, new)
 
1485
            self.check(b, a)
 
1486
 
 
1487
 
 
1488
class Test_imports2(Test_imports):
 
1489
    fixer = "imports2"
 
1490
    from ..fixes.fix_imports2 import MAPPING as modules
 
1491
 
 
1492
 
 
1493
class Test_imports_fixer_order(Test_imports):
 
1494
 
 
1495
    fixer = None
 
1496
 
 
1497
    def setUp(self):
 
1498
        Test_imports.setUp(self, ['imports', 'imports2'])
 
1499
        from ..fixes.fix_imports2 import MAPPING as mapping2
 
1500
        self.modules = mapping2.copy()
 
1501
        from ..fixes.fix_imports import MAPPING as mapping1
 
1502
        for key in ('dbhash', 'dumbdbm', 'dbm', 'gdbm'):
 
1503
            self.modules[key] = mapping1[key]
 
1504
 
 
1505
 
 
1506
class Test_urllib(FixerTestCase):
 
1507
    fixer = "urllib"
 
1508
    from ..fixes.fix_urllib import MAPPING as modules
 
1509
 
 
1510
    def test_import_module(self):
 
1511
        for old, changes in self.modules.items():
 
1512
            b = "import %s" % old
 
1513
            a = "import %s" % ", ".join(map(itemgetter(0), changes))
 
1514
            self.check(b, a)
 
1515
 
 
1516
    def test_import_from(self):
 
1517
        for old, changes in self.modules.items():
 
1518
            all_members = []
 
1519
            for new, members in changes:
 
1520
                for member in members:
 
1521
                    all_members.append(member)
 
1522
                    b = "from %s import %s" % (old, member)
 
1523
                    a = "from %s import %s" % (new, member)
 
1524
                    self.check(b, a)
 
1525
 
 
1526
                    s = "from foo import %s" % member
 
1527
                    self.unchanged(s)
 
1528
 
 
1529
                b = "from %s import %s" % (old, ", ".join(members))
 
1530
                a = "from %s import %s" % (new, ", ".join(members))
1430
1531
                self.check(b, a)
1431
1532
 
1432
 
                s = "from foo import %s" % member
 
1533
                s = "from foo import %s" % ", ".join(members)
1433
1534
                self.unchanged(s)
1434
1535
 
1435
 
            b = "from %s import %s" % (old, ", ".join(members))
1436
 
            a = "from %s import %s" % (new, ", ".join(members))
 
1536
            # test the breaking of a module into multiple replacements
 
1537
            b = "from %s import %s" % (old, ", ".join(all_members))
 
1538
            a = "\n".join(["from %s import %s" % (new, ", ".join(members))
 
1539
                            for (new, members) in changes])
1437
1540
            self.check(b, a)
1438
1541
 
1439
 
            s = "from foo import %s" % ", ".join(members)
1440
 
            self.unchanged(s)
1441
 
 
1442
1542
    def test_import_module_as(self):
1443
 
        for old, (new, members) in self.modules.items():
1444
 
            b = "import %s as foo_bar" % old
1445
 
            a = "import %s as foo_bar" % new
1446
 
            self.check(b, a)
1447
 
 
1448
 
            b = "import %s as foo_bar" % old
1449
 
            a = "import %s as foo_bar" % new
1450
 
            self.check(b, a)
 
1543
        for old in self.modules:
 
1544
            s = "import %s as foo" % old
 
1545
            self.warns_unchanged(s, "This module is now multiple modules")
1451
1546
 
1452
1547
    def test_import_from_as(self):
1453
 
        for old, (new, members) in self.modules.items():
1454
 
            for member in members:
1455
 
                b = "from %s import %s as foo_bar" % (old, member)
1456
 
                a = "from %s import %s as foo_bar" % (new, member)
1457
 
                self.check(b, a)
 
1548
        for old, changes in self.modules.items():
 
1549
            for new, members in changes:
 
1550
                for member in members:
 
1551
                    b = "from %s import %s as foo_bar" % (old, member)
 
1552
                    a = "from %s import %s as foo_bar" % (new, member)
 
1553
                    self.check(b, a)
1458
1554
 
1459
1555
    def test_star(self):
1460
1556
        for old in self.modules:
1462
1558
            self.warns_unchanged(s, "Cannot handle star imports")
1463
1559
 
1464
1560
    def test_import_module_usage(self):
1465
 
        for old, (new, members) in self.modules.items():
1466
 
            for member in members:
1467
 
                b = """
1468
 
                    import %s
1469
 
                    foo(%s, %s.%s)
1470
 
                    """ % (old, old, old, member)
1471
 
                a = """
1472
 
                    import %s
1473
 
                    foo(%s, %s.%s)
1474
 
                    """ % (new, new, new, member)
1475
 
                self.check(b, a)
1476
 
 
1477
 
    def test_from_import_usage(self):
1478
 
        for old, (new, members) in self.modules.items():
1479
 
            for member in members:
1480
 
                b = """
1481
 
                    from %s import %s
1482
 
                    foo(%s, %s())
1483
 
                    """ % (old, member, member, member)
1484
 
                a = """
1485
 
                    from %s import %s
1486
 
                    foo(%s, %s())
1487
 
                    """ % (new, member, member, member)
1488
 
                self.check(b, a)
1489
 
            b = """
1490
 
                from %s import %s
1491
 
                foo(%s)
1492
 
                """ % (old, ", ".join(members), ", ".join(members))
1493
 
            a = """
1494
 
                from %s import %s
1495
 
                foo(%s)
1496
 
                """ % (new, ", ".join(members), ", ".join(members))
1497
 
            self.check(b, a)
 
1561
        for old, changes in self.modules.items():
 
1562
            for new, members in changes:
 
1563
                for member in members:
 
1564
                    b = """
 
1565
                        import %s
 
1566
                        foo(%s.%s)
 
1567
                        """ % (old, old, member)
 
1568
                    a = """
 
1569
                        import %s
 
1570
                        foo(%s.%s)
 
1571
                        """ % (", ".join([n for (n, mems)
 
1572
                                           in self.modules[old]]),
 
1573
                                         new, member)
 
1574
                    self.check(b, a)
1498
1575
 
1499
1576
 
1500
1577
class Test_input(FixerTestCase):
2312
2389
        a = """b = 0x12"""
2313
2390
        self.check(b, a)
2314
2391
 
 
2392
    def test_comments_and_spacing(self):
 
2393
        b = """b =   0x12L"""
 
2394
        a = """b =   0x12"""
 
2395
        self.check(b, a)
 
2396
 
 
2397
        b = """b = 0755 # spam"""
 
2398
        a = """b = 0o755 # spam"""
 
2399
        self.check(b, a)
 
2400
 
2315
2401
    def test_unchanged_int(self):
2316
2402
        s = """5"""
2317
2403
        self.unchanged(s)
3260
3346
        a = "from . import foo.bar as bang"
3261
3347
        self.check_both(b, a)
3262
3348
 
 
3349
    def test_prefix(self):
 
3350
        b = """
 
3351
        # prefix
 
3352
        import foo.bar
 
3353
        """
 
3354
        a = """
 
3355
        # prefix
 
3356
        from . import foo.bar
 
3357
        """
 
3358
        self.check_both(b, a)
 
3359
 
 
3360
class Test_sys_exc(FixerTestCase):
 
3361
    fixer = "sys_exc"
 
3362
 
 
3363
    def test_0(self):
 
3364
        b = "sys.exc_type"
 
3365
        a = "sys.exc_info()[0]"
 
3366
        self.check(b, a)
 
3367
 
 
3368
    def test_1(self):
 
3369
        b = "sys.exc_value"
 
3370
        a = "sys.exc_info()[1]"
 
3371
        self.check(b, a)
 
3372
 
 
3373
    def test_2(self):
 
3374
        b = "sys.exc_traceback"
 
3375
        a = "sys.exc_info()[2]"
 
3376
        self.check(b, a)
 
3377
 
 
3378
    def test_3(self):
 
3379
        b = "sys.exc_type # Foo"
 
3380
        a = "sys.exc_info()[0] # Foo"
 
3381
        self.check(b, a)
 
3382
 
 
3383
    def test_4(self):
 
3384
        b = "sys.  exc_type"
 
3385
        a = "sys.  exc_info()[0]"
 
3386
        self.check(b, a)
 
3387
 
 
3388
    def test_5(self):
 
3389
        b = "sys  .exc_type"
 
3390
        a = "sys  .exc_info()[0]"
 
3391
        self.check(b, a)
 
3392
 
 
3393
 
 
3394
class Test_paren(FixerTestCase):
 
3395
    fixer = "paren"
 
3396
 
 
3397
    def test_0(self):
 
3398
        b = """[i for i in 1, 2 ]"""
 
3399
        a = """[i for i in (1, 2) ]"""
 
3400
        self.check(b, a)
 
3401
 
 
3402
    def test_1(self):
 
3403
        b = """[i for i in 1, 2, ]"""
 
3404
        a = """[i for i in (1, 2,) ]"""
 
3405
        self.check(b, a)
 
3406
 
 
3407
    def test_2(self):
 
3408
        b = """[i for i  in     1, 2 ]"""
 
3409
        a = """[i for i  in     (1, 2) ]"""
 
3410
        self.check(b, a)
 
3411
 
 
3412
    def test_3(self):
 
3413
        b = """[i for i in 1, 2 if i]"""
 
3414
        a = """[i for i in (1, 2) if i]"""
 
3415
        self.check(b, a)
 
3416
 
 
3417
    def test_4(self):
 
3418
        b = """[i for i in 1,    2    ]"""
 
3419
        a = """[i for i in (1,    2)    ]"""
 
3420
        self.check(b, a)
 
3421
 
 
3422
    def test_5(self):
 
3423
        b = """(i for i in 1, 2)"""
 
3424
        a = """(i for i in (1, 2))"""
 
3425
        self.check(b, a)
 
3426
 
 
3427
    def test_6(self):
 
3428
        b = """(i for i in 1   ,2   if i)"""
 
3429
        a = """(i for i in (1   ,2)   if i)"""
 
3430
        self.check(b, a)
 
3431
 
 
3432
    def test_unchanged_0(self):
 
3433
        s = """[i for i in (1, 2)]"""
 
3434
        self.unchanged(s)
 
3435
 
 
3436
    def test_unchanged_1(self):
 
3437
        s = """[i for i in foo()]"""
 
3438
        self.unchanged(s)
 
3439
 
 
3440
    def test_unchanged_2(self):
 
3441
        s = """[i for i in (1, 2) if nothing]"""
 
3442
        self.unchanged(s)
 
3443
 
 
3444
    def test_unchanged_3(self):
 
3445
        s = """(i for i in (1, 2))"""
 
3446
        self.unchanged(s)
 
3447
 
 
3448
    def test_unchanged_4(self):
 
3449
        s = """[i for i in m]"""
 
3450
        self.unchanged(s)
 
3451
 
 
3452
class Test_metaclass(FixerTestCase):
 
3453
 
 
3454
    fixer = 'metaclass'
 
3455
 
 
3456
    def test_unchanged(self):
 
3457
        self.unchanged("class X(): pass")
 
3458
        self.unchanged("class X(object): pass")
 
3459
        self.unchanged("class X(object1, object2): pass")
 
3460
        self.unchanged("class X(object1, object2, object3): pass")
 
3461
        self.unchanged("class X(metaclass=Meta): pass")
 
3462
        self.unchanged("class X(b, arg=23, metclass=Meta): pass")
 
3463
        self.unchanged("class X(b, arg=23, metaclass=Meta, other=42): pass")
 
3464
 
 
3465
        s = """
 
3466
        class X:
 
3467
            def __metaclass__(self): pass
 
3468
        """
 
3469
        self.unchanged(s)
 
3470
 
 
3471
    def test_comments(self):
 
3472
        b = """
 
3473
        class X:
 
3474
            # hi
 
3475
            __metaclass__ = AppleMeta
 
3476
        """
 
3477
        a = """
 
3478
        class X(metaclass=AppleMeta):
 
3479
            # hi
 
3480
            pass
 
3481
        """
 
3482
        self.check(b, a)
 
3483
 
 
3484
        b = """
 
3485
        class X:
 
3486
            __metaclass__ = Meta
 
3487
            # Bedtime!
 
3488
        """
 
3489
        a = """
 
3490
        class X(metaclass=Meta):
 
3491
            pass
 
3492
            # Bedtime!
 
3493
        """
 
3494
        self.check(b, a)
 
3495
 
 
3496
    def test_meta(self):
 
3497
        # no-parent class, odd body
 
3498
        b = """
 
3499
        class X():
 
3500
            __metaclass__ = Q
 
3501
            pass
 
3502
        """
 
3503
        a = """
 
3504
        class X(metaclass=Q):
 
3505
            pass
 
3506
        """
 
3507
        self.check(b, a)
 
3508
 
 
3509
        # one parent class, no body
 
3510
        b = """class X(object): __metaclass__ = Q"""
 
3511
        a = """class X(object, metaclass=Q): pass"""
 
3512
        self.check(b, a)
 
3513
 
 
3514
 
 
3515
        # one parent, simple body
 
3516
        b = """
 
3517
        class X(object):
 
3518
            __metaclass__ = Meta
 
3519
            bar = 7
 
3520
        """
 
3521
        a = """
 
3522
        class X(object, metaclass=Meta):
 
3523
            bar = 7
 
3524
        """
 
3525
        self.check(b, a)
 
3526
 
 
3527
        b = """
 
3528
        class X:
 
3529
            __metaclass__ = Meta; x = 4; g = 23
 
3530
        """
 
3531
        a = """
 
3532
        class X(metaclass=Meta):
 
3533
            x = 4; g = 23
 
3534
        """
 
3535
        self.check(b, a)
 
3536
 
 
3537
        # one parent, simple body, __metaclass__ last
 
3538
        b = """
 
3539
        class X(object):
 
3540
            bar = 7
 
3541
            __metaclass__ = Meta
 
3542
        """
 
3543
        a = """
 
3544
        class X(object, metaclass=Meta):
 
3545
            bar = 7
 
3546
        """
 
3547
        self.check(b, a)
 
3548
 
 
3549
        # redefining __metaclass__
 
3550
        b = """
 
3551
        class X():
 
3552
            __metaclass__ = A
 
3553
            __metaclass__ = B
 
3554
            bar = 7
 
3555
        """
 
3556
        a = """
 
3557
        class X(metaclass=B):
 
3558
            bar = 7
 
3559
        """
 
3560
        self.check(b, a)
 
3561
 
 
3562
        # multiple inheritance, simple body
 
3563
        b = """
 
3564
        class X(clsA, clsB):
 
3565
            __metaclass__ = Meta
 
3566
            bar = 7
 
3567
        """
 
3568
        a = """
 
3569
        class X(clsA, clsB, metaclass=Meta):
 
3570
            bar = 7
 
3571
        """
 
3572
        self.check(b, a)
 
3573
 
 
3574
        # keywords in the class statement
 
3575
        b = """class m(a, arg=23): __metaclass__ = Meta"""
 
3576
        a = """class m(a, arg=23, metaclass=Meta): pass"""
 
3577
        self.check(b, a)
 
3578
 
3263
3579
 
3264
3580
if __name__ == "__main__":
3265
3581
    import __main__