Source

rope_py3k / ropetest / refactor / __init__.py

  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
import unittest

import rope.base.codeanalyze
import rope.refactor.introduce_parameter
import ropetest.refactor.change_signature_test
import ropetest.refactor.extracttest
import ropetest.refactor.importutilstest
import ropetest.refactor.inlinetest
import ropetest.refactor.movetest
import ropetest.refactor.renametest
from rope.base.exceptions import RefactoringError
from rope.refactor.encapsulate_field import EncapsulateField
from rope.refactor.introduce_factory import IntroduceFactoryRefactoring
from rope.refactor.localtofield import LocalToField
from rope.refactor.method_object import MethodObject
from ropetest import testutils


class MethodObjectTest(unittest.TestCase):

    def setUp(self):
        super(MethodObjectTest, self).setUp()
        self.project = testutils.sample_project()
        self.pycore = self.project.get_pycore()
        self.mod = self.pycore.create_module(self.project.root, 'mod')

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

    def test_empty_method(self):
        code = 'def func():\n    pass\n'
        self.mod.write(code)
        replacer = MethodObject(self.project, self.mod, code.index('func'))
        self.assertEquals(
            'class _New(object):\n\n    def __call__(self):\n        pass\n',
            replacer.get_new_class('_New'))

    def test_trivial_return(self):
        code = 'def func():\n    return 1\n'
        self.mod.write(code)
        replacer = MethodObject(self.project, self.mod, code.index('func'))
        self.assertEquals(
            'class _New(object):\n\n    def __call__(self):\n        return 1\n',
            replacer.get_new_class('_New'))

    def test_multi_line_header(self):
        code = 'def func(\n    ):\n    return 1\n'
        self.mod.write(code)
        replacer = MethodObject(self.project, self.mod, code.index('func'))
        self.assertEquals(
            'class _New(object):\n\n    def __call__(self):\n        return 1\n',
            replacer.get_new_class('_New'))

    def test_a_single_parameter(self):
        code = 'def func(param):\n    return 1\n'
        self.mod.write(code)
        replacer = MethodObject(self.project, self.mod, code.index('func'))
        self.assertEquals(
            'class _New(object):\n\n'
            '    def __init__(self, param):\n        self.param = param\n\n'
            '    def __call__(self):\n        return 1\n',
            replacer.get_new_class('_New'))

    def test_self_parameter(self):
        code = 'def func(self):\n    return 1\n'
        self.mod.write(code)
        replacer = MethodObject(self.project, self.mod, code.index('func'))
        self.assertEquals(
            'class _New(object):\n\n'
            '    def __init__(self, host):\n        self.self = host\n\n'
            '    def __call__(self):\n        return 1\n',
            replacer.get_new_class('_New'))

    def test_simple_using_passed_parameters(self):
        code = 'def func(param):\n    return param\n'
        self.mod.write(code)
        replacer = MethodObject(self.project, self.mod, code.index('func'))
        self.assertEquals(
            'class _New(object):\n\n'
            '    def __init__(self, param):\n        self.param = param\n\n'
            '    def __call__(self):\n        return self.param\n',
            replacer.get_new_class('_New'))

    def test_self_keywords_and_args_parameters(self):
        code = 'def func(arg, *args, **kwds):\n' \
               '    result = arg + args[0] + kwds[arg]\n    return result\n'
        self.mod.write(code)
        replacer = MethodObject(self.project, self.mod, code.index('func'))
        self.assertEquals(
            'class _New(object):\n\n'
            '    def __init__(self, arg, args, kwds):\n'
            '        self.arg = arg\n        self.args = args\n        self.kwds = kwds\n\n'
            '    def __call__(self):\n'
            '        result = self.arg + self.args[0] + self.kwds[self.arg]\n'
            '        return result\n',
            replacer.get_new_class('_New'))

    @testutils.assert_raises(RefactoringError)
    def test_performing_on_not_a_function(self):
        code = 'my_var = 10\n'
        self.mod.write(code)
        replacer = MethodObject(self.project, self.mod, code.index('my_var'))

    def test_changing_the_module(self):
        code = 'def func():\n    return 1\n'
        self.mod.write(code)
        replacer = MethodObject(self.project, self.mod, code.index('func'))
        self.project.do(replacer.get_changes('_New'))
        self.assertEquals(
            'def func():\n    return _New()()\n\n\n'
            'class _New(object):\n\n    def __call__(self):\n        return 1\n',
            self.mod.read())

    def test_changing_the_module_and_class_methods(self):
        code = 'class C(object):\n\n    def a_func(self):\n        return 1\n\n' \
               '    def another_func(self):\n        pass\n'
        self.mod.write(code)
        replacer = MethodObject(self.project, self.mod, code.index('func'))
        self.project.do(replacer.get_changes('_New'))
        self.assertEquals(
            'class C(object):\n\n    def a_func(self):\n        return _New(self)()\n\n'
            '    def another_func(self):\n        pass\n\n\n'
            'class _New(object):\n\n'
            '    def __init__(self, host):\n        self.self = host\n\n'
            '    def __call__(self):\n        return 1\n',
            self.mod.read())


