264
263
'<HookPoint(foo), callbacks=[%s(my callback)]>' %
265
264
callback_repr, repr(hook))
271
executions.append('callback')
272
self.assertEquals('argument', arg)
274
hook = HookPoint("foo", "no docs", None, None)
275
hook.hook(callback, None)
276
hook.hook(callback, None)
277
result = hook.fire('argument')
278
self.assertTrue(result, 'no exceptions raised')
279
self.assertEqual(['callback', 'callback'], executions)
281
def test_fire_passes_exceptions(self):
282
class TestError(StandardError):
283
__doc__ = """A test exception."""
288
hook = HookPoint("foo", "no docs", None, None)
289
hook.hook(callback, None)
291
result = hook.fire('argument')
292
self.fail('execution should not reach here')
294
self.assertTrue(isinstance(e, TestError))
296
def test_fire_with_passed_exceptions(self):
298
class TestPassedError(StandardError):
299
__doc__ = """A test exception that should be passed."""
300
class TestSuppressedError(StandardError):
301
__doc__ = """A test exception that should be suppressed."""
303
def callback(excClass):
304
arguments.append(excClass)
307
hook = HookPoint("foo", "no docs", None, None,
308
suppress_exceptions=True, passed_exceptions=[TestPassedError])
309
hook.hook(callback, None)
311
# this exception should be suppressed, but logged
312
result = hook.fire(TestSuppressedError)
313
self.assertFalse(result)
315
# this exception should be passed through
316
result = hook.fire(TestPassedError)
317
self.fail('execution should not reach here')
319
self.assertTrue(isinstance(e, TestPassedError))
320
# hook should have been called twice
321
self.assertEqual(2, len(arguments))
324
def test_suppressed_fire_logs_exceptions(self):
325
class TestError(StandardError):
326
__doc__ = """A test exception."""
331
hook = HookPoint("foo", "no docs", None, None, suppress_exceptions=True)
332
hook.hook(callback, None)
336
warnings.append(args[0] % args[1:])
337
_warning = trace.warning
338
trace.warning = warning
343
self.fail('exceptions should be captured')
345
trace.warning = _warning
347
self.assertEqual(1, len(warnings))
350
267
class TestHookRegistry(tests.TestCase):