/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_hooks.py

  • Committer: Robert Collins
  • Date: 2007-07-15 15:40:37 UTC
  • mto: (2592.3.33 repository)
  • mto: This revision was merged to the branch mainline in revision 2624.
  • Revision ID: robertc@robertcollins.net-20070715154037-3ar8g89decddc9su
Make GraphIndex accept nodes as key, value, references, so that the method
signature is closer to what a simple key->value index delivers. Also
change the behaviour when the reference list count is zero to accept
key, value as nodes, and emit key, value to make it identical in that case
to a simple key->value index. This may not be a good idea, but for now it
seems ok.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests for the core Hooks logic."""
18
18
 
19
 
from bzrlib import (
20
 
    branch,
21
 
    errors,
22
 
    tests,
23
 
    )
24
19
from bzrlib.hooks import (
25
 
    HookPoint,
26
20
    Hooks,
27
 
    known_hooks,
28
 
    known_hooks_key_to_object,
29
 
    known_hooks_key_to_parent_and_attribute,
30
 
    )
31
 
 
32
 
 
33
 
class TestHooks(tests.TestCase):
34
 
 
35
 
    def test_create_hook_first(self):
36
 
        hooks = Hooks()
37
 
        doc = ("Invoked after changing the tip of a branch object. Called with"
38
 
            "a bzrlib.branch.PostChangeBranchTipParams object")
39
 
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
40
 
        hooks.create_hook(hook)
41
 
        self.assertEqual(hook, hooks['post_tip_change'])
42
 
 
43
 
    def test_create_hook_name_collision_errors(self):
44
 
        hooks = Hooks()
45
 
        doc = ("Invoked after changing the tip of a branch object. Called with"
46
 
            "a bzrlib.branch.PostChangeBranchTipParams object")
47
 
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
48
 
        hook2 = HookPoint("post_tip_change", None, None, None)
49
 
        hooks.create_hook(hook)
50
 
        self.assertRaises(errors.DuplicateKey, hooks.create_hook, hook2)
51
 
        self.assertEqual(hook, hooks['post_tip_change'])
52
 
 
53
 
    def test_docs(self):
54
 
        """docs() should return something reasonable about the Hooks."""
55
 
        class MyHooks(Hooks):
56
 
            pass
57
 
        hooks = MyHooks()
58
 
        hooks['legacy'] = []
59
 
        hook1 = HookPoint('post_tip_change',
60
 
            "Invoked after the tip of a branch changes. Called with "
61
 
            "a ChangeBranchTipParams object.", (1, 4), None)
62
 
        hook2 = HookPoint('pre_tip_change',
63
 
            "Invoked before the tip of a branch changes. Called with "
64
 
            "a ChangeBranchTipParams object. Hooks should raise "
65
 
            "TipChangeRejected to signal that a tip change is not permitted.",
66
 
            (1, 6), None)
67
 
        hooks.create_hook(hook1)
68
 
        hooks.create_hook(hook2)
69
 
        self.assertEqualDiff(
70
 
            "MyHooks\n"
71
 
            "-------\n"
72
 
            "\n"
73
 
            "legacy\n"
74
 
            "~~~~~~\n"
75
 
            "\n"
76
 
            "An old-style hook. For documentation see the __init__ method of 'MyHooks'\n"
77
 
            "\n"
78
 
            "post_tip_change\n"
79
 
            "~~~~~~~~~~~~~~~\n"
80
 
            "\n"
81
 
            "Introduced in: 1.4\n"
82
 
            "\n"
83
 
            "Invoked after the tip of a branch changes. Called with a\n"
84
 
            "ChangeBranchTipParams object.\n"
85
 
            "\n"
86
 
            "pre_tip_change\n"
87
 
            "~~~~~~~~~~~~~~\n"
88
 
            "\n"
89
 
            "Introduced in: 1.6\n"
90
 
            "\n"
91
 
            "Invoked before the tip of a branch changes. Called with a\n"
92
 
            "ChangeBranchTipParams object. Hooks should raise TipChangeRejected to\n"
93
 
            "signal that a tip change is not permitted.\n", hooks.docs())
94
 
 
95
 
    def test_install_named_hook_raises_unknown_hook(self):
96
 
        hooks = Hooks()
97
 
        self.assertRaises(errors.UnknownHook, hooks.install_named_hook, 'silly',
98
 
                          None, "")
99
 
 
100
 
    def test_install_named_hook_appends_known_hook(self):
 
21
    )
 
22
from bzrlib.errors import (
 
23
    UnknownHook,
 
24
    )
 
25
 
 
26
from bzrlib.tests import TestCase
 
27
 
 
28
 
 
29
class TestHooks(TestCase):
 
30
 
 
31
    def test_install_hook_raises_unknown_hook(self):
 
32
        """install_hook should raise UnknownHook if a hook is unknown."""
 
33
        hooks = Hooks()
 
34
        self.assertRaises(UnknownHook, hooks.install_hook, 'silly', None)
 
35
 
 
36
    def test_install_hook_appends_known_hook(self):
 
37
        """install_hook should append the callable for known hooks."""
101
38
        hooks = Hooks()
102
39
        hooks['set_rh'] = []
103
 
        hooks.install_named_hook('set_rh', None, "demo")
 
40
        hooks.install_hook('set_rh', None)
104
41
        self.assertEqual(hooks['set_rh'], [None])
105
42
 
106
 
    def test_install_named_hook_and_retrieve_name(self):
 
43
    def test_name_hook_and_retrieve_name(self):
 
44
        """name_hook puts the name in the names mapping."""
107
45
        hooks = Hooks()
108
46
        hooks['set_rh'] = []
109
 
        hooks.install_named_hook('set_rh', None, "demo")
 
47
        hooks.install_hook('set_rh', None)
 
48
        hooks.name_hook(None, 'demo')
110
49
        self.assertEqual("demo", hooks.get_hook_name(None))
111
50
 
112
 
 
113
 
class TestHook(tests.TestCase):
114
 
 
115
 
    def test___init__(self):
116
 
        doc = ("Invoked after changing the tip of a branch object. Called with"
117
 
            "a bzrlib.branch.PostChangeBranchTipParams object")
118
 
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
119
 
        self.assertEqual(doc, hook.__doc__)
120
 
        self.assertEqual("post_tip_change", hook.name)
121
 
        self.assertEqual((0, 15), hook.introduced)
122
 
        self.assertEqual(None, hook.deprecated)
123
 
        self.assertEqual([], list(hook))
124
 
 
125
 
    def test_docs(self):
126
 
        doc = ("Invoked after changing the tip of a branch object. Called with"
127
 
            " a bzrlib.branch.PostChangeBranchTipParams object")
128
 
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
129
 
        self.assertEqual("post_tip_change\n"
130
 
            "~~~~~~~~~~~~~~~\n"
131
 
            "\n"
132
 
            "Introduced in: 0.15\n"
133
 
            "\n"
134
 
            "Invoked after changing the tip of a branch object. Called with a\n"
135
 
            "bzrlib.branch.PostChangeBranchTipParams object\n", hook.docs())
136
 
 
137
 
    def test_hook(self):
138
 
        hook = HookPoint("foo", "no docs", None, None)
139
 
        def callback():
140
 
            pass
141
 
        hook.hook(callback, "my callback")
142
 
        self.assertEqual([callback], list(hook))
143
 
 
144
 
    def test___repr(self):
145
 
        # The repr should list all the callbacks, with names.
146
 
        hook = HookPoint("foo", "no docs", None, None)
147
 
        def callback():
148
 
            pass
149
 
        hook.hook(callback, "my callback")
150
 
        callback_repr = repr(callback)
151
 
        self.assertEqual(
152
 
            '<HookPoint(foo), callbacks=[%s(my callback)]>' %
153
 
            callback_repr, repr(hook))
154
 
 
155
 
 
156
 
class TestHookRegistry(tests.TestCase):
157
 
 
158
 
    def test_items_are_reasonable_keys(self):
159
 
        # All the items in the known_hooks registry need to map from
160
 
        # (module_name, member_name) tuples to the callable used to get an
161
 
        # empty Hooks for that attribute. This is used to support the test
162
 
        # suite which needs to generate empty hooks (and HookPoints) to ensure
163
 
        # isolation and prevent tests failing spuriously.
164
 
        for key, factory in known_hooks.items():
165
 
            self.assertTrue(callable(factory),
166
 
                "The factory(%r) for %r is not callable" % (factory, key))
167
 
            obj = known_hooks_key_to_object(key)
168
 
            self.assertIsInstance(obj, Hooks)
169
 
            new_hooks = factory()
170
 
            self.assertIsInstance(obj, Hooks)
171
 
            self.assertEqual(type(obj), type(new_hooks))
172
 
            self.assertEqual("No hook name", new_hooks.get_hook_name(None))
173
 
 
174
 
    def test_known_hooks_key_to_object(self):
175
 
        self.assertIs(branch.Branch.hooks,
176
 
            known_hooks_key_to_object(('bzrlib.branch', 'Branch.hooks')))
177
 
 
178
 
    def test_known_hooks_key_to_parent_and_attribute(self):
179
 
        self.assertEqual((branch.Branch, 'hooks'),
180
 
            known_hooks_key_to_parent_and_attribute(
181
 
            ('bzrlib.branch', 'Branch.hooks')))
182
 
        self.assertEqual((branch, 'Branch'),
183
 
            known_hooks_key_to_parent_and_attribute(
184
 
            ('bzrlib.branch', 'Branch')))
 
51
    def test_get_unnamed_hook_name_is_unnamed(self):
 
52
        hooks = Hooks()
 
53
        hooks['set_rh'] = []
 
54
        hooks.install_hook('set_rh', None)
 
55
        self.assertEqual("No hook name", hooks.get_hook_name(None))