~malept/ubuntu/lucid/python2.6/dev-dependency-fix

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-02-13 12:51:00 UTC
  • Revision ID: james.westby@ubuntu.com-20090213125100-uufgcb9yeqzujpqw
Tags: upstream-2.6.1
ImportĀ upstreamĀ versionĀ 2.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python2.5
 
2
""" Test suite for the fixer modules """
 
3
# Author: Collin Winter
 
4
 
 
5
# Testing imports
 
6
try:
 
7
    from tests import support
 
8
except ImportError:
 
9
    import support
 
10
 
 
11
# Python imports
 
12
import os
 
13
import unittest
 
14
from itertools import chain
 
15
from operator import itemgetter
 
16
 
 
17
# Local imports
 
18
from lib2to3 import pygram, pytree, refactor, fixer_util
 
19
 
 
20
 
 
21
class 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
 
 
77
class 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
 
 
104
class 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
 
 
162
class 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
 
 
291
class Test_intern(FixerTestCase):
 
292
    fixer = "intern"
 
293
 
 
294
    def test_prefix_preservation(self):
 
295
        b = """x =   intern(  a  )"""
 
296
        a = """x =   sys.intern(  a  )"""
 
297
        self.check(b, a)
 
298
 
 
299
        b = """y = intern("b" # test
 
300
              )"""
 
301
        a = """y = sys.intern("b" # test
 
302
              )"""
 
303
        self.check(b, a)
 
304
 
 
305
        b = """z = intern(a+b+c.d,   )"""
 
306
        a = """z = sys.intern(a+b+c.d,   )"""
 
307
        self.check(b, a)
 
308
 
 
309
    def test(self):
 
310
        b = """x = intern(a)"""
 
311
        a = """x = sys.intern(a)"""
 
312
        self.check(b, a)
 
313
 
 
314
        b = """z = intern(a+b+c.d,)"""
 
315
        a = """z = sys.intern(a+b+c.d,)"""
 
316
        self.check(b, a)
 
317
 
 
318
        b = """intern("y%s" % 5).replace("y", "")"""
 
319
        a = """sys.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
 
 
340
class Test_print(FixerTestCase):
 
341
    fixer = "print"
 
342
 
 
343
    def test_prefix_preservation(self):
 
344
        b = """print 1,   1+1,   1+1+1"""
 
345
        a = """print(1,   1+1,   1+1+1)"""
 
346
        self.check(b, a)
 
347
 
 
348
    def test_idempotency(self):
 
349
        s = """print()"""
 
350
        self.unchanged(s)
 
351
 
 
352
        s = """print('')"""
 
353
        self.unchanged(s)
 
354
 
 
355
    def test_idempotency_print_as_function(self):
 
356
        print_stmt = pygram.python_grammar.keywords.pop("print")
 
357
        try:
 
358
            s = """print(1, 1+1, 1+1+1)"""
 
359
            self.unchanged(s)
 
360
 
 
361
            s = """print()"""
 
362
            self.unchanged(s)
 
363
 
 
364
            s = """print('')"""
 
365
            self.unchanged(s)
 
366
        finally:
 
367
            pygram.python_grammar.keywords["print"] = print_stmt
 
368
 
 
369
    def test_1(self):
 
370
        b = """print 1, 1+1, 1+1+1"""
 
371
        a = """print(1, 1+1, 1+1+1)"""
 
372
        self.check(b, a)
 
373
 
 
374
    def test_2(self):
 
375
        b = """print 1, 2"""
 
376
        a = """print(1, 2)"""
 
377
        self.check(b, a)
 
378
 
 
379
    def test_3(self):
 
380
        b = """print"""
 
381
        a = """print()"""
 
382
        self.check(b, a)
 
383
 
 
384
    def test_4(self):
 
385
        # from bug 3000
 
386
        b = """print whatever; print"""
 
387
        a = """print(whatever); print()"""
 
388
        self.check(b, a)
 
389
 
 
390
    def test_5(self):
 
391
        b = """print; print whatever;"""
 
392
        a = """print(); print(whatever);"""
 
393
 
 
394
    def test_tuple(self):
 
395
        b = """print (a, b, c)"""
 
396
        a = """print((a, b, c))"""
 
397
        self.check(b, a)
 
398
 
 
399
    # trailing commas
 
400
 
 
401
    def test_trailing_comma_1(self):
 
402
        b = """print 1, 2, 3,"""
 
403
        a = """print(1, 2, 3, end=' ')"""
 
404
        self.check(b, a)
 
405
 
 
406
    def test_trailing_comma_2(self):
 
407
        b = """print 1, 2,"""
 
408
        a = """print(1, 2, end=' ')"""
 
409
        self.check(b, a)
 
410
 
 
411
    def test_trailing_comma_3(self):
 
412
        b = """print 1,"""
 
413
        a = """print(1, end=' ')"""
 
414
        self.check(b, a)
 
415
 
 
416
    # >> stuff
 
417
 
 
418
    def test_vargs_without_trailing_comma(self):
 
419
        b = """print >>sys.stderr, 1, 2, 3"""
 
420
        a = """print(1, 2, 3, file=sys.stderr)"""
 
421
        self.check(b, a)
 
422
 
 
423
    def test_with_trailing_comma(self):
 
424
        b = """print >>sys.stderr, 1, 2,"""
 
425
        a = """print(1, 2, end=' ', file=sys.stderr)"""
 
426
        self.check(b, a)
 
427
 
 
428
    def test_no_trailing_comma(self):
 
429
        b = """print >>sys.stderr, 1+1"""
 
430
        a = """print(1+1, file=sys.stderr)"""
 
431
        self.check(b, a)
 
432
 
 
433
    def test_spaces_before_file(self):
 
434
        b = """print >>  sys.stderr"""
 
435
        a = """print(file=sys.stderr)"""
 
436
        self.check(b, a)
 
437
 
 
438
    # With from __future__ import print_function
 
439
    def test_with_future_print_function(self):
 
440
        # XXX: These tests won't actually do anything until the parser
 
441
        #      is fixed so it won't crash when it sees print(x=y).
 
442
        #      When #2412 is fixed, the try/except block can be taken
 
443
        #      out and the tests can be run like normal.
 
444
        # MvL: disable entirely for now, so that it doesn't print to stdout
 
445
        return
 
446
        try:
 
447
            s = "from __future__ import print_function\n"\
 
448
                "print('Hai!', end=' ')"
 
449
            self.unchanged(s)
 
450
 
 
451
            b = "print 'Hello, world!'"
 
452
            a = "print('Hello, world!')"
 
453
            self.check(b, a)
 
454
 
 
455
            s = "from __future__ import *\n"\
 
456
                "print('Hai!', end=' ')"
 
457
            self.unchanged(s)
 
458
        except:
 
459
            return
 
460
        else:
 
461
            self.assertFalse(True, "#2421 has been fixed -- printing tests "\
 
462
                                   "need to be updated!")
 
463
 
 
464
class Test_exec(FixerTestCase):
 
465
    fixer = "exec"
 
466
 
 
467
    def test_prefix_preservation(self):
 
468
        b = """  exec code in ns1,   ns2"""
 
469
        a = """  exec(code, ns1,   ns2)"""
 
470
        self.check(b, a)
 
471
 
 
472
    def test_basic(self):
 
473
        b = """exec code"""
 
474
        a = """exec(code)"""
 
475
        self.check(b, a)
 
476
 
 
477
    def test_with_globals(self):
 
478
        b = """exec code in ns"""
 
479
        a = """exec(code, ns)"""
 
480
        self.check(b, a)
 
481
 
 
482
    def test_with_globals_locals(self):
 
483
        b = """exec code in ns1, ns2"""
 
484
        a = """exec(code, ns1, ns2)"""
 
485
        self.check(b, a)
 
486
 
 
487
    def test_complex_1(self):
 
488
        b = """exec (a.b()) in ns"""
 
489
        a = """exec((a.b()), ns)"""
 
490
        self.check(b, a)
 
491
 
 
492
    def test_complex_2(self):
 
493
        b = """exec a.b() + c in ns"""
 
494
        a = """exec(a.b() + c, ns)"""
 
495
        self.check(b, a)
 
496
 
 
497
    # These should not be touched
 
498
 
 
499
    def test_unchanged_1(self):
 
500
        s = """exec(code)"""
 
501
        self.unchanged(s)
 
502
 
 
503
    def test_unchanged_2(self):
 
504
        s = """exec (code)"""
 
505
        self.unchanged(s)
 
506
 
 
507
    def test_unchanged_3(self):
 
508
        s = """exec(code, ns)"""
 
509
        self.unchanged(s)
 
510
 
 
511
    def test_unchanged_4(self):
 
512
        s = """exec(code, ns1, ns2)"""
 
513
        self.unchanged(s)
 
514
 
 
515
class Test_repr(FixerTestCase):
 
516
    fixer = "repr"
 
517
 
 
518
    def test_prefix_preservation(self):
 
519
        b = """x =   `1 + 2`"""
 
520
        a = """x =   repr(1 + 2)"""
 
521
        self.check(b, a)
 
522
 
 
523
    def test_simple_1(self):
 
524
        b = """x = `1 + 2`"""
 
525
        a = """x = repr(1 + 2)"""
 
526
        self.check(b, a)
 
527
 
 
528
    def test_simple_2(self):
 
529
        b = """y = `x`"""
 
530
        a = """y = repr(x)"""
 
531
        self.check(b, a)
 
532
 
 
533
    def test_complex(self):
 
534
        b = """z = `y`.__repr__()"""
 
535
        a = """z = repr(y).__repr__()"""
 
536
        self.check(b, a)
 
537
 
 
538
    def test_tuple(self):
 
539
        b = """x = `1, 2, 3`"""
 
540
        a = """x = repr((1, 2, 3))"""
 
541
        self.check(b, a)
 
542
 
 
543
    def test_nested(self):
 
544
        b = """x = `1 + `2``"""
 
545
        a = """x = repr(1 + repr(2))"""
 
546
        self.check(b, a)
 
547
 
 
548
    def test_nested_tuples(self):
 
549
        b = """x = `1, 2 + `3, 4``"""
 
550
        a = """x = repr((1, 2 + repr((3, 4))))"""
 
551
        self.check(b, a)
 
552
 
 
553
class Test_except(FixerTestCase):
 
554
    fixer = "except"
 
555
 
 
556
    def test_prefix_preservation(self):
 
557
        b = """
 
558
            try:
 
559
                pass
 
560
            except (RuntimeError, ImportError),    e:
 
561
                pass"""
 
562
        a = """
 
563
            try:
 
564
                pass
 
565
            except (RuntimeError, ImportError) as    e:
 
566
                pass"""
 
567
        self.check(b, a)
 
568
 
 
569
    def test_simple(self):
 
570
        b = """
 
571
            try:
 
572
                pass
 
573
            except Foo, e:
 
574
                pass"""
 
575
        a = """
 
576
            try:
 
577
                pass
 
578
            except Foo as e:
 
579
                pass"""
 
580
        self.check(b, a)
 
581
 
 
582
    def test_simple_no_space_before_target(self):
 
583
        b = """
 
584
            try:
 
585
                pass
 
586
            except Foo,e:
 
587
                pass"""
 
588
        a = """
 
589
            try:
 
590
                pass
 
591
            except Foo as e:
 
592
                pass"""
 
593
        self.check(b, a)
 
594
 
 
595
    def test_tuple_unpack(self):
 
596
        b = """
 
597
            def foo():
 
598
                try:
 
599
                    pass
 
600
                except Exception, (f, e):
 
601
                    pass
 
602
                except ImportError, e:
 
603
                    pass"""
 
604
 
 
605
        a = """
 
606
            def foo():
 
607
                try:
 
608
                    pass
 
609
                except Exception as xxx_todo_changeme:
 
610
                    (f, e) = xxx_todo_changeme.args
 
611
                    pass
 
612
                except ImportError as e:
 
613
                    pass"""
 
614
        self.check(b, a)
 
615
 
 
616
    def test_multi_class(self):
 
617
        b = """
 
618
            try:
 
619
                pass
 
620
            except (RuntimeError, ImportError), e:
 
621
                pass"""
 
622
 
 
623
        a = """
 
624
            try:
 
625
                pass
 
626
            except (RuntimeError, ImportError) as e:
 
627
                pass"""
 
628
        self.check(b, a)
 
629
 
 
630
    def test_list_unpack(self):
 
631
        b = """
 
632
            try:
 
633
                pass
 
634
            except Exception, [a, b]:
 
635
                pass"""
 
636
 
 
637
        a = """
 
638
            try:
 
639
                pass
 
640
            except Exception as xxx_todo_changeme:
 
641
                [a, b] = xxx_todo_changeme.args
 
642
                pass"""
 
643
        self.check(b, a)
 
644
 
 
645
    def test_weird_target_1(self):
 
646
        b = """
 
647
            try:
 
648
                pass
 
649
            except Exception, d[5]:
 
650
                pass"""
 
651
 
 
652
        a = """
 
653
            try:
 
654
                pass
 
655
            except Exception as xxx_todo_changeme:
 
656
                d[5] = xxx_todo_changeme
 
657
                pass"""
 
658
        self.check(b, a)
 
659
 
 
660
    def test_weird_target_2(self):
 
661
        b = """
 
662
            try:
 
663
                pass
 
664
            except Exception, a.foo:
 
665
                pass"""
 
666
 
 
667
        a = """
 
668
            try:
 
669
                pass
 
670
            except Exception as xxx_todo_changeme:
 
671
                a.foo = xxx_todo_changeme
 
672
                pass"""
 
673
        self.check(b, a)
 
674
 
 
675
    def test_weird_target_3(self):
 
676
        b = """
 
677
            try:
 
678
                pass
 
679
            except Exception, a().foo:
 
680
                pass"""
 
681
 
 
682
        a = """
 
683
            try:
 
684
                pass
 
685
            except Exception as xxx_todo_changeme:
 
686
                a().foo = xxx_todo_changeme
 
687
                pass"""
 
688
        self.check(b, a)
 
689
 
 
690
    def test_bare_except(self):
 
691
        b = """
 
692
            try:
 
693
                pass
 
694
            except Exception, a:
 
695
                pass
 
696
            except:
 
697
                pass"""
 
698
 
 
699
        a = """
 
700
            try:
 
701
                pass
 
702
            except Exception as a:
 
703
                pass
 
704
            except:
 
705
                pass"""
 
706
        self.check(b, a)
 
707
 
 
708
    def test_bare_except_and_else_finally(self):
 
709
        b = """
 
710
            try:
 
711
                pass
 
712
            except Exception, a:
 
713
                pass
 
714
            except:
 
715
                pass
 
716
            else:
 
717
                pass
 
718
            finally:
 
719
                pass"""
 
720
 
 
721
        a = """
 
722
            try:
 
723
                pass
 
724
            except Exception as a:
 
725
                pass
 
726
            except:
 
727
                pass
 
728
            else:
 
729
                pass
 
730
            finally:
 
731
                pass"""
 
732
        self.check(b, a)
 
733
 
 
734
    def test_multi_fixed_excepts_before_bare_except(self):
 
735
        b = """
 
736
            try:
 
737
                pass
 
738
            except TypeError, b:
 
739
                pass
 
740
            except Exception, a:
 
741
                pass
 
742
            except:
 
743
                pass"""
 
744
 
 
745
        a = """
 
746
            try:
 
747
                pass
 
748
            except TypeError as b:
 
749
                pass
 
750
            except Exception as a:
 
751
                pass
 
752
            except:
 
753
                pass"""
 
754
        self.check(b, a)
 
755
 
 
756
    # These should not be touched:
 
757
 
 
758
    def test_unchanged_1(self):
 
759
        s = """
 
760
            try:
 
761
                pass
 
762
            except:
 
763
                pass"""
 
764
        self.unchanged(s)
 
765
 
 
766
    def test_unchanged_2(self):
 
767
        s = """
 
768
            try:
 
769
                pass
 
770
            except Exception:
 
771
                pass"""
 
772
        self.unchanged(s)
 
773
 
 
774
    def test_unchanged_3(self):
 
775
        s = """
 
776
            try:
 
777
                pass
 
778
            except (Exception, SystemExit):
 
