1
# Copyright (C) 2010 Canonical Ltd
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.
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.
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
17
"""Tests for bzrlib.pyutils."""
23
from bzrlib.pyutils import (
29
class TestGetNamedObject(tests.TestCase):
30
"""Tests for get_named_object."""
32
def test_module_only(self):
34
self.assertIs(sys, get_named_object('sys'))
36
def test_dotted_module(self):
37
self.assertIs(branch, get_named_object('bzrlib.branch'))
39
def test_module_attr(self):
41
branch.Branch, get_named_object('bzrlib.branch', 'Branch'))
43
def test_dotted_attr(self):
46
get_named_object('bzrlib.branch', 'Branch.hooks'))
48
def test_package(self):
49
# bzrlib.tests is a package, not simply a module
50
self.assertIs(tests, get_named_object('bzrlib.tests'))
52
def test_package_attr(self):
53
# bzrlib.tests is a package, not simply a module
55
tests.TestCase, get_named_object('bzrlib.tests', 'TestCase'))
57
def test_import_error(self):
58
self.assertRaises(ImportError, get_named_object, 'NO_SUCH_MODULE')
60
def test_attribute_error(self):
62
AttributeError, get_named_object, 'sys', 'NO_SUCH_ATTR')
66
class TestCalcParent_name(tests.TestCase):
67
"""Tests for calc_parent_name."""
69
def test_dotted_member(self):
71
('mod_name', 'attr1', 'attr2'),
72
calc_parent_name('mod_name', 'attr1.attr2'))
74
def test_undotted_member(self):
76
('mod_name', None, 'attr1'),
77
calc_parent_name('mod_name', 'attr1'))
79
def test_dotted_module_no_member(self):
81
('mod', None, 'sub_mod'),
82
calc_parent_name('mod.sub_mod'))
84
def test_undotted_module_no_member(self):
85
err = self.assertRaises(AssertionError, calc_parent_name, 'mod_name')
87
"No parent object for top-level module 'mod_name'", err.args[0])