# Path: flatland/util/signals.py
# ANY = symbol('ANY')
# ANY_ID = 0
#     ANY = ANY
# class Signal(object):
# class NamedSignal(Signal):
# class Namespace(object):
#     def __init__(self, doc=None):
#     def connect(self, receiver, sender=ANY, weak=True):
#     def send(self, sender=None, **kwargs):
#     def has_receivers_for(self, sender):
#     def receivers_for(self, sender):
#     def disconnect(self, receiver, sender=ANY):
#     def _disconnect(self, receiver_id, sender_id):
#     def _cleanup_receiver(self, receiver_ref):
#     def _cleanup_sender(self, sender_ref):
#     def _clear_state(self):
#     def __init__(self, name, doc=None):
#     def __repr__(self):
#     def __init__(self):
#     def signal(self, name, doc=None):
# def _hashable_identity(obj):


from flatland.util.signals import (
    ANY,
    ANY_ID,
    NamedSignal,
    Namespace,
    Signal,
    receiver_connected,
    )
from tests._util import eq_, assert_raises


def test_meta_connect_failure():

    def meta_received(**kw):
        raise TypeError('boom')

    assert not receiver_connected.receivers
    receiver_connected.connect(meta_received)

    def receiver(**kw):
        pass
    sig = Signal()

    assert_raises(TypeError, sig.connect, receiver)
    assert not sig.receivers
    assert not sig._by_receiver
    eq_(sig._by_sender, {ANY_ID: set()})

    receiver_connected._clear_state()


def test_singletons():
    ns = Namespace()
    assert not ns.signals
    s1 = ns.signal('abc')
    assert s1 is ns.signal('abc')
    assert s1 is not ns.signal('def')
    assert 'abc' in ns.signals
    # weak by default, already out of scope

# Path: flatland/util/signals.py
# ANY = symbol('ANY')
# ANY_ID = 0
#     ANY = ANY
# class Signal(object):
# class NamedSignal(Signal):
# class Namespace(object):
#     def __init__(self, doc=None):
#     def connect(self, receiver, sender=ANY, weak=True):
#     def send(self, sender=None, **kwargs):
#     def has_receivers_for(self, sender):
#     def receivers_for(self, sender):
#     def disconnect(self, receiver, sender=ANY):
#     def _disconnect(self, receiver_id, sender_id):
#     def _cleanup_receiver(self, receiver_ref):
#     def _cleanup_sender(self, sender_ref):
#     def _clear_state(self):
#     def __init__(self, name, doc=None):
#     def __repr__(self):
#     def __init__(self):
#     def signal(self, name, doc=None):
# def _hashable_identity(obj):


# Path: tests/test_signals.py
from flatland.util.signals import (
    ANY,
    ANY_ID,
    NamedSignal,
    Namespace,
    Signal,
    receiver_connected,
    )
from tests._util import eq_, assert_raises



def test_meta_connect_failure():

    def meta_received(**kw):
        raise TypeError('boom')

    assert not receiver_connected.receivers
    receiver_connected.connect(meta_received)

    def receiver(**kw):
        pass
    sig = Signal()

    assert_raises(TypeError, sig.connect, receiver)
    assert not sig.receivers
    assert not sig._by_receiver
    eq_(sig._by_sender, {ANY_ID: set()})

    receiver_connected._clear_state()


def test_singletons():
    ns = Namespace()
    assert not ns.signals
    s1 = ns.signal('abc')
    assert s1 is ns.signal('abc')
    assert s1 is not ns.signal('def')
    assert 'abc' in ns.signals
    # weak by default, already out of scope

    assert 'def' not in ns.signals