779
                pass"""
 
780
        self.unchanged(s)
 
781
 
 
782
class Test_raise(FixerTestCase):
 
783
    fixer = "raise"
 
784
 
 
785
    def test_basic(self):
 
786
        b = """raise Exception, 5"""
 
787
        a = """raise Exception(5)"""
 
788
        self.check(b, a)
 
789
 
 
790
    def test_prefix_preservation(self):
 
791
        b = """raise Exception,5"""
 
792
        a = """raise Exception(5)"""
 
793
        self.check(b, a)
 
794
 
 
795
        b = """raise   Exception,    5"""
 
796
        a = """raise   Exception(5)"""
 
797
        self.check(b, a)
 
798
 
 
799
    def test_with_comments(self):
 
800
        b = """raise Exception, 5 # foo"""
 
801
        a = """raise Exception(5) # foo"""
 
802
        self.check(b, a)
 
803
 
 
804
        b = """raise E, (5, 6) % (a, b) # foo"""
 
805
        a = """raise E((5, 6) % (a, b)) # foo"""
 
806
        self.check(b, a)
 
807
 
 
808
        b = """def foo():
 
809
                    raise Exception, 5, 6 # foo"""
 
810
        a = """def foo():
 
811
                    raise Exception(5).with_traceback(6) # foo"""
 
812
        self.check(b, a)
 
813
 
 
814
    def test_tuple_value(self):
 
815
        b = """raise Exception, (5, 6, 7)"""
 
816
        a = """raise Exception(5, 6, 7)"""
 
817
        self.check(b, a)
 
818
 
 
819
    def test_tuple_detection(self):
 
820
        b = """raise E, (5, 6) % (a, b)"""
 
821
        a = """raise E((5, 6) % (a, b))"""
 
822
        self.check(b, a)
 
823
 
 
824
    def test_tuple_exc_1(self):
 
825
        b = """raise (((E1, E2), E3), E4), V"""
 
826
        a = """raise E1(V)"""
 
827
        self.check(b, a)
 
828
 
 
829
    def test_tuple_exc_2(self):
 
830
        b = """raise (E1, (E2, E3), E4), V"""
 
831
        a = """raise E1(V)"""
 
832
        self.check(b, a)
 
833
 
 
834
    # These should produce a warning
 
835
 
 
836
    def test_string_exc(self):
 
837
        s = """raise 'foo'"""
 
838
        self.warns_unchanged(s, "Python 3 does not support string exceptions")
 
839
 
 
840
    def test_string_exc_val(self):
 
841
        s = """raise "foo", 5"""
 
842
        self.warns_unchanged(s, "Python 3 does not support string exceptions")
 
843
 
 
844
    def test_string_exc_val_tb(self):
 
845
        s = """raise "foo", 5, 6"""
 
846
        self.warns_unchanged(s, "Python 3 does not support string exceptions")
 
847
 
 
848
    # These should result in traceback-assignment
 
849
 
 
850
    def test_tb_1(self):
 
851
        b = """def foo():
 
852
                    raise Exception, 5, 6"""
 
853
        a = """def foo():
 
854
                    raise Exception(5).with_traceback(6)"""
 
855
        self.check(b, a)
 
856
 
 
857
    def test_tb_2(self):
 
858
        b = """def foo():
 
859
                    a = 5
 
860
                    raise Exception, 5, 6
 
861
                    b = 6"""
 
862
        a = """def foo():
 
863
                    a = 5
 
864
                    raise Exception(5).with_traceback(6)
 
865
                    b = 6"""
 
866
        self.check(b, a)
 
867
 
 
868
    def test_tb_3(self):
 
869
        b = """def foo():
 
870
                    raise Exception,5,6"""
 
871
        a = """def foo():
 
872
                    raise Exception(5).with_traceback(6)"""
 
873
        self.check(b, a)
 
874
 
 
875
    def test_tb_4(self):
 
876
        b = """def foo():
 
877
                    a = 5
 
878
                    raise Exception,5,6
 
879
                    b = 6"""
 
880
        a = """def foo():
 
881
                    a = 5
 
882
                    raise Exception(5).with_traceback(6)
 
883
                    b = 6"""
 
884
        self.check(b, a)
 
885
 
 
886
    def test_tb_5(self):
 
887
        b = """def foo():
 
888
                    raise Exception, (5, 6, 7), 6"""
 
889
        a = """def foo():
 
890
                    raise Exception(5, 6, 7).with_traceback(6)"""
 
891
        self.check(b, a)
 
892
 
 
893
    def test_tb_6(self):
 
894
        b = """def foo():
 
895
                    a = 5
 
896
                    raise Exception, (5, 6, 7), 6
 
897
                    b = 6"""
 
898
        a = """def foo():
 
899
                    a = 5
 
900
                    raise Exception(5, 6, 7).with_traceback(6)
 
901
                    b = 6"""
 
902
        self.check(b, a)
 
903
 
 
904
class Test_throw(FixerTestCase):
 
905
    fixer = "throw"
 
906
 
 
907
    def test_1(self):
 
908
        b = """g.throw(Exception, 5)"""
 
909
        a = """g.throw(Exception(5))"""
 
910
        self.check(b, a)
 
911
 
 
912
    def test_2(self):
 
913
        b = """g.throw(Exception,5)"""
 
914
        a = """g.throw(Exception(5))"""
 
915
        self.check(b, a)
 
916
 
 
917
    def test_3(self):
 
918
        b = """g.throw(Exception, (5, 6, 7))"""
 
919
        a = """g.throw(Exception(5, 6, 7))"""
 
920
        self.check(b, a)
 
921
 
 
922
    def test_4(self):
 
923
        b = """5 + g.throw(Exception, 5)"""
 
924
        a = """5 + g.throw(Exception(5))"""
 
925
        self.check(b, a)
 
926
 
 
927
    # These should produce warnings
 
928
 
 
929
    def test_warn_1(self):
 
930
        s = """g.throw("foo")"""
 
931
        self.warns_unchanged(s, "Python 3 does not support string exceptions")
 
932
 
 
933
    def test_warn_2(self):
 
934
        s = """g.throw("foo", 5)"""
 
935
        self.warns_unchanged(s, "Python 3 does not support string exceptions")
 
936
 
 
937
    def test_warn_3(self):
 
938
        s = """g.throw("foo", 5, 6)"""
 
939
        self.warns_unchanged(s, "Python 3 does not support string exceptions")
 
940
 
 
941
    # These should not be touched
 
942
 
 
943
    def test_untouched_1(self):
 
944
        s = """g.throw(Exception)"""
 
945
        self.unchanged(s)
 
946
 
 
947
    def test_untouched_2(self):
 
948
        s = """g.throw(Exception(5, 6))"""
 
949
        self.unchanged(s)
 
950
 
 
951
    def test_untouched_3(self):
 
952
        s = """5 + g.throw(Exception(5, 6))"""
 
953
        self.unchanged(s)
 
954
 
 
955
    # These should result in traceback-assignment
 
956
 
 
957
    def test_tb_1(self):
 
958
        b = """def foo():
 
959
                    g.throw(Exception, 5, 6)"""
 
960
        a = """def foo():
 
961
                    g.throw(Exception(5).with_traceback(6))"""
 
962
        self.check(b, a)
 
963
 
 
964
    def test_tb_2(self):
 
965
        b = """def foo():
 
966
                    a = 5
 
967
                    g.throw(Exception, 5, 6)
 
968
                    b = 6"""
 
969
        a = """def foo():
 
970
                    a = 5
 
971
                    g.throw(Exception(5).with_traceback(6))
 
972
                    b = 6"""
 
973
        self.check(b, a)
 
974
 
 
975
    def test_tb_3(self):
 
976
        b = """def foo():
 
977
                    g.throw(Exception,5,6)"""
 
978
        a = """def foo():
 
979
                    g.throw(Exception(5).with_traceback(6))"""
 
980
        self.check(b, a)
 
981
 
 
982
    def test_tb_4(self):
 
983
        b = """def foo():
 
984
                    a = 5
 
985
                    g.throw(Exception,5,6)
 
986
                    b = 6"""
 
987
        a = """def foo():
 
988
                    a = 5
 
989
                    g.throw(Exception(5).with_traceback(6))
 
990
                    b = 6"""
 
991
        self.check(b, a)
 
992
 
 
993
    def test_tb_5(self):
 
994
        b = """def foo():
 
995
                    g.throw(Exception, (5, 6, 7), 6)"""
 
996
        a = """def foo():
 
997
                    g.throw(Exception(5, 6, 7).with_traceback(6))"""
 
998
        self.check(b, a)
 
999
 
 
1000
    def test_tb_6(self):
 
1001
        b = """def foo():
 
1002
                    a = 5
 
1003
                    g.throw(Exception, (5, 6, 7), 6)
 
1004
                    b = 6"""
 
1005
        a = """def foo():
 
1006
                    a = 5
 
1007
                    g.throw(Exception(5, 6, 7).with_traceback(6))
 
1008
                    b = 6"""
 
1009
        self.check(b, a)
 
1010
 
 
1011
    def test_tb_7(self):
 
1012
        b = """def foo():
 
1013
                    a + g.throw(Exception, 5, 6)"""
 
1014
        a = """def foo():
 
1015
                    a + g.throw(Exception(5).with_traceback(6))"""
 
1016
        self.check(b, a)
 
1017
 
 
1018
    def test_tb_8(self):
 
1019
        b = """def foo():
 
1020
                    a = 5
 
1021
                    a + g.throw(Exception, 5, 6)
 
1022
                    b = 6"""
 
1023
        a = """def foo():
 
1024
                    a = 5
 
1025
                    a + g.throw(Exception(5).with_traceback(6))
 
1026
                    b = 6"""
 
1027
        self.check(b, a)
 
1028
 
 
1029
class Test_long(FixerTestCase):
 
1030
    fixer = "long"
 
1031
 
 
1032
    def test_1(self):
 
1033
        b = """x = long(x)"""
 
1034
        a = """x = int(x)"""
 
1035
        self.check(b, a)
 
1036
 
 
1037
    def test_2(self):
 
1038
        b = """y = isinstance(x, long)"""
 
1039
        a = """y = isinstance(x, int)"""
 
1040
        self.check(b, a)
 
1041
 
 
1042
    def test_3(self):
 
1043
        b = """z = type(x) in (int, long)"""
 
1044
        a = """z = type(x) in (int, int)"""
 
1045
        self.check(b, a)
 
1046
 
 
1047
    def test_4(self):
 
1048
        b = """a = 12L"""
 
1049
        a = """a = 12"""
 
1050
        self.check(b, a)
 
1051
 
 
1052
    def test_5(self):
 
1053
        b = """b = 0x12l"""
 
1054
        a = """b = 0x12"""
 
1055
        self.check(b, a)
 
1056
 
 
1057
    def test_unchanged_1(self):
 
1058
        s = """a = 12"""
 
1059
        self.unchanged(s)
 
1060
 
 
1061
    def test_unchanged_2(self):
 
1062
        s = """b = 0x12"""
 
1063
        self.unchanged(s)
 
1064
 
 
1065
    def test_unchanged_3(self):
 
1066
        s = """c = 3.14"""
 
1067
        self.unchanged(s)
 
1068
 
 
1069
    def test_prefix_preservation(self):
 
1070
        b = """x =   long(  x  )"""
 
1071
        a = """x =   int(  x  )"""
 
1072
        self.check(b, a)
 
1073
 
 
1074
class Test_dict(FixerTestCase):
 
1075
    fixer = "dict"
 
1076
 
 
1077
    def test_prefix_preservation(self):
 
1078
        b = "if   d. keys  (  )  : pass"
 
1079
        a = "if   list(d. keys  (  ))  : pass"
 
1080
        self.check(b, a)
 
1081
 
 
1082
        b = "if   d. items  (  )  : pass"
 
1083
        a = "if   list(d. items  (  ))  : pass"
 
1084
        self.check(b, a)
 
1085
 
 
1086
        b = "if   d. iterkeys  ( )  : pass"
 
1087
        a = "if   iter(d. keys  ( ))  : pass"
 
1088
        self.check(b, a)
 
1089
 
 
1090
        b = "[i for i in    d.  iterkeys(  )  ]"
 
1091
        a = "[i for i in    d.  keys(  )  ]"
 
1092
        self.check(b, a)
 
1093
 
 
1094
    def test_trailing_comment(self):
 
1095
        b = "d.keys() # foo"
 
1096
        a = "list(d.keys()) # foo"
 
1097
        self.check(b, a)
 
1098
 
 
1099
        b = "d.items()  # foo"
 
1100
        a = "list(d.items())  # foo"
 
1101
        self.check(b, a)
 
1102
 
 
1103
        b = "d.iterkeys()  # foo"
 
1104
        a = "iter(d.keys())  # foo"
 
1105
        self.check(b, a)
 
1106
 
 
1107
        b = """[i for i in d.iterkeys() # foo
 
1108
               ]"""
 
1109
        a = """[i for i in d.keys() # foo
 
1110
               ]"""
 
1111
        self.check(b, a)
 
1112
 
 
1113
    def test_unchanged(self):
 
1114
        for wrapper in fixer_util.consuming_calls:
 
1115
            s = "s = %s(d.keys())" % wrapper
 
1116
            self.unchanged(s)
 
1117
 
 
1118
            s = "s = %s(d.values())" % wrapper
 
1119
            self.unchanged(s)
 
1120
 
 
1121
            s = "s = %s(d.items())" % wrapper
 
1122
            self.unchanged(s)
 
1123
 
 
1124
    def test_01(self):
 
1125
        b = "d.keys()"
 
1126
        a = "list(d.keys())"
 
1127
        self.check(b, a)
 
1128
 
 
1129
        b = "a[0].foo().keys()"
 
1130
        a = "list(a[0].foo().keys())"
 
1131
        self.check(b, a)
 
1132
 
 
1133
    def test_02(self):
 
1134
        b = "d.items()"
 
1135
        a = "list(d.items())"
 
1136
        self.check(b, a)
 
1137
 
 
1138
    def test_03(self):
 
1139
        b = "d.values()"
 
1140
        a = "list(d.values())"
 
1141
        self.check(b, a)
 
1142
 
 
1143
    def test_04(self):
 
1144
        b = "d.iterkeys()"
 
1145
        a = "iter(d.keys())"
 
1146
        self.check(b, a)
 
1147
 
 
1148
    def test_05(self):
 
1149
        b = "d.iteritems()"
 
1150
        a = "iter(d.items())"
 
1151
        self.check(b, a)
 
1152
 
 
1153
    def test_06(self):
 
1154
        b = "d.itervalues()"
 
1155
        a = "iter(d.values())"
 
1156
        self.check(b, a)
 
1157
 
 
1158
    def test_07(self):
 
1159
        s = "list(d.keys())"
 
1160
        self.unchanged(s)
 
1161
 
 
1162
    def test_08(self):
 
1163
        s = "sorted(d.keys())"
 
1164
        self.unchanged(s)
 
1165
 
 
1166
    def test_09(self):
 
1167
        b = "iter(d.keys())"
 
1168
        a = "iter(list(d.keys()))"
 
1169
        self.check(b, a)
 
1170
 
 
1171
    def test_10(self):
 
1172
        b = "foo(d.keys())"
 
1173
        a = "foo(list(d.keys()))"
 
1174
        self.check(b, a)
 
1175
 
 
1176
    def test_11(self):
 
1177
        b = "for i in d.keys(): print i"
 
1178
        a = "for i in list(d.keys()): print i"
 
1179
        self.check(b, a)
 
1180
 
 
1181
    def test_12(self):
 
1182
        b = "for i in d.iterkeys(): print i"
 
1183
        a = "for i in d.keys(): print i"
 
1184
        self.check(b, a)
 
1185
 
 
1186
    def test_13(self):
 
1187
        b = "[i for i in d.keys()]"
 
1188
        a = "[i for i in list(d.keys())]"
 
1189
        self.check(b, a)
 
1190
 
 
1191
    def test_14(self):
 
1192
        b = "[i for i in d.iterkeys()]"
 
1193
        a = "[i for i in d.keys()]"
 
1194
        self.check(b, a)
 
1195
 
 
1196
    def test_15(self):
 
1197
        b = "(i for i in d.keys())"
 
1198
        a = "(i for i in list(d.keys()))"
 
1199
        self.check(b, a)
 
1200
 
 
1201
    def test_16(self):
 
1202
        b = "(i for i in d.iterkeys())"
 
