test_fromlist.py :  » 3.1.2-Python » Lib » Lib » importlib » test » import_ » 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 » 3.1.2 Python » Lib 
Lib » Lib » importlib » test » import_ » test_fromlist.py
"""Test that the semantics relating to the 'fromlist' argument are correct."""
from .. import util
from  import util
import unittest

class ReturnValue(unittest.TestCase):

    """The use of fromlist influences what import returns.

    If direct ``import ...`` statement is used, the root module or package is
    returned [import return]. But if fromlist is set, then the specified module
    is actually returned (whether it is a relative import or not)
    [from return].

    """

    def test_return_from_import(self):
        # [import return]
        with util.mock_modules('pkg.__init__', 'pkg.module') as importer:
            with util.import_state(meta_path=[importer]):
                module = import_util.import_('pkg.module')
                self.assertEquals(module.__name__, 'pkg')

    def test_return_from_from_import(self):
        # [from return]
        with util.mock_modules('pkg.__init__', 'pkg.module')as importer:
            with util.import_state(meta_path=[importer]):
                module = import_util.import_('pkg.module', fromlist=['attr'])
                self.assertEquals(module.__name__, 'pkg.module')


class HandlingFromlist(unittest.TestCase):

    """Using fromlist triggers different actions based on what is being asked
    of it.

    If fromlist specifies an object on a module, nothing special happens
    [object case]. This is even true if the object does not exist [bad object].

    If a package is being imported, then what is listed in fromlist may be
    treated as a module to be imported [module]. But once again, even if
    something in fromlist does not exist as a module, no error is thrown
    [no module]. And this extends to what is contained in __all__ when '*' is
    imported [using *]. And '*' does not need to be the only name in the
    fromlist [using * with others].

    """

    def test_object(self):
        # [object case]
        with util.mock_modules('module') as importer:
            with util.import_state(meta_path=[importer]):
                module = import_util.import_('module', fromlist=['attr'])
                self.assertEquals(module.__name__, 'module')

    def test_unexistent_object(self):
        # [bad object]
        with util.mock_modules('module') as importer:
            with util.import_state(meta_path=[importer]):
                module = import_util.import_('module', fromlist=['non_existent'])
                self.assertEquals(module.__name__, 'module')
                self.assertTrue(not hasattr(module, 'non_existent'))

    def test_module_from_package(self):
        # [module]
        with util.mock_modules('pkg.__init__', 'pkg.module') as importer:
            with util.import_state(meta_path=[importer]):
                module = import_util.import_('pkg', fromlist=['module'])
                self.assertEquals(module.__name__, 'pkg')
                self.assertTrue(hasattr(module, 'module'))
                self.assertEquals(module.module.__name__, 'pkg.module')

    def test_no_module_from_package(self):
        # [no module]
        with util.mock_modules('pkg.__init__') as importer:
            with util.import_state(meta_path=[importer]):
                module = import_util.import_('pkg', fromlist='non_existent')
                self.assertEquals(module.__name__, 'pkg')
                self.assertTrue(not hasattr(module, 'non_existent'))

    def test_empty_string(self):
        with util.mock_modules('pkg.__init__', 'pkg.mod') as importer:
            with util.import_state(meta_path=[importer]):
                module = import_util.import_('pkg.mod', fromlist=[''])
                self.assertEquals(module.__name__, 'pkg.mod')

    def test_using_star(self):
        # [using *]
        with util.mock_modules('pkg.__init__', 'pkg.module') as mock:
            with util.import_state(meta_path=[mock]):
                mock['pkg'].__all__ = ['module']
                module = import_util.import_('pkg', fromlist=['*'])
                self.assertEquals(module.__name__, 'pkg')
                self.assertTrue(hasattr(module, 'module'))
                self.assertEqual(module.module.__name__, 'pkg.module')

    def test_star_with_others(self):
        # [using * with others]
        context = util.mock_modules('pkg.__init__', 'pkg.module1', 'pkg.module2')
        with context as mock:
            with util.import_state(meta_path=[mock]):
                mock['pkg'].__all__ = ['module1']
                module = import_util.import_('pkg', fromlist=['module2', '*'])
                self.assertEquals(module.__name__, 'pkg')
                self.assertTrue(hasattr(module, 'module1'))
                self.assertTrue(hasattr(module, 'module2'))
                self.assertEquals(module.module1.__name__, 'pkg.module1')
                self.assertEquals(module.module2.__name__, 'pkg.module2')


def test_main():
    from test.support import run_unittest
    run_unittest(ReturnValue, HandlingFromlist)

if __name__ == '__main__':
    test_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.