bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
| 
5436.2.1
by Andrew Bennetts
 Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.  | 
1  | 
# Copyright (C) 2010 Canonical Ltd
 | 
2  | 
#
 | 
|
3  | 
# This program is free software; you can redistribute it and/or modify
 | 
|
4  | 
# it under the terms of the GNU General Public License as published by
 | 
|
5  | 
# the Free Software Foundation; either version 2 of the License, or
 | 
|
6  | 
# (at your option) any later version.
 | 
|
7  | 
#
 | 
|
8  | 
# This program is distributed in the hope that it will be useful,
 | 
|
9  | 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|
10  | 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|
11  | 
# GNU General Public License for more details.
 | 
|
12  | 
#
 | 
|
13  | 
# You should have received a copy of the GNU General Public License
 | 
|
14  | 
# along with this program; if not, write to the Free Software
 | 
|
15  | 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 | 
|
16  | 
||
17  | 
"""Tests for bzrlib.pyutils."""
 | 
|
18  | 
||
19  | 
from bzrlib import (  | 
|
20  | 
branch,  | 
|
21  | 
tests,  | 
|
22  | 
    )
 | 
|
23  | 
from bzrlib.pyutils import (  | 
|
24  | 
calc_parent_name,  | 
|
25  | 
get_named_object,  | 
|
26  | 
    )
 | 
|
27  | 
||
28  | 
||
29  | 
class TestGetNamedObject(tests.TestCase):  | 
|
30  | 
"""Tests for get_named_object."""  | 
|
31  | 
||
32  | 
def test_module_only(self):  | 
|
33  | 
import sys  | 
|
34  | 
self.assertIs(sys, get_named_object('sys'))  | 
|
35  | 
||
36  | 
def test_dotted_module(self):  | 
|
37  | 
self.assertIs(branch, get_named_object('bzrlib.branch'))  | 
|
38  | 
||
39  | 
def test_module_attr(self):  | 
|
40  | 
self.assertIs(  | 
|
41  | 
branch.Branch, get_named_object('bzrlib.branch', 'Branch'))  | 
|
42  | 
||
43  | 
def test_dotted_attr(self):  | 
|
44  | 
self.assertIs(  | 
|
45  | 
branch.Branch.hooks,  | 
|
46  | 
get_named_object('bzrlib.branch', 'Branch.hooks'))  | 
|
47  | 
||
48  | 
def test_package(self):  | 
|
49  | 
        # bzrlib.tests is a package, not simply a module
 | 
|
50  | 
self.assertIs(tests, get_named_object('bzrlib.tests'))  | 
|
51  | 
||
52  | 
def test_package_attr(self):  | 
|
53  | 
        # bzrlib.tests is a package, not simply a module
 | 
|
54  | 
self.assertIs(  | 
|
55  | 
tests.TestCase, get_named_object('bzrlib.tests', 'TestCase'))  | 
|
56  | 
||
57  | 
def test_import_error(self):  | 
|
58  | 
self.assertRaises(ImportError, get_named_object, 'NO_SUCH_MODULE')  | 
|
59  | 
||
60  | 
def test_attribute_error(self):  | 
|
61  | 
self.assertRaises(  | 
|
62  | 
AttributeError, get_named_object, 'sys', 'NO_SUCH_ATTR')  | 
|
63  | 
||
64  | 
||
65  | 
||
66  | 
class TestCalcParent_name(tests.TestCase):  | 
|
67  | 
"""Tests for calc_parent_name."""  | 
|
68  | 
||
69  | 
def test_dotted_member(self):  | 
|
70  | 
self.assertEqual(  | 
|
71  | 
('mod_name', 'attr1', 'attr2'),  | 
|
72  | 
calc_parent_name('mod_name', 'attr1.attr2'))  | 
|
73  | 
||
74  | 
def test_undotted_member(self):  | 
|
75  | 
self.assertEqual(  | 
|
76  | 
('mod_name', None, 'attr1'),  | 
|
77  | 
calc_parent_name('mod_name', 'attr1'))  | 
|
78  | 
||
79  | 
def test_dotted_module_no_member(self):  | 
|
80  | 
self.assertEqual(  | 
|
81  | 
('mod', None, 'sub_mod'),  | 
|
82  | 
calc_parent_name('mod.sub_mod'))  | 
|
83  | 
||
84  | 
def test_undotted_module_no_member(self):  | 
|
85  | 
err = self.assertRaises(AssertionError, calc_parent_name, 'mod_name')  | 
|
86  | 
self.assertEqual(  | 
|
87  | 
"No parent object for top-level module 'mod_name'", err.args[0])  | 
|
88  |