1203
        a = "(i for i in d.keys())"
 
1204
        self.check(b, a)
 
1205
 
 
1206
    def test_17(self):
 
1207
        b = "iter(d.iterkeys())"
 
1208
        a = "iter(d.keys())"
 
1209
        self.check(b, a)
 
1210
 
 
1211
    def test_18(self):
 
1212
        b = "list(d.iterkeys())"
 
1213
        a = "list(d.keys())"
 
1214
        self.check(b, a)
 
1215
 
 
1216
    def test_19(self):
 
1217
        b = "sorted(d.iterkeys())"
 
1218
        a = "sorted(d.keys())"
 
1219
        self.check(b, a)
 
1220
 
 
1221
    def test_20(self):
 
1222
        b = "foo(d.iterkeys())"
 
1223
        a = "foo(iter(d.keys()))"
 
1224
        self.check(b, a)
 
1225
 
 
1226
    def test_21(self):
 
1227
        b = "print h.iterkeys().next()"
 
1228
        a = "print iter(h.keys()).next()"
 
1229
        self.check(b, a)
 
1230
 
 
1231
    def test_22(self):
 
1232
        b = "print h.keys()[0]"
 
1233
        a = "print list(h.keys())[0]"
 
1234
        self.check(b, a)
 
1235
 
 
1236
    def test_23(self):
 
1237
        b = "print list(h.iterkeys().next())"
 
1238
        a = "print list(iter(h.keys()).next())"
 
1239
        self.check(b, a)
 
1240
 
 
1241
    def test_24(self):
 
1242
        b = "for x in h.keys()[0]: print x"
 
1243
        a = "for x in list(h.keys())[0]: print x"
 
1244
        self.check(b, a)
 
1245
 
 
1246
class Test_xrange(FixerTestCase):
 
1247
    fixer = "xrange"
 
1248
 
 
1249
    def test_prefix_preservation(self):
 
1250
        b = """x =    xrange(  10  )"""
 
1251
        a = """x =    range(  10  )"""
 
1252
        self.check(b, a)
 
1253
 
 
1254
        b = """x = xrange(  1  ,  10   )"""
 
1255
        a = """x = range(  1  ,  10   )"""
 
1256
        self.check(b, a)
 
1257
 
 
1258
        b = """x = xrange(  0  ,  10 ,  2 )"""
 
1259
        a = """x = range(  0  ,  10 ,  2 )"""
 
1260
        self.check(b, a)
 
1261
 
 
1262
    def test_single_arg(self):
 
1263
        b = """x = xrange(10)"""
 
1264
        a = """x = range(10)"""
 
1265
        self.check(b, a)
 
1266
 
 
1267
    def test_two_args(self):
 
1268
        b = """x = xrange(1, 10)"""
 
1269
        a = """x = range(1, 10)"""
 
1270
        self.check(b, a)
 
1271
 
 
1272
    def test_three_args(self):
 
1273
        b = """x = xrange(0, 10, 2)"""
 
1274
        a = """x = range(0, 10, 2)"""
 
1275
        self.check(b, a)
 
1276
 
 
1277
    def test_wrap_in_list(self):
 
1278
        b = """x = range(10, 3, 9)"""
 
1279
        a = """x = list(range(10, 3, 9))"""
 
1280
        self.check(b, a)
 
1281
 
 
1282
        b = """x = foo(range(10, 3, 9))"""
 
1283
        a = """x = foo(list(range(10, 3, 9)))"""
 
1284
        self.check(b, a)
 
1285
 
 
1286
        b = """x = range(10, 3, 9) + [4]"""
 
1287
        a = """x = list(range(10, 3, 9)) + [4]"""
 
1288
        self.check(b, a)
 
1289
 
 
1290
    def test_xrange_in_for(self):
 
1291
        b = """for i in xrange(10):\n    j=i"""
 
1292
        a = """for i in range(10):\n    j=i"""
 
1293
        self.check(b, a)
 
1294
 
 
1295
        b = """[i for i in xrange(10)]"""
 
1296
        a = """[i for i in range(10)]"""
 
1297
        self.check(b, a)
 
1298
 
 
1299
    def test_range_in_for(self):
 
1300
        self.unchanged("for i in range(10): pass")
 
1301
        self.unchanged("[i for i in range(10)]")
 
1302
 
 
1303
    def test_in_contains_test(self):
 
1304
        self.unchanged("x in range(10, 3, 9)")
 
1305
 
 
1306
    def test_in_consuming_context(self):
 
1307
        for call in fixer_util.consuming_calls:
 
1308
            self.unchanged("a = %s(range(10))" % call)
 
1309
 
 
1310
class Test_raw_input(FixerTestCase):
 
1311
    fixer = "raw_input"
 
1312
 
 
1313
    def test_prefix_preservation(self):
 
1314
        b = """x =    raw_input(   )"""
 
1315
        a = """x =    input(   )"""
 
1316
        self.check(b, a)
 
1317
 
 
1318
        b = """x = raw_input(   ''   )"""
 
1319
        a = """x = input(   ''   )"""
 
1320
        self.check(b, a)
 
1321
 
 
1322
    def test_1(self):
 
1323
        b = """x = raw_input()"""
 
1324
        a = """x = input()"""
 
1325
        self.check(b, a)
 
1326
 
 
1327
    def test_2(self):
 
1328
        b = """x = raw_input('')"""
 
1329
        a = """x = input('')"""
 
1330
        self.check(b, a)
 
1331
 
 
1332
    def test_3(self):
 
1333
        b = """x = raw_input('prompt')"""
 
1334
        a = """x = input('prompt')"""
 
1335
        self.check(b, a)
 
1336
 
 
1337
    def test_4(self):
 
1338
        b = """x = raw_input(foo(a) + 6)"""
 
1339
        a = """x = input(foo(a) + 6)"""
 
1340
        self.check(b, a)
 
1341
 
 
1342
    def test_5(self):
 
1343
        b = """x = raw_input(invite).split()"""
 
1344
        a = """x = input(invite).split()"""
 
1345
        self.check(b, a)
 
1346
 
 
1347
    def test_6(self):
 
1348
        b = """x = raw_input(invite) . split ()"""
 
1349
        a = """x = input(invite) . split ()"""
 
1350
        self.check(b, a)
 
1351
 
 
1352
    def test_8(self):
 
1353
        b = "x = int(raw_input())"
 
1354
        a = "x = int(input())"
 
1355
        self.check(b, a)
 
1356
 
 
1357
class Test_funcattrs(FixerTestCase):
 
1358
    fixer = "funcattrs"
 
1359
 
 
1360
    attrs = ["closure", "doc", "name", "defaults", "code", "globals", "dict"]
 
1361
 
 
1362
    def test(self):
 
1363
        for attr in self.attrs:
 
1364
            b = "a.func_%s" % attr
 
1365
            a = "a.__%s__" % attr
 
1366
            self.check(b, a)
 
1367
 
 
1368
            b = "self.foo.func_%s.foo_bar" % attr
 
1369
            a = "self.foo.__%s__.foo_bar" % attr
 
1370
            self.check(b, a)
 
1371
 
 
1372
    def test_unchanged(self):
 
1373
        for attr in self.attrs:
 
1374
            s = "foo(func_%s + 5)" % attr
 
1375
            self.unchanged(s)
 
1376
 
 
1377
            s = "f(foo.__%s__)" % attr
 
1378
            self.unchanged(s)
 
1379
 
 
1380
            s = "f(foo.__%s__.foo)" % attr
 
1381
            self.unchanged(s)
 
1382
 
 
1383
class Test_xreadlines(FixerTestCase):
 
1384
    fixer = "xreadlines"
 
1385
 
 
1386
    def test_call(self):
 
1387
        b = "for x in f.xreadlines(): pass"
 
1388
        a = "for x in f: pass"
 
1389
        self.check(b, a)
 
1390
 
 
1391
        b = "for x in foo().xreadlines(): pass"
 
1392
        a = "for x in foo(): pass"
 
1393
        self.check(b, a)
 
1394
 
 
1395
        b = "for x in (5 + foo()).xreadlines(): pass"
 
1396
        a = "for x in (5 + foo()): pass"
 
1397
        self.check(b, a)
 
1398
 
 
1399
    def test_attr_ref(self):
 
1400
        b = "foo(f.xreadlines + 5)"
 
1401
        a = "foo(f.__iter__ + 5)"
 
1402
        self.check(b, a)
 
1403
 
 
1404
        b = "foo(f().xreadlines + 5)"
 
1405
        a = "foo(f().__iter__ + 5)"
 
1406
        self.check(b, a)
 
1407
 
 
1408
        b = "foo((5 + f()).xreadlines + 5)"
 
1409
        a = "foo((5 + f()).__iter__ + 5)"
 
1410
        self.check(b, a)
 
1411
 
 
1412
    def test_unchanged(self):
 
1413
        s = "for x in f.xreadlines(5): pass"
 
1414
        self.unchanged(s)
 
1415
 
 
1416
        s = "for x in f.xreadlines(k=5): pass"
 
1417
        self.unchanged(s)
 
1418
 
 
1419
        s = "for x in f.xreadlines(*k, **v): pass"
 
1420
        self.unchanged(s)
 
1421
 
 
1422
        s = "foo(xreadlines)"
 
1423
        self.unchanged(s)
 
1424
 
 
1425
class Test_imports(FixerTestCase):
 
1426
    fixer = "imports"
 
1427
    from ..fixes.fix_imports import MAPPING as modules
 
1428
 
 
1429
    def test_import_module(self):
 
1430
        for old, new in self.modules.items():
 
1431
            b = "import %s" % old
 
1432
            a = "import %s" % new
 
1433
            self.check(b, a)
 
1434
 
 
1435
            b = "import foo, %s, bar" % old
 
1436
            a = "import foo, %s, bar" % new
 
1437
            self.check(b, a)
 
1438
 
 
1439
    def test_import_from(self):
 
1440
        for old, new in self.modules.items():
 
1441
            b = "from %s import foo" % old
 
1442
            a = "from %s import foo" % new
 
1443
            self.check(b, a)
 
1444
 
 
1445
            b = "from %s import foo, bar" % old
 
1446
            a = "from %s import foo, bar" % new
 
1447
            self.check(b, a)
 
1448
 
 
1449
            b = "from %s import (yes, no)" % old
 
1450
            a = "from %s import (yes, no)" % 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
            b = """
 
1488
                from %s import x
 
1489
                %s = 23
 
1490
                """ % (old, old)
 
1491
            a = """
 
1492
                from %s import x
 
1493
                %s = 23
 
1494
                """ % (new, old)
 
1495
            self.check(b, a)
 
1496
 
 
1497
            s = """
 
1498
                def f():
 
1499
                    %s.method()
 
1500
                """ % (old,)
 
1501
            self.unchanged(s)
 
1502
 
 
1503
            # test nested usage
 
1504
            b = """
 
1505
                import %s
 
1506
                %s.bar(%s.foo)
 
1507
                """ % (old, old, old)
 
1508
            a = """
 
1509
                import %s
 
1510
                %s.bar(%s.foo)
 
1511
                """ % (new, new, new)
 
1512
            self.check(b, a)
 
1513
 
 
1514
            b = """
 
1515
                import %s
 
1516
                x.%s
 
1517
                """ % (old, old)
 
1518
            a = """
 
1519
                import %s
 
1520
                x.%s
 
1521
                """ % (new, old)
 
1522
            self.check(b, a)
 
1523
 
 
1524
 
 
1525
 
 
1526
class Test_imports2(Test_imports):
 
1527
    fixer = "imports2"
 
1528
    from ..fixes.fix_imports2 import MAPPING as modules
 
1529
 
 
1530
 
 
1531
class Test_imports_fixer_order(Test_imports):
 
1532
 
 
1533
    fixer = None
 
1534
 
 
1535
    def setUp(self):
 
1536
        Test_imports.setUp(self, ['imports', 'imports2'])
 
1537
        from ..fixes.fix_imports2 import MAPPING as mapping2
 
1538
        self.modules = mapping2.copy()
 
1539
        from ..fixes.fix_imports import MAPPING as mapping1
 
1540
        for key in ('dbhash', 'dumbdbm', 'dbm', 'gdbm'):
 
1541
            self.modules[key] = mapping1[key]
 
1542
 
 
1543
 
 
1544
class Test_urllib(FixerTestCase):
 
1545
    fixer = "urllib"
 
1546
    from ..fixes.fix_urllib import MAPPING as modules
 
1547
 
 
1548
    def test_import_module(self):
 
1549
        for old, changes in self.modules.items():
 
1550
            b = "import %s" % old
 
1551
            a = "import %s" % ", ".join(map(itemgetter(0), changes))
 
1552
            self.check(b, a)
 
1553
 
 
1554
    def test_import_from(self):
 
1555
        for old, changes in self.modules.items():
 
1556
            all_members = []
 
1557
            for new, members in changes:
 
1558
                for member in members:
 
1559
                    all_members.append(member)
 
1560
                    b = "from %s import %s" % (old, member)
 
1561
                    a = "from %s import %s" % (new, member)
 
1562
                    self.check(b, a)
 
1563
 
 
1564
                    s = "from foo import %s" % member
 
1565
                    self.unchanged(s)
 
1566
 
 
1567
                b = "from %s import %s" % (old, ", ".join(members))
 
1568
                a = "from %s import %s" % (new, ", ".join(members))
 
1569
                self.check(b, a)
 
1570
 
 
1571
                s = "from foo import %s" % ", ".join(members)
 
1572
                self.unchanged(s)
 
1573
 
 
1574
            # test the breaking of a module into multiple replacements
 
1575
            b = "from %s import %s" % (old, ", ".join(all_members))
 
1576
            a = "\n".join(["from %s import %s" % (new, ", ".join(members))
 
1577
                            for (new, members) in changes])
 
1578
            self.check(b, a)
 
1579
 
 
1580
    def test_import_module_as(self):
 
1581
        for old in self.modules:
 
1582
            s = "import %s as foo" % old
 
1583
            self.warns_unchanged(s, "This module is now multiple modules")
 
1584
 
 
1585
    def test_import_from_as(self):
 
1586
        for old, changes in self.modules.items():
 
1587
            for new, members in changes:
 
1588
                for member in members:
 
1589
                    b = "from %s import %s as foo_bar" % (old, member)
 
1590
                    a = "from %s import %s as foo_bar" % (new, member)
 
1591
                    self.check(b, a)
 
1592
 
 
1593
    def test_star(self):
 
1594
        for old in self.modules:
 
1595
            s = "from %s import *" % old
 
1596
            self.warns_unchanged(s, "Cannot handle star imports")
 
1597
 
 
1598
    def test_import_module_usage(self):
 
1599
        for old, changes in self.modules.items():
 
1600
            for new, members in changes:
 
1601
                for member in members:
 
1602
                    b = """
 
1603
                        import %s
 
1604
                        foo(%s.%s)
 
1605
                        """ % (old, old, member)
 
1606
                    a = """
 
1607
                        import %s
 
1608
                        foo(%s.%s)
 
1609
                        """ % (", ".join([n for (n, mems)
 
1610
                                           in self.modules[old]]),
 
1611
                                         new, member)
 
1612
                    self.check(b, a)
 
1613
 
 
1614
 
 
1615
class Test_input(FixerTestCase):
 
1616
    fixer = "input"
 
1617
 
 
1618
    def test_prefix_preservation(self):
 
1619
        b = """x =   input(   )"""
 
1620
        a = """x =   eval(input(   ))"""
 
1621
        self.check(b, a)
 
1622
 
 
1623
        b = """x = input(   ''   )"""
 
1624
        a = """x = eval(input(   ''   ))"""
 
1625
        self.check(b, a)
 
1626
 
 
1627
    def test_trailing_comment(self):
 
1628
        b = """x = input()  #  foo"""
 
1629
        a = """x = eval(input())  #  foo"""
 
1630
        self.check(b, a)
 
1631
 
 
1632
    def test_idempotency(self):
 
1633
        s = """x = eval(input())"""
 
1634
        self.unchanged(s)
 
1635
 
 
1636
        s = """x = eval(input(''))"""
 
1637
        self.unchanged(s)
 
1638
 
 
1639
        s = """x = eval(input(foo(5) + 9))"""
 
1640
        self.unchanged(s)
 
1641
 
 
1642
    def test_1(self):
 
1643
        b = """x = input()"""
 
