~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_hooks.py

  • Committer: Robert Collins
  • Date: 2009-03-12 02:43:46 UTC
  • mto: This revision was merged to the branch mainline in revision 4133.
  • Revision ID: robertc@robertcollins.net-20090312024346-jx3vpibkrwo1qxar
Create a single registry of all Hooks classes, removing the test suite knowledge of such hooks and allowing plugins to sensibly and safely define new hooks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tests for the core Hooks logic."""
18
18
 
19
 
from bzrlib import errors
 
19
from bzrlib import branch, errors
20
20
from bzrlib.hooks import (
21
21
    HookPoint,
22
22
    Hooks,
 
23
    known_hooks,
 
24
    known_hooks_key_to_object,
 
25
    known_hooks_key_to_parent_and_attribute,
23
26
    )
24
27
from bzrlib.errors import (
25
28
    UnknownHook,
180
183
        self.assertEqual(
181
184
            '<HookPoint(foo), callbacks=[%s(my callback)]>' %
182
185
            callback_repr, repr(hook))
 
186
 
 
187
 
 
188
class TestHookRegistry(TestCase):
 
189
 
 
190
    def test_items_are_reasonable_keys(self):
 
191
        # All the items in the known_hooks registry need to map from
 
192
        # (module_name, member_name) tuples to the callable used to get an
 
193
        # empty Hooks of for that attribute. This is used to support the test
 
194
        # suite which needs to generate empty hooks (and HookPoints) to ensure
 
195
        # isolation and prevent tests failing spuriously.
 
196
        for key, factory in known_hooks.items():
 
197
            self.assertTrue(callable(factory),
 
198
                "The factory(%r) for %r is not callable" % (factory, key))
 
199
            obj = known_hooks_key_to_object(key)
 
200
            self.assertIsInstance(obj, Hooks)
 
201
            new_hooks = factory()
 
202
            self.assertIsInstance(obj, Hooks)
 
203
            self.assertEqual(type(obj), type(new_hooks))
 
204
 
 
205
    def test_known_hooks_key_to_object(self):
 
206
        self.assertIs(branch.Branch.hooks,
 
207
            known_hooks_key_to_object(('bzrlib.branch', 'Branch.hooks')))
 
208
 
 
209
    def test_known_hooks_key_to_parent_and_attribute(self):
 
210
        self.assertEqual((branch.Branch, 'hooks'),
 
211
            known_hooks_key_to_parent_and_attribute(
 
212
            ('bzrlib.branch', 'Branch.hooks')))
 
213
        self.assertEqual((branch, 'Branch'),
 
214
            known_hooks_key_to_parent_and_attribute(
 
215
            ('bzrlib.branch', 'Branch')))