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