2020-06-20 14:53:44 +02:00
|
|
|
import pytest
|
2020-02-16 14:27:17 +01:00
|
|
|
|
|
|
|
import core.event
|
|
|
|
|
2020-06-20 15:11:53 +02:00
|
|
|
|
2020-06-20 14:53:44 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def someEvent():
|
2020-06-20 15:11:53 +02:00
|
|
|
class Event:
|
2020-06-20 14:53:44 +02:00
|
|
|
def __init__(self):
|
|
|
|
core.event.clear()
|
|
|
|
self.id = "some event"
|
|
|
|
self.called = 0
|
|
|
|
self.call_args = []
|
|
|
|
self.call_kwargs = []
|
|
|
|
|
|
|
|
def callback(self, *args, **kwargs):
|
|
|
|
self.called += 1
|
|
|
|
if args:
|
|
|
|
self.call_args.append(list(args))
|
|
|
|
if kwargs:
|
|
|
|
self.call_kwargs.append(kwargs)
|
|
|
|
|
|
|
|
return Event()
|
|
|
|
|
|
|
|
|
|
|
|
def test_simple_callback(someEvent):
|
|
|
|
assert someEvent.called == 0
|
|
|
|
|
|
|
|
core.event.register(someEvent.id, someEvent.callback)
|
|
|
|
core.event.register(someEvent.id, someEvent.callback)
|
|
|
|
|
|
|
|
core.event.trigger(someEvent.id)
|
|
|
|
|
|
|
|
assert someEvent.called == 2
|
|
|
|
|
2020-06-20 15:11:53 +02:00
|
|
|
|
2020-06-20 14:53:44 +02:00
|
|
|
def test_args_callback(someEvent):
|
|
|
|
core.event.register(someEvent.id, someEvent.callback, "a", "b")
|
|
|
|
core.event.trigger(someEvent.id)
|
|
|
|
|
|
|
|
assert someEvent.called == 1
|
|
|
|
assert len(someEvent.call_args) == 1
|
|
|
|
assert someEvent.call_args[0] == ["a", "b"]
|
|
|
|
|
2020-06-20 15:11:53 +02:00
|
|
|
|
2020-06-20 14:53:44 +02:00
|
|
|
def test_kwargs_callback(someEvent):
|
|
|
|
core.event.register(
|
|
|
|
someEvent.id, someEvent.callback, "a", "b", key1="test", key2="another"
|
|
|
|
)
|
|
|
|
core.event.trigger(someEvent.id)
|
|
|
|
|
|
|
|
assert someEvent.called == 1
|
|
|
|
assert len(someEvent.call_args) == 1
|
|
|
|
assert someEvent.call_args[0] == ["a", "b"]
|
|
|
|
assert len(someEvent.call_kwargs) == 1
|
2020-06-20 15:11:53 +02:00
|
|
|
assert someEvent.call_kwargs[0] == {"key1": "test", "key2": "another"}
|
|
|
|
|
2020-06-20 14:53:44 +02:00
|
|
|
|
|
|
|
def test_arg_trigger(someEvent):
|
|
|
|
core.event.register(someEvent.id, someEvent.callback)
|
|
|
|
core.event.trigger(someEvent.id, "a", "b")
|
|
|
|
|
|
|
|
assert someEvent.called == 1
|
|
|
|
assert len(someEvent.call_args) == 1
|
|
|
|
assert someEvent.call_args[0] == ["a", "b"]
|
|
|
|
|
2020-06-20 15:11:53 +02:00
|
|
|
|
2020-06-20 14:53:44 +02:00
|
|
|
def test_kwargs_trigger(someEvent):
|
|
|
|
core.event.register(someEvent.id, someEvent.callback)
|
|
|
|
core.event.trigger(someEvent.id, "a", "c", key1="test", key2="something")
|
|
|
|
|
|
|
|
assert someEvent.called == 1
|
|
|
|
assert len(someEvent.call_args) == 1
|
|
|
|
assert someEvent.call_args[0] == ["a", "c"]
|
|
|
|
assert len(someEvent.call_kwargs) == 1
|
2020-06-20 15:11:53 +02:00
|
|
|
assert someEvent.call_kwargs[0] == {"key1": "test", "key2": "something"}
|
|
|
|
|
2020-04-04 08:44:41 +02:00
|
|
|
|
2020-02-16 14:27:17 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|