1644
        a = """x = eval(input())"""
 
1645
        self.check(b, a)
 
1646
 
 
1647
    def test_2(self):
 
1648
        b = """x = input('')"""
 
1649
        a = """x = eval(input(''))"""
 
1650
        self.check(b, a)
 
1651
 
 
1652
    def test_3(self):
 
1653
        b = """x = input('prompt')"""
 
1654
        a = """x = eval(input('prompt'))"""
 
1655
        self.check(b, a)
 
1656
 
 
1657
    def test_4(self):
 
1658
        b = """x = input(foo(5) + 9)"""
 
1659
        a = """x = eval(input(foo(5) + 9))"""
 
1660
        self.check(b, a)
 
1661
 
 
1662
class Test_tuple_params(FixerTestCase):
 
1663
    fixer = "tuple_params"
 
1664
 
 
1665
    def test_unchanged_1(self):
 
1666
        s = """def foo(): pass"""
 
1667
        self.unchanged(s)
 
1668
 
 
1669
    def test_unchanged_2(self):
 
1670
        s = """def foo(a, b, c): pass"""
 
1671
        self.unchanged(s)
 
1672
 
 
1673
    def test_unchanged_3(self):
 
1674
        s = """def foo(a=3, b=4, c=5): pass"""
 
1675
        self.unchanged(s)
 
1676
 
 
1677
    def test_1(self):
 
1678
        b = """
 
1679
            def foo(((a, b), c)):
 
1680
                x = 5"""
 
1681
 
 
1682
        a = """
 
1683
            def foo(xxx_todo_changeme):
 
1684
                ((a, b), c) = xxx_todo_changeme
 
1685
                x = 5"""
 
1686
        self.check(b, a)
 
1687
 
 
1688
    def test_2(self):
 
1689
        b = """
 
1690
            def foo(((a, b), c), d):
 
1691
                x = 5"""
 
1692
 
 
1693
        a = """
 
1694
            def foo(xxx_todo_changeme, d):
 
1695
                ((a, b), c) = xxx_todo_changeme
 
1696
                x = 5"""
 
1697
        self.check(b, a)
 
1698
 
 
1699
    def test_3(self):
 
1700
        b = """
 
1701
            def foo(((a, b), c), d) -> e:
 
1702
                x = 5"""
 
1703
 
 
1704
        a = """
 
1705
            def foo(xxx_todo_changeme, d) -> e:
 
1706
                ((a, b), c) = xxx_todo_changeme
 
1707
                x = 5"""
 
1708
        self.check(b, a)
 
1709
 
 
1710
    def test_semicolon(self):
 
1711
        b = """
 
1712
            def foo(((a, b), c)): x = 5; y = 7"""
 
1713
 
 
1714
        a = """
 
1715
            def foo(xxx_todo_changeme): ((a, b), c) = xxx_todo_changeme; x = 5; y = 7"""
 
1716
        self.check(b, a)
 
1717
 
 
1718
    def test_keywords(self):
 
1719
        b = """
 
1720
            def foo(((a, b), c), d, e=5) -> z:
 
1721
                x = 5"""
 
1722
 
 
1723
        a = """
 
1724
            def foo(xxx_todo_changeme, d, e=5) -> z:
 
1725
                ((a, b), c) = xxx_todo_changeme
 
1726
                x = 5"""
 
1727
        self.check(b, a)
 
1728
 
 
1729
    def test_varargs(self):
 
1730
        b = """
 
1731
            def foo(((a, b), c), d, *vargs, **kwargs) -> z:
 
1732
                x = 5"""
 
1733
 
 
1734
        a = """
 
1735
            def foo(xxx_todo_changeme, d, *vargs, **kwargs) -> z:
 
1736
                ((a, b), c) = xxx_todo_changeme
 
1737
                x = 5"""
 
1738
        self.check(b, a)
 
1739
 
 
1740
    def test_multi_1(self):
 
1741
        b = """
 
1742
            def foo(((a, b), c), (d, e, f)) -> z:
 
1743
                x = 5"""
 
1744
 
 
1745
        a = """
 
1746
            def foo(xxx_todo_changeme, xxx_todo_changeme1) -> z:
 
1747
                ((a, b), c) = xxx_todo_changeme
 
1748
                (d, e, f) = xxx_todo_changeme1
 
1749
                x = 5"""
 
1750
        self.check(b, a)
 
1751
 
 
1752
    def test_multi_2(self):
 
1753
        b = """
 
1754
            def foo(x, ((a, b), c), d, (e, f, g), y) -> z:
 
1755
                x = 5"""
 
1756
 
 
1757
        a = """
 
1758
            def foo(x, xxx_todo_changeme, d, xxx_todo_changeme1, y) -> z:
 
1759
                ((a, b), c) = xxx_todo_changeme
 
1760
                (e, f, g) = xxx_todo_changeme1
 
1761
                x = 5"""
 
1762
        self.check(b, a)
 
1763
 
 
1764
    def test_docstring(self):
 
1765
        b = """
 
1766
            def foo(((a, b), c), (d, e, f)) -> z:
 
1767
                "foo foo foo foo"
 
1768
                x = 5"""
 
1769
 
 
1770
        a = """
 
1771
            def foo(xxx_todo_changeme, xxx_todo_changeme1) -> z:
 
1772
                "foo foo foo foo"
 
1773
                ((a, b), c) = xxx_todo_changeme
 
1774
                (d, e, f) = xxx_todo_changeme1
 
1775
                x = 5"""
 
1776
        self.check(b, a)
 
1777
 
 
1778
    def test_lambda_no_change(self):
 
1779
        s = """lambda x: x + 5"""
 
1780
        self.unchanged(s)
 
1781
 
 
1782
    def test_lambda_parens_single_arg(self):
 
1783
        b = """lambda (x): x + 5"""
 
1784
        a = """lambda x: x + 5"""
 
1785
        self.check(b, a)
 
1786
 
 
1787
        b = """lambda(x): x + 5"""
 
1788
        a = """lambda x: x + 5"""
 
1789
        self.check(b, a)
 
1790
 
 
1791
        b = """lambda ((((x)))): x + 5"""
 
1792
        a = """lambda x: x + 5"""
 
1793
        self.check(b, a)
 
1794
 
 
1795
        b = """lambda((((x)))): x + 5"""
 
1796
        a = """lambda x: x + 5"""
 
1797
        self.check(b, a)
 
1798
 
 
1799
    def test_lambda_simple(self):
 
1800
        b = """lambda (x, y): x + f(y)"""
 
1801
        a = """lambda x_y: x_y[0] + f(x_y[1])"""
 
1802
        self.check(b, a)
 
1803
 
 
1804
        b = """lambda(x, y): x + f(y)"""
 
1805
        a = """lambda x_y: x_y[0] + f(x_y[1])"""
 
1806
        self.check(b, a)
 
1807
 
 
1808
        b = """lambda (((x, y))): x + f(y)"""
 
1809
        a = """lambda x_y: x_y[0] + f(x_y[1])"""
 
1810
        self.check(b, a)
 
1811
 
 
1812
        b = """lambda(((x, y))): x + f(y)"""
 
1813
        a = """lambda x_y: x_y[0] + f(x_y[1])"""
 
1814
        self.check(b, a)
 
1815
 
 
1816
    def test_lambda_one_tuple(self):
 
1817
        b = """lambda (x,): x + f(x)"""
 
1818
        a = """lambda x1: x1[0] + f(x1[0])"""
 
1819
        self.check(b, a)
 
1820
 
 
1821
        b = """lambda (((x,))): x + f(x)"""
 
1822
        a = """lambda x1: x1[0] + f(x1[0])"""
 
1823
        self.check(b, a)
 
1824
 
 
1825
    def test_lambda_simple_multi_use(self):
 
1826
        b = """lambda (x, y): x + x + f(x) + x"""
 
1827
        a = """lambda x_y: x_y[0] + x_y[0] + f(x_y[0]) + x_y[0]"""
 
1828
        self.check(b, a)
 
1829
 
 
1830
    def test_lambda_simple_reverse(self):
 
1831
        b = """lambda (x, y): y + x"""
 
1832
        a = """lambda x_y: x_y[1] + x_y[0]"""
 
1833
        self.check(b, a)
 
1834
 
 
1835
    def test_lambda_nested(self):
 
1836
        b = """lambda (x, (y, z)): x + y + z"""
 
1837
        a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]"""
 
1838
        self.check(b, a)
 
1839
 
 
1840
        b = """lambda (((x, (y, z)))): x + y + z"""
 
1841
        a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]"""
 
1842
        self.check(b, a)
 
1843
 
 
1844
    def test_lambda_nested_multi_use(self):
 
1845
        b = """lambda (x, (y, z)): x + y + f(y)"""
 
1846
        a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + f(x_y_z[1][0])"""
 
1847
        self.check(b, a)
 
1848
 
 
1849
class Test_methodattrs(FixerTestCase):
 
1850
    fixer = "methodattrs"
 
1851
 
 
1852
    attrs = ["func", "self", "class"]
 
1853
 
 
1854
    def test(self):
 
1855
        for attr in self.attrs:
 
1856
            b = "a.im_%s" % attr
 
1857
            if attr == "class":
 
1858
                a = "a.__self__.__class__"
 
1859
            else:
 
1860
                a = "a.__%s__" % attr
 
1861
            self.check(b, a)
 
1862
 
 
1863
            b = "self.foo.im_%s.foo_bar" % attr
 
1864
            if attr == "class":
 
1865
                a = "self.foo.__self__.__class__.foo_bar"
 
1866
            else:
 
1867
                a = "self.foo.__%s__.foo_bar" % attr
 
1868
            self.check(b, a)
 
1869
 
 
1870
    def test_unchanged(self):
 
1871
        for attr in self.attrs:
 
1872
            s = "foo(im_%s + 5)" % attr
 
1873
            self.unchanged(s)
 
1874
 
 
1875
            s = "f(foo.__%s__)" % attr
 
1876
            self.unchanged(s)
 
1877
 
 
1878
            s = "f(foo.__%s__.foo)" % attr
 
1879
            self.unchanged(s)
 
1880
 
 
1881
class Test_next(FixerTestCase):
 
1882
    fixer = "next"
 
1883
 
 
1884
    def test_1(self):
 
1885
        b = """it.next()"""
 
1886
        a = """next(it)"""
 
1887
        self.check(b, a)
 
1888
 
 
1889
    def test_2(self):
 
1890
        b = """a.b.c.d.next()"""
 
1891
        a = """next(a.b.c.d)"""
 
1892
        self.check(b, a)
 
1893
 
 
1894
    def test_3(self):
 
1895
        b = """(a + b).next()"""
 
1896
        a = """next((a + b))"""
 
1897
        self.check(b, a)
 
1898
 
 
1899
    def test_4(self):
 
1900
        b = """a().next()"""
 
1901
        a = """next(a())"""
 
1902
        self.check(b, a)
 
1903
 
 
1904
    def test_5(self):
 
1905
        b = """a().next() + b"""
 
1906
        a = """next(a()) + b"""
 
1907
        self.check(b, a)
 
1908
 
 
1909
    def test_6(self):
 
1910
        b = """c(      a().next() + b)"""
 
1911
        a = """c(      next(a()) + b)"""
 
1912
        self.check(b, a)
 
1913
 
 
1914
    def test_prefix_preservation_1(self):
 
1915
        b = """
 
1916
            for a in b:
 
1917
                foo(a)
 
1918
                a.next()
 
1919
            """
 
1920
        a = """
 
1921
            for a in b:
 
1922
                foo(a)
 
1923
                next(a)
 
1924
            """
 
1925
        self.check(b, a)
 
1926
 
 
1927
    def test_prefix_preservation_2(self):
 
1928
        b = """
 
1929
            for a in b:
 
1930
                foo(a) # abc
 
1931
                # def
 
1932
                a.next()
 
1933
            """
 
1934
        a = """
 
1935
            for a in b:
 
1936
                foo(a) # abc
 
1937
                # def
 
1938
                next(a)
 
1939
            """
 
1940
        self.check(b, a)
 
1941
 
 
1942
    def test_prefix_preservation_3(self):
 
1943
        b = """
 
1944
            next = 5
 
1945
            for a in b:
 
1946
                foo(a)
 
1947
                a.next()
 
1948
            """
 
1949
        a = """
 
1950
            next = 5
 
1951
            for a in b:
 
1952
                foo(a)
 
1953
                a.__next__()
 
1954
            """
 
1955
        self.check(b, a, ignore_warnings=True)
 
1956
 
 
1957
    def test_prefix_preservation_4(self):
 
1958
        b = """
 
1959
            next = 5
 
1960
            for a in b:
 
1961
                foo(a) # abc
 
1962
                # def
 
1963
                a.next()
 
1964
            """
 
1965
        a = """
 
1966
            next = 5
 
1967
            for a in b:
 
1968
                foo(a) # abc
 
1969
                # def
 
1970
                a.__next__()
 
1971
            """
 
1972
        self.check(b, a, ignore_warnings=True)
 
1973
 
 
1974
    def test_prefix_preservation_5(self):
 
1975
        b = """
 
1976
            next = 5
 
1977
            for a in b:
 
1978
                foo(foo(a), # abc
 
1979
                    a.next())
 
1980
            """
 
1981
        a = """
 
1982
            next = 5
 
1983
            for a in b:
 
1984
                foo(foo(a), # abc
 
1985
                    a.__next__())
 
1986
            """
 
1987
        self.check(b, a, ignore_warnings=True)
 
1988
 
 
1989
    def test_prefix_preservation_6(self):
 
1990
        b = """
 
1991
            for a in b:
 
1992
                foo(foo(a), # abc
 
1993
                    a.next())
 
1994
            """
 
1995
        a = """
 
1996
            for a in b:
 
1997
                foo(foo(a), # abc
 
1998
                    next(a))
 
1999
            """
 
2000
        self.check(b, a)
 
2001
 
 
2002
    def test_method_1(self):
 
2003
        b = """
 
2004
            class A:
 
2005
                def next(self):
 
2006
                    pass
 
2007
            """
 
2008
        a = """
 
2009
            class A:
 
2010
                def __next__(self):
 
2011
                    pass
 
2012
            """
 
2013
        self.check(b, a)
 
2014
 
 
2015
    def test_method_2(self):
 
2016
        b = """
 
2017
            class A(object):
 
2018
                def next(self):
 
2019
                    pass
 
2020
            """
 
2021
        a = """
 
2022
            class A(object):
 
2023
                def __next__(self):
 
2024
                    pass
 
2025
            """
 
2026
        self.check(b, a)
 
2027
 
 
2028
    def test_method_3(self):
 
2029
        b = """
 
2030
            class A:
 
2031
                def next(x):
 
2032
                    pass
 
2033
            """
 
2034
        a = """
 
2035
            class A:
 
2036
                def __next__(x):
 
2037
                    pass
 
2038
            """
 
2039
        self.check(b, a)
 
2040
 
 
2041
    def test_method_4(self):
 
2042
        b = """
 
2043
            class A:
 
2044
                def __init__(self, foo):
 
2045
                    self.foo = foo
 
2046
 
 
2047
                def next(self):
 
2048
                    pass
 
2049
 
 
2050
                def __iter__(self):
 
2051
                    return self
 
2052
            """
 
2053
        a = """
 
2054
            class A:
 
2055
                def __init__(self, foo):
 
2056
                    self.foo = foo
 
2057
 
 
2058
                def __next__(self):
 
2059
                    pass
 
2060
 
 
2061
                def __iter__(self):
 
2062
                    return self
 
2063
            """
 
2064
        self.check(b, a)
 
2065
 
 
2066
    def test_method_unchanged(self):
 
2067
        s = """
 
2068
            class A:
 
2069
                def next(self, a, b):
 
2070
                    pass
 
2071
            """
 
2072
        self.unchanged(s)
 
2073
 
 
2074
    def test_shadowing_assign_simple(self):
 
2075
        s = """
 
2076
            next = foo
 
2077
 
 
2078
            class A:
 
2079
                def next(self, a, b):
 
2080
                    pass
 
2081
            """
 
2082
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2083
 
 
2084
    def test_shadowing_assign_tuple_1(self):
 
2085
        s = """
 
2086
            (next, a) = foo
 
2087
 
 
2088
            class A:
 
2089
                def next(self, a, b):
 
