def test_remove_all_listeners(ee: EventEmitter, arg_swallower: Callable[..., None]) -> None: ee.on("event", arg_swallower) assert ee.listener_count("event") == 1 assert arg_swallower is ee.listeners("event")[0] ee.remove_all_listeners() assert ee.listener_count("event") == 0
def test_on(ee: EventEmitter, mock: Mock) -> None: ee.on("event", mock.method) assert ee.listener_count("event") == 1 assert mock.method is ee.listeners("event")[0] assert ee.emit("event", 1, data=2) mock.method.assert_called_with(1, data=2) assert ee.listener_count("event") == 1 assert mock.method is ee.listeners("event")[0]
def test_once_does_not_emits_error_when_not_listening_for_errors( ee: EventEmitter, error_helper: "EEExceptionHelper") -> None: ee.once("event", error_helper.error_raiser) assert ee.listener_count("event") == 1 assert ee.listener_count("error") == 0 assert ee.emit("event", 1, data=2) error_helper.assert_error_was_not_emitted() assert ee.listener_count("event") == 0 assert ee.listener_count("error") == 0
def test_on_decorator(ee: EventEmitter, mock: Mock) -> None: @ee.on("event") def handler(*args, **kwargs) -> None: mock.method(*args, **kwargs) assert ee.listener_count("event") == 1 assert handler is ee.listeners("event")[0] assert ee.emit("event", 1, data=2) mock.method.assert_called_with(1, data=2) assert ee.listener_count("event") == 1 assert handler is ee.listeners("event")[0]
async def test_coroutine_listener_once(ee_with_event_loop: EventEmitter, mock: Mock, deferred: Future) -> None: async def handler(*args, **kwargs) -> None: mock.method(*args, **kwargs) deferred.set_result(True) ee_with_event_loop.once("event", handler) assert ee_with_event_loop.listener_count("event") == 1 assert handler is ee_with_event_loop.listeners("event")[0] assert ee_with_event_loop.emit("event", 1, data=2) assert await deferred assert not ee_with_event_loop.emit("event", 1, data=2) mock.method.assert_called_once_with(1, data=2) assert ee_with_event_loop.listener_count("event") == 0 assert handler not in ee_with_event_loop.listeners("event")
async def test_coroutine_listener_decorator_does_not_emits_error_when_not_listening_for_errors( ee_with_event_loop: EventEmitter, error_helper: "EEExceptionHelper", event_loop: AbstractEventLoop, ) -> None: error_helper.with_deferred(event_loop) @ee_with_event_loop.on("event") async def error_raiser(*args, **kwargs) -> None: await error_helper.error_raiser_async(*args, **kwargs) assert ee_with_event_loop.listener_count("event") == 1 assert ee_with_event_loop.emit("event", 1, data=2) await error_helper.assert_error_was_not_emitted_async() assert ee_with_event_loop.listener_count("event") == 1
def test_once_decorator_emits_error_when_listening_for_errors( ee: EventEmitter, error_helper: "EEExceptionHelper") -> None: @ee.once("event") def handler(*args, **kwargs) -> None: error_helper.error_raiser(*args, **kwargs) @ee.once("error") def error_handler(*args, **kwargs) -> None: error_helper.error_listener(*args, **kwargs) assert ee.listener_count("event") == 1 assert ee.listener_count("error") == 1 assert ee.emit("event", 1, data=2) error_helper.assert_error_was_emitted() assert ee.listener_count("event") == 0 assert ee.listener_count("error") == 0