##// END OF EJS Templates
TST: add test for searching for removal with bare functions
Thomas A Caswell -
Show More
@@ -1,73 +1,85 b''
1 1 from backcall import callback_prototype
2 2 import unittest
3 3 from unittest.mock import Mock
4 import nose.tools as nt
4 5
5 6 from IPython.core import events
6 7 import IPython.testing.tools as tt
7 8
8 9
9 10 @events._define_event
10 11 def ping_received():
11 12 pass
12 13
13 14
14 15 @events._define_event
15 16 def event_with_argument(argument):
16 17 pass
17 18
18 19
19 20 class CallbackTests(unittest.TestCase):
20 21 def setUp(self):
21 22 self.em = events.EventManager(get_ipython(), {'ping_received': ping_received, 'event_with_argument': event_with_argument})
22 23
23 24 def test_register_unregister(self):
24 25 cb = Mock()
25 26
26 27 self.em.register('ping_received', cb)
27 28 self.em.trigger('ping_received')
28 29 self.assertEqual(cb.call_count, 1)
29 30
30 31 self.em.unregister('ping_received', cb)
31 32 self.em.trigger('ping_received')
32 33 self.assertEqual(cb.call_count, 1)
33
34
35 def test_bare_function_missed_unregister(self):
36 def cb1():
37 ...
38
39 def cb2():
40 ...
41
42 self.em.register('ping_received', cb1)
43 nt.assert_raises(ValueError, self.em.unregister, 'ping_received', cb2)
44 self.em.unregister('ping_received', cb1)
45
34 46 def test_cb_error(self):
35 47 cb = Mock(side_effect=ValueError)
36 48 self.em.register('ping_received', cb)
37 49 with tt.AssertPrints("Error in callback"):
38 50 self.em.trigger('ping_received')
39 51
40 52 def test_unregister_during_callback(self):
41 53 invoked = [False] * 3
42 54
43 55 def func1(*_):
44 56 invoked[0] = True
45 57 self.em.unregister('ping_received', func1)
46 58 self.em.register('ping_received', func3)
47 59
48 60 def func2(*_):
49 61 invoked[1] = True
50 62 self.em.unregister('ping_received', func2)
51 63
52 64 def func3(*_):
53 65 invoked[2] = True
54 66
55 67 self.em.register('ping_received', func1)
56 68 self.em.register('ping_received', func2)
57 69
58 70 self.em.trigger('ping_received')
59 71 self.assertEqual([True, True, False], invoked)
60 72 self.assertEqual([func3], self.em.callbacks['ping_received'])
61 73
62 74 def test_ignore_event_arguments_if_no_argument_required(self):
63 75 call_count = [0]
64 76 def event_with_no_argument():
65 77 call_count[0] += 1
66 78
67 79 self.em.register('event_with_argument', event_with_no_argument)
68 80 self.em.trigger('event_with_argument', 'the argument')
69 81 self.assertEqual(call_count[0], 1)
70 82
71 83 self.em.unregister('event_with_argument', event_with_no_argument)
72 84 self.em.trigger('ping_received')
73 85 self.assertEqual(call_count[0], 1)
General Comments 0
You need to be logged in to leave comments. Login now