usefunctiontest.py :  » Development » Rope » rope-0.9.2 » ropetest » refactor » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Rope 
Rope » rope 0.9.2 » ropetest » refactor » usefunctiontest.py
import unittest

from rope.base import exceptions
from ropetest import testutils
from rope.refactor.usefunction import UseFunction


class UseFunctionTest(unittest.TestCase):

    def setUp(self):
        super(UseFunctionTest, self).setUp()
        self.project = testutils.sample_project()
        self.mod1 = testutils.create_module(self.project, 'mod1')
        self.mod2 = testutils.create_module(self.project, 'mod2')

    def tearDown(self):
        testutils.remove_project(self.project)
        super(UseFunctionTest, self).tearDown()

    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_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())

    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.assertEquals('def f(p):\n    r = p + 1\n    print(r)\nf(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_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_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())

    @testutils.assert_raises(exceptions.RefactoringError)
    def test_when_performing_on_non_functions(self):
        code = 'var = 1\n'
        self.mod1.write(code)
        user = UseFunction(self.project, self.mod1, code.rindex('var'))

    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())

    # TODO: probably new options should be added to restructure
    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())

    @testutils.assert_raises(exceptions.RefactoringError)
    def test_exception_when_performing_a_function_with_yield(self):
        code = 'def func():\n    yield 1\n'
        self.mod1.write(code)
        user = UseFunction(self.project, self.mod1, code.index('func'))

    @testutils.assert_raises(exceptions.RefactoringError)
    def test_exception_when_performing_a_function_two_returns(self):
        code = 'def func():\n    return 1\n    return 2\n'
        self.mod1.write(code)
        user = UseFunction(self.project, self.mod1, code.index('func'))

    @testutils.assert_raises(exceptions.RefactoringError)
    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)
        user = UseFunction(self.project, self.mod1, code.index('func'))


if __name__ == '__main__':
    unittest.main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.