1. zjes
  2. rope_py3k

Commits

Ali Gholami Rudi  committed ee1c174

encapsulate_field: added getter and setter arguments

  • Participants
  • Parent commits 942443b
  • Branches trunk

Comments (0)

Files changed (3)

File rope/refactor/encapsulate_field.py

View file
  • Ignore whitespace
                 return True
         return False
 
-    def get_changes(self, task_handle=taskhandle.NullTaskHandle()):
+    def get_changes(self, getter=None, setter=None,
+                    task_handle=taskhandle.NullTaskHandle()):
         changes = ChangeSet('Encapsulate field <%s>' % self.name)
         job_set = task_handle.create_jobset(
             'Collecting Changes', len(self.pycore.get_python_files()))
-        rename_in_module = GetterSetterRenameInModule(self.pycore, self.name,
-                                                      [self.pyname])
+        if getter is None:
+            getter = 'get_' + self.name
+        if setter is None:
+            setter = 'set_' + self.name
+        rename_in_module = GetterSetterRenameInModule(
+            self.pycore, self.name, [self.pyname], getter, setter)
         for file in self.pycore.get_python_files():
             job_set.started_job('Working on <%s>' % file.path)
             if file == self.resource:
-                self._change_holding_module(changes, rename_in_module)
+                self._change_holding_module(changes, rename_in_module,
+                                            getter, setter)
             else:
                 result = rename_in_module.get_changed_module(file)
                 if result is not None:
             job_set.finished_job()
         return changes
 
+    def get_field_name(self):
+        return self.name
+
     def _get_defining_class_scope(self):
         defining_scope = self._get_defining_scope()
         if defining_scope.get_kind() == 'Function':
         pymodule, line = self.pyname.get_definition_location()
         return pymodule.get_scope().get_inner_scope_for_line(line)
 
-    def _change_holding_module(self, changes, rename_in_module):
+    def _change_holding_module(self, changes, rename_in_module, getter, setter):
         pymodule = self.pycore.resource_to_pyobject(self.resource)
         class_scope = self._get_defining_class_scope()
         defining_object = self._get_defining_scope().pyobject
             class_scope = pymodule.get_scope().\
                           get_inner_scope_for_line(class_scope.get_start())
         indents = sourceutils.get_indent(self.pycore) * ' '
-        getter = 'def get_%s(self):\n%sreturn self.%s' % \
-                 (self.name, indents, self.name)
-        setter = 'def set_%s(self, value):\n%sself.%s = value' % \
-                 (self.name, indents, self.name)
+        getter = 'def %s(self):\n%sreturn self.%s' % \
+                 (getter, indents, self.name)
+        setter = 'def %s(self, value):\n%sself.%s = value' % \
+                 (setter, indents, self.name)
         new_source = sourceutils.add_methods(pymodule, class_scope,
                                              [getter, setter])
         changes.add_change(ChangeContents(pymodule.get_resource(), new_source))
 
 class GetterSetterRenameInModule(object):
 
-    def __init__(self, pycore, name, pynames):
+    def __init__(self, pycore, name, pynames, getter, setter):
         self.pycore = pycore
         self.name = name
         self.occurrences_finder = occurrences.FilteredFinder(pycore, name,
                                                              pynames)
-        self.getter = 'get_' + name
-        self.setter = 'set_' + name
+        self.getter = getter
+        self.setter = setter
 
     def get_changed_module(self, resource=None, pymodule=None, skip_start=0, skip_end=0):
-        return _FindChangesForModule(self, resource, pymodule,
-                                     skip_start, skip_end).get_changed_module()
+        finder = _FindChangesForModule(self, resource, pymodule,
+                                       skip_start, skip_end)
+        return finder.get_changed_module()
 
 
 class _FindChangesForModule(object):

File rope/refactor/functionutils.py

View file
  • Ignore whitespace
         start = lines.get_line_start(start_line)
         end = lines.get_line_end(end_line)
         start = pymodule.source_code.find('def ', start) + 4
+        # XXX: problems for one-liners with a colon in a str
         end = pymodule.source_code.rfind(':', start, end)
         return DefinitionInfo._read(pyfunction, pymodule.source_code[start:end])
 

File ropetest/refactor/__init__.py

View file
  • Ignore whitespace
         self.mod = testutils.create_module(self.project, 'mod')
         self.mod1 = testutils.create_module(self.project, '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 = self.a_class + self.setter_and_getter
+        self.added_methods = '\n    def get_attr(self):\n        return self.attr\n\n' \
+                             '    def set_attr(self, value):\n        self.attr = value\n'
+        self.encapsulated = self.a_class + self.added_methods
 
     def tearDown(self):
         testutils.remove_project(self.project)
         super(EncapsulateFieldTest, self).tearDown()
 
-    def _perform_encapsulate_field(self, resource, offset):
+    def _encapsulate(self, resource, offset, **args):
         changes = EncapsulateField(self.project, resource, offset).\
-                  get_changes()
+                  get_changes(**args)
         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._encapsulate(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._encapsulate(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._encapsulate(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._encapsulate(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._encapsulate(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._encapsulate(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._encapsulate(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',
     @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)
+        self._encapsulate(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)
+        self._encapsulate(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)
+        self._encapsulate(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._encapsulate(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._encapsulate(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._encapsulate(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._encapsulate(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_occurrences_inside_the_class(self):
         new_class = self.a_class + '\n    def a_func(self):\n        self.attr = 1\n'
         self.mod.write(new_class)
-        self._perform_encapsulate_field(self.mod, self.mod.read().index('attr') + 1)
+        self._encapsulate(self.mod, self.mod.read().index('attr') + 1)
         expected = self.a_class + '\n    def a_func(self):\n        self.set_attr(1)\n' + \
-                   self.setter_and_getter
+                   self.added_methods
+        self.assertEquals(expected, self.mod.read())
+
+    def test_getter_and_setter_parameters(self):
+        self.mod.write(self.a_class)
+        self._encapsulate(self.mod, self.mod.read().index('attr') + 1,
+                          getter='getAttr', setter='setAttr')
+        new_methods = self.added_methods.replace('get_attr', 'getAttr').\
+                      replace('set_attr', 'setAttr')
+        expected = self.a_class + new_methods
         self.assertEquals(expected, self.mod.read())