~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« 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-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

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