Exemplo n.º 1
0
 def test_packaged_package_creation_with_nested_src(self):
     src = self.project.root.create_folder('src')
     testutils.create_package(self.project, 'pkg1', src)
     nested_package = testutils.create_package(self.project, 'pkg1.pkg2',
                                               src)
     self.assertEquals(self.project.get_resource('src/pkg1/pkg2'),
                       nested_package)
Exemplo n.º 2
0
 def test_get_definition_location_for_packages(self):
     testutils.create_package(self.project, 'pkg')
     init_module = self.project.get_module('pkg.__init__')
     mod = libutils.get_string_module(self.project, 'import pkg\n')
     pkg_pyname = mod['pkg']
     self.assertEquals((init_module, 1),
                       pkg_pyname.get_definition_location())
Exemplo n.º 3
0
 def xxx_test_from_package_import_package(self):
     pkg1 = testutils.create_package(self.project, 'pkg1')
     pkg2 = testutils.create_package(self.project, 'pkg2', pkg1)
     module = testutils.create_module(self.project, 'mod', pkg2)
     mod = self.pycore.get_string_module('from pkg1 import pkg2\n')
     package = mod['pkg2']
     self.assertEquals(0, len(package.get_attributes()))
Exemplo n.º 4
0
 def test_moving_modules_normal_import(self):
     pkg2 = testutils.create_package(self.project, "pkg2")
     pkg3 = testutils.create_package(self.project, "pkg3", pkg2)
     pkg4 = testutils.create_package(self.project, "pkg4", pkg3)
     code = "import pkg.mod4\n" "print(pkg.mod4)"
     self.mod1.write(code)
     self._move(self.mod4, None, pkg4)
     self.assertTrue(self.project.find_module("pkg2.pkg3.pkg4.mod4") is not None)
     expected = "import pkg2.pkg3.pkg4.mod4\n" "print(pkg2.pkg3.pkg4.mod4)"
     self.assertEquals(expected, self.mod1.read())
Exemplo n.º 5
0
 def test_source_folders_preference(self):
     testutils.create_package(self.project, 'pkg1')
     testutils.create_package(self.project, 'pkg1.src2')
     lost = testutils.create_module(self.project, 'pkg1.src2.lost')
     self.assertEqual(self.project.find_module('lost'), None)
     self.project.close()
     from rope.base.project import Project
     self.project = Project(self.project.address,
                            source_folders=['pkg1/src2'])
     self.assertEqual(self.project.find_module('lost'), lost)
