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