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