class IntroduceFactoryTest(unittest.TestCase):

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

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

    def _introduce_factory(self, resource, offset, factory_name,
                                   global_factory=False):
        factory_introducer = IntroduceFactoryRefactoring(self.project,
                                                         resource, offset)
        changes = factory_introducer.get_changes(factory_name, global_factory)
        self.project.do(changes)

    def test_adding_the_method(self):
        code = 'class AClass(object):\n    an_attr = 10\n'
        mod = self.pycore.create_module(self.project.root, 'mod')
        mod.write(code)
        expected = 'class AClass(object):\n    an_attr = 10\n\n' \
                   '    @staticmethod\n    def create(*args, **kwds):\n' \
                   '        return AClass(*args, **kwds)\n'
        self._introduce_factory(mod, mod.read().index('AClass') + 1, 'create')
        self.assertEquals(expected, mod.read())

    def test_changing_occurances_in_the_main_module(self):
        code = 'class AClass(object):\n    an_attr = 10\na_var = AClass()'
        mod = self.pycore.create_module(self.project.root, 'mod')
        mod.write(code)
        expected = 'class AClass(object):\n    an_attr = 10\n\n' \
                   '    @staticmethod\n    def create(*args, **kwds):\n' \
                   '        return AClass(*args, **kwds)\n'\
                   'a_var = AClass.create()'
        self._introduce_factory(mod, mod.read().index('AClass') + 1, 'create')
        self.assertEquals(expected, mod.read())

    def test_changing_occurances_with_arguments(self):
        code = 'class AClass(object):\n    def __init__(self, arg):\n        pass\n' \
               'a_var = AClass(10)\n'
        mod = self.pycore.create_module(self.project.root, 'mod')
        mod.write(code)
        expected = 'class AClass(object):\n    def __init__(self, arg):\n        pass\n\n' \
                   '    @staticmethod\n    def create(*args, **kwds):\n' \
                   '        return AClass(*args, **kwds)\n' \
                   'a_var = AClass.create(10)\n'
        self._introduce_factory(mod, mod.read().index('AClass') + 1, 'create')
        self.assertEquals(expected, mod.read())

    def test_changing_occurances_in_other_modules(self):
        mod1 = self.pycore.create_module(self.project.root, 'mod1')
        mod2 = self.pycore.create_module(self.project.root, 'mod2')
        mod1.write('class AClass(object):\n    an_attr = 10\n')
        mod2.write('import mod1\na_var = mod1.AClass()\n')
        self._introduce_factory(mod1, mod1.read().index('AClass') + 1, 'create')
        expected1 = 'class AClass(object):\n    an_attr = 10\n\n' \
                   '    @staticmethod\n    def create(*args, **kwds):\n' \
                   '        return AClass(*args, **kwds)\n'
        expected2 = 'import mod1\na_var = mod1.AClass.create()\n'
        self.assertEquals(expected1, mod1.read())
        self.assertEquals(expected2, mod2.read())

    @testutils.assert_raises(RefactoringError)
    def test_raising_exception_for_non_classes(self):
        mod = self.pycore.create_module(self.project.root, 'mod')
        mod.write('def a_func():\n    pass\n')
        self._introduce_factory(mod, mod.read().index('a_func') + 1, 'create')

    def test_undoing_introduce_factory(self):
        mod1 = self.pycore.create_module(self.project.root, 'mod1')
        mod2 = self.pycore.create_module(self.project.root, 'mod2')
        code1 = 'class AClass(object):\n    an_attr = 10\n'
        mod1.write(code1)
        code2 = 'from mod1 import AClass\na_var = AClass()\n'
        mod2.write(code2)
        self._introduce_factory(mod1, mod1.read().index('AClass') + 1, 'create')
        self.project.history.undo()
        self.assertEquals(code1, mod1.read())
        self.assertEquals(code2, mod2.read())

    def test_using_on_an_occurance_outside_the_main_module(self):
        mod1 = self.pycore.create_module(self.project.root, 'mod1')
        mod2 = self.pycore.create_module(self.project.root, 'mod2')
        mod1.write('class AClass(object):\n    an_attr = 10\n')
        mod2.write('import mod1\na_var = mod1.AClass()\n')
        self._introduce_factory(mod2, mod2.read().index('AClass') + 1, 'create')
        expected1 = 'class AClass(object):\n    an_attr = 10\n\n' \
                   '    @staticmethod\n    def create(*args, **kwds):\n' \
                   '        return AClass(*args, **kwds)\n'
        expected2 = 'import mod1\na_var = mod1.AClass.create()\n'
        self.assertEquals(expected1, mod1.read())
        self.assertEquals(expected2, mod2.read())

    def test_introduce_factory_in_nested_scopes(self):
        code = 'def create_var():\n'\
               '    class AClass(object):\n'\
               '        an_attr = 10\n'\
               '    return AClass()\n'
        mod = self.pycore.create_module(self.project.root, 'mod')
        mod.write(code)
        expected = 'def create_var():\n'\
                   '    class AClass(object):\n'\
                   '        an_attr = 10\n\n'\
                   '        @staticmethod\n        def create(*args, **kwds):\n'\
                   '            return AClass(*args, **kwds)\n'\
                   '    return AClass.create()\n'
        self._introduce_factory(mod, mod.read().index('AClass') + 1, 'create')
        self.assertEquals(expected, mod.read())

    def test_adding_factory_for_global_factories(self):
        code = 'class AClass(object):\n    an_attr = 10\n'
        mod = self.pycore.create_module(self.project.root, 'mod')
        mod.write(code)
        expected = 'class AClass(object):\n    an_attr = 10\n\n' \
                   'def create(*args, **kwds):\n' \
                   '    return AClass(*args, **kwds)\n'
        self._introduce_factory(mod, mod.read().index('AClass') + 1,
                                        'create', global_factory=True)
        self.assertEquals(expected, mod.read())

    @testutils.assert_raises(RefactoringError)
    def test_raising_exception_for_global_factory_for_nested_classes(self):
        code = 'def create_var():\n'\
               '    class AClass(object):\n'\
               '        an_attr = 10\n'\
               '    return AClass()\n'
        mod = self.pycore.create_module(self.project.root, 'mod')
        mod.write(code)
        self._introduce_factory(mod, mod.read().index('AClass') + 1,
                                           'create', global_factory=True)

    def test_changing_occurances_in_the_main_module_for_global_factories(self):
        code = 'class AClass(object):\n    an_attr = 10\na_var = AClass()'
        mod = self.pycore.create_module(self.project.root, 'mod')
        mod.write(code)
        expected = 'class AClass(object):\n    an_attr = 10\n\n' \
                   'def create(*args, **kwds):\n' \
                   '    return AClass(*args, **kwds)\n'\
                   'a_var = create()'
        self._introduce_factory(mod, mod.read().index('AClass') + 1,
                                           'create', global_factory=True)
        self.assertEquals(expected, mod.read())

    def test_changing_occurances_in_other_modules_for_global_factories(self):
        mod1 = self.pycore.create_module(self.project.root, 'mod1')
        mod2 = self.pycore.create_module(self.project.root, 'mod2')
        mod1.write('class AClass(object):\n    an_attr = 10\n')
        mod2.write('import mod1\na_var = mod1.AClass()\n')
        self._introduce_factory(mod1, mod1.read().index('AClass') + 1,
                                           'create', global_factory=True)
        expected1 = 'class AClass(object):\n    an_attr = 10\n\n' \
                    'def create(*args, **kwds):\n' \
                    '    return AClass(*args, **kwds)\n'
        expected2 = 'import mod1\na_var = mod1.create()\n'
        self.assertEquals(expected1, mod1.read())
        self.assertEquals(expected2, mod2.read())

    def test_importing_if_necessary_in_other_modules_for_global_factories(self):
        mod1 = self.pycore.create_module(self.project.root, 'mod1')
        mod2 = self.pycore.create_module(self.project.root, 'mod2')
        mod1.write('class AClass(object):\n    an_attr = 10\n')
        mod2.write('from mod1 import AClass\npair = AClass(), AClass\n')
        self._introduce_factory(mod1, mod1.read().index('AClass') + 1,
                                           'create', global_factory=True)
        expected1 = 'class AClass(object):\n    an_attr = 10\n\n' \
                    'def create(*args, **kwds):\n' \
                    '    return AClass(*args, **kwds)\n'
        expected2 = 'from mod1 import AClass\nimport mod1\npair = mod1.create(), AClass\n'
        self.assertEquals(expected1, mod1.read())
        self.assertEquals(expected2, mod2.read())

    # XXX: Should we replace `a_class` here with `AClass.create` too
    def test_changing_occurances_for_renamed_classes(self):
        code = 'class AClass(object):\n    an_attr = 10\na_class = AClass\na_var = a_class()'
        mod = self.pycore.create_module(self.project.root, 'mod')
        mod.write(code)
        expected = 'class AClass(object):\n    an_attr = 10\n\n' \
                   '    @staticmethod\n    def create(*args, **kwds):\n' \
                   '        return AClass(*args, **kwds)\n' \
                   'a_class = AClass\n' \
                   'a_var = a_class()'
        self._introduce_factory(mod, mod.read().index('a_class') + 1, 'create')
        self.assertEquals(expected, mod.read())

    def test_changing_occurrences_in_the_same_module_with_conflicting_ranges(self):
        mod = self.pycore.create_module(self.project.root, 'mod')
        mod.write('class C(object):\n'
                  '    def create(self):\n        return C()\n')
        self._introduce_factory(mod, mod.read().index('C'), 'create_c', True)
        self.assertTrue(mod.read().startswith(
                        'class C(object):\n    def create(self):\n        return create_c()\n'))

    def _transform_module_to_package(self, resource):
        self.project.do(rope.refactor.ModuleToPackage(
                        self.project, resource).get_changes())

    def test_transform_module_to_package(self):
        mod1 = self.pycore.create_module(self.project.root, 'mod1')
        mod1.write('import mod2\nfrom mod2 import AClass\n')
        mod2 = self.pycore.create_module(self.project.root, 'mod2')
        mod2.write('class AClass(object):\n    pass\n')
        self._transform_module_to_package(mod2)
        mod2 = self.project.get_resource('mod2')
        root_folder = self.project.root
        self.assertFalse(root_folder.has_child('mod2.py'))
        self.assertEquals('class AClass(object):\n    pass\n', root_folder.get_child('mod2').
                          get_child('__init__.py').read())

    def test_transform_module_to_package_undoing(self):
        pkg = self.pycore.create_package(self.project.root, 'pkg')
        mod = self.pycore.create_module(pkg, 'mod')
        self._transform_module_to_package(mod)
        self.assertFalse(pkg.has_child('mod.py'))
        self.assertTrue(pkg.get_child('mod').has_child('__init__.py'))
        self.project.history.undo()
        self.assertTrue(pkg.has_child('mod.py'))
        self.assertFalse(pkg.has_child('mod'))

    def test_transform_module_to_package_with_relative_imports(self):
        pkg = self.pycore.create_package(self.project.root, 'pkg')
        mod1 = self.pycore.create_module(pkg, 'mod1')
        mod1.write('import mod2\nfrom mod2 import AClass\n')
        mod2 = self.pycore.create_module(pkg, 'mod2')
        mod2.write('class AClass(object):\n    pass\n')
        self._transform_module_to_package(mod1)
        new_init = self.project.get_resource('pkg/mod1/__init__.py')
        self.assertEquals('import pkg.mod2\nfrom pkg.mod2 import AClass\n',
                          new_init.read())