2090
                    pass
 
2091
            """
 
2092
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2093
 
 
2094
    def test_shadowing_assign_tuple_2(self):
 
2095
        s = """
 
2096
            (a, (b, (next, c)), a) = foo
 
2097
 
 
2098
            class A:
 
2099
                def next(self, a, b):
 
2100
                    pass
 
2101
            """
 
2102
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2103
 
 
2104
    def test_shadowing_assign_list_1(self):
 
2105
        s = """
 
2106
            [next, a] = foo
 
2107
 
 
2108
            class A:
 
2109
                def next(self, a, b):
 
2110
                    pass
 
2111
            """
 
2112
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2113
 
 
2114
    def test_shadowing_assign_list_2(self):
 
2115
        s = """
 
2116
            [a, [b, [next, c]], a] = foo
 
2117
 
 
2118
            class A:
 
2119
                def next(self, a, b):
 
2120
                    pass
 
2121
            """
 
2122
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2123
 
 
2124
    def test_builtin_assign(self):
 
2125
        s = """
 
2126
            def foo():
 
2127
                __builtin__.next = foo
 
2128
 
 
2129
            class A:
 
2130
                def next(self, a, b):
 
2131
                    pass
 
2132
            """
 
2133
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2134
 
 
2135
    def test_builtin_assign_in_tuple(self):
 
2136
        s = """
 
2137
            def foo():
 
2138
                (a, __builtin__.next) = foo
 
2139
 
 
2140
            class A:
 
2141
                def next(self, a, b):
 
2142
                    pass
 
2143
            """
 
2144
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2145
 
 
2146
    def test_builtin_assign_in_list(self):
 
2147
        s = """
 
2148
            def foo():
 
2149
                [a, __builtin__.next] = foo
 
2150
 
 
2151
            class A:
 
2152
                def next(self, a, b):
 
2153
                    pass
 
2154
            """
 
2155
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2156
 
 
2157
    def test_assign_to_next(self):
 
2158
        s = """
 
2159
            def foo():
 
2160
                A.next = foo
 
2161
 
 
2162
            class A:
 
2163
                def next(self, a, b):
 
2164
                    pass
 
2165
            """
 
2166
        self.unchanged(s)
 
2167
 
 
2168
    def test_assign_to_next_in_tuple(self):
 
2169
        s = """
 
2170
            def foo():
 
2171
                (a, A.next) = foo
 
2172
 
 
2173
            class A:
 
2174
                def next(self, a, b):
 
2175
                    pass
 
2176
            """
 
2177
        self.unchanged(s)
 
2178
 
 
2179
    def test_assign_to_next_in_list(self):
 
2180
        s = """
 
2181
            def foo():
 
2182
                [a, A.next] = foo
 
2183
 
 
2184
            class A:
 
2185
                def next(self, a, b):
 
2186
                    pass
 
2187
            """
 
2188
        self.unchanged(s)
 
2189
 
 
2190
    def test_shadowing_import_1(self):
 
2191
        s = """
 
2192
            import foo.bar as next
 
2193
 
 
2194
            class A:
 
2195
                def next(self, a, b):
 
2196
                    pass
 
2197
            """
 
2198
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2199
 
 
2200
    def test_shadowing_import_2(self):
 
2201
        s = """
 
2202
            import bar, bar.foo as next
 
2203
 
 
2204
            class A:
 
2205
                def next(self, a, b):
 
2206
                    pass
 
2207
            """
 
2208
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2209
 
 
2210
    def test_shadowing_import_3(self):
 
2211
        s = """
 
2212
            import bar, bar.foo as next, baz
 
2213
 
 
2214
            class A:
 
2215
                def next(self, a, b):
 
2216
                    pass
 
2217
            """
 
2218
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2219
 
 
2220
    def test_shadowing_import_from_1(self):
 
2221
        s = """
 
2222
            from x import next
 
2223
 
 
2224
            class A:
 
2225
                def next(self, a, b):
 
2226
                    pass
 
2227
            """
 
2228
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2229
 
 
2230
    def test_shadowing_import_from_2(self):
 
2231
        s = """
 
2232
            from x.a import next
 
2233
 
 
2234
            class A:
 
2235
                def next(self, a, b):
 
2236
                    pass
 
2237
            """
 
2238
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2239
 
 
2240
    def test_shadowing_import_from_3(self):
 
2241
        s = """
 
2242
            from x import a, next, b
 
2243
 
 
2244
            class A:
 
2245
                def next(self, a, b):
 
2246
                    pass
 
2247
            """
 
2248
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2249
 
 
2250
    def test_shadowing_import_from_4(self):
 
2251
        s = """
 
2252
            from x.a import a, next, b
 
2253
 
 
2254
            class A:
 
2255
                def next(self, a, b):
 
2256
                    pass
 
2257
            """
 
2258
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2259
 
 
2260
    def test_shadowing_funcdef_1(self):
 
2261
        s = """
 
2262
            def next(a):
 
2263
                pass
 
2264
 
 
2265
            class A:
 
2266
                def next(self, a, b):
 
2267
                    pass
 
2268
            """
 
2269
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2270
 
 
2271
    def test_shadowing_funcdef_2(self):
 
2272
        b = """
 
2273
            def next(a):
 
2274
                pass
 
2275
 
 
2276
            class A:
 
2277
                def next(self):
 
2278
                    pass
 
2279
 
 
2280
            it.next()
 
2281
            """
 
2282
        a = """
 
2283
            def next(a):
 
2284
                pass
 
2285
 
 
2286
            class A:
 
2287
                def __next__(self):
 
2288
                    pass
 
2289
 
 
2290
            it.__next__()
 
2291
            """
 
2292
        self.warns(b, a, "Calls to builtin next() possibly shadowed")
 
2293
 
 
2294
    def test_shadowing_global_1(self):
 
2295
        s = """
 
2296
            def f():
 
2297
                global next
 
2298
                next = 5
 
2299
            """
 
2300
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2301
 
 
2302
    def test_shadowing_global_2(self):
 
2303
        s = """
 
2304
            def f():
 
2305
                global a, next, b
 
2306
                next = 5
 
2307
            """
 
2308
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2309
 
 
2310
    def test_shadowing_for_simple(self):
 
2311
        s = """
 
2312
            for next in it():
 
2313
                pass
 
2314
 
 
2315
            b = 5
 
2316
            c = 6
 
2317
            """
 
2318
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2319
 
 
2320
    def test_shadowing_for_tuple_1(self):
 
2321
        s = """
 
2322
            for next, b in it():
 
2323
                pass
 
2324
 
 
2325
            b = 5
 
2326
            c = 6
 
2327
            """
 
2328
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2329
 
 
2330
    def test_shadowing_for_tuple_2(self):
 
2331
        s = """
 
2332
            for a, (next, c), b in it():
 
2333
                pass
 
2334
 
 
2335
            b = 5
 
2336
            c = 6
 
2337
            """
 
2338
        self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
 
2339
 
 
2340
    def test_noncall_access_1(self):
 
2341
        b = """gnext = g.next"""
 
2342
        a = """gnext = g.__next__"""
 
2343
        self.check(b, a)
 
2344
 
 
2345
    def test_noncall_access_2(self):
 
2346
        b = """f(g.next + 5)"""
 
2347
        a = """f(g.__next__ + 5)"""
 
2348
        self.check(b, a)
 
2349
 
 
2350
    def test_noncall_access_3(self):
 
2351
        b = """f(g().next + 5)"""
 
2352
        a = """f(g().__next__ + 5)"""
 
2353
        self.check(b, a)
 
2354
 
 
2355
class Test_nonzero(FixerTestCase):
 
2356
    fixer = "nonzero"
 
2357
 
 
2358
    def test_1(self):
 
2359
        b = """
 
2360
            class A:
 
2361
                def __nonzero__(self):
 
2362
                    pass
 
2363
            """
 
2364
        a = """
 
2365
            class A:
 
2366
                def __bool__(self):
 
2367
                    pass
 
2368
            """
 
2369
        self.check(b, a)
 
2370
 
 
2371
    def test_2(self):
 
2372
        b = """
 
2373
            class A(object):
 
2374
                def __nonzero__(self):
 
2375
                    pass
 
2376
            """
 
2377
        a = """
 
2378
            class A(object):
 
2379
                def __bool__(self):
 
2380
                    pass
 
2381
            """
 
2382
        self.check(b, a)
 
2383
 
 
2384
    def test_unchanged_1(self):
 
2385
        s = """
 
2386
            class A(object):
 
2387
                def __bool__(self):
 
2388
                    pass
 
2389
            """
 
2390
        self.unchanged(s)
 
2391
 
 
2392
    def test_unchanged_2(self):
 
2393
        s = """
 
2394
            class A(object):
 
2395
                def __nonzero__(self, a):
 
2396
                    pass
 
2397
            """
 
2398
        self.unchanged(s)
 
2399
 
 
2400
    def test_unchanged_func(self):
 
2401
        s = """
 
2402
            def __nonzero__(self):
 
2403
                pass
 
2404
            """
 
2405
        self.unchanged(s)
 
2406
 
 
2407
class Test_numliterals(FixerTestCase):
 
2408
    fixer = "numliterals"
 
2409
 
 
2410
    def test_octal_1(self):
 
2411
        b = """0755"""
 
2412
        a = """0o755"""
 
2413
        self.check(b, a)
 
2414
 
 
2415
    def test_long_int_1(self):
 
2416
        b = """a = 12L"""
 
2417
        a = """a = 12"""
 
2418
        self.check(b, a)
 
2419
 
 
2420
    def test_long_int_2(self):
 
2421
        b = """a = 12l"""
 
2422
        a = """a = 12"""
 
2423
        self.check(b, a)
 
2424
 
 
2425
    def test_long_hex(self):
 
2426
        b = """b = 0x12l"""
 
2427
        a = """b = 0x12"""
 
2428
        self.check(b, a)
 
2429
 
 
2430
    def test_comments_and_spacing(self):
 
2431
        b = """b =   0x12L"""
 
2432
        a = """b =   0x12"""
 
2433
        self.check(b, a)
 
2434
 
 
2435
        b = """b = 0755 # spam"""
 
2436
        a = """b = 0o755 # spam"""
 
2437
        self.check(b, a)
 
2438
 
 
2439
    def test_unchanged_int(self):
 
2440
        s = """5"""
 
2441
        self.unchanged(s)
 
2442
 
 
2443
    def test_unchanged_float(self):
 
2444
        s = """5.0"""
 
2445
        self.unchanged(s)
 
2446
 
 
2447
    def test_unchanged_octal(self):
 
2448
        s = """0o755"""
 
2449
        self.unchanged(s)
 
2450
 
 
2451
    def test_unchanged_hex(self):
 
2452
        s = """0xABC"""
 
2453
        self.unchanged(s)
 
2454
 
 
2455
    def test_unchanged_exp(self):
 
2456
        s = """5.0e10"""
 
2457
        self.unchanged(s)
 
2458
 
 
2459
    def test_unchanged_complex_int(self):
 
2460
        s = """5 + 4j"""
 
2461
        self.unchanged(s)
 
2462
 
 
2463
    def test_unchanged_complex_float(self):
 
2464
        s = """5.4 + 4.9j"""
 
2465
        self.unchanged(s)
 
2466
 
 
2467
    def test_unchanged_complex_bare(self):
 
2468
        s = """4j"""
 
2469
        self.unchanged(s)
 
2470
        s = """4.4j"""
 
2471
        self.unchanged(s)
 
2472
 
 
2473
class Test_renames(FixerTestCase):
 
2474
    fixer = "renames"
 
2475
 
 
2476
    modules = {"sys":  ("maxint", "maxsize"),
 
2477
              }
 
2478
 
 
2479
    def test_import_from(self):
 
2480
        for mod, (old, new) in self.modules.items():
 
2481
            b = "from %s import %s" % (mod, old)
 
2482
            a = "from %s import %s" % (mod, new)
 
2483
            self.check(b, a)
 
2484
 
 
2485
            s = "from foo import %s" % old
 
2486
            self.unchanged(s)
 
2487
 
 
2488
    def test_import_from_as(self):
 
2489
        for mod, (old, new) in self.modules.items():
 
2490
            b = "from %s import %s as foo_bar" % (mod, old)
 
2491
            a = "from %s import %s as foo_bar" % (mod, new)
 
2492
            self.check(b, a)
 
2493
 
 
2494
    def test_import_module_usage(self):
 
2495
        for mod, (old, new) in self.modules.items():
 
2496
            b = """
 
2497
                import %s
 
2498
                foo(%s, %s.%s)
 
2499
                """ % (mod, mod, mod, old)
 
2500
            a = """
 
2501
                import %s
 
2502
                foo(%s, %s.%s)
 
2503
                """ % (mod, mod, mod, new)
 
2504
            self.check(b, a)
 
2505
 
 
2506
    def XXX_test_from_import_usage(self):
 
2507
        # not implemented yet
 
2508
        for mod, (old, new) in self.modules.items():
 
2509
            b = """
 
2510
                from %s import %s
 
2511
                foo(%s, %s)
 
2512
                """ % (mod, old, mod, old)
 
2513
            a = """
 
2514
                from %s import %s
 
2515
                foo(%s, %s)
 
2516
                """ % (mod, new, mod, new)
 
2517
            self.check(b, a)
 
2518
 
 
2519
class Test_unicode(FixerTestCase):
 
2520
    fixer = "unicode"
 
2521
 
 
2522
    def test_unicode_call(self):
 
2523
        b = """unicode(x, y, z)"""
 
2524
        a = """str(x, y, z)"""
 
2525
        self.check(b, a)
 
2526
 
 
2527
    def test_unicode_literal_1(self):
 
2528
        b = '''u"x"'''
 
2529
        a = '''"x"'''
 
2530
        self.check(b, a)
 
2531
 
 
2532
    def test_unicode_literal_2(self):
 
2533
        b = """ur'x'"""
 
2534
        a = """r'x'"""
 
2535
        self.check(b, a)
 
2536
 
 
2537
    def test_unicode_literal_3(self):
 
2538
        b = """UR'''x'''"""
 
2539
        a = """R'''x'''"""
 
2540
        self.check(b, a)
 
2541
 
 
2542
class Test_callable(FixerTestCase):
 
2543
    fixer = "callable"
 
2544
 
 
2545
    def test_prefix_preservation(self):
 
2546
        b = """callable(    x)"""
 
2547
        a = """hasattr(    x, '__call__')"""
 
2548
        self.check(b, a)
 
2549
 
 
2550
        b = """if     callable(x): pass"""
 
2551
        a = """if     hasattr(x, '__call__'): pass"""
 
2552
        self.check(b, a)
 
2553
 
 
2554
    def test_callable_call(self):
 
2555
        b = """callable(x)"""
 
2556
        a = """hasattr(x, '__call__')"""
 
2557
        self.check(b, a)
 
2558
 
 
2559
    def test_callable_should_not_change(self):
 
2560
        a = """callable(*x)"""
 
2561
        self.unchanged(a)
 
2562
 
 
2563
        a = """callable(x, y)"""
 
2564
        self.unchanged(a)
 
2565
 
 
2566
        a = """callable(x, kw=y)"""
 
2567
        self.unchanged(a)
 
2568
 
 
2569
        a = """callable()"""
 
2570
        self.unchanged(a)
 
2571
 
 
2572
class Test_filter(FixerTestCase):
 
2573
    fixer = "filter"
 
2574
 
 
2575
    def test_prefix_preservation(self):
 
2576
        b = """x =   filter(    foo,     'abc'   )"""
 
2577
        a = """x =   list(filter(    foo,     'abc'   ))"""
 
2578
        self.check(b, a)
 
2579
 
 
2580
        b = """x =   filter(  None , 'abc'  )"""
 
2581
        a = """x =   [_f for _f in 'abc' if _f]"""
 
2582
        self.check(b, a)
 
2583
 
 
2584
    def test_filter_basic(self):
 
2585
        b = """x = filter(None, 'abc')"""
 
2586
        a = """x = [_f for _f in 'abc' if _f]"""
 
2587
        self.check(b, a)
 
2588
 
 
2589
        b = """x = len(filter(f, 'abc'))"""
 
2590
        a = """x = len(list(filter(f, 'abc')))"""
 
2591
        self.check(b, a)
 
2592
 
 
2593
        b = """x = filter(lambda x: x%2 == 0, range(10))"""
 
