def test__listeners(self): def listener(e): event_list.append(e.type) ## Testing the `listener` list and the `event` function event_list = [] hkshell.event(0) hkshell.listeners.append(listener) hkshell.event(1) hkshell.listeners.append(listener) # The following event will be received by the `listener` function twice hkshell.event(2) hkshell.listeners.remove(listener) hkshell.event(3) hkshell.listeners.remove(listener) hkshell.event(4) self.assertEqual(event_list, [1, 2, 2, 3]) ## Testing the `append_listener` and `remove_listener` functions event_list = [] hkshell.event(0) hkshell.append_listener(listener) # a listener cannot be appended if it's in the list self.assertRaises( hkutils.HkException, lambda: hkshell.append_listener(listener)) hkshell.event(1) hkshell.remove_listener(listener) # a listener cannot be removed if it's not in the list self.assertRaises( hkutils.HkException, lambda: hkshell.remove_listener(listener)) hkshell.event(2) self.assertEqual(event_list, [1]) ## Testing instance variables of Event def listener2(e): event_list.append(e) event_list = [] hkshell.append_listener(listener2) hkshell.event(type='mytype') hkshell.event(type='mytype', command='mycommand') hkshell.remove_listener(listener2) self.assertEqual(event_list[0].type, 'mytype') self.assertEqual(event_list[0].command, None) self.assertEqual(event_list[1].type, 'mytype') self.assertEqual(event_list[1].command, 'mycommand')
def touch_posts_cmd(self, postindices): hkshell.event(type='before', command='touch_posts_cmd') for postindex in postindices: self.p(postindex).touch() hkshell.event(type='after')
def my_cmd(self, fun): hkshell.event(type='before', command='my_cmd') fun() hkshell.event(type='after')
def my_cmd(fun): hkshell.event(type='before') fun() hkshell.event(type='after')
def f(): # function already defined # pylint: disable=E0102 hkshell.event(type='f_body', command='f_cmd2') return 1
def f(): hkshell.event(type='f_body', command='f') return 1