From a43917594e68459d75ea7661054f00d593c95adf Mon Sep 17 00:00:00 2001 From: tobi-wan-kenobi Date: Sat, 4 Apr 2020 08:44:41 +0200 Subject: [PATCH] [tests/event] Add some more tests for trigger-time args --- core/event.py | 16 +++++++++++----- doc/NOTES.md | 1 + tests/core/test_event.py | 14 +++++++++++++- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/core/event.py b/core/event.py index 2a059a9..f876a9a 100644 --- a/core/event.py +++ b/core/event.py @@ -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, []): - callback() + if len(args) + len(kwargs) == 0: + callback() + else: + callback(*args, **kwargs) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/doc/NOTES.md b/doc/NOTES.md index 788072a..e5d16a7 100644 --- a/doc/NOTES.md +++ b/doc/NOTES.md @@ -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` diff --git a/tests/core/test_event.py b/tests/core/test_event.py index 7a35852..9f5866c 100644 --- a/tests/core/test_event.py +++ b/tests/core/test_event.py @@ -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