class EncapsulateFieldTest(unittest.TestCase):

    def setUp(self):
        super(EncapsulateFieldTest, self).setUp()
        self.project = testutils.sample_project()
        self.pycore = self.project.get_pycore()
        self.mod = self.pycore.create_module(self.project.root, 'mod')
        self.mod1 = self.pycore.create_module(self.project.root, 'mod1')
        self.a_class = 'class A(object):\n    def __init__(self):\n        self.attr = 1\n'
        self.setter_and_getter = '\n    def get_attr(self):\n        return self.attr\n\n' \
                                 '    def set_attr(self, value):\n        self.attr = value\n'
        self.encapsulated = 'class A(object):\n    def __init__(self):\n        self.attr = 1\n\n' \
                            '    def get_attr(self):\n        return self.attr\n\n' \
                            '    def set_attr(self, value):\n        self.attr = value\n'

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

    def _perform_encapsulate_field(self, resource, offset):
        changes = EncapsulateField(self.project, resource, offset).\
                  get_changes()
        self.project.do(changes)

    def test_adding_getters_and_setters(self):
        code = self.a_class
        self.mod.write(code)
        self._perform_encapsulate_field(self.mod, code.index('attr') + 1)
        self.assertEquals(self.encapsulated, self.mod.read())

    def test_changing_getters_in_other_modules(self):
        self.mod1.write('import mod\na_var = mod.A()\nrange(a_var.attr)\n')
        self.mod.write(self.a_class)
        self._perform_encapsulate_field(self.mod, self.mod.read().index('attr') + 1)
        self.assertEquals('import mod\na_var = mod.A()\nrange(a_var.get_attr())\n',
                          self.mod1.read())

    def test_changing_setters_in_other_modules(self):
        self.mod1.write('import mod\na_var = mod.A()\na_var.attr = 1\n')
        self.mod.write(self.a_class)
        self._perform_encapsulate_field(self.mod, self.mod.read().index('attr') + 1)
        self.assertEquals('import mod\na_var = mod.A()\na_var.set_attr(1)\n',
                          self.mod1.read())

    def test_changing_getters_in_setters(self):
        self.mod1.write('import mod\na_var = mod.A()\na_var.attr = 1 + a_var.attr\n')
        self.mod.write(self.a_class)
        self._perform_encapsulate_field(self.mod, self.mod.read().index('attr') + 1)
        self.assertEquals(
            'import mod\na_var = mod.A()\na_var.set_attr(1 + a_var.get_attr())\n',
            self.mod1.read())

    def test_appending_to_class_end(self):
        self.mod1.write(self.a_class + 'a_var = A()\n')
        self._perform_encapsulate_field(self.mod1, self.mod1.read().index('attr') + 1)
        self.assertEquals(self.encapsulated + 'a_var = A()\n',
                          self.mod1.read())

    def test_performing_in_other_modules(self):
        self.mod1.write('import mod\na_var = mod.A()\nrange(a_var.attr)\n')
        self.mod.write(self.a_class)
        self._perform_encapsulate_field(self.mod1, self.mod1.read().index('attr') + 1)
        self.assertEquals(self.encapsulated, self.mod.read())
        self.assertEquals('import mod\na_var = mod.A()\nrange(a_var.get_attr())\n',
                          self.mod1.read())

    def test_changing_main_module_occurances(self):
        self.mod1.write(self.a_class + 'a_var = A()\na_var.attr = a_var.attr * 2\n')
        self._perform_encapsulate_field(self.mod1, self.mod1.read().index('attr') + 1)
        self.assertEquals(
            self.encapsulated +
            'a_var = A()\na_var.set_attr(a_var.get_attr() * 2)\n',
            self.mod1.read())

    @testutils.assert_raises(RefactoringError)
    def test_raising_exception_when_performed_on_non_attributes(self):
        self.mod1.write('attr = 10')
        self._perform_encapsulate_field(self.mod1, self.mod1.read().index('attr') + 1)

    @testutils.assert_raises(RefactoringError)
    def test_raising_exception_on_tuple_assignments(self):
        self.mod.write(self.a_class)
        self.mod1.write('import mod\na_var = mod.A()\na_var.attr = 1\na_var.attr, b = 1, 2\n')
        self._perform_encapsulate_field(self.mod1, self.mod1.read().index('attr') + 1)

    @testutils.assert_raises(RefactoringError)
    def test_raising_exception_on_tuple_assignments2(self):
        self.mod.write(self.a_class)
        self.mod1.write('import mod\na_var = mod.A()\na_var.attr = 1\nb, a_var.attr = 1, 2\n')
        self._perform_encapsulate_field(self.mod1, self.mod1.read().index('attr') + 1)

    def test_tuple_assignments_and_function_calls(self):
        self.mod1.write('import mod\ndef func(a1=0, a2=0):\n    pass\n'
                        'a_var = mod.A()\nfunc(a_var.attr, a2=2)\n')
        self.mod.write(self.a_class)
        self._perform_encapsulate_field(self.mod, self.mod.read().index('attr') + 1)
        self.assertEquals('import mod\ndef func(a1=0, a2=0):\n    pass\n'
                          'a_var = mod.A()\nfunc(a_var.get_attr(), a2=2)\n',
                          self.mod1.read())

    def test_tuple_assignments(self):
        self.mod1.write('import mod\na_var = mod.A()\na, b = a_var.attr, 1\n')
        self.mod.write(self.a_class)
        self._perform_encapsulate_field(self.mod, self.mod.read().index('attr') + 1)
        self.assertEquals(
            'import mod\na_var = mod.A()\na, b = a_var.get_attr(), 1\n',
            self.mod1.read())

    def test_changing_augmented_assignments(self):
        self.mod1.write('import mod\na_var = mod.A()\na_var.attr += 1\n')
        self.mod.write(self.a_class)
        self._perform_encapsulate_field(self.mod, self.mod.read().index('attr') + 1)
        self.assertEquals(
            'import mod\na_var = mod.A()\na_var.set_attr(a_var.get_attr() + 1)\n',
            self.mod1.read())

    def test_changing_augmented_assignments2(self):
        self.mod1.write('import mod\na_var = mod.A()\na_var.attr <<= 1\n')
        self.mod.write(self.a_class)
        self._perform_encapsulate_field(self.mod, self.mod.read().index('attr') + 1)
        self.assertEquals(
            'import mod\na_var = mod.A()\na_var.set_attr(a_var.get_attr() << 1)\n',
            self.mod1.read())


