def test_resources_parameter(self): mod1 = testutils.create_module(self.project, "mod1") mod1.write("def a_func(param):\n pass\n") self.mod.write( dedent("""\ import mod1 mod1.a_func(1) """) ) signature = change_signature.ChangeSignature( self.project, mod1, mod1.read().index("a_func") + 1 ) signature.get_changes( [change_signature.ArgumentRemover(0)], resources=[mod1] ).do() self.assertEqual( dedent("""\ import mod1 mod1.a_func(1) """), self.mod.read(), ) self.assertEqual( dedent("""\ def a_func(): pass """), mod1.read(), )
def get_changes(self, refactor, input_string, in_hierarchy=False): """ Function description. :return Rope.changes: """ args = re.sub(r'[\s\(\)]+', '', input_string).split(',') olds = [arg[0] for arg in refactor.get_args()] changers = [] for arg in [a for a in olds if a not in args]: changers.append(change_signature.ArgumentRemover(olds.index(arg))) olds.remove(arg) order = [] for index, arg in enumerate(args): if arg not in olds: changers.append(change_signature.ArgumentAdder(index, arg)) olds.insert(index, arg) order.append(olds.index(arg)) changers.append( change_signature.ArgumentReorderer(order, autodef='None')) return refactor.get_changes(changers, in_hierarchy=in_hierarchy)
def test_changing_signature_for_constructors_when_using_super(self): self.mod.write( dedent("""\ class A(object): def __init__(self, p): pass class B(A): def __init__(self, p): super(B, self).__init__(p) """) ) signature = change_signature.ChangeSignature( self.project, self.mod, self.mod.read().index("__init__") + 1 ) signature.get_changes([change_signature.ArgumentRemover(1)]).do() self.assertEqual( dedent("""\ class A(object): def __init__(self): pass class B(A): def __init__(self, p): super(B, self).__init__() """), 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 = change_signature.ChangeSignature( self.project, self.mod, self.mod.read().index('a_func') + 1) self.project.do(signature.get_changes( [change_signature.ArgumentRemover(2)])) self.assertEquals('def a_func(p1, *args):\n pass\na_func(1)\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 = change_signature.ChangeSignature( self.project, self.mod, self.mod.read().index('a_func') + 1) self.project.do(signature.get_changes( [change_signature.ArgumentRemover(0)])) self.assertEquals('def a_func():\n pass\na_func()\n', self.mod.read())
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 = change_signature.ChangeSignature( self.project, self.mod, self.mod.read().index('a_func') + 1) self.project.do(signature.get_changes( [change_signature.ArgumentRemover(1)])) self.assertEquals('def a_func(p1):\n pass\na_func(*[1, 2, 3])\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.assertEquals( 'class C(object):\n def __init__(self):\n pass\n' 'c = C()\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 = change_signature.ChangeSignature( self.project, self.mod, self.mod.read().index('a_func') + 1) signature.get_changes(changers).do() self.assertEquals('def a_func(p2):\n pass\na_func()\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 = change_signature.ChangeSignature( self.project, mod1, mod1.read().index('a_func') + 1) signature.get_changes([change_signature.ArgumentRemover(0)], resources=[mod1]).do() self.assertEquals('import mod1\nmod1.a_func(1)\n', self.mod.read()) self.assertEquals('def a_func():\n pass\n', mod1.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 = change_signature.ChangeSignature( self.project, self.mod, self.mod.read().index('a_func') + 1) self.project.do(signature.get_changes( [change_signature.ArgumentRemover(1)])) self.assertEquals( 'class A(object):\n def a_func(self):\n pass\n' 'a_var = A()\nA.a_func(a_var)', 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 = change_signature.ChangeSignature( self.project, self.mod, self.mod.read().index('a_func') + 1) signature.get_changes(changers).do() self.assertEquals('def a_func(p2, p3):\n pass\na_func(2, 3)\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 = change_signature.ChangeSignature( self.project, self.mod, self.mod.read().index('__init__') + 1) signature.get_changes([change_signature.ArgumentRemover(1)]).do() self.assertEquals( '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_removing_keyword_arg2(self): self.mod.write( dedent("""\ def a_func(p1, *args, **kwds): pass a_func(1) """) ) signature = change_signature.ChangeSignature( self.project, self.mod, self.mod.read().index("a_func") + 1 ) self.project.do(signature.get_changes([change_signature.ArgumentRemover(2)])) self.assertEqual( dedent("""\ def a_func(p1, *args): pass a_func(1) """), self.mod.read(), )
def xxx_test_removing_arguments_star_args3(self): self.mod.write( dedent("""\ def a_func(p1, *args): pass a_func(*[1, 2, 3]) """) ) signature = change_signature.ChangeSignature( self.project, self.mod, self.mod.read().index("a_func") + 1 ) self.project.do(signature.get_changes([change_signature.ArgumentRemover(1)])) self.assertEqual( dedent("""\ def a_func(p1): pass a_func(*[1, 2, 3]) """), self.mod.read(), )
def test_removing_arguments_with_defaults(self): self.mod.write( dedent("""\ def a_func(p1=1): pass a_func(1) """) ) signature = change_signature.ChangeSignature( self.project, self.mod, self.mod.read().index("a_func") + 1 ) self.project.do(signature.get_changes([change_signature.ArgumentRemover(0)])) self.assertEqual( dedent("""\ def a_func(): pass a_func() """), self.mod.read(), )
def test_changing_signature_for_constructors(self): self.mod.write( dedent("""\ class C(object): def __init__(self, p): pass c = C(1) """) ) signature = change_signature.ChangeSignature( self.project, self.mod, self.mod.read().index("C") + 1 ) signature.get_changes([change_signature.ArgumentRemover(1)]).do() self.assertEqual( dedent("""\ class C(object): def __init__(self): pass c = C() """), self.mod.read(), )
def test_doing_multiple_changes(self): changers = [] self.mod.write( dedent("""\ def a_func(p1): pass a_func(1) """) ) changers.append(change_signature.ArgumentRemover(0)) changers.append(change_signature.ArgumentAdder(0, "p2", None, None)) signature = change_signature.ChangeSignature( self.project, self.mod, self.mod.read().index("a_func") + 1 ) signature.get_changes(changers).do() self.assertEqual( dedent("""\ def a_func(p2): pass a_func() """), self.mod.read(), )
def test_doing_multiple_changes2(self): changers = [] self.mod.write( dedent("""\ def a_func(p1, p2): pass a_func(p2=2) """) ) changers.append(change_signature.ArgumentAdder(2, "p3", None, "3")) changers.append(change_signature.ArgumentReorderer([1, 0, 2])) changers.append(change_signature.ArgumentRemover(1)) signature = change_signature.ChangeSignature( self.project, self.mod, self.mod.read().index("a_func") + 1 ) signature.get_changes(changers).do() self.assertEqual( dedent("""\ def a_func(p2, p3): pass a_func(2, 3) """), self.mod.read(), )
def test_differentiating_class_accesses_from_instance_accesses(self): self.mod.write( dedent("""\ class A(object): def a_func(self, param): pass a_var = A() A.a_func(a_var, param=1)""" ) ) signature = change_signature.ChangeSignature( self.project, self.mod, self.mod.read().index("a_func") + 1 ) self.project.do(signature.get_changes([change_signature.ArgumentRemover(1)])) self.assertEqual( dedent("""\ class A(object): def a_func(self): pass a_var = A() A.a_func(a_var)""" ), self.mod.read() )