Source

rope_py3k / ropetest / refactor / change_signature_test.py

Full commit
Ali Gholami Rudi 2bc99f0 
Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi 2bc99f0 
Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi d9e0df5 
Ali Gholami Rudi 2bc99f0 






Ali Gholami Rudi 495804b 
Ali Gholami Rudi 59111cd 
Ali Gholami Rudi ee90187 
Ali Gholami Rudi 2bc99f0 

Ali Gholami Rudi 495804b 
Ali Gholami Rudi 2bc99f0 


Ali Gholami Rudi e9561b4 

Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi e9561b4 



Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi e9561b4 


Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi e9561b4 

Ali Gholami Rudi d9e0df5 





Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 





zjes 6e28c44 
Ali Gholami Rudi e9561b4 


Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi 979e5e3 
Ali Gholami Rudi e9561b4 

Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 



Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi e9561b4 


Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi e9561b4 

Ali Gholami Rudi ee90187 
Ali Gholami Rudi e9561b4 

Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi e9561b4 

Ali Gholami Rudi bfd42c5 

Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 22696ee 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi 22696ee 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi 22696ee 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi 22696ee 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi 22696ee 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi e9561b4 


Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi e9561b4 


Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi bf8c09d 
Ali Gholami Rudi 22696ee 
Ali Gholami Rudi bfd42c5 

Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi bf8c09d 
Ali Gholami Rudi 22696ee 
Ali Gholami Rudi bfd42c5 

Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi 41c4490 
Ali Gholami Rudi e9561b4 

Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 

zjes 6e28c44 
Ali Gholami Rudi e9561b4 



Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 

zjes 6e28c44 
Ali Gholami Rudi e9561b4 



Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 

zjes 6e28c44 
Ali Gholami Rudi e9561b4 



Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 

zjes 6e28c44 
Ali Gholami Rudi e9561b4 

Ali Gholami Rudi 979e5e3 
Ali Gholami Rudi 8cb84a4 

Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi 8cb84a4 
Ali Gholami Rudi d9e0df5 
Ali Gholami Rudi 8cb84a4 
Ali Gholami Rudi e9561b4 

Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi e9561b4 


Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi e9561b4 

Ali Gholami Rudi bfd42c5 

Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi e9561b4 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi 41c4490 
Ali Gholami Rudi df22086 

Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi df22086 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi df22086 



Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi df22086 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi df22086 

Ali Gholami Rudi 22696ee 

Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi 22696ee 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi 22696ee 

Ali Gholami Rudi df22086 

Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi df22086 
Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi 41c4490 
Ali Gholami Rudi 8cb84a4 




Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi 8cb84a4 
Ali Gholami Rudi f44ad86 
zjes 6e28c44 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi 8cb84a4 






Ali Gholami Rudi 4a31a93 
Ali Gholami Rudi 8cb84a4 
Ali Gholami Rudi f44ad86 
zjes 6e28c44 
Ali Gholami Rudi bfd42c5 
Ali Gholami Rudi 2bc99f0 
Ali Gholami Rudi 73275ae 





Ali Gholami Rudi f44ad86 
Ali Gholami Rudi 73275ae 
zjes 6e28c44 
Ali Gholami Rudi 73275ae 



Ali Gholami Rudi da9d25b 





Ali Gholami Rudi d9e0df5 
zjes 6e28c44 
Ali Gholami Rudi da9d25b 



Ali Gholami Rudi bf8c09d 




Ali Gholami Rudi f44ad86 
zjes 6e28c44 
Ali Gholami Rudi bf8c09d 



Ali Gholami Rudi da9d25b 
Ali Gholami Rudi bf8c09d 




Ali Gholami Rudi f44ad86 
zjes 6e28c44 
Ali Gholami Rudi bf8c09d 



Ali Gholami Rudi 6f7f6a0 






Ali Gholami Rudi f44ad86 
zjes 6e28c44 
Ali Gholami Rudi 6f7f6a0 




