Skip to content

Commit

Permalink
Add some tests when doing rename refactoring with superclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
lieryan committed Feb 27, 2024
1 parent aa02352 commit c2745fd
Showing 1 changed file with 81 additions and 2 deletions.
83 changes: 81 additions & 2 deletions ropetest/refactor/renametest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def tearDown(self):
testutils.remove_project(self.project)
super().tearDown()

def _local_rename(self, source_code, offset, new_name):
def _local_rename(self, source_code, offset, new_name, **kwds):
testmod = testutils.create_module(self.project, "testmod")
testmod.write(source_code)
changes = Rename(self.project, testmod, offset).get_changes(
new_name, resources=[testmod]
new_name, resources=[testmod], **kwds
)
self.project.do(changes)
return testmod.read()
Expand Down Expand Up @@ -1392,6 +1392,85 @@ def test_renaming_modules_aliased_many_dots(self):


class RenameRefactoringWithSuperclassTest(RenameTestMixin, unittest.TestCase):
ORIGINAL_CODE = dedent("""\
class Parent:
def a_method(self):
pass
class Child(Parent):
def a_method(self, strg):
return super(Child, self).a_method(strg, *args, **kwargs)
""")
BOTH_RENAMED = dedent("""\
class Parent:
def new_method(self):
pass
class Child(Parent):
def new_method(self, strg):
return super(Child, self).new_method(strg, *args, **kwargs)
""")

PARENT_RENAMED = dedent("""\
class Parent:
def new_method(self):
pass
class Child(Parent):
def a_method(self, strg):
return super(Child, self).new_method(strg, *args, **kwargs)
""")

CHILD_RENAMED = dedent("""\
class Parent:
def a_method(self):
pass
class Child(Parent):
def new_method(self, strg):
return super(Child, self).a_method(strg, *args, **kwargs)
""")

FROM_PARENT = "a_method(self)" # from Parent.a_method
FROM_CHILD = "a_method(self, strg" # from Child.a_method
FROM_CALLER = "a_method(strg, *args" # from super() line

def test_rename_with_superclass_in_hierarchy_from_parent(self):
code = self.ORIGINAL_CODE
offset = code.index(self.FROM_PARENT)
refactored = self._local_rename(code, offset, "new_method", in_hierarchy=True)
self.assertEqual(refactored, self.BOTH_RENAMED)

def test_rename_with_superclass_not_in_hierarchy_from_parent(self):
code = self.ORIGINAL_CODE
offset = code.index(self.FROM_PARENT)
refactored = self._local_rename(code, offset, "new_method", in_hierarchy=False)
self.assertEqual(refactored, self.PARENT_RENAMED)

def test_rename_with_superclass_in_hierarchy_from_child(self):
code = self.ORIGINAL_CODE
offset = code.index(self.FROM_CHILD)
refactored = self._local_rename(code, offset, "new_method", in_hierarchy=True)
self.assertEqual(refactored, self.BOTH_RENAMED)

def test_rename_with_superclass_not_in_hierarchy_from_child(self):
code = self.ORIGINAL_CODE
offset = code.index(self.FROM_CHILD)
refactored = self._local_rename(code, offset, "new_method", in_hierarchy=False)
self.assertEqual(refactored, self.CHILD_RENAMED)

def test_rename_with_superclass_in_hierarchy_from_caller(self):
code = self.ORIGINAL_CODE
offset = code.index(self.FROM_CALLER)
refactored = self._local_rename(code, offset, "new_method", in_hierarchy=True)
self.assertEqual(refactored, self.BOTH_RENAMED)

def test_rename_with_superclass_not_in_hierarchy_from_caller(self):
code = self.ORIGINAL_CODE
offset = code.index(self.FROM_CALLER)
refactored = self._local_rename(code, offset, "new_method", in_hierarchy=False)
self.assertEqual(refactored, self.PARENT_RENAMED)

def test_renaming_methods_in_subclasses(self):
mod = testutils.create_module(self.project, "mod1")
mod.write(dedent("""\
Expand Down

0 comments on commit c2745fd

Please sign in to comment.