[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 = {}
def register(event, callback, *args, **kwargs):
__callbacks.setdefault(event, []).append(
lambda: callback(*args, **kwargs)
)
cb = callback
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():
__callbacks.clear()
def trigger(event):
def trigger(event, *args, **kwargs):
for callback in __callbacks.get(event, []):
if len(args) + len(kwargs) == 0:
callback()
else:
callback(*args, **kwargs)
# 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)
- API documentation
- github pages
- Add module that indicates arbitrary errors that occurred (via eventing)
## TODO
- theme: `load` vs. `__load` vs. `load_keywords`

View file

@ -2,12 +2,12 @@ import unittest
import core.event
class event(unittest.TestCase):
def setUp(self):
self.someEvent = 'event'
self.called = {}
self.params = []
core.event.clear()
def callback1(self):
self.called['callback1'] = True
@ -43,4 +43,16 @@ class event(unittest.TestCase):
self.assertEqual(1, len(self.called.keys()))
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