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())
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())
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())
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())
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())
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())
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())
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())
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())
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())
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())
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())
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())
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')
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(), )
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)
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"))
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(), )
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(), )
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"))
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(), )
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(), )
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(), )
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'))
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)
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"))
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)
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())
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())
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'))
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())
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())