1
# Copyright (C) 2007-2012, 2016 Canonical Ltd
1
# Copyright (C) 2007-2010 Canonical Ltd
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
38
39
class TestHooks(tests.TestCase):
41
def test_create_hook_first(self):
42
hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
43
doc = ("Invoked after changing the tip of a branch object. Called with"
44
"a bzrlib.branch.PostChangeBranchTipParams object")
45
hook = HookPoint("post_tip_change", doc, (0, 15), None)
46
self.applyDeprecated(deprecated_in((2, 4)), hooks.create_hook, hook)
47
self.assertEqual(hook, hooks['post_tip_change'])
49
def test_create_hook_name_collision_errors(self):
50
hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
51
doc = ("Invoked after changing the tip of a branch object. Called with"
52
"a bzrlib.branch.PostChangeBranchTipParams object")
53
hook = HookPoint("post_tip_change", doc, (0, 15), None)
54
hook2 = HookPoint("post_tip_change", None, None, None)
55
self.applyDeprecated(deprecated_in((2, 4)), hooks.create_hook, hook)
56
self.assertRaises(errors.DuplicateKey, self.applyDeprecated,
57
deprecated_in((2, 4, 0)), hooks.create_hook, hook2)
58
self.assertEqual(hook, hooks['post_tip_change'])
40
60
def test_docs(self):
41
61
"""docs() should return something reasonable about the Hooks."""
42
62
class MyHooks(Hooks):
142
162
'TestHooks.hooks', 'set_rh', set_rh, "demo")
143
163
set_rh_lazy_hooks = _mod_hooks._lazy_hooks[
144
164
('bzrlib.tests.test_hooks', 'TestHooks.hooks', 'set_rh')]
145
self.assertEqual(1, len(set_rh_lazy_hooks))
146
self.assertEqual(set_rh, set_rh_lazy_hooks[0][0].get_obj())
147
self.assertEqual("demo", set_rh_lazy_hooks[0][1])
165
self.assertEquals(1, len(set_rh_lazy_hooks))
166
self.assertEquals(set_rh, set_rh_lazy_hooks[0][0].get_obj())
167
self.assertEquals("demo", set_rh_lazy_hooks[0][1])
148
168
self.assertEqual(list(TestHooks.hooks['set_rh']), [set_rh])
150
170
set_rh = lambda: None
172
192
for key, callbacks in _mod_hooks._lazy_hooks.iteritems():
173
193
(module_name, member_name, hook_name) = key
174
194
obj = pyutils.get_named_object(module_name, member_name)
175
self.assertEqual(obj._module, module_name)
176
self.assertEqual(obj._member_name, member_name)
195
self.assertEquals(obj._module, module_name)
196
self.assertEquals(obj._member_name, member_name)
177
197
self.assertTrue(hook_name in obj)
178
198
self.assertIs(callbacks, obj[hook_name]._callbacks)
266
286
self.assertIs(branch.Branch.hooks,
267
287
known_hooks_key_to_object(('bzrlib.branch', 'Branch.hooks')))
289
def test_known_hooks_key_to_parent_and_attribute_deprecated(self):
290
self.assertEqual((branch.Branch, 'hooks'),
291
self.applyDeprecated(deprecated_in((2,3)),
292
known_hooks_key_to_parent_and_attribute,
293
('bzrlib.branch', 'Branch.hooks')))
294
self.assertEqual((branch, 'Branch'),
295
self.applyDeprecated(deprecated_in((2,3)),
296
known_hooks_key_to_parent_and_attribute,
297
('bzrlib.branch', 'Branch')))
269
299
def test_known_hooks_key_to_parent_and_attribute(self):
270
300
self.assertEqual((branch.Branch, 'hooks'),
271
301
known_hooks.key_to_parent_and_attribute(