# Path: flowpipe/event.py
# class Event(object):
#     """Very simple implementation of an event system.event
# 
#     The event simply calls the registered functions with the given arguments.
#     Please note that the integrity of the listeners is not enforced or checked.
#     """
# 
#     def __init__(self, name):
#         """Initialize the list of listeners
# 
#         Args:
#             name (str): The (unique) name of the signal
#         """
#         self.name = name
#         self._listeners = []
# 
#     def emit(self, *args, **kwargs):
#         """Call all the listeners with the given args and kwargs."""
#         for listener in self._listeners:
#             listener(*args, **kwargs)
# 
#     def register(self, listener):
#         """Register the given function object if it is not yet registered."""
#         if not self.is_registered(listener):
#             self._listeners.append(listener)
# 
#     def deregister(self, listener):
#         """Deregister the given function object if it is registered."""
#         if self.is_registered(listener):
#             self._listeners.pop(self._listeners.index(listener))
#             log.debug("{0} deregistered".format(listener))
#         else:
#             log.exception("{0} was never registered".format(listener))
# 
#     def is_registered(self, listener):
#         """Whether the given function object is already registered."""
#         return listener in self._listeners
# 
#     def clear(self):
#         """Remove all listeners from this event."""
#         for listener in self._listeners:
#             self.deregister(listener)


from flowpipe.event import Event


def test_listeners_only_registered_once():
    def listener(x, y):
        pass

    event = Event("test")
    event.register(listener)
    event.register(listener)
    assert event.is_registered(listener)
    assert 1 == len(event._listeners)


def test_deregister_if_registered():
    def listener(x, y):
        pass

    event = Event("test")
    event.register(listener)
    event.deregister(listener)
    assert not event.is_registered(listener)

    event.deregister(listener)


def test_event_emitt():
    def listener(arg, kwarg):
        assert arg == 123
        assert kwarg == "test"


# Path: flowpipe/event.py
# class Event(object):
#     """Very simple implementation of an event system.event
# 
#     The event simply calls the registered functions with the given arguments.
#     Please note that the integrity of the listeners is not enforced or checked.
#     """
# 
#     def __init__(self, name):
#         """Initialize the list of listeners
# 
#         Args:
#             name (str): The (unique) name of the signal
#         """
#         self.name = name
#         self._listeners = []
# 
#     def emit(self, *args, **kwargs):
#         """Call all the listeners with the given args and kwargs."""
#         for listener in self._listeners:
#             listener(*args, **kwargs)
# 
#     def register(self, listener):
#         """Register the given function object if it is not yet registered."""
#         if not self.is_registered(listener):
#             self._listeners.append(listener)
# 
#     def deregister(self, listener):
#         """Deregister the given function object if it is registered."""
#         if self.is_registered(listener):
#             self._listeners.pop(self._listeners.index(listener))
#             log.debug("{0} deregistered".format(listener))
#         else:
#             log.exception("{0} was never registered".format(listener))
# 
#     def is_registered(self, listener):
#         """Whether the given function object is already registered."""
#         return listener in self._listeners
# 
#     def clear(self):
#         """Remove all listeners from this event."""
#         for listener in self._listeners:
#             self.deregister(listener)


# Path: tests/test_event.py
from flowpipe.event import Event



def test_listeners_only_registered_once():
    def listener(x, y):
        pass

    event = Event("test")
    event.register(listener)
    event.register(listener)
    assert event.is_registered(listener)
    assert 1 == len(event._listeners)


def test_deregister_if_registered():
    def listener(x, y):
        pass

    event = Event("test")
    event.register(listener)
    event.deregister(listener)
    assert not event.is_registered(listener)

    event.deregister(listener)


def test_event_emitt():
    def listener(arg, kwarg):
        assert arg == 123
        assert kwarg == "test"


    event = Event("test")