Ali Gholami Rudi 259c5b8 



Ali Gholami Rudi f44ad86 
Ali Gholami Rudi 259c5b8 
zjes 6e28c44 
Ali Gholami Rudi 259c5b8 

Ali Gholami Rudi 0a14a1a 







zjes 6e28c44 

Ali Gholami Rudi 0a14a1a 
Ali Gholami Rudi 1713f94 











zjes 6e28c44 
Ali Gholami Rudi 1713f94 
Ali Gholami Rudi 2bc99f0 

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

import rope.base.exceptions
from rope.refactor import change_signature
from rope.refactor.change_signature import *
from ropetest import testutils


class ChangeSignatureTest(unittest.TestCase):

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

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

    def test_normalizing_parameters_for_trivial_case(self):
        code = 'def a_func():\n    pass\na_func()'
        self.mod.write(code)
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentNormalizer()]))
        self.assertEqual(code, self.mod.read())

    def test_normalizing_parameters_for_trivial_case2(self):
        code = 'def a_func(param):\n    pass\na_func(2)'
        self.mod.write(code)
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentNormalizer()]))
        self.assertEqual(code, self.mod.read())

    def test_normalizing_parameters_for_unneeded_keyword(self):
        self.mod.write('def a_func(param):\n    pass\na_func(param=1)')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentNormalizer()]))
        self.assertEqual('def a_func(param):\n    pass\na_func(1)', self.mod.read())

    def test_normalizing_parameters_for_unneeded_keyword_for_methods(self):
        code = 'class A(object):\n' \
               '    def a_func(self, param):\n' \
               '        pass\n' \
               'a_var = A()\n' \
               'a_var.a_func(param=1)\n'
        self.mod.write(code)
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentNormalizer()]))
        expected = 'class A(object):\n' \
                   '    def a_func(self, param):\n' \
                   '        pass\n' \
                   'a_var = A()\n' \
                   'a_var.a_func(1)\n'
        self.assertEqual(expected, self.mod.read())

    def test_normalizing_parameters_for_unsorted_keyword(self):
        self.mod.write('def a_func(p1, p2):\n    pass\na_func(p2=2, p1=1)')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentNormalizer()]))
        self.assertEqual('def a_func(p1, p2):\n    pass\na_func(1, 2)',
                          self.mod.read())

    @testutils.assert_raises(rope.base.exceptions.RefactoringError)
    def test_raising_exceptions_for_non_functions(self):
        self.mod.write('a_var = 10')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_var') + 1)

    def test_normalizing_parameters_for_args_parameter(self):
        self.mod.write('def a_func(*arg):\n    pass\na_func(1, 2)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentNormalizer()]))
        self.assertEqual('def a_func(*arg):\n    pass\na_func(1, 2)\n',
                          self.mod.read())

    def test_normalizing_parameters_for_args_parameter_and_keywords(self):
        self.mod.write('def a_func(param, *args):\n    pass\na_func(*[1, 2, 3])\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentNormalizer()]))
        self.assertEqual('def a_func(param, *args):\n    pass\n'
                          'a_func(*[1, 2, 3])\n', self.mod.read())

    def test_normalizing_functions_from_other_modules(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write('def a_func(param):\n    pass\n')
        self.mod.write('import mod1\nmod1.a_func(param=1)\n')
        signature = ChangeSignature(self.project, mod1,
                                    mod1.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentNormalizer()]))
        self.assertEqual('import mod1\nmod1.a_func(1)\n', self.mod.read())

    def test_normalizing_parameters_for_keyword_parameters(self):
        self.mod.write('def a_func(p1, **kwds):\n    pass\n'
                       'a_func(p2=2, p1=1)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentNormalizer()]))
        self.assertEqual('def a_func(p1, **kwds):\n    pass\n'
                          'a_func(1, p2=2)\n', self.mod.read())

    def test_removing_arguments(self):
        self.mod.write('def a_func(p1):\n    pass\na_func(1)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentRemover(0)]))
        self.assertEqual('def a_func():\n    pass\na_func()\n',
                          self.mod.read())

    def test_removing_arguments_with_multiple_args(self):
        self.mod.write('def a_func(p1, p2):\n    pass\na_func(1, 2)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentRemover(0)]))
        self.assertEqual('def a_func(p2):\n    pass\na_func(2)\n',
                          self.mod.read())

    def test_removing_arguments_passed_as_keywords(self):
        self.mod.write('def a_func(p1):\n    pass\na_func(p1=1)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentRemover(0)]))
        self.assertEqual('def a_func():\n    pass\na_func()\n',
                          self.mod.read())

    def test_removing_arguments_with_defaults(self):
        self.mod.write('def a_func(p1=1):\n    pass\na_func(1)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentRemover(0)]))
        self.assertEqual('def a_func():\n    pass\na_func()\n',
                          self.mod.read())

    def test_removing_arguments_star_args(self):
        self.mod.write('def a_func(p1, *args):\n    pass\na_func(1)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentRemover(1)]))
        self.assertEqual('def a_func(p1):\n    pass\na_func(1)\n',
                          self.mod.read())

    def test_removing_keyword_arg(self):
        self.mod.write('def a_func(p1, **kwds):\n    pass\na_func(1)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentRemover(1)]))
        self.assertEqual('def a_func(p1):\n    pass\na_func(1)\n',
                          self.mod.read())

    def test_removing_keyword_arg2(self):
        self.mod.write('def a_func(p1, *args, **kwds):\n    pass\na_func(1)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentRemover(2)]))
        self.assertEqual('def a_func(p1, *args):\n    pass\na_func(1)\n',
                          self.mod.read())

    # XXX: What to do here for star args?
    def xxx_test_removing_arguments_star_args2(self):
        self.mod.write('def a_func(p1, *args):\n    pass\n'
                       'a_func(2, 3, p1=1)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentRemover(1)]))
        self.assertEqual('def a_func(p1):\n    pass\na_func(p1=1)\n',
                          self.mod.read())

    # XXX: What to do here for star args?
    def xxx_test_removing_arguments_star_args3(self):
        self.mod.write('def a_func(p1, *args):\n    pass\n'
                       'a_func(*[1, 2, 3])\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentRemover(1)]))
        self.assertEqual('def a_func(p1):\n    pass\na_func(*[1, 2, 3])\n',
                          self.mod.read())

    def test_adding_arguments_for_normal_args_changing_definition(self):
        self.mod.write('def a_func():\n    pass\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentAdder(0, 'p1')]))
        self.assertEqual('def a_func(p1):\n    pass\n', self.mod.read())

    def test_adding_arguments_for_normal_args_with_defaults(self):
        self.mod.write('def a_func():\n    pass\na_func()\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        adder = ArgumentAdder(0, 'p1', 'None')
        self.project.do(signature.get_changes([adder]))
        self.assertEqual('def a_func(p1=None):\n    pass\na_func()\n',
                          self.mod.read())

    def test_adding_arguments_for_normal_args_changing_calls(self):
        self.mod.write('def a_func():\n    pass\na_func()\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        adder = ArgumentAdder(0, 'p1', 'None', '1')
        self.project.do(signature.get_changes([adder]))
        self.assertEqual('def a_func(p1=None):\n    pass\na_func(1)\n',
                          self.mod.read())

    def test_adding_arguments_for_normal_args_changing_calls_with_keywords(self):
        self.mod.write('def a_func(p1=0):\n    pass\na_func()\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        adder = ArgumentAdder(1, 'p2', '0', '1')
        self.project.do(signature.get_changes([adder]))
        self.assertEqual('def a_func(p1=0, p2=0):\n    pass\na_func(p2=1)\n',
                          self.mod.read())

    def test_adding_arguments_for_normal_args_changing_calls_with_no_value(self):
        self.mod.write('def a_func(p2=0):\n    pass\na_func(1)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        adder = ArgumentAdder(0, 'p1', '0', None)
        self.project.do(signature.get_changes([adder]))
        self.assertEqual('def a_func(p1=0, p2=0):\n    pass\na_func(p2=1)\n',
                          self.mod.read())

    @testutils.assert_raises(rope.base.exceptions.RefactoringError)
    def test_adding_duplicate_parameter_and_raising_exceptions(self):
        self.mod.write('def a_func(p1):\n    pass\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentAdder(1, 'p1')]))

    def test_inlining_default_arguments(self):
        self.mod.write('def a_func(p1=0):\n    pass\na_func()\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentDefaultInliner(0)]))
        self.assertEqual('def a_func(p1=0):\n    pass\n'
                          'a_func(0)\n', self.mod.read())

    def test_inlining_default_arguments2(self):
        self.mod.write('def a_func(p1=0):\n    pass\na_func(1)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentDefaultInliner(0)]))
        self.assertEqual('def a_func(p1=0):\n    pass\n'
                          'a_func(1)\n', self.mod.read())

    def test_preserving_args_and_keywords_order(self):
        self.mod.write('def a_func(*args, **kwds):\n    pass\n'
                       'a_func(3, 1, 2, a=1, c=3, b=2)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentNormalizer()]))
        self.assertEqual('def a_func(*args, **kwds):\n    pass\n'
                          'a_func(3, 1, 2, a=1, c=3, b=2)\n', self.mod.read())

    def test_change_order_for_only_one_parameter(self):
        self.mod.write('def a_func(p1):\n    pass\na_func(1)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentReorderer([0])]))
        self.assertEqual('def a_func(p1):\n    pass\na_func(1)\n',
                          self.mod.read())

    def test_change_order_for_two_parameter(self):
        self.mod.write('def a_func(p1, p2):\n    pass\na_func(1, 2)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentReorderer([1, 0])]))
        self.assertEqual('def a_func(p2, p1):\n    pass\na_func(2, 1)\n',
                          self.mod.read())

    def test_reordering_multi_line_function_headers(self):
        self.mod.write('def a_func(p1,\n p2):\n    pass\na_func(1, 2)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentReorderer([1, 0])]))
        self.assertEqual('def a_func(p2, p1):\n    pass\na_func(2, 1)\n',
                          self.mod.read())

    def test_changing_order_with_static_params(self):
        self.mod.write('def a_func(p1, p2=0, p3=0):\n    pass\na_func(1, 2)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentReorderer([0, 2, 1])]))
        self.assertEqual('def a_func(p1, p3=0, p2=0):\n    pass\n'
                          'a_func(1, p2=2)\n', self.mod.read())

    def test_doing_multiple_changes(self):
        changers = []
        self.mod.write('def a_func(p1):\n    pass\na_func(1)\n')
        changers.append(change_signature.ArgumentRemover(0))
        changers.append(change_signature.ArgumentAdder(0, 'p2', None, None))
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        signature.get_changes(changers).do()
        self.assertEqual('def a_func(p2):\n    pass\na_func()\n',
                          self.mod.read())

    def test_doing_multiple_changes2(self):
        changers = []
        self.mod.write('def a_func(p1, p2):\n    pass\na_func(p2=2)\n')
        changers.append(change_signature.ArgumentAdder(2, 'p3', None, '3'))
        changers.append(change_signature.ArgumentReorderer([1, 0, 2]))
        changers.append(change_signature.ArgumentRemover(1))
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        signature.get_changes(changers).do()
        self.assertEqual('def a_func(p2, p3):\n    pass\na_func(2, 3)\n',
                          self.mod.read())

    def test_changing_signature_in_subclasses(self):
        self.mod.write(
            'class A(object):\n    def a_method(self):\n        pass\n'
            'class B(A):\n    def a_method(self):\n        pass\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_method') + 1)
        signature.get_changes([change_signature.ArgumentAdder(1, 'p1')],
                                 in_hierarchy=True).do()
        self.assertEqual(
            'class A(object):\n    def a_method(self, p1):\n        pass\n'
            'class B(A):\n    def a_method(self, p1):\n        pass\n',
            self.mod.read())

    def test_differentiating_class_accesses_from_instance_accesses(self):
        self.mod.write(
            'class A(object):\n    def a_func(self, param):\n        pass\n'
            'a_var = A()\nA.a_func(a_var, param=1)')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('a_func') + 1)
        self.project.do(signature.get_changes([ArgumentRemover(1)]))
        self.assertEqual(
            'class A(object):\n    def a_func(self):\n        pass\n'
            'a_var = A()\nA.a_func(a_var)', self.mod.read())

    def test_changing_signature_for_constructors(self):
        self.mod.write(
            'class C(object):\n    def __init__(self, p):\n        pass\n'
            'c = C(1)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('C') + 1)
        signature.get_changes([change_signature.ArgumentRemover(1)]).do()
        self.assertEqual(
            'class C(object):\n    def __init__(self):\n        pass\n'
            'c = C()\n',
            self.mod.read())

    def test_changing_signature_for_constructors2(self):
        self.mod.write(
            'class C(object):\n    def __init__(self, p):\n        pass\n'
            'c = C(1)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('__init__') + 1)
        signature.get_changes([change_signature.ArgumentRemover(1)]).do()
        self.assertEqual(
            'class C(object):\n    def __init__(self):\n        pass\n'
            'c = C()\n',
            self.mod.read())

    def test_changing_signature_for_constructors_when_using_super(self):
        self.mod.write(
            'class A(object):\n    def __init__(self, p):\n        pass\n'
            'class B(A):\n    '
            'def __init__(self, p):\n        super(B, self).__init__(p)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().index('__init__') + 1)
        signature.get_changes([change_signature.ArgumentRemover(1)]).do()
        self.assertEqual(
            'class A(object):\n    def __init__(self):\n        pass\n'
            'class B(A):\n    '
            'def __init__(self, p):\n        super(B, self).__init__()\n',
            self.mod.read())

    def test_redordering_arguments_reported_by_mft(self):
        self.mod.write('def f(a, b, c):\n    pass\nf(1, 2, 3)\n')
        signature = ChangeSignature(self.project, self.mod,
                                    self.mod.read().rindex('f'))
        signature.get_changes(
            [change_signature.ArgumentReorderer([1, 2, 0])]).do()
        self.assertEqual('def f(b, c, a):\n    pass\nf(2, 3, 1)\n',
                          self.mod.read())

    def test_resources_parameter(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        mod1.write('def a_func(param):\n    pass\n')
        self.mod.write('import mod1\nmod1.a_func(1)\n')
        signature = ChangeSignature(self.project, mod1,
                                    mod1.read().index('a_func') + 1)
        signature.get_changes([change_signature.ArgumentRemover(0)],
                              resources=[mod1]).do()
        self.assertEqual('import mod1\nmod1.a_func(1)\n', self.mod.read())
        self.assertEqual('def a_func():\n    pass\n', mod1.read())

    def test_reordering_and_automatic_defaults(self):
        code = 'def f(p1, p2=2):\n' \
               '    pass\n' \
               'f(1, 2)\n'
        self.mod.write(code)
        signature = ChangeSignature(self.project, self.mod,
                                    code.index('f('))
        reorder = change_signature.ArgumentReorderer([1, 0], autodef='1')
        signature.get_changes([reorder]).do()
        expected = 'def f(p2=2, p1=1):\n' \
                   '    pass\n' \
                   'f(2, 1)\n'
        self.assertEqual(expected, self.mod.read())


if __name__ == '__main__':
    unittest.main()