rope / ropetest / refactor / renametest.py

The trunk branch has multiple heads

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
import unittest

import rope.base.codeanalyze
import rope.refactor.occurrences
from rope.refactor import rename
from rope.refactor.rename import Rename
from ropetest import testutils


class RenameRefactoringTest(unittest.TestCase):

    def setUp(self):
        super(RenameRefactoringTest, self).setUp()
        self.project = testutils.sample_project()
        self.pycore = self.project.get_pycore()

    def tearDown(self):
        testutils.remove_project(self.project)
        super(RenameRefactoringTest, self).tearDown()

    def _local_rename(self, source_code, offset, new_name):
        testmod = testutils.create_module(self.project, 'testmod')
        testmod.write(source_code)
        changes = Rename(self.project, testmod, offset).\
            get_changes(new_name, resources=[testmod])
        self.project.do(changes)
        return testmod.read()

    def _rename(self, resource, offset, new_name, **kwds):
        changes = Rename(self.project, resource, offset).\
                  get_changes(new_name, **kwds)
        self.project.do(changes)

    def test_simple_global_variable_renaming(self):
        refactored = self._local_rename('a_var = 20\n', 2, 'new_var')
        self.assertEquals('new_var = 20\n', refactored)

    def test_variable_renaming_only_in_its_scope(self):
        refactored = self._local_rename(
            'a_var = 20\ndef a_func():\n    a_var = 10\n', 32, 'new_var')
        self.assertEquals('a_var = 20\ndef a_func():\n    new_var = 10\n',
                          refactored)

    def test_not_renaming_dot_name(self):
        refactored = self._local_rename(
            "replace = True\n'aaa'.replace('a', 'b')\n", 1, 'new_var')
        self.assertEquals("new_var = True\n'aaa'.replace('a', 'b')\n",
                          refactored)

    def test_renaming_multiple_names_in_the_same_line(self):
        refactored = self._local_rename(
            'a_var = 10\na_var = 10 + a_var / 2\n', 2, 'new_var')
        self.assertEquals('new_var = 10\nnew_var = 10 + new_var / 2\n',
                          refactored)

    def test_renaming_names_when_getting_some_attribute(self):
        refactored = self._local_rename(
            "a_var = 'a b c'\na_var.split('\\n')\n", 2, 'new_var')
        self.assertEquals("new_var = 'a b c'\nnew_var.split('\\n')\n",
                          refactored)

    def test_renaming_names_when_getting_some_attribute2(self):
        refactored = self._local_rename(
            "a_var = 'a b c'\na_var.split('\\n')\n", 20, 'new_var')
        self.assertEquals("new_var = 'a b c'\nnew_var.split('\\n')\n",
                          refactored)

    def test_renaming_function_parameters1(self):
        refactored = self._local_rename(
            "def f(a_param):\n    print(a_param)\n", 8, 'new_param')
        self.assertEquals("def f(new_param):\n    print(new_param)\n",
                          refactored)

    def test_renaming_function_parameters2(self):
        refactored = self._local_rename(
            "def f(a_param):\n    print(a_param)\n", 30, 'new_param')
        self.assertEquals("def f(new_param):\n    print(new_param)\n",
                          refactored)

    def test_renaming_occurrences_inside_functions(self):
        code = 'def a_func(p1):\n    a = p1\na_func(1)\n'
        refactored = self._local_rename(code, code.index('p1') + 1, 'new_param')
        self.assertEquals(
            'def a_func(new_param):\n    a = new_param\na_func(1)\n', 
            refactored)

    def test_renaming_arguments_for_normal_args_changing_calls(self):
        code = 'def a_func(p1=None, p2=None):\n    pass\na_func(p2=1)\n'
        refactored = self._local_rename(code, code.index('p2') + 1, 'p3')
        self.assertEquals(
            'def a_func(p1=None, p3=None):\n    pass\na_func(p3=1)\n',
            refactored)

    def test_renaming_function_parameters_of_class_init(self):
        code = 'class A(object):\n    def __init__(self, a_param):\n        pass\n' \
               'a_var = A(a_param=1)\n'
        refactored = self._local_rename(code, code.index('a_param') + 1, 'new_param')
        expected = 'class A(object):\n    def __init__(self, new_param):\n        pass\n' \
                   'a_var = A(new_param=1)\n'
        self.assertEquals(expected, refactored)

    def test_renaming_functions_parameters_and_occurances_in_other_modules(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        mod2 = testutils.create_module(self.project, 'mod2')
        mod1.write('def a_func(a_param):\n    print(a_param)\n')
        mod2.write('from mod1 import a_func\na_func(a_param=10)\n')
        self._rename(mod1, mod1.read().index('a_param') + 1, 'new_param')
        self.assertEquals('def a_func(new_param):\n    print(new_param)\n',
                          mod1.read())
        self.assertEquals('from mod1 import a_func\na_func(new_param=10)\n',
                          mod2.read())

    def test_renaming_with_backslash_continued_names(self):
        refactored = self._local_rename(
            "replace = True\n'ali'.\\\nreplace\n", 2, 'is_replace')
        self.assertEquals("is_replace = True\n'ali'.\\\nreplace\n",
                          refactored)

    def test_not_renaming_string_contents(self):
        refactored = self._local_rename("a_var = 20\na_string='a_var'\n",
                                        2, 'new_var')
        self.assertEquals("new_var = 20\na_string='a_var'\n",
                          refactored)

    def test_not_renaming_comment_contents(self):
        refactored = self._local_rename("a_var = 20\n# a_var\n",
                                        2, 'new_var')
        self.assertEquals("new_var = 20\n# a_var\n", refactored)

    def test_renaming_all_occurances_in_containing_scope(self):
        code = 'if True:\n    a_var = 1\nelse:\n    a_var = 20\n'
        refactored = self._local_rename(code, 16, 'new_var')
        self.assertEquals(
            'if True:\n    new_var = 1\nelse:\n    new_var = 20\n', refactored)

    def test_renaming_a_variable_with_arguement_name(self):
        code = 'a_var = 10\ndef a_func(a_var):\n    print(a_var)\n'
        refactored = self._local_rename(code, 1, 'new_var')
        self.assertEquals(
            'new_var = 10\ndef a_func(a_var):\n    print(a_var)\n', refactored)

    def test_renaming_an_arguement_with_variable_name(self):
        code = 'a_var = 10\ndef a_func(a_var):\n    print(a_var)\n'
        refactored = self._local_rename(code, len(code) - 3, 'new_var')
        self.assertEquals(
            'a_var = 10\ndef a_func(new_var):\n    print(new_var)\n',
            refactored)

    def test_renaming_function_with_local_variable_name(self):
        code = 'def a_func():\n    a_func=20\na_func()'
        refactored = self._local_rename(code, len(code) - 3, 'new_func')
        self.assertEquals('def new_func():\n    a_func=20\nnew_func()',
                          refactored)

    def test_renaming_functions(self):
        code = 'def a_func():\n    pass\na_func()\n'
        refactored = self._local_rename(code, len(code) - 5, 'new_func')
        self.assertEquals('def new_func():\n    pass\nnew_func()\n',
                          refactored)

    def test_renaming_functions_across_modules(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write('def a_func():\n    pass\na_func()\n')
        mod2 = testutils.create_module(self.project, 'mod2')
        mod2.write('import mod1\nmod1.a_func()\n')
        self._rename(mod1, len(mod1.read()) - 5, 'new_func')
        self.assertEquals('def new_func():\n    pass\nnew_func()\n',
                          mod1.read())
        self.assertEquals('import mod1\nmod1.new_func()\n', mod2.read())

    def test_renaming_functions_across_modules_from_import(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write('def a_func():\n    pass\na_func()\n')
        mod2 = testutils.create_module(self.project, 'mod2')
        mod2.write('from mod1 import a_func\na_func()\n')
        self._rename(mod1, len(mod1.read()) - 5, 'new_func')
        self.assertEquals('def new_func():\n    pass\nnew_func()\n',
                          mod1.read())
        self.assertEquals('from mod1 import new_func\nnew_func()\n',
                          mod2.read())

    def test_renaming_functions_from_another_module(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write('def a_func():\n    pass\na_func()\n')
        mod2 = testutils.create_module(self.project, 'mod2')
        mod2.write('import mod1\nmod1.a_func()\n')
        self._rename(mod2, len(mod2.read()) - 5, 'new_func')
        self.assertEquals('def new_func():\n    pass\nnew_func()\n',
                          mod1.read())
        self.assertEquals('import mod1\nmod1.new_func()\n', mod2.read())

    def test_applying_all_changes_together(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write('import mod2\nmod2.a_func()\n')
        mod2 = testutils.create_module(self.project, 'mod2')
        mod2.write('def a_func():\n    pass\na_func()\n')
        self._rename(mod2, len(mod2.read()) - 5, 'new_func')
        self.assertEquals('import mod2\nmod2.new_func()\n', mod1.read())
        self.assertEquals('def new_func():\n    pass\nnew_func()\n',
                          mod2.read())

    def test_renaming_modules(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write('def a_func():\n    pass\n')
        mod2 = testutils.create_module(self.project, 'mod2')
        mod2.write('from mod1 import a_func\n')
        self._rename(mod2, mod2.read().index('mod1') + 1, 'newmod')
        self.assertTrue(not mod1.exists() and
                        self.pycore.find_module('newmod') is not None)
        self.assertEquals('from newmod import a_func\n', mod2.read())

    def test_renaming_packages(self):
        pkg = testutils.create_package(self.project, 'pkg')
        mod1 = testutils.create_module(self.project, 'mod1', pkg)
        mod1.write('def a_func():\n    pass\n')
        mod2 = testutils.create_module(self.project, 'mod2', pkg)
        mod2.write('from pkg.mod1 import a_func\n')
        self._rename(mod2, 6, 'newpkg')
        self.assertTrue(self.pycore.find_module('newpkg.mod1') is not None)
        new_mod2 = self.pycore.find_module('newpkg.mod2')
        self.assertEquals('from newpkg.mod1 import a_func\n', new_mod2.read())

    def test_module_dependencies(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write('class AClass(object):\n    pass\n')
        mod2 = testutils.create_module(self.project, 'mod2')
        mod2.write('import mod1\na_var = mod1.AClass()\n')
        self.pycore.resource_to_pyobject(mod2).get_attributes()['mod1']
        mod1.write('def AClass():\n    return 0\n')

        self._rename(mod2, len(mod2.read()) - 3, 'a_func')
        self.assertEquals('def a_func():\n    return 0\n', mod1.read())
        self.assertEquals('import mod1\na_var = mod1.a_func()\n', mod2.read())

    def test_renaming_class_attributes(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write('class AClass(object):\n    def __init__(self):\n'
                   '        self.an_attr = 10\n')
        mod2 = testutils.create_module(self.project, 'mod2')
        mod2.write('import mod1\na_var = mod1.AClass()\n'
                   'another_var = a_var.an_attr')

        self._rename(mod1, mod1.read().index('an_attr'), 'attr')
        self.assertEquals('class AClass(object):\n    def __init__(self):\n'
                          '        self.attr = 10\n', mod1.read())
        self.assertEquals(
            'import mod1\na_var = mod1.AClass()\nanother_var = a_var.attr',
            mod2.read())

    def test_renaming_class_attributes2(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write('class AClass(object):\n    def __init__(self):\n'
                   '        an_attr = 10\n        self.an_attr = 10\n')
        mod2 = testutils.create_module(self.project, 'mod2')
        mod2.write('import mod1\na_var = mod1.AClass()\n'
                   'another_var = a_var.an_attr')

        self._rename(mod1, mod1.read().rindex('an_attr'), 'attr')
        self.assertEquals(
            'class AClass(object):\n    def __init__(self):\n'
            '        an_attr = 10\n        self.attr = 10\n', mod1.read())
        self.assertEquals(
            'import mod1\na_var = mod1.AClass()\nanother_var = a_var.attr',
            mod2.read())

    def test_renaming_methods_in_subclasses(self):
        mod = testutils.create_module(self.project, 'mod1')
        mod.write('class A(object):\n    def a_method(self):\n        pass\n'
                  'class B(A):\n    def a_method(self):\n        pass\n')

        self._rename(mod, mod.read().rindex('a_method') + 1, 'new_method',
                     in_hierarchy=True)
        self.assertEquals(
            'class A(object):\n    def new_method(self):\n        pass\n'
            'class B(A):\n    def new_method(self):\n        pass\n', mod.read())

    def test_renaming_methods_in_sibling_classes(self):
        mod = testutils.create_module(self.project, 'mod1')
        mod.write('class A(object):\n    def a_method(self):\n        pass\n'
                  'class B(A):\n    def a_method(self):\n        pass\n'
                  'class C(A):\n    def a_method(self):\n        pass\n')

        self._rename(mod, mod.read().rindex('a_method') + 1, 'new_method',
                     in_hierarchy=True)
        self.assertEquals(
            'class A(object):\n    def new_method(self):\n        pass\n'
            'class B(A):\n    def new_method(self):\n        pass\n'
            'class C(A):\n    def new_method(self):\n        pass\n', mod.read())

    def test_not_renaming_methods_in_hierarchies(self):
        mod = testutils.create_module(self.project, 'mod1')
        mod.write('class A(object):\n    def a_method(self):\n        pass\n'
                  'class B(A):\n    def a_method(self):\n        pass\n')

        self._rename(mod, mod.read().rindex('a_method') + 1, 'new_method',
                     in_hierarchy=False)
        self.assertEquals(
            'class A(object):\n    def a_method(self):\n        pass\n'
            'class B(A):\n    def new_method(self):\n        pass\n', mod.read())

    def test_undoing_refactorings(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write('def a_func():\n    pass\na_func()\n')
        self._rename(mod1, len(mod1.read()) - 5, 'new_func')
        self.project.history.undo()
        self.assertEquals('def a_func():\n    pass\na_func()\n', mod1.read())

    def test_undoing_renaming_modules(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write('def a_func():\n    pass\n')
        mod2 = testutils.create_module(self.project, 'mod2')
        mod2.write('from mod1 import a_func\n')
        self._rename(mod2, 6, 'newmod')
        self.project.history.undo()
        self.assertEquals('mod1.py', mod1.path)
        self.assertEquals('from mod1 import a_func\n', mod2.read())

    def test_rename_in_module_renaming_one_letter_names_for_expressions(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write('a = 10\nprint(1+a)\n')
        pymod = self.pycore.get_module('mod1')
        old_pyname = pymod['a']
        finder = rope.refactor.occurrences.FilteredFinder(
            self.pycore, 'a', [old_pyname])
        refactored = rename.rename_in_module(
            finder, 'new_var', pymodule=pymod, replace_primary=True)
        self.assertEquals('new_var = 10\nprint(1+new_var)\n', refactored)

    def test_renaming_for_loop_variable(self):
        code = 'for var in range(10):\n    print(var)\n'
        refactored = self._local_rename(code, code.find('var') + 1, 'new_var')
        self.assertEquals('for new_var in range(10):\n    print(new_var)\n',
                          refactored)

    def test_renaming_parameters(self):
        code = 'def a_func(param):\n    print(param)\na_func(param=hey)\n'
        refactored = self._local_rename(code, code.find('param') + 1,
                                        'new_param')
        self.assertEquals('def a_func(new_param):\n    print(new_param)\n'
                          'a_func(new_param=hey)\n', refactored)

    def test_renaming_parameters_not_renaming_others(self):
        code = 'def a_func(param):\n    print(param)\nparam=10\na_func(param)\n'
        refactored = self._local_rename(code, code.find('param') + 1, 'new_param')
        self.assertEquals('def a_func(new_param):\n    print(new_param)\n'
                          'param=10\na_func(param)\n', refactored)

    def test_renaming_parameters_not_renaming_others2(self):
        code = 'def a_func(param):\n    print(param)\nparam=10\na_func(param=param)'
        refactored = self._local_rename(code, code.find('param') + 1, 'new_param')
        self.assertEquals('def a_func(new_param):\n    print(new_param)\n'
                          'param=10\na_func(new_param=param)', refactored)

    def test_renaming_parameters_with_multiple_params(self):
        code = 'def a_func(param1, param2):\n    print(param1)\n'\
               'a_func(param1=1, param2=2)\n'
        refactored = self._local_rename(code, code.find('param1') + 1, 'new_param')
        self.assertEquals(
            'def a_func(new_param, param2):\n    print(new_param)\n'
            'a_func(new_param=1, param2=2)\n', refactored)

    def test_renaming_parameters_with_multiple_params2(self):
        code = 'def a_func(param1, param2):\n    print(param1)\n' \
               'a_func(param1=1, param2=2)\n'
        refactored = self._local_rename(code, code.rfind('param2') + 1,
                                        'new_param')
        self.assertEquals('def a_func(param1, new_param):\n    print(param1)\n'
                          'a_func(param1=1, new_param=2)\n', refactored)

    def test_renaming_parameters_on_calls(self):
        code = 'def a_func(param):\n    print(param)\na_func(param = hey)\n'
        refactored = self._local_rename(code, code.rfind('param') + 1,
                                        'new_param')
        self.assertEquals('def a_func(new_param):\n    print(new_param)\n'
                          'a_func(new_param = hey)\n', refactored)

    def test_renaming_parameters_spaces_before_call(self):
        code = 'def a_func(param):\n    print(param)\na_func  (param=hey)\n'
        refactored = self._local_rename(code, code.rfind('param') + 1,
                                        'new_param')
        self.assertEquals('def a_func(new_param):\n    print(new_param)\n'
                          'a_func  (new_param=hey)\n', refactored)

    def test_renaming_parameter_like_objects_after_keywords(self):
        code = 'def a_func(param):\n    print(param)\ndict(param=hey)\n'
        refactored = self._local_rename(code, code.find('param') + 1, 'new_param')
        self.assertEquals('def a_func(new_param):\n    print(new_param)\n'
                          'dict(param=hey)\n', refactored)

    def test_renaming_variables_in_init_dot_pys(self):
        pkg = testutils.create_package(self.project, 'pkg')
        init_dot_py = pkg.get_child('__init__.py')
        init_dot_py.write('a_var = 10\n')
        mod = testutils.create_module(self.project, 'mod')
        mod.write('import pkg\nprint(pkg.a_var)\n')
        self._rename(mod, mod.read().index('a_var') + 1, 'new_var')
        self.assertEquals('new_var = 10\n', init_dot_py.read())
        self.assertEquals('import pkg\nprint(pkg.new_var)\n', mod.read())

    def test_renaming_variables_in_init_dot_pys2(self):
        pkg = testutils.create_package(self.project, 'pkg')
        init_dot_py = pkg.get_child('__init__.py')
        init_dot_py.write('a_var = 10\n')
        mod = testutils.create_module(self.project, 'mod')
        mod.write('import pkg\nprint(pkg.a_var)\n')
        self._rename(init_dot_py,
                     init_dot_py.read().index('a_var') + 1, 'new_var')
        self.assertEquals('new_var = 10\n', init_dot_py.read())
        self.assertEquals('import pkg\nprint(pkg.new_var)\n', mod.read())

    def test_renaming_variables_in_init_dot_pys3(self):
        pkg = testutils.create_package(self.project, 'pkg')
        init_dot_py = pkg.get_child('__init__.py')
        init_dot_py.write('a_var = 10\n')
        mod = testutils.create_module(self.project, 'mod')
        mod.write('import pkg\nprint(pkg.a_var)\n')
        self._rename(mod, mod.read().index('a_var') + 1, 'new_var')
        self.assertEquals('new_var = 10\n', init_dot_py.read())
        self.assertEquals('import pkg\nprint(pkg.new_var)\n', mod.read())

    def test_renaming_resources_using_rename_module_refactoring(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        mod2 = testutils.create_module(self.project, 'mod2')
        mod1.write('a_var = 1')
        mod2.write('import mod1\nmy_var = mod1.a_var\n')
        renamer = rename.Rename(self.project, mod1)
        renamer.get_changes('newmod').do()
        self.assertEquals('import newmod\nmy_var = newmod.a_var\n', mod2.read())

    def test_renaming_resources_using_rename_module_refactoring_for_packages(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        pkg = testutils.create_package(self.project, 'pkg')
        mod1.write('import pkg\nmy_pkg = pkg')
        renamer = rename.Rename(self.project, pkg)
        renamer.get_changes('newpkg').do()
        self.assertEquals('import newpkg\nmy_pkg = newpkg', mod1.read())

    def test_renaming_resources_using_rename_module_refactoring_for_init_dot_py(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        pkg = testutils.create_package(self.project, 'pkg')
        mod1.write('import pkg\nmy_pkg = pkg')
        renamer = rename.Rename(self.project, pkg.get_child('__init__.py'))
        renamer.get_changes('newpkg').do()
        self.assertEquals('import newpkg\nmy_pkg = newpkg', mod1.read())

    def test_renaming_global_variables(self):
        code = 'a_var = 1\ndef a_func():\n    global a_var\n    var = a_var\n'
        refactored = self._local_rename(code, code.index('a_var'), 'new_var')
        self.assertEquals(
            'new_var = 1\ndef a_func():\n    global new_var\n    var = new_var\n',
            refactored)

    def test_renaming_global_variables2(self):
        code = 'a_var = 1\ndef a_func():\n    global a_var\n    var = a_var\n'
        refactored = self._local_rename(code, code.rindex('a_var'), 'new_var')
        self.assertEquals(
            'new_var = 1\ndef a_func():\n    global new_var\n    var = new_var\n',
            refactored)

    def test_renaming_when_unsure(self):
        code = 'class C(object):\n    def a_func(self):\n        pass\n' \
               'def f(arg):\n    arg.a_func()\n'
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write(code)
        self._rename(mod1, code.index('a_func'),
                     'new_func', unsure=self._true)
        self.assertEquals(
            'class C(object):\n    def new_func(self):\n        pass\n' \
            'def f(arg):\n    arg.new_func()\n',
            mod1.read())

    def _true(self, *args):
        return True

    def test_renaming_when_unsure_with_confirmation(self):
        def confirm(occurrence):
            return False
        code = 'class C(object):\n    def a_func(self):\n        pass\n' \
               'def f(arg):\n    arg.a_func()\n'
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write(code)
        self._rename(mod1, code.index('a_func'), 'new_func', unsure=confirm)
        self.assertEquals(
            'class C(object):\n    def new_func(self):\n        pass\n' \
            'def f(arg):\n    arg.a_func()\n', mod1.read())

    def test_renaming_when_unsure_not_renaming_knowns(self):
        code = 'class C1(object):\n    def a_func(self):\n        pass\n' \
               'class C2(object):\n    def a_func(self):\n        pass\n' \
               'c1 = C1()\nc1.a_func()\nc2 = C2()\nc2.a_func()\n'
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write(code)
        self._rename(mod1, code.index('a_func'), 'new_func', unsure=self._true)
        self.assertEquals(
            'class C1(object):\n    def new_func(self):\n        pass\n' \
            'class C2(object):\n    def a_func(self):\n        pass\n' \
            'c1 = C1()\nc1.new_func()\nc2 = C2()\nc2.a_func()\n',
            mod1.read())

    def test_renaming_in_strings_and_comments(self):
        code = 'a_var = 1\n# a_var\n'
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write(code)
        self._rename(mod1, code.index('a_var'), 'new_var', docs=True)
        self.assertEquals('new_var = 1\n# new_var\n', mod1.read())

    def test_not_renaming_in_strings_and_comments_where_not_visible(self):
        code = 'def f():\n    a_var = 1\n# a_var\n'
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write(code)
        self._rename(mod1, code.index('a_var'), 'new_var', docs=True)
        self.assertEquals('def f():\n    new_var = 1\n# a_var\n', mod1.read())

    def test_not_renaming_all_text_occurrences_in_strings_and_comments(self):
        code = 'a_var = 1\n# a_vard _a_var\n'
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write(code)
        self._rename(mod1, code.index('a_var'), 'new_var', docs=True)
        self.assertEquals('new_var = 1\n# a_vard _a_var\n', mod1.read())

    def test_renaming_occurrences_in_overwritten_scopes(self):
        refactored = self._local_rename(
            'a_var = 20\ndef f():\n    print(a_var)\n'
            'def f():\n    print(a_var)\n', 2, 'new_var')
        self.assertEquals('new_var = 20\ndef f():\n    print(new_var)\n'
                          'def f():\n    print(new_var)\n', refactored)

    def test_renaming_occurrences_in_overwritten_scopes2(self):
        code = 'def f():\n    a_var = 1\n    print(a_var)\n' \
               'def f():\n    a_var = 1\n    print(a_var)\n'
        refactored = self._local_rename(code, code.index('a_var') + 1, 'new_var')
        self.assertEquals(code.replace('a_var', 'new_var', 2), refactored)

    def test_dos_line_ending_and_renaming(self):
        code = '\r\na = 1\r\n\r\nprint(2 + a + 2)\r\n'
        offset = code.replace('\r\n', '\n').rindex('a')
        refactored = self._local_rename(code, offset, 'b')
        self.assertEquals('\nb = 1\n\nprint(2 + b + 2)\n',
                          refactored.replace('\r\n', '\n'))

    def test_multi_byte_strs_and_renaming(self):
        s = u'{LATIN SMALL LETTER I WITH DIAERESIS}' * 4
        code = u'# -*- coding: utf-8 -*-\n# ' + s + \
                '\na = 1\nprint(2 + a + 2)\n'
        refactored = self._local_rename(code, code.rindex('a'), 'b')
        self.assertEquals(u'# -*- coding: utf-8 -*-\n# ' + s +
                          '\nb = 1\nprint(2 + b + 2)\n', refactored)

    def test_resources_parameter(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        mod2 = testutils.create_module(self.project, 'mod2')
        mod1.write('def f():\n    pass\n')
        mod2.write('import mod1\nmod1.f()\n')
        self._rename(mod1, mod1.read().rindex('f'), 'g',
                     resources=[mod1])
        self.assertEquals('def g():\n    pass\n', mod1.read())
        self.assertEquals('import mod1\nmod1.f()\n', mod2.read())

    def test_resources_parameter_not_changing_defining_module(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        mod2 = testutils.create_module(self.project, 'mod2')
        mod1.write('def f():\n    pass\n')
        mod2.write('import mod1\nmod1.f()\n')
        self._rename(mod1, mod1.read().rindex('f'), 'g',
                     resources=[mod2])
        self.assertEquals('def f():\n    pass\n', mod1.read())
        self.assertEquals('import mod1\nmod1.g()\n', mod2.read())


class ChangeOccurrencesTest(unittest.TestCase):

    def setUp(self):
        self.project = testutils.sample_project()
        self.mod = testutils.create_module(self.project, 'mod')

    def tearDown(self):
        testutils.remove_project(self.project)
        super(ChangeOccurrencesTest, self).tearDown()

    def test_simple_case(self):
        self.mod.write('a_var = 1\nprint(a_var)\n')
        changer = rename.ChangeOccurrences(self.project, self.mod,
                                           self.mod.read().index('a_var'))
        changer.get_changes('new_var').do()
        self.assertEquals('new_var = 1\nprint(new_var)\n', self.mod.read())

    def test_only_performing_inside_scopes(self):
        self.mod.write('a_var = 1\nnew_var = 2\ndef f():\n    print(a_var)\n')
        changer = rename.ChangeOccurrences(self.project, self.mod,
                                           self.mod.read().rindex('a_var'))
        changer.get_changes('new_var').do()
        self.assertEquals(
            'a_var = 1\nnew_var = 2\ndef f():\n    print(new_var)\n',
            self.mod.read())

    def test_only_performing_on_calls(self):
        self.mod.write('def f1():\n    pass\ndef f2():\n    pass\n'
                       'g = f1\na = f1()\n')
        changer = rename.ChangeOccurrences(self.project, self.mod,
                                           self.mod.read().rindex('f1'))
        changer.get_changes('f2', only_calls=True).do()
        self.assertEquals(
            'def f1():\n    pass\ndef f2():\n    pass\ng = f1\na = f2()\n',
            self.mod.read())

    def test_only_performing_on_reads(self):
        self.mod.write('a = 1\nb = 2\nprint(a)\n')
        changer = rename.ChangeOccurrences(self.project, self.mod,
                                           self.mod.read().rindex('a'))
        changer.get_changes('b', writes=False).do()
        self.assertEquals('a = 1\nb = 2\nprint(b)\n', self.mod.read())


class ImplicitInterfacesTest(unittest.TestCase):

    def setUp(self):
        super(ImplicitInterfacesTest, self).setUp()
        self.project = testutils.sample_project(validate_objectdb=True)
        self.pycore = self.project.get_pycore()
        self.mod1 = testutils.create_module(self.project, 'mod1')
        self.mod2 = testutils.create_module(self.project, 'mod2')

    def tearDown(self):
        testutils.remove_project(self.project)
        super(ImplicitInterfacesTest, self).tearDown()

    def _rename(self, resource, offset, new_name, **kwds):
        changes = Rename(self.project, resource, offset).\
                  get_changes(new_name, **kwds)
        self.project.do(changes)

    def test_performing_rename_on_parameters(self):
        self.mod1.write('def f(arg):\n    arg.run()\n')
        self.mod2.write('import mod1\n\n\n'
                        'class A(object):\n    def run(self):\n        pass\n'
                        'class B(object):\n    def run(self):\n        pass\n'
                        'mod1.f(A())\nmod1.f(B())\n')
        self.pycore.analyze_module(self.mod2)
        self._rename(self.mod1, self.mod1.read().index('run'), 'newrun')
        self.assertEquals('def f(arg):\n    arg.newrun()\n', self.mod1.read())
        self.assertEquals(
            'import mod1\n\n\n'
            'class A(object):\n    def newrun(self):\n        pass\n'
            'class B(object):\n    def newrun(self):\n        pass\n'
            'mod1.f(A())\nmod1.f(B())\n', self.mod2.read())


def suite():
    result = unittest.TestSuite()
    result.addTests(unittest.makeSuite(RenameRefactoringTest))
    result.addTests(unittest.makeSuite(ChangeOccurrencesTest))
    result.addTests(unittest.makeSuite(ImplicitInterfacesTest))
    return result


if __name__ == '__main__':
    unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.