class LocalToFieldTest(unittest.TestCase):

    def setUp(self):
        super(LocalToFieldTest, self).setUp()
        self.project = testutils.sample_project()
        self.pycore = self.project.get_pycore()
        self.mod = self.pycore.create_module(self.project.root, 'mod')

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

    def _perform_convert_local_variable_to_field(self, resource, offset):
        changes = LocalToField(
            self.project, resource, offset).get_changes()
        self.project.do(changes)

    def test_simple_local_to_field(self):
        code = 'class A(object):\n    def a_func(self):\n' \
               '        var = 10\n'
        self.mod.write(code)
        self._perform_convert_local_variable_to_field(self.mod,
                                                      code.index('var') + 1)
        expected = 'class A(object):\n    def a_func(self):\n' \
                   '        self.var = 10\n'
        self.assertEquals(expected, self.mod.read())

    @testutils.assert_raises(RefactoringError)
    def test_raising_exception_when_performed_on_a_global_var(self):
        self.mod.write('var = 10\n')
        self._perform_convert_local_variable_to_field(
            self.mod, self.mod.read().index('var') + 1)

    @testutils.assert_raises(RefactoringError)
    def test_raising_exception_when_performed_on_field(self):
        code = 'class A(object):\n    def a_func(self):\n' \
               '        self.var = 10\n'
        self.mod.write(code)
        self._perform_convert_local_variable_to_field(
            self.mod, self.mod.read().index('var') + 1)

    @testutils.assert_raises(RefactoringError)
    def test_raising_exception_when_performed_on_a_parameter(self):
        code = 'class A(object):\n    def a_func(self, var):\n' \
               '        a = var\n'
        self.mod.write(code)
        self._perform_convert_local_variable_to_field(
            self.mod, self.mod.read().index('var') + 1)

    # NOTE: This situation happens alot and is normally not an error
    #@testutils.assert_raises(RefactoringError)
    def test_not_raising_exception_when_there_is_a_field_with_the_same_name(self):
        code = 'class A(object):\n    def __init__(self):\n        self.var = 1\n' \
               '    def a_func(self):\n        var = 10\n'
        self.mod.write(code)
        self._perform_convert_local_variable_to_field(
            self.mod, self.mod.read().rindex('var') + 1)

    def test_local_to_field_with_self_renamed(self):
        code = 'class A(object):\n    def a_func(myself):\n' \
               '        var = 10\n'
        self.mod.write(code)
        self._perform_convert_local_variable_to_field(self.mod,
                                                         code.index('var') + 1)
        expected = 'class A(object):\n    def a_func(myself):\n' \
                   '        myself.var = 10\n'
        self.assertEquals(expected, self.mod.read())


