bumblebee-status/tests/core/test_event.py

80 lines
2.1 KiB
Python
Raw Permalink Normal View History

2020-06-20 14:53:44 +02:00
import pytest
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"}
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4