/brz/remove-bazaar

To get this branch, use:
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
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
17
"""Tests for breezy.pyutils."""
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
18
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
19
from breezy import (
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
20
    branch,
21
    tests,
22
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
23
from breezy.pyutils import (
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __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):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
37
        self.assertIs(branch, get_named_object('breezy.branch'))
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
38
39
    def test_module_attr(self):
40
        self.assertIs(
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
41
            branch.Branch, get_named_object('breezy.branch', 'Branch'))
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
42
43
    def test_dotted_attr(self):
44
        self.assertIs(
45
            branch.Branch.hooks,
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
46
            get_named_object('breezy.branch', 'Branch.hooks'))
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
47
48
    def test_package(self):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
49
        # breezy.tests is a package, not simply a module
50
        self.assertIs(tests, get_named_object('breezy.tests'))
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
51
52
    def test_package_attr(self):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
53
        # breezy.tests is a package, not simply a module
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
54
        self.assertIs(
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
55
            tests.TestCase, get_named_object('breezy.tests', 'TestCase'))
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
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
class TestCalcParent_name(tests.TestCase):
66
    """Tests for calc_parent_name."""
67
68
    def test_dotted_member(self):
69
        self.assertEqual(
70
            ('mod_name', 'attr1', 'attr2'),
71
            calc_parent_name('mod_name', 'attr1.attr2'))
72
73
    def test_undotted_member(self):
74
        self.assertEqual(
75
            ('mod_name', None, 'attr1'),
76
            calc_parent_name('mod_name', 'attr1'))
77
78
    def test_dotted_module_no_member(self):
79
        self.assertEqual(
80
            ('mod', None, 'sub_mod'),
81
            calc_parent_name('mod.sub_mod'))
82
83
    def test_undotted_module_no_member(self):
84
        err = self.assertRaises(AssertionError, calc_parent_name, 'mod_name')
85
        self.assertEqual(
86
            "No parent object for top-level module 'mod_name'", err.args[0])