예제 #1
0
 def test_occurrences_in_other_modules(self):
     code = "def f(p):\n    return p + 1\n"
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex("f"))
     self.mod2.write("print(2 + 1)\n")
     self.project.do(user.get_changes())
     self.assertEquals("import mod1\nprint(mod1.f(2))\n", self.mod2.read())
예제 #2
0
 def test_simple_function2(self):
     code = 'def f(p):\n    print(p + 1)\nprint(1 + 1)\n'
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex('f'))
     self.project.do(user.get_changes())
     self.assertEquals('def f(p):\n    print(p + 1)\nf(1)\n',
                       self.mod1.read())
예제 #3
0
 def test_functions_with_multiple_statements(self):
     code = 'def f(p):\n    r = p + 1\n    print(r)\nr = 2 + 1\nprint(r)\n'
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex('f'))
     self.project.do(user.get_changes())
     self.assertEqual('def f(p):\n    r = p + 1\n    print(r)\nf(2)\n',
                      self.mod1.read())
예제 #4
0
 def test_returning_a_single_expression(self):
     code = 'def f(p):\n    return p + 1\nprint(2 + 1)\n'
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex('f'))
     self.project.do(user.get_changes())
     self.assertEqual('def f(p):\n    return p + 1\nprint(f(2))\n',
                      self.mod1.read())
예제 #5
0
 def test_differing_in_the_inner_temp_names(self):
     code = 'def f(p):\n    a = p + 1\n    print(a)\nb = 2 + 1\nprint(b)\n'
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex('f'))
     self.project.do(user.get_changes())
     self.assertEquals('def f(p):\n    a = p + 1\n    print(a)\nf(2)\n',
                       self.mod1.read())
예제 #6
0
 def test_simple_function2(self):
     code = 'def f(p):\n    print(p + 1)\nprint(1 + 1)\n'
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex('f'))
     self.project.do(user.get_changes())
     self.assertEqual('def f(p):\n    print(p + 1)\nf(1)\n',
                      self.mod1.read())
예제 #7
0
 def test_occurrences_in_other_modules(self):
     code = 'def f(p):\n    return p + 1\n'
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex('f'))
     self.mod2.write('print(2 + 1)\n')
     self.project.do(user.get_changes())
     self.assertEqual('import mod1\nprint(mod1.f(2))\n', self.mod2.read())
예제 #8
0
 def test_differing_in_the_inner_temp_names(self):
     code = 'def f(p):\n    a = p + 1\n    print(a)\nb = 2 + 1\nprint(b)\n'
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex('f'))
     self.project.do(user.get_changes())
     self.assertEqual('def f(p):\n    a = p + 1\n    print(a)\nf(2)\n',
                      self.mod1.read())
예제 #9
0
 def test_functions_with_multiple_statements(self):
     code = 'def f(p):\n    r = p + 1\n    print(r)\nr = 2 + 1\nprint(r)\n'
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex('f'))
     self.project.do(user.get_changes())
     self.assertEquals('def f(p):\n    r = p + 1\n    print(r)\nf(2)\n',
                       self.mod1.read())
예제 #10
0
 def xxx_test_being_a_bit_more_intelligent_when_returning_assigneds(self):
     code = 'def f(p):\n    a = p + 1\n    return a\n'\
            'var = 2 + 1\nprint(var)\n'
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex('f'))
     self.project.do(user.get_changes())
     self.assertEquals('def f(p):\n    a = p + 1\n    return a\n'
                       'var = f(p)\nprint(var)\n', self.mod1.read())
예제 #11
0
 def test_returning_a_single_expression(self):
     code = 'def f(p):\n    return p + 1\nprint(2 + 1)\n'
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex('f'))
     self.project.do(user.get_changes())
     self.assertEquals(
         'def f(p):\n    return p + 1\nprint(f(2))\n',
         self.mod1.read())
예제 #12
0
 def test_returning(self):
     code = 'def f(p):\n    return p + 1\nr = 2 + 1\nprint(r)\n'
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex('f'))
     self.project.do(user.get_changes())
     self.assertEquals(
         'def f(p):\n    return p + 1\nr = f(2)\nprint(r)\n',
         self.mod1.read())
예제 #13
0
 def xxx_test_being_a_bit_more_intelligent_when_returning_assigneds(self):
     code = 'def f(p):\n    a = p + 1\n    return a\n'\
            'var = 2 + 1\nprint(var)\n'
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex('f'))
     self.project.do(user.get_changes())
     self.assertEquals('def f(p):\n    a = p + 1\n    return a\n'
                       'var = f(p)\nprint(var)\n', self.mod1.read())
예제 #14
0
 def test_simple_case(self):
     code = dedent("""\
         def f():
             pass
     """)
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex("f"))
     self.project.do(user.get_changes())
     self.assertEqual(code, self.mod1.read())
예제 #15
0
 def onRefactor(self):
     renamed = UseFunction(
         self.project, self.resource, self._startOffset)
     changes = renamed.get_changes([renamed.resource], task_handle=self._handle)
     for item in changes.changes:
         if isinstance(item, rope.base.change.ChangeContents):
             self.changes.append(
                 Change(item.resource.real_path, ChangeType.EDIT, get_diff(item)))
         else:
             raise Exception('Unknown Change')
예제 #16
0
 def test_occurrences_in_other_modules(self):
     code = dedent("""\
         def f(p):
             return p + 1
     """)
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex("f"))
     self.mod2.write("print(2 + 1)\n")
     self.project.do(user.get_changes())
     self.assertEqual(
         dedent("""\
             import mod1
             print(mod1.f(2))
         """),
         self.mod2.read(),
     )
