コード例 #1
0
async def test_04_signal_with_methods(events):
    asignal = Signal()

    class A(object):
        def __init__(self, name):
            self.ev = events[name]

        called = False

        async def handler(self, arg, kw):
            self.called = (arg, kw)
            self.ev.set()

    a1 = A('a1')
    a2 = A('a2')

    asignal.connect(a1.handler)
    asignal.connect(a2.handler)

    assert len(asignal.subscribers) == 2

    await asignal.notify(1, kw='a')
    await events.wait()

    assert a1.called == (1, 'a')
    assert a2.called == (1, 'a')
コード例 #2
0
def test_13_disconnect_wrapper():

    c = dict(called=0, disconnect_handler=None)

    asignal = Signal()

    @asignal.on_disconnect
    def asignal(handler, subscribers, disconnect, notify):
        c['called'] += 1
        c['disconnect_handler'] = handler
        assert len(subscribers) == 1
        disconnect(handler)
        return 'foo'

    def handler(*args, **kwargs):
        pass

    asignal.connect(handler)
    res = asignal.disconnect(handler)

    assert res == 'foo'
    assert c['called'] == 1
    assert c['disconnect_handler'] == handler
    assert len(asignal.subscribers) == 0

    c = dict(called=0, disconnect_handler=None)

    class A(metaclass=SignalAndHandlerInitMeta):

        click = Signal()

        @click.on_disconnect
        def click(self, handler, subscribers, disconnect, notify):
            c['called'] += 1
            c['disconnect_handler'] = handler
            assert len(subscribers) == 1
            disconnect(handler)
            return 'foo'

        @handler('click')
        def handler(self, *args, **kwargs):
            pass

    a = A()

    def handler2(*args, **kwargs):
        pass

    a.click.connect(handler2)
    res = a.click.disconnect(handler2)
    assert res == 'foo'
    assert c['called'] == 1
    assert c['disconnect_handler'] == handler2
    # class-level handlers are excluded
    assert len(a.click.subscribers) == 0
コード例 #3
0
async def test_01_signal_with_functions(events):
    asignal = Signal()
    c = dict(called1=False, called2=False)

    def handler1(arg, kw):
        # let python get the outer var here without using PY3 "nonlocal"
        c['called1'] = (arg, kw)

    def handler2(arg, kw):
        c['called2'] = (arg, kw)

    asignal.connect(handler1)
    asignal.connect(handler2)

    assert len(asignal.subscribers) == 2

    await asignal.notify(1, kw='a')
    assert c['called1'] == (1, 'a')
    assert c['called2'] == (1, 'a')
コード例 #4
0
async def test_03_signal_with_mixed_functions(events):
    asignal = Signal()
    c = dict(called1=False, called2=False)
    events.define('h1')

    async def handler1(arg, kw):
        c['called1'] = (arg, kw)
        events.h1.set()

    def handler2(arg, kw):
        c['called2'] = (arg, kw)

    asignal.connect(handler1)
    asignal.connect(handler2)

    assert len(asignal.subscribers) == 2

    await asignal.notify(1, kw='a')
    assert c['called2'] == (1, 'a')
    await events.wait()
    assert c['called1'] == (1, 'a')
コード例 #5
0
async def test_12_connect_wrapper(events):

    c = dict(called=0,
             connect_handler=None,
             handler_called=0,
             handler_args=None)

    asignal = Signal()

    @asignal.on_connect
    def asignal(handler, subscribers, connect, notify):
        c['called'] += 1
        c['connect_handler'] = handler
        assert len(subscribers) == 0
        connect(handler)
        return 'foo'

    def handler(*args, **kwargs):
        c['handler_called'] += 1
        c['handler_args'] = (args, kwargs)

    res = asignal.connect(handler)
    res2 = await asignal.notify('bar', k=1)

    assert res == 'foo'
    assert c['called'] == 1
    assert c['connect_handler'] == handler
    assert c['handler_called'] == 1
    assert c['handler_args'] == (('bar', ), {'k': 1})

    c = dict(called=0,
             connect_handler=None,
             handler_called=0,
             handler_args=None,
             handler2_called=0,
             handler2_args=None)

    class A(metaclass=SignalAndHandlerInitMeta):

        click = Signal()

        @click.on_connect
        def click(self, handler, subscribers, connect, notify):
            c['called'] += 1
            c['connect_handler'] = handler
            assert len(subscribers) == 0
            connect(handler)
            return 'foo'

        @handler('click')
        def handler(self, *args, **kwargs):
            c['handler_called'] += 1
            c['handler_args'] = (args, kwargs)

    a = A()

    def handler2(*args, **kwargs):
        c['handler2_called'] += 1
        c['handler2_args'] = (args, kwargs)

    res = a.click.connect(handler2)
    res2 = await a.click.notify('bar', k=1)

    assert res == 'foo'
    assert c['called'] == 1
    assert c['handler_called'] == 1
    assert c['connect_handler'] == handler2
    assert c['handler_args'] == (('bar', ), {'k': 1})
    assert c['handler2_called'] == 1
    assert c['handler2_args'] == (('bar', ), {'k': 1})
コード例 #6
0
def test_11_notify_wrapper(events):

    c = dict(called=0, wrap_args=None, handler_called=0, handler_args=None)

    asignal = Signal()

    @asignal.on_notify
    def asignal(subscribers, notify, *args, **kwargs):
        c['called'] += 1
        c['wrap_args'] = (args, kwargs)
        assert len(subscribers) == 1
        notify('foo', k=2)
        return 'foo'

    def handler(*args, **kwargs):
        c['handler_called'] += 1
        c['handler_args'] = (args, kwargs)

    asignal.connect(handler)
    res = asignal.notify('bar', k=1)

    assert res == 'foo'
    assert c['called'] == 1
    assert c['wrap_args'] == (('bar', ), {'k': 1})
    assert c['handler_called'] == 1
    assert c['handler_args'] == (('foo', ), {'k': 2})

    c = dict(called=0,
             wrap_args=None,
             handler_called=0,
             handler_args=None,
             handler2_called=0,
             handler2_args=None)

    class A(metaclass=SignalAndHandlerInitMeta):

        click = Signal()

        @click.on_notify
        def click(self, subscribers, notify, *args, **kwargs):
            c['called'] += 1
            c['wrap_args'] = (args, kwargs)
            assert len(subscribers) == 2
            assert isinstance(self, A)
            notify('foo', k=2)
            return 'foo'

        @handler('click')
        def handler(self, *args, **kwargs):
            c['handler_called'] += 1
            c['handler_args'] = (args, kwargs)

    a = A()

    def handler2(*args, **kwargs):
        c['handler2_called'] += 1
        c['handler2_args'] = (args, kwargs)

    a.click.connect(handler2)
    res = a.click.notify('bar', k=1)
    assert res == 'foo'
    assert c['called'] == 1
    assert c['wrap_args'] == (('bar', ), {'k': 1})
    assert c['handler_called'] == 1
    assert c['handler_args'] == (('foo', ), {'k': 2})
    assert c['handler2_called'] == 1
    assert c['handler2_args'] == (('foo', ), {'k': 2})