class IntroduceParameterTest(unittest.TestCase):

    def setUp(self):
        super(IntroduceParameterTest, self).setUp()
        self.project = testutils.sample_project()
        self.pycore = self.project.get_pycore()
        self.mod = self.pycore.create_module(self.project.root, 'mod')

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

    def _introduce_parameter(self, offset, name):
        rope.refactor.introduce_parameter.IntroduceParameter(
            self.project, self.mod, offset).get_changes(name).do()

    def test_simple_case(self):
        self.mod.write('var = 1\ndef f():\n    b = var\n')
        offset = self.mod.read().rindex('var')
        self._introduce_parameter(offset, 'var')
        self.assertEquals('var = 1\ndef f(var=var):\n    b = var\n', self.mod.read())

    def test_changing_function_body(self):
        self.mod.write('var = 1\ndef f():\n    b = var\n')
        offset = self.mod.read().rindex('var')
        self._introduce_parameter(offset, 'p1')
        self.assertEquals('var = 1\ndef f(p1=var):\n    b = p1\n', self.mod.read())

    @testutils.assert_raises(RefactoringError)
    def test_unknown_variables(self):
        self.mod.write('def f():\n    b = var + c\n')
        offset = self.mod.read().rindex('var')
        self._introduce_parameter(offset, 'p1')
        self.assertEquals('def f(p1=var):\n    b = p1 + c\n', self.mod.read())

    @testutils.assert_raises(RefactoringError)
    def test_failing_when_not_inside(self):
        self.mod.write('var = 10\nb = var\n')
        offset = self.mod.read().rindex('var')
        self._introduce_parameter(offset, 'p1')

    def test_attribute_accesses(self):
        self.mod.write('class C(object):\n    a = 10\nc = C()\ndef f():\n    b = c.a\n')
        offset = self.mod.read().rindex('a')
        self._introduce_parameter(offset, 'p1')
        self.assertEquals('class C(object):\n    a = 10\nc = C()\ndef f(p1=c.a):\n    b = p1\n',
                          self.mod.read())

    def test_introducing_parameters_for_methods(self):
        self.mod.write('var = 1\nclass C(object):\n    def f(self):\n        b = var\n')
        offset = self.mod.read().rindex('var')
        self._introduce_parameter(offset, 'p1')
        self.assertEquals('var = 1\nclass C(object):\n    def f(self, p1=var):\n        b = p1\n',
                          self.mod.read())


def suite():
    result = unittest.TestSuite()
    result.addTests(ropetest.refactor.renametest.suite())
    result.addTests(unittest.makeSuite(ropetest.refactor.extracttest.ExtractMethodTest))
    result.addTests(unittest.makeSuite(IntroduceFactoryTest))
    result.addTests(unittest.makeSuite(ropetest.refactor.movetest.MoveRefactoringTest))
    result.addTests(ropetest.refactor.inlinetest.suite())
    result.addTests(unittest.makeSuite(EncapsulateFieldTest))
    result.addTests(unittest.makeSuite(LocalToFieldTest))
    result.addTests(unittest.makeSuite(ropetest.refactor.change_signature_test.ChangeSignatureTest))
    result.addTests(unittest.makeSuite(IntroduceParameterTest))
    result.addTests(unittest.makeSuite(ropetest.refactor.importutilstest.ImportUtilsTest))
    return result


if __name__ == '__main__':
    import sys
    if len(sys.argv) > 1:
        unittest.main()
    else:
        runner = unittest.TextTestRunner()
        runner.run(suite())