예제 #17
0
 def refactor_use_function(self, offset):
     """Use the function at point wherever possible."""
     try:
         refactor = UseFunction(self.project, self.resource, offset)
     except RefactoringError as e:
         raise Fault('Refactoring error: {}'.format(e), code=400)
     return self._get_changes(refactor)
예제 #18
0
 def test_exception_when_performing_a_function_with_yield(self):
     code = dedent("""\
         def func():
             yield 1
     """)
     self.mod1.write(code)
     with self.assertRaises(exceptions.RefactoringError):
         UseFunction(self.project, self.mod1, code.index("func"))
예제 #19
0
 def test_simple_function2(self):
     code = dedent("""\
         def f(p):
             print(p + 1)
         print(1 + 1)
     """)
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex("f"))
     self.project.do(user.get_changes())
     self.assertEqual(
         dedent("""\
             def f(p):
                 print(p + 1)
             f(1)
         """),
         self.mod1.read(),
     )
예제 #20
0
 def test_returning_a_single_expression(self):
     code = dedent("""\
         def f(p):
             return p + 1
         print(2 + 1)
     """)
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex("f"))
     self.project.do(user.get_changes())
     self.assertEqual(
         dedent("""\
             def f(p):
                 return p + 1
             print(f(2))
         """),
         self.mod1.read(),
     )
예제 #21
0
 def test_exception_when_returns_is_not_the_last_statement(self):
     code = dedent("""\
         def func():
             return 2
             a = 1
     """)
     self.mod1.write(code)
     with self.assertRaises(exceptions.RefactoringError):
         UseFunction(self.project, self.mod1, code.index("func"))
예제 #22
0
 def test_differing_in_the_inner_temp_names(self):
     code = dedent("""\
         def f(p):
             a = p + 1
             print(a)
         b = 2 + 1
         print(b)
     """)
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex("f"))
     self.project.do(user.get_changes())
     self.assertEqual(
         dedent("""\
             def f(p):
                 a = p + 1
                 print(a)
             f(2)
         """),
         self.mod1.read(),
     )
예제 #23
0
 def test_functions_with_multiple_statements(self):
     code = dedent("""\
         def f(p):
             r = p + 1
             print(r)
         r = 2 + 1
         print(r)
     """)
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex("f"))
     self.project.do(user.get_changes())
     self.assertEqual(
         dedent("""\
             def f(p):
                 r = p + 1
                 print(r)
             f(2)
         """),
         self.mod1.read(),
     )
예제 #24
0
 def xxx_test_being_a_bit_more_intelligent_when_returning_assigneds(self):
     code = dedent("""\
         def f(p):
             a = p + 1
             return a
         var = 2 + 1
         print(var)
     """)
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex("f"))
     self.project.do(user.get_changes())
     self.assertEqual(
         dedent("""\
             def f(p):
                 a = p + 1
                 return a
             var = f(p)
             print(var)
         """),
         self.mod1.read(),
     )
예제 #25
0
 def test_exception_when_performing_a_function_two_returns(self):
     code = 'def func():\n    return 1\n    return 2\n'
     self.mod1.write(code)
     with self.assertRaises(exceptions.RefactoringError):
         UseFunction(self.project, self.mod1, code.index('func'))
예제 #26
0
 def refactor_use_function(self, offset):
     """Use the function at point wherever possible."""
     refactor = UseFunction(self.project, self.resource, offset)
     changes = refactor.get_changes()
     return translate_changes(changes)
예제 #27
0
 def test_when_performing_on_non_functions(self):
     code = "var = 1\n"
     self.mod1.write(code)
     with self.assertRaises(exceptions.RefactoringError):
         UseFunction(self.project, self.mod1, code.rindex("var"))
예제 #28
0
 def refactor_use_function(self, offset):
     """Use the function at point wherever possible."""
     refactor = UseFunction(self.project, self.resource, offset)
     return self._get_changes(refactor)
예제 #29
0
 def test_simple_case(self):
     code = 'def f():\n    pass\n'
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex('f'))
     self.project.do(user.get_changes())
     self.assertEquals(code, self.mod1.read())
예제 #30
0
 def test_returning(self):
     code = "def f(p):\n    return p + 1\nr = 2 + 1\nprint(r)\n"
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex("f"))
     self.project.do(user.get_changes())
     self.assertEquals("def f(p):\n    return p + 1\nr = f(2)\nprint(r)\n", self.mod1.read())
예제 #31
0
 def test_exception_when_returns_is_not_the_last_statement(self):
     code = 'def func():\n    return 2\n    a = 1\n'
     self.mod1.write(code)
     with self.assertRaises(exceptions.RefactoringError):
         UseFunction(self.project, self.mod1, code.index('func'))
예제 #32
0
 def test_simple_case(self):
     code = 'def f():\n    pass\n'
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex('f'))
     self.project.do(user.get_changes())
     self.assertEqual(code, self.mod1.read())
예제 #33
0
파일: refactor.py 프로젝트: exedre/elpy
 def refactor_use_function(self, offset):
     """Use the function at point wherever possible."""
     refactor = UseFunction(self.project, self.resource, offset)
     changes = refactor.get_changes()
     return translate_changes(changes)
예제 #34
0
 def test_simple_function(self):
     code = "def f(p):\n    print(p)\nprint(1)\n"
     self.mod1.write(code)
     user = UseFunction(self.project, self.mod1, code.rindex("f"))
     self.project.do(user.get_changes())
     self.assertEquals("def f(p):\n    print(p)\nf(1)\n", self.mod1.read())