[tests/event] Add some more tests for trigger-time args

This commit is contained in:
tobi-wan-kenobi 2020-04-04 08:44:41 +02:00
parent 52e5ad7b43
commit a43917594e
3 changed files with 25 additions and 6 deletions

View file

@ -2,15 +2,21 @@
__callbacks = {} __callbacks = {}
def register(event, callback, *args, **kwargs): def register(event, callback, *args, **kwargs):
__callbacks.setdefault(event, []).append( cb = callback
lambda: callback(*args, **kwargs) if len(args) + len(kwargs) > 0:
) print("registering lambda: {} {}".format(len(args), len(kwargs)))
cb = lambda: callback(*args, **kwargs)
__callbacks.setdefault(event, []).append(cb)
def clear(): def clear():
__callbacks.clear() __callbacks.clear()
def trigger(event): def trigger(event, *args, **kwargs):
for callback in __callbacks.get(event, []): for callback in __callbacks.get(event, []):
callback() if len(args) + len(kwargs) == 0:
callback()
else:
callback(*args, **kwargs)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -28,6 +28,7 @@
- allow handlers to specify whether to update or not (e.g. scroll) - allow handlers to specify whether to update or not (e.g. scroll)
- API documentation - API documentation
- github pages - github pages
- Add module that indicates arbitrary errors that occurred (via eventing)
## TODO ## TODO
- theme: `load` vs. `__load` vs. `load_keywords` - theme: `load` vs. `__load` vs. `load_keywords`

View file

@ -2,12 +2,12 @@ import unittest
import core.event import core.event
class event(unittest.TestCase): class event(unittest.TestCase):
def setUp(self): def setUp(self):
self.someEvent = 'event' self.someEvent = 'event'
self.called = {} self.called = {}
self.params = [] self.params = []
core.event.clear()
def callback1(self): def callback1(self):
self.called['callback1'] = True self.called['callback1'] = True
@ -43,4 +43,16 @@ class event(unittest.TestCase):
self.assertEqual(1, len(self.called.keys())) self.assertEqual(1, len(self.called.keys()))
self.assertEqual(['a', 'b', 'test', 'x'], self.params) self.assertEqual(['a', 'b', 'test', 'x'], self.params)
def test_arg_trigger(self):
core.event.register(self.someEvent, self.callback_args)
core.event.trigger(self.someEvent, 'a', 'b')
self.assertEqual(1, len(self.called.keys()))
self.assertEqual(['a', 'b'], self.params)
def test_kwargs_trigger(self):
core.event.register(self.someEvent, self.callback_kwargs)
core.event.trigger(self.someEvent, 'a', 'b', key1='test', key2='x')
self.assertEqual(1, len(self.called.keys()))
self.assertEqual(['a', 'b', 'test', 'x'], self.params)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4