2594
        a = """x = [x for x in range(10) if x%2 == 0]"""
 
2595
        self.check(b, a)
 
2596
 
 
2597
        # Note the parens around x
 
2598
        b = """x = filter(lambda (x): x%2 == 0, range(10))"""
 
2599
        a = """x = [x for x in range(10) if x%2 == 0]"""
 
2600
        self.check(b, a)
 
2601
 
 
2602
        # XXX This (rare) case is not supported
 
2603
##         b = """x = filter(f, 'abc')[0]"""
 
2604
##         a = """x = list(filter(f, 'abc'))[0]"""
 
2605
##         self.check(b, a)
 
2606
 
 
2607
    def test_filter_nochange(self):
 
2608
        a = """b.join(filter(f, 'abc'))"""
 
2609
        self.unchanged(a)
 
2610
        a = """(a + foo(5)).join(filter(f, 'abc'))"""
 
2611
        self.unchanged(a)
 
2612
        a = """iter(filter(f, 'abc'))"""
 
2613
        self.unchanged(a)
 
2614
        a = """list(filter(f, 'abc'))"""
 
2615
        self.unchanged(a)
 
2616
        a = """list(filter(f, 'abc'))[0]"""
 
2617
        self.unchanged(a)
 
2618
        a = """set(filter(f, 'abc'))"""
 
2619
        self.unchanged(a)
 
2620
        a = """set(filter(f, 'abc')).pop()"""
 
2621
        self.unchanged(a)
 
2622
        a = """tuple(filter(f, 'abc'))"""
 
2623
        self.unchanged(a)
 
2624
        a = """any(filter(f, 'abc'))"""
 
2625
        self.unchanged(a)
 
2626
        a = """all(filter(f, 'abc'))"""
 
2627
        self.unchanged(a)
 
2628
        a = """sum(filter(f, 'abc'))"""
 
2629
        self.unchanged(a)
 
2630
        a = """sorted(filter(f, 'abc'))"""
 
2631
        self.unchanged(a)
 
2632
        a = """sorted(filter(f, 'abc'), key=blah)"""
 
2633
        self.unchanged(a)
 
2634
        a = """sorted(filter(f, 'abc'), key=blah)[0]"""
 
2635
        self.unchanged(a)
 
2636
        a = """for i in filter(f, 'abc'): pass"""
 
2637
        self.unchanged(a)
 
2638
        a = """[x for x in filter(f, 'abc')]"""
 
2639
        self.unchanged(a)
 
2640
        a = """(x for x in filter(f, 'abc'))"""
 
2641
        self.unchanged(a)
 
2642
 
 
2643
    def test_future_builtins(self):
 
2644
        a = "from future_builtins import spam, filter; filter(f, 'ham')"
 
2645
        self.unchanged(a)
 
2646
 
 
2647
        b = """from future_builtins import spam; x = filter(f, 'abc')"""
 
2648
        a = """from future_builtins import spam; x = list(filter(f, 'abc'))"""
 
2649
        self.check(b, a)
 
2650
 
 
2651
        a = "from future_builtins import *; filter(f, 'ham')"
 
2652
        self.unchanged(a)
 
2653
 
 
2654
class Test_map(FixerTestCase):
 
2655
    fixer = "map"
 
2656
 
 
2657
    def check(self, b, a):
 
2658
        self.unchanged("from future_builtins import map; " + b, a)
 
2659
        FixerTestCase.check(self, b, a)
 
2660
 
 
2661
    def test_prefix_preservation(self):
 
2662
        b = """x =    map(   f,    'abc'   )"""
 
2663
        a = """x =    list(map(   f,    'abc'   ))"""
 
2664
        self.check(b, a)
 
2665
 
 
2666
    def test_trailing_comment(self):
 
2667
        b = """x = map(f, 'abc')   #   foo"""
 
2668
        a = """x = list(map(f, 'abc'))   #   foo"""
 
2669
        self.check(b, a)
 
2670
 
 
2671
    def test_map_basic(self):
 
2672
        b = """x = map(f, 'abc')"""
 
2673
        a = """x = list(map(f, 'abc'))"""
 
2674
        self.check(b, a)
 
2675
 
 
2676
        b = """x = len(map(f, 'abc', 'def'))"""
 
2677
        a = """x = len(list(map(f, 'abc', 'def')))"""
 
2678
        self.check(b, a)
 
2679
 
 
2680
        b = """x = map(None, 'abc')"""
 
2681
        a = """x = list('abc')"""
 
2682
        self.check(b, a)
 
2683
 
 
2684
        b = """x = map(None, 'abc', 'def')"""
 
2685
        a = """x = list(map(None, 'abc', 'def'))"""
 
2686
        self.check(b, a)
 
2687
 
 
2688
        b = """x = map(lambda x: x+1, range(4))"""
 
2689
        a = """x = [x+1 for x in range(4)]"""
 
2690
        self.check(b, a)
 
2691
 
 
2692
        # Note the parens around x
 
2693
        b = """x = map(lambda (x): x+1, range(4))"""
 
2694
        a = """x = [x+1 for x in range(4)]"""
 
2695
        self.check(b, a)
 
2696
 
 
2697
        b = """
 
2698
            foo()
 
2699
            # foo
 
2700
            map(f, x)
 
2701
            """
 
2702
        a = """
 
2703
            foo()
 
2704
            # foo
 
2705
            list(map(f, x))
 
2706
            """
 
2707
        self.warns(b, a, "You should use a for loop here")
 
2708
 
 
2709
        # XXX This (rare) case is not supported
 
2710
##         b = """x = map(f, 'abc')[0]"""
 
2711
##         a = """x = list(map(f, 'abc'))[0]"""
 
2712
##         self.check(b, a)
 
2713
 
 
2714
    def test_map_nochange(self):
 
2715
        a = """b.join(map(f, 'abc'))"""
 
2716
        self.unchanged(a)
 
2717
        a = """(a + foo(5)).join(map(f, 'abc'))"""
 
2718
        self.unchanged(a)
 
2719
        a = """iter(map(f, 'abc'))"""
 
2720
        self.unchanged(a)
 
2721
        a = """list(map(f, 'abc'))"""
 
2722
        self.unchanged(a)
 
2723
        a = """list(map(f, 'abc'))[0]"""
 
2724
        self.unchanged(a)
 
2725
        a = """set(map(f, 'abc'))"""
 
2726
        self.unchanged(a)
 
2727
        a = """set(map(f, 'abc')).pop()"""
 
2728
        self.unchanged(a)
 
2729
        a = """tuple(map(f, 'abc'))"""
 
2730
        self.unchanged(a)
 
2731
        a = """any(map(f, 'abc'))"""
 
2732
        self.unchanged(a)
 
2733
        a = """all(map(f, 'abc'))"""
 
2734
        self.unchanged(a)
 
2735
        a = """sum(map(f, 'abc'))"""
 
2736
        self.unchanged(a)
 
2737
        a = """sorted(map(f, 'abc'))"""
 
2738
        self.unchanged(a)
 
2739
        a = """sorted(map(f, 'abc'), key=blah)"""
 
2740
        self.unchanged(a)
 
2741
        a = """sorted(map(f, 'abc'), key=blah)[0]"""
 
2742
        self.unchanged(a)
 
2743
        a = """for i in map(f, 'abc'): pass"""
 
2744
        self.unchanged(a)
 
2745
        a = """[x for x in map(f, 'abc')]"""
 
2746
        self.unchanged(a)
 
2747
        a = """(x for x in map(f, 'abc'))"""
 
2748
        self.unchanged(a)
 
2749
 
 
2750
    def test_future_builtins(self):
 
2751
        a = "from future_builtins import spam, map, eggs; map(f, 'ham')"
 
2752
        self.unchanged(a)
 
2753
 
 
2754
        b = """from future_builtins import spam, eggs; x = map(f, 'abc')"""
 
2755
        a = """from future_builtins import spam, eggs; x = list(map(f, 'abc'))"""
 
2756
        self.check(b, a)
 
2757
 
 
2758
        a = "from future_builtins import *; map(f, 'ham')"
 
2759
        self.unchanged(a)
 
2760
 
 
2761
class Test_zip(FixerTestCase):
 
2762
    fixer = "zip"
 
2763
 
 
2764
    def check(self, b, a):
 
2765
        self.unchanged("from future_builtins import zip; " + b, a)
 
2766
        FixerTestCase.check(self, b, a)
 
2767
 
 
2768
    def test_zip_basic(self):
 
2769
        b = """x = zip(a, b, c)"""
 
2770
        a = """x = list(zip(a, b, c))"""
 
2771
        self.check(b, a)
 
2772
 
 
2773
        b = """x = len(zip(a, b))"""
 
2774
        a = """x = len(list(zip(a, b)))"""
 
2775
        self.check(b, a)
 
2776
 
 
2777
    def test_zip_nochange(self):
 
2778
        a = """b.join(zip(a, b))"""
 
2779
        self.unchanged(a)
 
2780
        a = """(a + foo(5)).join(zip(a, b))"""
 
2781
        self.unchanged(a)
 
2782
        a = """iter(zip(a, b))"""
 
2783
        self.unchanged(a)
 
2784
        a = """list(zip(a, b))"""
 
2785
        self.unchanged(a)
 
2786
        a = """list(zip(a, b))[0]"""
 
2787
        self.unchanged(a)
 
2788
        a = """set(zip(a, b))"""
 
2789
        self.unchanged(a)
 
2790
        a = """set(zip(a, b)).pop()"""
 
2791
        self.unchanged(a)
 
2792
        a = """tuple(zip(a, b))"""
 
2793
        self.unchanged(a)
 
2794
        a = """any(zip(a, b))"""
 
2795
        self.unchanged(a)
 
2796
        a = """all(zip(a, b))"""
 
2797
        self.unchanged(a)
 
2798
        a = """sum(zip(a, b))"""
 
2799
        self.unchanged(a)
 
2800
        a = """sorted(zip(a, b))"""
 
2801
        self.unchanged(a)
 
2802
        a = """sorted(zip(a, b), key=blah)"""
 
2803
        self.unchanged(a)
 
2804
        a = """sorted(zip(a, b), key=blah)[0]"""
 
2805
        self.unchanged(a)
 
2806
        a = """for i in zip(a, b): pass"""
 
2807
        self.unchanged(a)
 
2808
        a = """[x for x in zip(a, b)]"""
 
2809
        self.unchanged(a)
 
2810
        a = """(x for x in zip(a, b))"""
 
2811
        self.unchanged(a)
 
2812
 
 
2813
    def test_future_builtins(self):
 
2814
        a = "from future_builtins import spam, zip, eggs; zip(a, b)"
 
2815
        self.unchanged(a)
 
2816
 
 
2817
        b = """from future_builtins import spam, eggs; x = zip(a, b)"""
 
2818
        a = """from future_builtins import spam, eggs; x = list(zip(a, b))"""
 
2819
        self.check(b, a)
 
2820
 
 
2821
        a = "from future_builtins import *; zip(a, b)"
 
2822
        self.unchanged(a)
 
2823
 
 
2824
class Test_standarderror(FixerTestCase):
 
2825
    fixer = "standarderror"
 
2826
 
 
2827
    def test(self):
 
2828
        b = """x =    StandardError()"""
 
2829
        a = """x =    Exception()"""
 
2830
        self.check(b, a)
 
2831
 
 
2832
        b = """x = StandardError(a, b, c)"""
 
2833
        a = """x = Exception(a, b, c)"""
 
2834
        self.check(b, a)
 
2835
 
 
2836
        b = """f(2 + StandardError(a, b, c))"""
 
2837
        a = """f(2 + Exception(a, b, c))"""
 
2838
        self.check(b, a)
 
2839
 
 
2840
class Test_types(FixerTestCase):
 
2841
    fixer = "types"
 
2842
 
 
2843
    def test_basic_types_convert(self):
 
2844
        b = """types.StringType"""
 
2845
        a = """bytes"""
 
2846
        self.check(b, a)
 
2847
 
 
2848
        b = """types.DictType"""
 
2849
        a = """dict"""
 
2850
        self.check(b, a)
 
2851
 
 
2852
        b = """types . IntType"""
 
2853
        a = """int"""
 
2854
        self.check(b, a)
 
2855
 
 
2856
        b = """types.ListType"""
 
2857
        a = """list"""
 
2858
        self.check(b, a)
 
2859
 
 
2860
        b = """types.LongType"""
 
2861
        a = """int"""
 
2862
        self.check(b, a)
 
2863
 
 
2864
        b = """types.NoneType"""
 
2865
        a = """type(None)"""
 
2866
        self.check(b, a)
 
2867
 
 
2868
class Test_idioms(FixerTestCase):
 
2869
    fixer = "idioms"
 
2870
 
 
2871
    def test_while(self):
 
2872
        b = """while 1: foo()"""
 
2873
        a = """while True: foo()"""
 
2874
        self.check(b, a)
 
2875
 
 
2876
        b = """while   1: foo()"""
 
2877
        a = """while   True: foo()"""
 
2878
        self.check(b, a)
 
2879
 
 
2880
        b = """
 
2881
            while 1:
 
2882
                foo()
 
2883
            """
 
2884
        a = """
 
2885
            while True:
 
2886
                foo()
 
2887
            """
 
2888
        self.check(b, a)
 
2889
 
 
2890
    def test_while_unchanged(self):
 
2891
        s = """while 11: foo()"""
 
2892
        self.unchanged(s)
 
2893
 
 
2894
        s = """while 0: foo()"""
 
2895
        self.unchanged(s)
 
2896
 
 
2897
        s = """while foo(): foo()"""
 
2898
        self.unchanged(s)
 
2899
 
 
2900
        s = """while []: foo()"""
 
2901
        self.unchanged(s)
 
2902
 
 
2903
    def test_eq_simple(self):
 
2904
        b = """type(x) == T"""
 
2905
        a = """isinstance(x, T)"""
 
2906
        self.check(b, a)
 
2907
 
 
2908
        b = """if   type(x) == T: pass"""
 
2909
        a = """if   isinstance(x, T): pass"""
 
2910
        self.check(b, a)
 
2911
 
 
2912
    def test_eq_reverse(self):
 
2913
        b = """T == type(x)"""
 
2914
        a = """isinstance(x, T)"""
 
2915
        self.check(b, a)
 
2916
 
 
2917
        b = """if   T == type(x): pass"""
 
2918
        a = """if   isinstance(x, T): pass"""
 
2919
        self.check(b, a)
 
2920
 
 
2921
    def test_eq_expression(self):
 
2922
        b = """type(x+y) == d.get('T')"""
 
2923
        a = """isinstance(x+y, d.get('T'))"""
 
2924
        self.check(b, a)
 
2925
 
 
2926
        b = """type(   x  +  y) == d.get('T')"""
 
2927
        a = """isinstance(x  +  y, d.get('T'))"""
 
2928
        self.check(b, a)
 
2929
 
 
2930
    def test_is_simple(self):
 
2931
        b = """type(x) is T"""
 
2932
        a = """isinstance(x, T)"""
 
2933
        self.check(b, a)
 
2934
 
 
2935
        b = """if   type(x) is T: pass"""
 
2936
        a = """if   isinstance(x, T): pass"""
 
2937
        self.check(b, a)
 
2938
 
 
2939
    def test_is_reverse(self):
 
2940
        b = """T is type(x)"""
 
2941
        a = """isinstance(x, T)"""
 
2942
        self.check(b, a)
 
2943
 
 
2944
        b = """if   T is type(x): pass"""
 
2945
        a = """if   isinstance(x, T): pass"""
 
2946
        self.check(b, a)
 
2947
 
 
2948
    def test_is_expression(self):
 
2949
        b = """type(x+y) is d.get('T')"""
 
2950
        a = """isinstance(x+y, d.get('T'))"""
 
2951
        self.check(b, a)
 
2952
 
 
2953
        b = """type(   x  +  y) is d.get('T')"""
 
2954
        a = """isinstance(x  +  y, d.get('T'))"""
 
2955
        self.check(b, a)
 
2956
 
 
2957
    def test_is_not_simple(self):
 
2958
        b = """type(x) is not T"""
 
2959
        a = """not isinstance(x, T)"""
 
2960
        self.check(b, a)
 
2961
 
 
2962
        b = """if   type(x) is not T: pass"""
 
