~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_hooks.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-02-18 20:16:49 UTC
  • mfrom: (5622.2.11 lazy-hooks)
  • Revision ID: pqm@pqm.ubuntu.com-20110218201649-woj0pygeukoq6z6h
(jelmer) Allow installing hooks without importing the hook point. (Jelmer
 Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from bzrlib import (
20
20
    branch,
21
21
    errors,
 
22
    pyutils,
22
23
    tests,
23
24
    )
24
25
from bzrlib.hooks import (
25
26
    HookPoint,
26
27
    Hooks,
 
28
    install_lazy_named_hook,
27
29
    known_hooks,
28
30
    known_hooks_key_to_object,
29
31
    known_hooks_key_to_parent_and_attribute,
 
32
    _lazy_hooks,
30
33
    )
31
34
from bzrlib.symbol_versioning import (
32
35
    deprecated_in,
112
115
        hooks.install_named_hook('set_rh', None, "demo")
113
116
        self.assertEqual("demo", hooks.get_hook_name(None))
114
117
 
 
118
    hooks = Hooks("bzrlib.tests.test_hooks", "TestHooks.hooks")
 
119
 
 
120
    def test_install_lazy_named_hook(self):
 
121
        # When the hook points are not yet registered the hook is
 
122
        # added to the _lazy_hooks dictionary in bzrlib.hooks.
 
123
        self.hooks.add_hook('set_rh', "doc", (0, 15))
 
124
        set_rh = lambda: None
 
125
        install_lazy_named_hook('bzrlib.tests.test_hooks',
 
126
            'TestHooks.hooks', 'set_rh', set_rh, "demo")
 
127
        set_rh_lazy_hooks = _lazy_hooks[
 
128
            ('bzrlib.tests.test_hooks', 'TestHooks.hooks', 'set_rh')]
 
129
        self.assertEquals(1, len(set_rh_lazy_hooks))
 
130
        self.assertEquals(set_rh, set_rh_lazy_hooks[0][0].get_obj())
 
131
        self.assertEquals("demo", set_rh_lazy_hooks[0][1])
 
132
        self.assertEqual(list(TestHooks.hooks['set_rh']), [set_rh])
 
133
 
115
134
    set_rh = lambda: None
116
135
 
117
136
    def test_install_named_hook_lazy(self):
131
150
            'set_rh', 'bzrlib.tests.test_hooks', 'TestHooks.set_rh',
132
151
            "demo")
133
152
 
 
153
    def test_valid_lazy_hooks(self):
 
154
        # Make sure that all the registered lazy hooks are referring to existing
 
155
        # hook points which allow lazy registration.
 
156
        for key, callbacks in _lazy_hooks.iteritems():
 
157
            (module_name, member_name, hook_name) = key
 
158
            obj = pyutils.get_named_object(module_name, member_name)
 
159
            self.assertEquals(obj._module, module_name)
 
160
            self.assertEquals(obj._member_name, member_name)
 
161
            self.assertTrue(hook_name in obj)
 
162
            self.assertIs(callbacks, obj[hook_name]._callbacks)
 
163
 
134
164
 
135
165
class TestHook(tests.TestCase):
136
166