2020-02-16 14:27:17 +01:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
import core.event
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-02-16 14:27:17 +01:00
|
|
|
class event(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.someEvent = "event"
|
2020-02-16 14:27:17 +01:00
|
|
|
self.called = {}
|
|
|
|
self.params = []
|
2020-04-04 08:44:41 +02:00
|
|
|
core.event.clear()
|
2020-02-16 14:27:17 +01:00
|
|
|
|
|
|
|
def callback1(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.called["callback1"] = True
|
2020-02-16 14:27:17 +01:00
|
|
|
|
|
|
|
def callback2(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.called["callback2"] = True
|
2020-02-16 14:27:17 +01:00
|
|
|
|
|
|
|
def callback_args(self, val1, val2):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.called["callback_args"] = True
|
2020-02-16 14:27:17 +01:00
|
|
|
self.params = [val1, val2]
|
|
|
|
|
|
|
|
def callback_kwargs(self, val1, val2, key1=None, key2=None):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.called["callback_kwargs"] = True
|
2020-02-16 14:27:17 +01:00
|
|
|
self.params = [val1, val2, key1, key2]
|
|
|
|
|
|
|
|
def test_simple_callback(self):
|
|
|
|
core.event.register(self.someEvent, self.callback1)
|
|
|
|
core.event.register(self.someEvent, self.callback2)
|
|
|
|
|
|
|
|
core.event.trigger(self.someEvent)
|
|
|
|
|
|
|
|
self.assertEqual(2, len(self.called.keys()))
|
|
|
|
|
|
|
|
def test_arg_callback(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
core.event.register(self.someEvent, self.callback_args, "a", "b")
|
2020-02-16 14:27:17 +01:00
|
|
|
core.event.trigger(self.someEvent)
|
|
|
|
self.assertEqual(1, len(self.called.keys()))
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(["a", "b"], self.params)
|
2020-02-16 14:27:17 +01:00
|
|
|
|
|
|
|
def test_kwargs_callback(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
core.event.register(
|
|
|
|
self.someEvent, self.callback_kwargs, "a", "b", key1="test", key2="x"
|
|
|
|
)
|
2020-02-16 14:27:17 +01:00
|
|
|
core.event.trigger(self.someEvent)
|
|
|
|
self.assertEqual(1, len(self.called.keys()))
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(["a", "b", "test", "x"], self.params)
|
2020-02-16 14:27:17 +01:00
|
|
|
|
2020-04-04 08:44:41 +02:00
|
|
|
def test_arg_trigger(self):
|
|
|
|
core.event.register(self.someEvent, self.callback_args)
|
2020-05-03 11:15:52 +02:00
|
|
|
core.event.trigger(self.someEvent, "a", "b")
|
2020-04-04 08:44:41 +02:00
|
|
|
self.assertEqual(1, len(self.called.keys()))
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(["a", "b"], self.params)
|
2020-04-04 08:44:41 +02:00
|
|
|
|
|
|
|
def test_kwargs_trigger(self):
|
|
|
|
core.event.register(self.someEvent, self.callback_kwargs)
|
2020-05-03 11:15:52 +02:00
|
|
|
core.event.trigger(self.someEvent, "a", "b", key1="test", key2="x")
|
2020-04-04 08:44:41 +02:00
|
|
|
self.assertEqual(1, len(self.called.keys()))
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(["a", "b", "test", "x"], self.params)
|
|
|
|
|
2020-04-04 08:44:41 +02:00
|
|
|
|
2020-02-16 14:27:17 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|