2963
        a = """if   not isinstance(x, T): pass"""
 
2964
        self.check(b, a)
 
2965
 
 
2966
    def test_is_not_reverse(self):
 
2967
        b = """T is not type(x)"""
 
2968
        a = """not isinstance(x, T)"""
 
2969
        self.check(b, a)
 
2970
 
 
2971
        b = """if   T is not type(x): pass"""
 
2972
        a = """if   not isinstance(x, T): pass"""
 
2973
        self.check(b, a)
 
2974
 
 
2975
    def test_is_not_expression(self):
 
2976
        b = """type(x+y) is not d.get('T')"""
 
2977
        a = """not isinstance(x+y, d.get('T'))"""
 
2978
        self.check(b, a)
 
2979
 
 
2980
        b = """type(   x  +  y) is not d.get('T')"""
 
2981
        a = """not isinstance(x  +  y, d.get('T'))"""
 
2982
        self.check(b, a)
 
2983
 
 
2984
    def test_ne_simple(self):
 
2985
        b = """type(x) != T"""
 
2986
        a = """not isinstance(x, T)"""
 
2987
        self.check(b, a)
 
2988
 
 
2989
        b = """if   type(x) != T: pass"""
 
2990
        a = """if   not isinstance(x, T): pass"""
 
2991
        self.check(b, a)
 
2992
 
 
2993
    def test_ne_reverse(self):
 
2994
        b = """T != type(x)"""
 
2995
        a = """not isinstance(x, T)"""
 
2996
        self.check(b, a)
 
2997
 
 
2998
        b = """if   T != type(x): pass"""
 
2999
        a = """if   not isinstance(x, T): pass"""
 
3000
        self.check(b, a)
 
3001
 
 
3002
    def test_ne_expression(self):
 
3003
        b = """type(x+y) != d.get('T')"""
 
3004
        a = """not isinstance(x+y, d.get('T'))"""
 
3005
        self.check(b, a)
 
3006
 
 
3007
        b = """type(   x  +  y) != d.get('T')"""
 
3008
        a = """not isinstance(x  +  y, d.get('T'))"""
 
3009
        self.check(b, a)
 
3010
 
 
3011
    def test_type_unchanged(self):
 
3012
        a = """type(x).__name__"""
 
3013
        self.unchanged(a)
 
3014
 
 
3015
    def test_sort_list_call(self):
 
3016
        b = """
 
3017
            v = list(t)
 
3018
            v.sort()
 
3019
            foo(v)
 
3020
            """
 
3021
        a = """
 
3022
            v = sorted(t)
 
3023
            foo(v)
 
3024
            """
 
3025
        self.check(b, a)
 
3026
 
 
3027
        b = """
 
3028
            v = list(foo(b) + d)
 
3029
            v.sort()
 
3030
            foo(v)
 
3031
            """
 
3032
        a = """
 
3033
            v = sorted(foo(b) + d)
 
3034
            foo(v)
 
3035
            """
 
3036
        self.check(b, a)
 
3037
 
 
3038
        b = """
 
3039
            while x:
 
3040
                v = list(t)
 
3041
                v.sort()
 
3042
                foo(v)
 
3043
            """
 
3044
        a = """
 
3045
            while x:
 
3046
                v = sorted(t)
 
3047
                foo(v)
 
3048
            """
 
3049
        self.check(b, a)
 
3050
 
 
3051
        b = """
 
3052
            v = list(t)
 
3053
            # foo
 
3054
            v.sort()
 
3055
            foo(v)
 
3056
            """
 
3057
        a = """
 
3058
            v = sorted(t)
 
3059
            # foo
 
3060
            foo(v)
 
3061
            """
 
3062
        self.check(b, a)
 
3063
 
 
3064
        b = r"""
 
3065
            v = list(   t)
 
3066
            v.sort()
 
3067
            foo(v)
 
3068
            """
 
3069
        a = r"""
 
3070
            v = sorted(   t)
 
3071
            foo(v)
 
3072
            """
 
3073
        self.check(b, a)
 
3074
 
 
3075
    def test_sort_simple_expr(self):
 
3076
        b = """
 
3077
            v = t
 
3078
            v.sort()
 
3079
            foo(v)
 
3080
            """
 
3081
        a = """
 
3082
            v = sorted(t)
 
3083
            foo(v)
 
3084
            """
 
3085
        self.check(b, a)
 
3086
 
 
3087
        b = """
 
3088
            v = foo(b)
 
3089
            v.sort()
 
3090
            foo(v)
 
3091
            """
 
3092
        a = """
 
3093
            v = sorted(foo(b))
 
3094
            foo(v)
 
3095
            """
 
3096
        self.check(b, a)
 
3097
 
 
3098
        b = """
 
3099
            v = b.keys()
 
3100
            v.sort()
 
3101
            foo(v)
 
3102
            """
 
3103
        a = """
 
3104
            v = sorted(b.keys())
 
3105
            foo(v)
 
3106
            """
 
3107
        self.check(b, a)
 
3108
 
 
3109
        b = """
 
3110
            v = foo(b) + d
 
3111
            v.sort()
 
3112
            foo(v)
 
3113
            """
 
3114
        a = """
 
3115
            v = sorted(foo(b) + d)
 
3116
            foo(v)
 
3117
            """
 
3118
        self.check(b, a)
 
3119
 
 
3120
        b = """
 
3121
            while x:
 
3122
                v = t
 
3123
                v.sort()
 
3124
                foo(v)
 
3125
            """
 
3126
        a = """
 
3127
            while x:
 
3128
                v = sorted(t)
 
3129
                foo(v)
 
3130
            """
 
3131
        self.check(b, a)
 
3132
 
 
3133
        b = """
 
3134
            v = t
 
3135
            # foo
 
3136
            v.sort()
 
3137
            foo(v)
 
3138
            """
 
3139
        a = """
 
3140
            v = sorted(t)
 
3141
            # foo
 
3142
            foo(v)
 
3143
            """
 
3144
        self.check(b, a)
 
3145
 
 
3146
        b = r"""
 
3147
            v =   t
 
3148
            v.sort()
 
3149
            foo(v)
 
3150
            """
 
3151
        a = r"""
 
3152
            v =   sorted(t)
 
3153
            foo(v)
 
3154
            """
 
3155
        self.check(b, a)
 
3156
 
 
3157
    def test_sort_unchanged(self):
 
3158
        s = """
 
3159
            v = list(t)
 
3160
            w.sort()
 
3161
            foo(w)
 
3162
            """
 
3163
        self.unchanged(s)
 
3164
 
 
3165
        s = """
 
3166
            v = list(t)
 
3167
            v.sort(u)
 
3168
            foo(v)
 
3169
            """
 
3170
        self.unchanged(s)
 
3171
 
 
3172
class Test_basestring(FixerTestCase):
 
3173
    fixer = "basestring"
 
3174
 
 
3175
    def test_basestring(self):
 
3176
        b = """isinstance(x, basestring)"""
 
3177
        a = """isinstance(x, str)"""
 
3178
        self.check(b, a)
 
3179
 
 
3180
class Test_buffer(FixerTestCase):
 
3181
    fixer = "buffer"
 
3182
 
 
3183
    def test_buffer(self):
 
3184
        b = """x = buffer(y)"""
 
3185
        a = """x = memoryview(y)"""
 
3186
        self.check(b, a)
 
3187
 
 
3188
class Test_future(FixerTestCase):
 
3189
    fixer = "future"
 
3190
 
 
3191
    def test_future(self):
 
3192
        b = """from __future__ import braces"""
 
3193
        a = """"""
 
3194
        self.check(b, a)
 
3195
 
 
3196
        b = """# comment\nfrom __future__ import braces"""
 
3197
        a = """# comment\n"""
 
3198
        self.check(b, a)
 
3199
 
 
3200
        b = """from __future__ import braces\n# comment"""
 
3201
        a = """\n# comment"""
 
3202
        self.check(b, a)
 
3203
 
 
3204
    def test_run_order(self):
 
3205
        self.assert_runs_after('print')
 
3206
 
 
3207
class Test_itertools(FixerTestCase):
 
3208
    fixer = "itertools"
 
3209
 
 
3210
    def checkall(self, before, after):
 
3211
        # Because we need to check with and without the itertools prefix
 
3212
        # and on each of the three functions, these loops make it all
 
3213
        # much easier
 
3214
        for i in ('itertools.', ''):
 
3215
            for f in ('map', 'filter', 'zip'):
 
3216
                b = before %(i+'i'+f)
 
3217
                a = after %(f)
 
3218
                self.check(b, a)
 
3219
 
 
3220
    def test_0(self):
 
3221
        # A simple example -- test_1 covers exactly the same thing,
 
3222
        # but it's not quite as clear.
 
3223
        b = "itertools.izip(a, b)"
 
3224
        a = "zip(a, b)"
 
3225
        self.check(b, a)
 
3226
 
 
3227
    def test_1(self):
 
3228
        b = """%s(f, a)"""
 
3229
        a = """%s(f, a)"""
 
3230
        self.checkall(b, a)
 
3231
 
 
3232
    def test_2(self):
 
3233
        b = """itertools.ifilterfalse(a, b)"""
 
3234
        a = """itertools.filterfalse(a, b)"""
 
3235
        self.check(b, a)
 
3236
 
 
3237
    def test_4(self):
 
3238
        b = """ifilterfalse(a, b)"""
 
3239
        a = """filterfalse(a, b)"""
 
3240
        self.check(b, a)
 
3241
 
 
3242
    def test_space_1(self):
 
3243
        b = """    %s(f, a)"""
 
3244
        a = """    %s(f, a)"""
 
3245
        self.checkall(b, a)
 
3246
 
 
3247
    def test_space_2(self):
 
3248
        b = """    itertools.ifilterfalse(a, b)"""
 
3249
        a = """    itertools.filterfalse(a, b)"""
 
3250
        self.check(b, a)
 
3251
 
 
3252
    def test_run_order(self):
 
3253
        self.assert_runs_after('map', 'zip', 'filter')
 
3254
 
 
3255
class Test_itertools_imports(FixerTestCase):
 
3256
    fixer = 'itertools_imports'
 
3257
 
 
3258
    def test_reduced(self):
 
3259
        b = "from itertools import imap, izip, foo"
 
3260
        a = "from itertools import foo"
 
3261
        self.check(b, a)
 
3262
 
 
3263
        b = "from itertools import bar, imap, izip, foo"
 
3264
        a = "from itertools import bar, foo"
 
3265
        self.check(b, a)
 
3266
 
 
3267
    def test_comments(self):
 
3268
        b = "#foo\nfrom itertools import imap, izip"
 
3269
        a = "#foo\n"
 
3270
        self.check(b, a)
 
3271
 
 
3272
    def test_none(self):
 
3273
        b = "from itertools import imap, izip"
 
3274
        a = ""
 
3275
        self.check(b, a)
 
3276
 
 
3277
        b = "from itertools import izip"
 
3278
        a = ""
 
3279
        self.check(b, a)
 
3280
 
 
3281
    def test_import_as(self):
 
3282
        b = "from itertools import izip, bar as bang, imap"
 
3283
        a = "from itertools import bar as bang"
 
3284
        self.check(b, a)
 
3285
 
 
3286
        s = "from itertools import bar as bang"
 
3287
        self.unchanged(s)
 
3288
 
 
3289
    def test_ifilter(self):
 
3290
        b = "from itertools import ifilterfalse"
 
3291
        a = "from itertools import filterfalse"
 
3292
        self.check(b, a)
 
3293
 
 
3294
        b = "from itertools import imap, ifilterfalse, foo"
 
3295
        a = "from itertools import filterfalse, foo"
 
3296
        self.check(b, a)
 
3297
 
 
3298
        b = "from itertools import bar, ifilterfalse, foo"
 
3299
        a = "from itertools import bar, filterfalse, foo"
 
3300
        self.check(b, a)
 
3301
 
 
3302
 
 
3303
    def test_unchanged(self):
 
3304
        s = "from itertools import foo"
 
3305
        self.unchanged(s)
 
3306
 
 
3307
class Test_import(FixerTestCase):
 
3308
    fixer = "import"
 
3309
 
 
3310
    def setUp(self):
 
3311
        FixerTestCase.setUp(self)
 
3312
        # Need to replace fix_import's exists method
 
3313
        # so we can check that it's doing the right thing
 
3314
        self.files_checked = []
 
3315
        self.present_files = set()
 
3316
        self.always_exists = True
 
3317
        def fake_exists(name):
 
3318
            self.files_checked.append(name)
 
3319
            return self.always_exists or (name in self.present_files)
 
3320
 
 
3321
        from ..fixes import fix_import
 
3322
        fix_import.exists = fake_exists
 
3323
 
 
3324
    def tearDown(self):
 
3325
        from lib2to3.fixes import fix_import
 
3326
        fix_import.exists = os.path.exists
 
3327
 
 
3328
    def check_both(self, b, a):
 
3329
        self.always_exists = True
 
3330
        FixerTestCase.check(self, b, a)
 
3331
        self.always_exists = False
 
3332
        FixerTestCase.unchanged(self, b)
 
3333
 
 
3334
    def test_files_checked(self):
 
3335
        def p(path):
 
3336
            # Takes a unix path and returns a path with correct separators
 
3337
            return os.path.pathsep.join(path.split("/"))
 
3338
 
 
3339
        self.always_exists = False
 
3340
        self.present_files = set(['__init__.py'])
 
3341
        expected_extensions = ('.py', os.path.pathsep, '.pyc', '.so',
 
3342
                               '.sl', '.pyd')
 
3343
        names_to_test = (p("/spam/eggs.py"), "ni.py", p("../../shrubbery.py"))
 
3344
 
 
3345
        for name in names_to_test:
 
3346
            self.files_checked = []
 
3347
            self.filename = name
 
3348
            self.unchanged("import jam")
 
3349
 
 
3350
            if os.path.dirname(name):
 
3351
                name = os.path.dirname(name) + '/jam'
 
3352
            else:
 
3353
                name = 'jam'
 
3354
            expected_checks = set(name + ext for ext in expected_extensions)
 
3355
            expected_checks.add("__init__.py")
 
3356
 
 
3357
            self.assertEqual(set(self.files_checked), expected_checks)
 
3358
 
 
3359
    def test_not_in_package(self):
 
3360
        s = "import bar"
 
3361
        self.always_exists = False
 
3362
        self.present_files = set(["bar.py"])
 
3363
        self.unchanged(s)
 
3364
 
 
3365
    def test_in_package(self):
 
3366
        b = "import bar"
 
3367
        a = "from . import bar"
 
3368
        self.always_exists = False
 
3369
        self.present_files = set(["__init__.py", "bar.py"])
 
3370
        self.check(b, a)
 
3371
 
 
3372
    def test_comments_and_indent(self):
 
3373
        b = "import bar # Foo"
 
3374
        a = "from . import bar # Foo"
 
3375
        self.check(b, a)
 
3376
 
 
3377
    def test_from(self):
 
3378
        b = "from foo import bar, baz"
 
3379
        a = "from .foo import bar, baz"
 
3380
        self.check_both(b, a)
 
3381
 
 
3382
        b = "from foo import bar"
 
3383
        a = "from .foo import bar"
 
3384
        self.check_both(b, a)
 
3385
 
 
3386
        b = "from foo import (bar, baz)"
 
3387
        a = "from .foo import (bar, baz)"
 
3388
        self.check_both(b, a)
 
3389
 
 
3390
    def test_dotted_from(self):
 
3391
        b = "from green.eggs import ham"
 
3392
        a = "from .green.eggs import ham"
 
3393
        self.check_both(b, a)
 
3394
 
 
3395
    def test_from_as(self):
 
3396
        b = "from green.eggs import ham as spam"
 
3397
        a = "from .green.eggs import ham as spam"
 
3398
        self.check_both(b, a)
 
3399
 
 
3400
    def test_import(self):
 
3401
        b = "import foo"
 
3402
        a = "from . import foo"
 
3403
        self.check_both(b, a)
 
3404
 
 
3405
        b = "import foo, bar"
 
3406
        a = "from . import foo, bar"
 
3407
        self.check_both(b, a)
 
3408
 
 
3409
    def test_dotted_import(self):
 
3410
        b = "import foo.bar"
 
3411
        a = "from . import foo.bar"
 
3412
        self.check_both(b, a)
 