Exemplo n.º 6
0
    def test_get_definition_location_for_nested_packages(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        pkg1 = testutils.create_package(self.project, 'pkg1')
        pkg2 = testutils.create_package(self.project, 'pkg2', pkg1)
        mod1.write('import pkg1.pkg2.mod2')

        init_dot_py = pkg2.get_child('__init__.py')
        found_pyname = get_definition_location(self.project, mod1.read(),
                                               mod1.read().index('pkg2') + 1)
        self.assertEquals(init_dot_py, found_pyname[0])
Exemplo n.º 7
0
 def test_moving_modules_with_multi_from_imports(self):
     pkg2 = testutils.create_package(self.project, "pkg2")
     pkg3 = testutils.create_package(self.project, "pkg3", pkg2)
     pkg4 = testutils.create_package(self.project, "pkg4", pkg3)
     code = "from pkg import mod4, mod5\n" "print(mod4)"
     self.mod1.write(code)
     self._move(self.mod4, None, pkg4)
     self.assertTrue(self.project.find_module("pkg2.pkg3.pkg4.mod4") is not None)
     expected = "from pkg import mod5\n" "from pkg2.pkg3.pkg4 import mod4\n" "print(mod4)"
     self.assertEquals(expected, self.mod1.read())
Exemplo n.º 8
0
 def test_multi_dot_imports3(self):
     pkg1 = testutils.create_package(self.project, 'pkg1')
     pkg2 = testutils.create_package(self.project, 'pkg2', pkg1)
     mod1 = testutils.create_module(self.project, 'mod1', pkg2)
     mod2 = testutils.create_module(self.project, 'mod2', pkg2)
     mod = self.pycore.get_string_module('import pkg1.pkg2.mod1\nimport pkg1.pkg2.mod2\n')
     package1 = mod['pkg1'].get_object()
     package2 = package1['pkg2'].get_object()
     self.assertEquals(2, len(package2.get_attributes()))
     self.assertTrue('mod1' in package2 and
                     'mod2' in package2)
Exemplo n.º 9
0
 def test_invalidating_cache_after_resource_change_for_nested_init_dot_pys(self):
     pkg1 = testutils.create_package(self.project, 'pkg1')
     pkg2 = testutils.create_package(self.project, 'pkg2', pkg1)
     mod = testutils.create_module(self.project, 'mod')
     init_dot_py = pkg2.get_child('__init__.py')
     init_dot_py.write('a_var = 10\n')
     mod.write('import pkg1\n')
     pymod = self.pycore.get_module('mod')
     self.assertTrue('a_var' in pymod['pkg1'].get_object()['pkg2'].get_object())
     init_dot_py.write('new_var = 10\n')
     self.assertTrue('a_var' not in pymod['pkg1'].get_object()['pkg2'].get_object())
Exemplo n.º 10
0
    def test_relative_modules_after_from_statements2(self):
        mod1 = testutils.create_module(self.project, 'mod1')
        pkg1 = testutils.create_package(self.project, 'pkg1')
        pkg2 = testutils.create_package(self.project, 'pkg2', pkg1)
        mod2 = testutils.create_module(self.project, 'mod2', pkg2)  # noqa
        mod1.write('import pkg1.pkg2.mod2')

        mod1_scope = self.project.get_pymodule(mod1).get_scope()
        name_finder = rope.base.evaluate.ScopeNameFinder(mod1_scope.pyobject)
        pkg2_pyobject = self.project.get_pymodule(pkg2)
        found_pyname = name_finder.get_pyname_at(mod1.read().index('pkg2') + 1)
        self.assertEquals(pkg2_pyobject, found_pyname.get_object())
Exemplo n.º 11
0
 def test_relative_modules_after_from_statements(self):
     pkg1 = testutils.create_package(self.project, 'pkg1')
     pkg2 = testutils.create_package(self.project, 'pkg2', pkg1)
     mod1 = testutils.create_module(self.project, 'mod1', pkg1)
     mod2 = testutils.create_module(self.project, 'mod2', pkg2)
     mod1.write('def a_func():\n    pass\n')
     code = 'from ..mod1 import a_func\n'
     mod2.write(code)
     mod2_scope = self.project.get_pymodule(mod2).get_scope()
     name_finder = rope.base.evaluate.ScopeNameFinder(mod2_scope.pyobject)
     mod1_pyobject = self.project.get_pymodule(mod1)
     found_pyname = name_finder.get_pyname_at(code.index('mod1') + 1)
     self.assertEquals(mod1_pyobject, found_pyname.get_object())
Exemplo n.º 12
0
    def test_get_definition_location_for_nested_packages(self):
        pycore = self.project.pycore
        mod1 = testutils.create_module(self.project, 'mod1')
        pkg1 = testutils.create_package(self.project, 'pkg1')
        pkg2 = testutils.create_package(self.project, 'pkg2', pkg1)
        mod2 = testutils.create_module(self.project, 'mod2', pkg2)
        mod1.write('import pkg1.pkg2.mod2')

        mod1_scope = pycore.resource_to_pyobject(mod1).get_scope()
        init_dot_py = pkg2.get_child('__init__.py')
        found_pyname = get_definition_location(self.project, mod1.read(),
                                               mod1.read().index('pkg2') + 1)
        self.assertEqual(init_dot_py, found_pyname[0])
Exemplo n.º 13
0
 def test_moving_modules_normal_import(self):
     pkg2 = testutils.create_package(self.project, 'pkg2')
     pkg3 = testutils.create_package(self.project, 'pkg3', pkg2)
     pkg4 = testutils.create_package(self.project, 'pkg4', pkg3)
     code = ('import pkg.mod4\n'
             'print(pkg.mod4)')
     self.mod1.write(code)
     self._move(self.mod4, None, pkg4)
     self.assertTrue(
         self.project.find_module('pkg2.pkg3.pkg4.mod4') is not None)
     expected = ('import pkg2.pkg3.pkg4.mod4\n'
                 'print(pkg2.pkg3.pkg4.mod4)')
     self.assertEquals(expected, self.mod1.read())
Exemplo n.º 14
0
 def test_moving_modules_with_multi_from_imports(self):
     pkg2 = testutils.create_package(self.project, 'pkg2')
     pkg3 = testutils.create_package(self.project, 'pkg3', pkg2)
     pkg4 = testutils.create_package(self.project, 'pkg4', pkg3)
     code = ('from pkg import mod4, mod5\n'
             'print(mod4)')
     self.mod1.write(code)
     self._move(self.mod4, None, pkg4)
     self.assertTrue(
         self.project.find_module('pkg2.pkg3.pkg4.mod4') is not None)
     expected = ('from pkg import mod5\n'
                 'from pkg2.pkg3.pkg4 import mod4\n'
                 'print(mod4)')
     self.assertEquals(expected, self.mod1.read())
Exemplo n.º 15
0
 def setUp(self):
     super(AutoImportTest, self).setUp()
     self.project = testutils.sample_project(extension_modules=["sys"])
     self.mod1 = testutils.create_module(self.project, "mod1")
     self.pkg = testutils.create_package(self.project, "pkg")
     self.mod2 = testutils.create_module(self.project, "mod2", self.pkg)
     self.importer = autoimport.AutoImport(self.project, observe=False)
Exemplo n.º 16
0
 def setUp(self):
     super(AutoImportObservingTest, self).setUp()
     self.project = testutils.sample_project()
     self.mod1 = testutils.create_module(self.project, "mod1")
     self.pkg = testutils.create_package(self.project, "pkg")
     self.mod2 = testutils.create_module(self.project, "mod2", self.pkg)
     self.importer = autoimport.AutoImport(self.project, observe=True)
Exemplo n.º 17
0
    def setUp(self):
        super(ImportUtilsTest, self).setUp()
        self.project = testutils.sample_project()
        self.import_tools = ImportTools(self.project)

        self.mod = testutils.create_module(self.project, "mod")
        self.pkg1 = testutils.create_package(self.project, "pkg1")
        self.mod1 = testutils.create_module(self.project, "mod1", self.pkg1)
        self.pkg2 = testutils.create_package(self.project, "pkg2")
        self.mod2 = testutils.create_module(self.project, "mod2", self.pkg2)
        self.mod3 = testutils.create_module(self.project, "mod3", self.pkg2)
        p1 = testutils.create_package(self.project, "p1")
        p2 = testutils.create_package(self.project, "p2", p1)
        p3 = testutils.create_package(self.project, "p3", p2)
        m1 = testutils.create_module(self.project, "m1", p3)  # noqa
        l = testutils.create_module(self.project, "l", p3)  # noqa
Exemplo n.º 18
0
 def test_renam_resources_use_rename_module_refactor_for_init_dot_py(self):
     mod1 = testutils.create_module(self.project, 'mod1')
     pkg = testutils.create_package(self.project, 'pkg')
     mod1.write('import pkg\nmy_pkg = pkg')
     renamer = rename.Rename(self.project, pkg.get_child('__init__.py'))
     renamer.get_changes('newpkg').do()
     self.assertEquals('import newpkg\nmy_pkg = newpkg', mod1.read())
Exemplo n.º 19
0
 def test_find_nested_module(self):
     src = self.project.root.create_folder('src')
     samplepkg = testutils.create_package(self.project, 'samplepkg', src)
     samplemod = testutils.create_module(self.project, 'samplemod',
                                         samplepkg)
     found_module = self.project.find_module('samplepkg.samplemod')
     self.assertEquals(samplemod, found_module)
Exemplo n.º 20
0
 def test_renaming_resources_using_rename_module_refactoring_for_packages(self):
     mod1 = testutils.create_module(self.project, 'mod1')
     pkg = testutils.create_package(self.project, 'pkg')
     mod1.write('import pkg\nmy_pkg = pkg')
     renamer = rename.Rename(self.project, pkg)
     renamer.get_changes('newpkg').do()
     self.assertEquals('import newpkg\nmy_pkg = newpkg', mod1.read())
Exemplo n.º 21
0
 def setUp(self):
     super(AutoImportTest, self).setUp()
     self.project = testutils.sample_project()
     self.mod1 = testutils.create_module(self.project, 'mod1')
     self.pkg = testutils.create_package(self.project, 'pkg')
     self.mod2 = testutils.create_module(self.project, 'mod2', self.pkg)
     self.importer = autoimport.AutoImport(self.project, observe=False)
Exemplo n.º 22
0
 def test_multi_dot_imports_as(self):
     pkg = testutils.create_package(self.project, 'pkg')
     mod1 = testutils.create_module(self.project, 'mod1', pkg)
     mod1.write('def f():\n    pass\n')
     mod = self.pycore.get_string_module('import pkg.mod1 as mod1\n')
     module = mod['mod1'].get_object()
     self.assertTrue('f' in module)
Exemplo n.º 23
0
 def test_get_definition_location_for_filtered_packages(self):
     pkg = testutils.create_package(self.project, 'pkg')
     testutils.create_module(self.project, 'mod', pkg)
     init_module = self.pycore.get_module('pkg.__init__')
     mod = self.pycore.get_string_module('import pkg.mod')
     pkg_pyname = mod['pkg']
     self.assertEquals((init_module, 1), pkg_pyname.get_definition_location())
Exemplo n.º 24
0
 def test_moving_package_and_retaining_blank_lines(self):
     pkg2 = testutils.create_package(self.project, "pkg2", self.pkg)
     code = (
         "import pkg.mod4\n\n"
         "from pkg import mod4\n"
         "from x import y\n"
         "from y import z\n"
         "from a import b\n"
         "from b import c\n"
         "print(pkg.mod4)\n"
         "print(mod4)"
     )
     self.mod1.write(code)
     self._move(self.mod4, None, pkg2)
     expected = (
         "import pkg.pkg2.mod4\n\n"
         "from x import y\n"
         "from y import z\n"
         "from a import b\n"
         "from b import c\n"
         "from pkg.pkg2 import mod4\n"
         "print(pkg.pkg2.mod4)\n"
         "print(mod4)"
     )
     self.assertEquals(expected, self.mod1.read())
Exemplo n.º 25
0
 def setUp(self):
     super(GenerateTest, self).setUp()
     self.project = testutils.sample_project()
     self.pycore = self.project.get_pycore()
     self.mod = testutils.create_module(self.project, 'mod1')
     self.mod2 = testutils.create_module(self.project, 'mod2')
     self.pkg = testutils.create_package(self.project, 'pkg')
Exemplo n.º 26
0
 def test_nested_modules(self):
     pkg = testutils.create_package(self.project, 'pkg')
     mod = testutils.create_module(self.project, 'mod', pkg)
     imported_module = self.pycore.get_module('pkg.mod')
     scope = self.pycore.get_string_scope('import pkg.mod\n')
     mod_pyobject = scope['pkg'].get_object()['mod']
     self.assertEquals((imported_module, 1),
                       mod_pyobject.get_definition_location())
Exemplo n.º 27
0
 def test_relative_imports_for_string_scopes(self):
     pkg = testutils.create_package(self.project, 'pkg')
     mod1 = testutils.create_module(self.project, 'mod1', pkg)
     mod2 = testutils.create_module(self.project, 'mod2', pkg)
     mod2.write('import mod1\n')
     mod1_object = self.pycore.resource_to_pyobject(mod1)
     mod2_scope = self.pycore.get_string_scope(mod2.read(), mod2)
     self.assertEquals(mod1_object, mod2_scope['mod1'].get_object())
Exemplo n.º 28
0
 def test_new_style_relative_imports(self):
     pkg = testutils.create_package(self.project, 'pkg')
     mod1 = testutils.create_module(self.project, 'mod1', pkg)
     mod2 = testutils.create_module(self.project, 'mod2', pkg)
     mod2.write('from . import mod1\n')
     mod1_object = self.pycore.resource_to_pyobject(mod1)
     mod2_object = self.pycore.resource_to_pyobject(mod2)
     self.assertEquals(mod1_object, mod2_object['mod1'].get_object())
Exemplo n.º 29
0
 def test_assist_on_relative_imports(self):
     pkg = testutils.create_package(self.project, 'pkg')
     mod1 = testutils.create_module(self.project, 'mod1', pkg)
     mod2 = testutils.create_module(self.project, 'mod2', pkg)
     mod1.write('def a_func():\n    pass\n')
     code = 'import mod1\nmod1.'
     result = self._assist(code, resource=mod2)
     self.assert_completion_in_result('a_func', 'attribute', result)
Exemplo n.º 30
0
 def test_get_doc_on_relative_imports(self):
     pkg = testutils.create_package(self.project, 'pkg')
     mod1 = testutils.create_module(self.project, 'mod1', pkg)
     mod2 = testutils.create_module(self.project, 'mod2', pkg)
     mod1.write('def a_func():\n    """hey"""\n    pass\n')
     code = 'import mod1\nmod1.a_func\n'
     result = get_doc(self.project, code, len(code) - 2, mod2)
     self.assertTrue(result.endswith('hey'))
Exemplo n.º 31
0
 def setUp(self):
     super(MoveRefactoringTest, self).setUp()
     self.project = testutils.sample_project()
     self.mod1 = testutils.create_module(self.project, 'mod1')
     self.mod2 = testutils.create_module(self.project, 'mod2')
     self.mod3 = testutils.create_module(self.project, 'mod3')
     self.pkg = testutils.create_package(self.project, 'pkg')
     self.mod4 = testutils.create_module(self.project, 'mod4', self.pkg)
     self.mod5 = testutils.create_module(self.project, 'mod5', self.pkg)
Exemplo n.º 32
0
 def test_relative_imports_for_string_modules(self):
     pkg = testutils.create_package(self.project, 'pkg')
     mod1 = testutils.create_module(self.project, 'mod1', pkg)
     mod2 = testutils.create_module(self.project, 'mod2', pkg)
     mod2.write('import mod1\n')
     mod1_object = self.pycore.resource_to_pyobject(mod1)
     mod2_object = libutils.get_string_module(self.project, mod2.read(),
                                              mod2)
     self.assertEquals(mod1_object, mod2_object['mod1'].get_object())
Exemplo n.º 33
0
 def test_multi_dot_imports2(self):
     pkg = testutils.create_package(self.project, 'pkg')
     mod1 = testutils.create_module(self.project, 'mod1', pkg)
     mod2 = testutils.create_module(self.project, 'mod2', pkg)
     mod = self.pycore.get_string_module('import pkg.mod1\nimport pkg.mod2\n')
     package = mod['pkg'].get_object()
     self.assertEquals(2, len(package.get_attributes()))
     self.assertTrue('mod1' in package and
                     'mod2' in package)
Exemplo n.º 34
0
 def test_moving_resources_using_move_module_for_init_dot_py(self):
     self.mod1.write('import pkg\nmy_pkg = pkg')
     pkg2 = testutils.create_package(self.project, 'pkg2')
     init = self.pkg.get_child('__init__.py')
     mover = move.create_move(self.project, init)
     mover.get_changes(pkg2).do()
     self.assertEquals('import pkg2.pkg\nmy_pkg = pkg2.pkg',
                       self.mod1.read())
     self.assertTrue(pkg2.get_child('pkg') is not None)
Exemplo n.º 35
0
 def test_transform_module_to_package_undoing(self):
     pkg = testutils.create_package(self.project, 'pkg')
     mod = testutils.create_module(self.project, 'mod', pkg)
     self._transform_module_to_package(mod)
     self.assertFalse(pkg.has_child('mod.py'))
     self.assertTrue(pkg.get_child('mod').has_child('__init__.py'))
     self.project.history.undo()
     self.assertTrue(pkg.has_child('mod.py'))
     self.assertFalse(pkg.has_child('mod'))
Exemplo n.º 36
0
 def test_get_location_on_relative_imports(self):
     pkg = testutils.create_package(self.project, 'pkg')
     mod1 = testutils.create_module(self.project, 'mod1', pkg)
     mod2 = testutils.create_module(self.project, 'mod2', pkg)
     mod1.write('def a_func():\n    pass\n')
     code = 'import mod1\nmod1.a_func\n'
     result = get_definition_location(self.project, code,
                                      len(code) - 2, mod2)
     self.assertEquals((mod1, 1), result)
Exemplo n.º 37
0
 def test_renaming_variables_in_init_dot_pys3(self):
     pkg = testutils.create_package(self.project, 'pkg')
     init_dot_py = pkg.get_child('__init__.py')
     init_dot_py.write('a_var = 10\n')
     mod = testutils.create_module(self.project, 'mod')
     mod.write('import pkg\nprint(pkg.a_var)\n')
     self._rename(mod, mod.read().index('a_var') + 1, 'new_var')
     self.assertEqual('new_var = 10\n', init_dot_py.read())
     self.assertEqual('import pkg\nprint(pkg.new_var)\n', mod.read())
Exemplo n.º 38
0
 def test_transform_module_to_package_undoing(self):
     pkg = testutils.create_package(self.project, "pkg")
     mod = testutils.create_module(self.project, "mod", pkg)
     self._transform_module_to_package(mod)
     self.assertFalse(pkg.has_child("mod.py"))
     self.assertTrue(pkg.get_child("mod").has_child("__init__.py"))
     self.project.history.undo()
     self.assertTrue(pkg.has_child("mod.py"))
     self.assertFalse(pkg.has_child("mod"))
Exemplo n.º 39
0
 def test_multi_source_folders2(self):
     testutils.create_module(self.project, 'mod1')
     src = self.project.root.create_folder('src')
     package = testutils.create_package(self.project, 'package', src)
     testutils.create_module(self.project, 'mod2', package)
     source_folders = self.project.get_source_folders()
     self.assertEqual(2, len(source_folders))
     self.assertTrue(self.project.root in source_folders and
                     src in source_folders)
Exemplo n.º 40
0
 def test_moving_modules_with_normal_and_from_imports(self):
     pkg2 = testutils.create_package(self.project, "pkg2")
     pkg3 = testutils.create_package(self.project, "pkg3", pkg2)
     pkg4 = testutils.create_package(self.project, "pkg4", pkg3)
     code = dedent("""\
         import pkg.mod4
         from pkg import mod4
         print(mod4)
         print(pkg.mod4)""")
     self.mod1.write(code)
     self._move(self.mod4, None, pkg4)
     self.assertTrue(
         self.project.find_module("pkg2.pkg3.pkg4.mod4") is not None)
     expected = dedent("""\
         import pkg2.pkg3.pkg4.mod4
         from pkg2.pkg3.pkg4 import mod4
         print(mod4)
         print(pkg2.pkg3.pkg4.mod4)""")
     self.assertEqual(expected, self.mod1.read())
Exemplo n.º 41
0
 def test_handling_relative_imports_when_inlining(self):
     pkg = testutils.create_package(self.project, 'pkg')
     mod3 = testutils.create_module(self.project, 'mod3', pkg)
     mod4 = testutils.create_module(self.project, 'mod4', pkg)
     mod4.write('var = 1\n')
     mod3.write('from . import mod4\n\ndef f():\n    print(mod4.var)\n')
     self.mod.write('import pkg.mod3\n\npkg.mod3.f()\n')
     self._inline2(self.mod, self.mod.read().index('f(') + 1)
     # Cannot determine the exact import
     self.assertTrue('\n\nprint(mod4.var)\n' in self.mod.read())
Exemplo n.º 42
0
 def test_renaming_packages(self):
     pkg = testutils.create_package(self.project, 'pkg')
     mod1 = testutils.create_module(self.project, 'mod1', pkg)
     mod1.write('def a_func():\n    pass\n')
     mod2 = testutils.create_module(self.project, 'mod2', pkg)
     mod2.write('from pkg.mod1 import a_func\n')
     self._rename(mod2, 6, 'newpkg')
     self.assertTrue(self.project.find_module('newpkg.mod1') is not None)
     new_mod2 = self.project.find_module('newpkg.mod2')
     self.assertEquals('from newpkg.mod1 import a_func\n', new_mod2.read())
Exemplo n.º 43
0
 def test_new_style_relative_imports2(self):
     pkg = testutils.create_package(self.project, 'pkg')
     mod1 = testutils.create_module(self.project, 'mod1')
     mod2 = testutils.create_module(self.project, 'mod2', pkg)
     mod1.write('def a_func():\n    pass\n')
     mod2.write('from ..mod1 import a_func\n')
     mod1_object = self.pycore.resource_to_pyobject(mod1)
     mod2_object = self.pycore.resource_to_pyobject(mod2)
     self.assertEquals(mod1_object['a_func'].get_object(),
                       mod2_object['a_func'].get_object())
Exemplo n.º 44
0
 def test_moving_modules_with_from_imports(self):
     pkg2 = testutils.create_package(self.project, 'pkg2')
     code = ('from pkg import mod4\n' 'print(mod4)')
     self.mod1.write(code)
     self._move(self.mod1, code.index('pkg') + 1, pkg2)
     self.assertFalse(self.pkg.exists())
     self.assertTrue(self.project.find_module('pkg2.pkg.mod4') is not None)
     self.assertTrue(self.project.find_module('pkg2.pkg.mod5') is not None)
     expected = ('from pkg2.pkg import mod4\n' 'print(mod4)')
     self.assertEquals(expected, self.mod1.read())
Exemplo n.º 45
0
 def test_transform_module_to_package_with_relative_imports(self):
     pkg = testutils.create_package(self.project, 'pkg')
     mod1 = testutils.create_module(self.project, 'mod1', pkg)
     mod1.write('import mod2\nfrom mod2 import AClass\n')
     mod2 = testutils.create_module(self.project, 'mod2', pkg)
     mod2.write('class AClass(object):\n    pass\n')
     self._transform_module_to_package(mod1)
     new_init = self.project.get_resource('pkg/mod1/__init__.py')
     self.assertEquals('import pkg.mod2\nfrom pkg.mod2 import AClass\n',
                       new_init.read())
Exemplo n.º 46
0
 def test_transform_module_to_package_with_relative_imports(self):
     pkg = testutils.create_package(self.project, "pkg")
     mod1 = testutils.create_module(self.project, "mod1", pkg)
     mod1.write("import mod2\nfrom mod2 import AClass\n")
     mod2 = testutils.create_module(self.project, "mod2", pkg)
     mod2.write("class AClass(object):\n    pass\n")
     self._transform_module_to_package(mod1)
     new_init = self.project.get_resource("pkg/mod1/__init__.py")
     self.assertEqual("import pkg.mod2\nfrom pkg.mod2 import AClass\n",
                      new_init.read())
Exemplo n.º 47
0
 def test_invalidating_cache_after_resource_change_for_init_dot_pys(self):
     pkg = testutils.create_package(self.project, 'pkg')
     mod = testutils.create_module(self.project, 'mod')
     init_dot_py = pkg.get_child('__init__.py')
     init_dot_py.write('a_var = 10\n')
     mod.write('import pkg\n')
     pymod = self.pycore.get_module('mod')
     self.assertTrue('a_var' in pymod['pkg'].get_object())
     init_dot_py.write('new_var = 10\n')
     self.assertTrue('a_var' not in pymod['pkg'].get_object().get_attributes())
Exemplo n.º 48
0
 def test_moving_packages(self):
     pkg2 = testutils.create_package(self.project, 'pkg2')
     code = 'import pkg.mod4\nprint(pkg.mod4)'
     self.mod1.write(code)
     self._move(self.mod1, code.index('pkg') + 1, pkg2)
     self.assertFalse(self.pkg.exists())
     self.assertTrue(self.pycore.find_module('pkg2.pkg.mod4') is not None)
     self.assertTrue(self.pycore.find_module('pkg2.pkg.mod4') is not None)
     self.assertTrue(self.pycore.find_module('pkg2.pkg.mod5') is not None)
     expected = 'import pkg2.pkg.mod4\nprint(pkg2.pkg.mod4)'
     self.assertEquals(expected, self.mod1.read())
Exemplo n.º 49
0
 def test_moving_packages(self):
     pkg2 = testutils.create_package(self.project, "pkg2")
     code = "import pkg.mod4\nprint(pkg.mod4)"
     self.mod1.write(code)
     self._move(self.mod1, code.index("pkg") + 1, pkg2)
     self.assertFalse(self.pkg.exists())
     self.assertTrue(self.project.find_module("pkg2.pkg.mod4") is not None)
     self.assertTrue(self.project.find_module("pkg2.pkg.mod4") is not None)
     self.assertTrue(self.project.find_module("pkg2.pkg.mod5") is not None)
     expected = "import pkg2.pkg.mod4\nprint(pkg2.pkg.mod4)"
     self.assertEqual(expected, self.mod1.read())
Exemplo n.º 50
0
 def test_relative_imports_and_changing_inlining_body(self):
     pkg = testutils.create_package(self.project, 'pkg')
     mod3 = testutils.create_module(self.project, 'mod3', pkg)
     mod4 = testutils.create_module(self.project, 'mod4', pkg)
     mod4.write('var = 1\n')
     mod3.write('import mod4\n\ndef f():\n    print(mod4.var)\n')
     self.mod.write('import pkg.mod3\n\npkg.mod3.f()\n')
     self._inline2(self.mod, self.mod.read().index('f(') + 1)
     self.assertEquals(
         'import pkg.mod3\nimport pkg.mod4\n\nprint(pkg.mod4.var)\n',
         self.mod.read())
Exemplo n.º 51
0
 def setUp(self):
     super(CodeAssistInProjectsTest, self).setUp()
     self.project = testutils.sample_project()
     self.pycore = self.project.pycore
     samplemod = testutils.create_module(self.project, 'samplemod')
     code = 'class SampleClass(object):\n' \
            '    def sample_method():\n        pass\n\n' \
            'def sample_func():\n    pass\n' \
            'sample_var = 10\n\n' \
            'def _underlined_func():\n    pass\n\n'
     samplemod.write(code)
     package = testutils.create_package(self.project, 'package')
     nestedmod = testutils.create_module(self.project, 'nestedmod', package)
Exemplo n.º 52
0
 def test_moving_resources_using_move_module_for_packages(self):
     self.mod1.write(
         dedent("""\
             import pkg
             my_pkg = pkg"""))
     pkg2 = testutils.create_package(self.project, "pkg2")
     mover = move.create_move(self.project, self.pkg)
     mover.get_changes(pkg2).do()
     expected = dedent("""\
         import pkg2.pkg
         my_pkg = pkg2.pkg""")
     self.assertEqual(expected, self.mod1.read())
     self.assertTrue(pkg2.get_child("pkg") is not None)
Exemplo n.º 53
0
 def test_moving_modules_with_from_imports(self):
     pkg2 = testutils.create_package(self.project, "pkg2")
     code = dedent("""\
         from pkg import mod4
         print(mod4)""")
     self.mod1.write(code)
     self._move(self.mod1, code.index("pkg") + 1, pkg2)
     self.assertFalse(self.pkg.exists())
     self.assertTrue(self.project.find_module("pkg2.pkg.mod4") is not None)
     self.assertTrue(self.project.find_module("pkg2.pkg.mod5") is not None)
     expected = dedent("""\
         from pkg2.pkg import mod4
         print(mod4)""")
     self.assertEqual(expected, self.mod1.read())
Exemplo n.º 54
0
 def test_moving_resources_using_move_module_for_init_dot_py(self):
     self.mod1.write(
         dedent("""\
             import pkg
             my_pkg = pkg"""))
     pkg2 = testutils.create_package(self.project, "pkg2")
     init = self.pkg.get_child("__init__.py")
     mover = move.create_move(self.project, init)
     mover.get_changes(pkg2).do()
     self.assertEqual(
         dedent("""\
             import pkg2.pkg
             my_pkg = pkg2.pkg"""),
         self.mod1.read(),
     )
     self.assertTrue(pkg2.get_child("pkg") is not None)
Exemplo n.º 55
0
 def test_moving_package_and_retaining_blank_lines(self):
     pkg2 = testutils.create_package(self.project, 'pkg2', self.pkg)
     code = ('import pkg.mod4\n\n'
             'from pkg import mod4\n'
             'from x import y\n'
             'from y import z\n'
             'from a import b\n'
             'from b import c\n'
             'print(pkg.mod4)\n'
             'print(mod4)')
     self.mod1.write(code)
     self._move(self.mod4, None, pkg2)
     expected = ('import pkg.pkg2.mod4\n\n'
                 'from x import y\n'
                 'from y import z\n'
                 'from a import b\n'
                 'from b import c\n'
                 'from pkg.pkg2 import mod4\n'
                 'print(pkg.pkg2.mod4)\n'
                 'print(mod4)')
     self.assertEquals(expected, self.mod1.read())
Exemplo n.º 56
0
    def test_handling_relative_imports_when_inlining(self):
        pkg = testutils.create_package(self.project, "pkg")
        mod3 = testutils.create_module(self.project, "mod3", pkg)
        mod4 = testutils.create_module(self.project, "mod4", pkg)
        mod4.write("var = 1\n")
        mod3.write(
            dedent("""\
                from . import mod4

                def f():
                    print(mod4.var)
            """))
        self.mod.write(
            dedent("""\
                import pkg.mod3

                pkg.mod3.f()
            """))
        self._inline2(self.mod, self.mod.read().index("f(") + 1)
        # Cannot determine the exact import
        self.assertTrue("\n\nprint(mod4.var)\n" in self.mod.read())
Exemplo n.º 57
0
 def test_reading_init_dot_py(self):
     pkg = testutils.create_package(self.project, 'pkg')
     init_dot_py = pkg.get_child('__init__.py')
     init_dot_py.write('a_var = 1\n')
     pkg_object = self.project.get_module('pkg')
     self.assertTrue('a_var' in pkg_object)
Exemplo n.º 58
0
 def test_package(self):
     pkg = testutils.create_package(self.project, 'pkg')
     mod = testutils.create_module(self.project, 'mod', pkg)  # noqa
     result = self.project.get_module('pkg')
     self.assertEquals(get_base_type('Module'), result.type)
Exemplo n.º 59
0
 def test_find_module_when_module_and_package_with_the_same_name(self):
     src = self.project.root
     testutils.create_module(self.project, 'sample', src)
     samplepkg = testutils.create_package(self.project, 'sample', src)
     found_module = self.project.find_module('sample')
     self.assertEquals(samplepkg, found_module)
Exemplo n.º 60
0
 def test_find_module_packages(self):
     src = self.project.root
     samplepkg = testutils.create_package(self.project, 'samplepkg', src)
     found_module = self.project.find_module('samplepkg')
     self.assertEquals(samplepkg, found_module)