3413
 
 
3414
    def test_dotted_import_as(self):
 
3415
        b = "import foo.bar as bang"
 
3416
        a = "from . import foo.bar as bang"
 
3417
        self.check_both(b, a)
 
3418
 
 
3419
    def test_prefix(self):
 
3420
        b = """
 
3421
        # prefix
 
3422
        import foo.bar
 
3423
        """
 
3424
        a = """
 
3425
        # prefix
 
3426
        from . import foo.bar
 
3427
        """
 
3428
        self.check_both(b, a)
 
3429
 
 
3430
 
 
3431
class Test_set_literal(FixerTestCase):
 
3432
 
 
3433
    fixer = "set_literal"
 
3434
 
 
3435
    def test_basic(self):
 
3436
        b = """set([1, 2, 3])"""
 
3437
        a = """{1, 2, 3}"""
 
3438
        self.check(b, a)
 
3439
 
 
3440
        b = """set((1, 2, 3))"""
 
3441
        a = """{1, 2, 3}"""
 
3442
        self.check(b, a)
 
3443
 
 
3444
        b = """set((1,))"""
 
3445
        a = """{1}"""
 
3446
        self.check(b, a)
 
3447
 
 
3448
        b = """set([1])"""
 
3449
        self.check(b, a)
 
3450
 
 
3451
        b = """set((a, b))"""
 
3452
        a = """{a, b}"""
 
3453
        self.check(b, a)
 
3454
 
 
3455
        b = """set([a, b])"""
 
3456
        self.check(b, a)
 
3457
 
 
3458
        b = """set((a*234, f(args=23)))"""
 
3459
        a = """{a*234, f(args=23)}"""
 
3460
        self.check(b, a)
 
3461
 
 
3462
        b = """set([a*23, f(23)])"""
 
3463
        a = """{a*23, f(23)}"""
 
3464
        self.check(b, a)
 
3465
 
 
3466
        b = """set([a-234**23])"""
 
3467
        a = """{a-234**23}"""
 
3468
        self.check(b, a)
 
3469
 
 
3470
    def test_listcomps(self):
 
3471
        b = """set([x for x in y])"""
 
3472
        a = """{x for x in y}"""
 
3473
        self.check(b, a)
 
3474
 
 
3475
        b = """set([x for x in y if x == m])"""
 
3476
        a = """{x for x in y if x == m}"""
 
3477
        self.check(b, a)
 
3478
 
 
3479
        b = """set([x for x in y for a in b])"""
 
3480
        a = """{x for x in y for a in b}"""
 
3481
        self.check(b, a)
 
3482
 
 
3483
        b = """set([f(x) - 23 for x in y])"""
 
3484
        a = """{f(x) - 23 for x in y}"""
 
3485
        self.check(b, a)
 
3486
 
 
3487
    def test_whitespace(self):
 
3488
        b = """set( [1, 2])"""
 
3489
        a = """{1, 2}"""
 
3490
        self.check(b, a)
 
3491
 
 
3492
        b = """set([1 ,  2])"""
 
3493
        a = """{1 ,  2}"""
 
3494
        self.check(b, a)
 
3495
 
 
3496
        b = """set([ 1 ])"""
 
3497
        a = """{ 1 }"""
 
3498
        self.check(b, a)
 
3499
 
 
3500
        b = """set( [1] )"""
 
3501
        a = """{1}"""
 
3502
        self.check(b, a)
 
3503
 
 
3504
        b = """set([  1,  2  ])"""
 
3505
        a = """{  1,  2  }"""
 
3506
        self.check(b, a)
 
3507
 
 
3508
        b = """set([x  for x in y ])"""
 
3509
        a = """{x  for x in y }"""
 
3510
        self.check(b, a)
 
3511
 
 
3512
        b = """set(
 
3513
                   [1, 2]
 
3514
               )
 
3515
            """
 
3516
        a = """{1, 2}\n"""
 
3517
        self.check(b, a)
 
3518
 
 
3519
    def test_comments(self):
 
3520
        b = """set((1, 2)) # Hi"""
 
3521
        a = """{1, 2} # Hi"""
 
3522
        self.check(b, a)
 
3523
 
 
3524
        # This isn't optimal behavior, but the fixer is optional.
 
3525
        b = """
 
3526
            # Foo
 
3527
            set( # Bar
 
3528
               (1, 2)
 
3529
            )
 
3530
            """
 
3531
        a = """
 
3532
            # Foo
 
3533
            {1, 2}
 
3534
            """
 
3535
        self.check(b, a)
 
3536
 
 
3537
    def test_unchanged(self):
 
3538
        s = """set()"""
 
3539
        self.unchanged(s)
 
3540
 
 
3541
        s = """set(a)"""
 
3542
        self.unchanged(s)
 
3543
 
 
3544
        s = """set(a, b, c)"""
 
3545
        self.unchanged(s)
 
3546
 
 
3547
        # Don't transform generators because they might have to be lazy.
 
3548
        s = """set(x for x in y)"""
 
3549
        self.unchanged(s)
 
3550
 
 
3551
        s = """set(x for x in y if z)"""
 
3552
        self.unchanged(s)
 
3553
 
 
3554
        s = """set(a*823-23**2 + f(23))"""
 
3555
        self.unchanged(s)
 
3556
 
 
3557
 
 
3558
class Test_sys_exc(FixerTestCase):
 
3559
    fixer = "sys_exc"
 
3560
 
 
3561
    def test_0(self):
 
3562
        b = "sys.exc_type"
 
3563
        a = "sys.exc_info()[0]"
 
3564
        self.check(b, a)
 
3565
 
 
3566
    def test_1(self):
 
3567
        b = "sys.exc_value"
 
3568
        a = "sys.exc_info()[1]"
 
3569
        self.check(b, a)
 
3570
 
 
3571
    def test_2(self):
 
3572
        b = "sys.exc_traceback"
 
3573
        a = "sys.exc_info()[2]"
 
3574
        self.check(b, a)
 
3575
 
 
3576
    def test_3(self):
 
3577
        b = "sys.exc_type # Foo"
 
3578
        a = "sys.exc_info()[0] # Foo"
 
3579
        self.check(b, a)
 
3580
 
 
3581
    def test_4(self):
 
3582
        b = "sys.  exc_type"
 
3583
        a = "sys.  exc_info()[0]"
 
3584
        self.check(b, a)
 
3585
 
 
3586
    def test_5(self):
 
3587
        b = "sys  .exc_type"
 
3588
        a = "sys  .exc_info()[0]"
 
3589
        self.check(b, a)
 
3590
 
 
3591
 
 
3592
class Test_paren(FixerTestCase):
 
3593
    fixer = "paren"
 
3594
 
 
3595
    def test_0(self):
 
3596
        b = """[i for i in 1, 2 ]"""
 
3597
        a = """[i for i in (1, 2) ]"""
 
3598
        self.check(b, a)
 
3599
 
 
3600
    def test_1(self):
 
3601
        b = """[i for i in 1, 2, ]"""
 
3602
        a = """[i for i in (1, 2,) ]"""
 
3603
        self.check(b, a)
 
3604
 
 
3605
    def test_2(self):
 
3606
        b = """[i for i  in     1, 2 ]"""
 
3607
        a = """[i for i  in     (1, 2) ]"""
 
3608
        self.check(b, a)
 
3609
 
 
3610
    def test_3(self):
 
3611
        b = """[i for i in 1, 2 if i]"""
 
3612
        a = """[i for i in (1, 2) if i]"""
 
3613
        self.check(b, a)
 
3614
 
 
3615
    def test_4(self):
 
3616
        b = """[i for i in 1,    2    ]"""
 
3617
        a = """[i for i in (1,    2)    ]"""
 
3618
        self.check(b, a)
 
3619
 
 
3620
    def test_5(self):
 
3621
        b = """(i for i in 1, 2)"""
 
3622
        a = """(i for i in (1, 2))"""
 
3623
        self.check(b, a)
 
3624
 
 
3625
    def test_6(self):
 
3626
        b = """(i for i in 1   ,2   if i)"""
 
3627
        a = """(i for i in (1   ,2)   if i)"""
 
3628
        self.check(b, a)
 
3629
 
 
3630
    def test_unchanged_0(self):
 
3631
        s = """[i for i in (1, 2)]"""
 
3632
        self.unchanged(s)
 
3633
 
 
3634
    def test_unchanged_1(self):
 
3635
        s = """[i for i in foo()]"""
 
3636
        self.unchanged(s)
 
3637
 
 
3638
    def test_unchanged_2(self):
 
3639
        s = """[i for i in (1, 2) if nothing]"""
 
3640
        self.unchanged(s)
 
3641
 
 
3642
    def test_unchanged_3(self):
 
3643
        s = """(i for i in (1, 2))"""
 
3644
        self.unchanged(s)
 
3645
 
 
3646
    def test_unchanged_4(self):
 
3647
        s = """[i for i in m]"""
 
3648
        self.unchanged(s)
 
3649
 
 
3650
class Test_metaclass(FixerTestCase):
 
3651
 
 
3652
    fixer = 'metaclass'
 
3653
 
 
3654
    def test_unchanged(self):
 
3655
        self.unchanged("class X(): pass")
 
3656
        self.unchanged("class X(object): pass")
 
3657
        self.unchanged("class X(object1, object2): pass")
 
3658
        self.unchanged("class X(object1, object2, object3): pass")
 
3659
        self.unchanged("class X(metaclass=Meta): pass")
 
3660
        self.unchanged("class X(b, arg=23, metclass=Meta): pass")
 
3661
        self.unchanged("class X(b, arg=23, metaclass=Meta, other=42): pass")
 
3662
 
 
3663
        s = """
 
3664
        class X:
 
3665
            def __metaclass__(self): pass
 
3666
        """
 
3667
        self.unchanged(s)
 
3668
 
 
3669
        s = """
 
3670
        class X:
 
3671
            a[23] = 74
 
3672
        """
 
3673
        self.unchanged(s)
 
3674
 
 
3675
    def test_comments(self):
 
3676
        b = """
 
3677
        class X:
 
3678
            # hi
 
3679
            __metaclass__ = AppleMeta
 
3680
        """
 
3681
        a = """
 
3682
        class X(metaclass=AppleMeta):
 
3683
            # hi
 
3684
            pass
 
3685
        """
 
3686
        self.check(b, a)
 
3687
 
 
3688
        b = """
 
3689
        class X:
 
3690
            __metaclass__ = Meta
 
3691
            # Bedtime!
 
3692
        """
 
3693
        a = """
 
3694
        class X(metaclass=Meta):
 
3695
            pass
 
3696
            # Bedtime!
 
3697
        """
 
3698
        self.check(b, a)
 
3699
 
 
3700
    def test_meta(self):
 
3701
        # no-parent class, odd body
 
3702
        b = """
 
3703
        class X():
 
3704
            __metaclass__ = Q
 
3705
            pass
 
3706
        """
 
3707
        a = """
 
3708
        class X(metaclass=Q):
 
3709
            pass
 
3710
        """
 
3711
        self.check(b, a)
 
3712
 
 
3713
        # one parent class, no body
 
3714
        b = """class X(object): __metaclass__ = Q"""
 
3715
        a = """class X(object, metaclass=Q): pass"""
 
3716
        self.check(b, a)
 
3717
 
 
3718
 
 
3719
        # one parent, simple body
 
3720
        b = """
 
3721
        class X(object):
 
3722
            __metaclass__ = Meta
 
3723
            bar = 7
 
3724
        """
 
3725
        a = """
 
3726
        class X(object, metaclass=Meta):
 
3727
            bar = 7
 
3728
        """
 
3729
        self.check(b, a)
 
3730
 
 
3731
        b = """
 
3732
        class X:
 
3733
            __metaclass__ = Meta; x = 4; g = 23
 
3734
        """
 
3735
        a = """
 
3736
        class X(metaclass=Meta):
 
3737
            x = 4; g = 23
 
3738
        """
 
3739
        self.check(b, a)
 
3740
 
 
3741
        # one parent, simple body, __metaclass__ last
 
3742
        b = """
 
3743
        class X(object):
 
3744
            bar = 7
 
3745
            __metaclass__ = Meta
 
3746
        """
 
3747
        a = """
 
3748
        class X(object, metaclass=Meta):
 
3749
            bar = 7
 
3750
        """
 
3751
        self.check(b, a)
 
3752
 
 
3753
        # redefining __metaclass__
 
3754
        b = """
 
3755
        class X():
 
3756
            __metaclass__ = A
 
3757
            __metaclass__ = B
 
3758
            bar = 7
 
3759
        """
 
3760
        a = """
 
3761
        class X(metaclass=B):
 
3762
            bar = 7
 
3763
        """
 
3764
        self.check(b, a)
 
3765
 
 
3766
        # multiple inheritance, simple body
 
3767
        b = """
 
3768
        class X(clsA, clsB):
 
3769
            __metaclass__ = Meta
 
3770
            bar = 7
 
3771
        """
 
3772
        a = """
 
3773
        class X(clsA, clsB, metaclass=Meta):
 
3774
            bar = 7
 
3775
        """
 
3776
        self.check(b, a)
 
3777
 
 
3778
        # keywords in the class statement
 
3779
        b = """class m(a, arg=23): __metaclass__ = Meta"""
 
3780
        a = """class m(a, arg=23, metaclass=Meta): pass"""
 
3781
        self.check(b, a)
 
3782
 
 
3783
        b = """
 
3784
        class X(expression(2 + 4)):
 
3785
            __metaclass__ = Meta
 
3786
        """
 
3787
        a = """
 
3788
        class X(expression(2 + 4), metaclass=Meta):
 
3789
            pass
 
3790
        """
 
3791
        self.check(b, a)
 
3792
 
 
3793
        b = """
 
3794
        class X(expression(2 + 4), x**4):
 
3795
            __metaclass__ = Meta
 
3796
        """
 
3797
        a = """
 
3798
        class X(expression(2 + 4), x**4, metaclass=Meta):
 
3799
            pass
 
3800
        """
 
3801
        self.check(b, a)
 
3802
 
 
3803
 
 
3804
class Test_getcwdu(FixerTestCase):
 
3805
 
 
3806
    fixer = 'getcwdu'
 
3807
 
 
3808
    def test_basic(self):
 
3809
        b = """os.getcwdu"""
 
3810
        a = """os.getcwd"""
 
3811
        self.check(b, a)
 
3812
 
 
3813
        b = """os.getcwdu()"""
 
3814
        a = """os.getcwd()"""
 
3815
        self.check(b, a)
 
3816
 
 
3817
        b = """meth = os.getcwdu"""
 
3818
        a = """meth = os.getcwd"""
 
3819
        self.check(b, a)
 
3820
 
 
3821
        b = """os.getcwdu(args)"""
 
3822
        a = """os.getcwd(args)"""
 
3823
        self.check(b, a)
 
3824
 
 
3825
    def test_comment(self):
 
3826
        b = """os.getcwdu() # Foo"""
 
3827
        a = """os.getcwd() # Foo"""
 
3828
        self.check(b, a)
 
3829
 
 
3830
    def test_unchanged(self):
 
3831
        s = """os.getcwd()"""
 
3832
        self.unchanged(s)
 
3833
 
 
3834
        s = """getcwdu()"""
 
3835
        self.unchanged(s)
 
3836
 
 
3837
        s = """os.getcwdb()"""
 
3838
        self.unchanged(s)
 
3839
 
 
3840
    def test_indentation(self):
 
3841
        b = """
 
3842
            if 1:
 
3843
                os.getcwdu()
 
3844
            """
 
3845
        a = """
 
3846
            if 1:
 
3847
                os.getcwd()
 
3848
            """
 
3849
        self.check(b, a)
 
3850
 
 
3851
    def test_multilation(self):
 
3852
        b = """os .getcwdu()"""
 
3853
        a = """os .getcwd()"""
 
3854
        self.check(b, a)
 
3855
 
 
3856
        b = """os.  getcwdu"""
 
3857
        a = """os.  getcwd"""
 
3858
        self.check(b, a)
 
3859
 
 
3860
        b = """os.getcwdu (  )"""
 
3861
        a = """os.getcwd (  )"""
 
3862
        self.check(b, a)
 
3863
 
 
3864
 
 
3865
if __name__ == "__main__":
 
3866
    import __main__
 
3867
    support.run_all_tests(__main__)