Beispiel #1
0
def test_emit_records_call():
    s = Signal()
    s.add('foo', record=True)
    s.add('bar', record=True)
    s.add('baz', record=False)
    assert s.emissions == ()
    s.emit('foo', 123)
    s.emit('bar', 'hello', 'world')
    s.emit('foo', 'and', this='that')
    s.emit('baz', 0)
    s.emit('bar', four=56)
    assert s.emissions == (
        ('foo', {
            'args': (123, ),
            'kwargs': {}
        }),
        ('bar', {
            'args': ('hello', 'world'),
            'kwargs': {}
        }),
        ('foo', {
            'args': ('and', ),
            'kwargs': {
                'this': 'that'
            }
        }),
        ('bar', {
            'args': (),
            'kwargs': {
                'four': 56
            }
        }),
    )
Beispiel #2
0
def test_emit_to_single_callback(args, kwargs):
    s = Signal('foo', 'bar')
    cb = Mock()
    s.register('foo', cb)
    s.emit('foo', *args, **kwargs)
    assert cb.call_args_list == [call(*args, **kwargs)]
    s.emit('bar', *args, **kwargs)
    assert cb.call_args_list == [call(*args, **kwargs)]
Beispiel #3
0
def test_emit_to_multiple_callbacks(args, kwargs):
    s = Signal('foo', 'bar')
    cb = Mock()
    s.register('foo', cb.a)
    s.register('foo', cb.b)
    s.register('foo', cb.c)
    s.register('bar', cb.d)
    s.register('bar', cb.e)
    s.register('bar', cb.f)
    s.emit('foo', *args, **kwargs)
    assert cb.mock_calls == [
        call.a(*args, **kwargs),
        call.b(*args, **kwargs),
        call.c(*args, **kwargs),
    ]
    s.emit('bar')
    assert cb.mock_calls == [
        call.a(*args, **kwargs),
        call.b(*args, **kwargs),
        call.c(*args, **kwargs),
        call.d(),
        call.e(),
        call.f(),
    ]
Beispiel #4
0
def test_emit_to_no_callback(args, kwargs):
    s = Signal('foo')
    s.emit('foo', *args, **kwargs)
Beispiel #5
0
def test_suspend_blocks_emissions():
    s = Signal('foo', 'bar', 'baz')
    cb = Mock()
    s.register('foo', cb.foo)
    s.register('bar', cb.bar)
    s.register('baz', cb.baz)

    s.emit('foo', 123, this='that')
    s.emit('bar', 456, hey='ho')
    s.emit('baz', 789, zip='zop')
    assert cb.mock_calls == [
        call.foo(123, this='that'),
        call.bar(456, hey='ho'),
        call.baz(789, zip='zop'),
    ]
    cb.reset_mock()

    with s.suspend('bar', 'foo'):
        s.emit('foo', 123, this='that')
        s.emit('bar', 456, hey='ho')
        s.emit('baz', 789, zip='zop')
    assert cb.mock_calls == [
        call.baz(789, zip='zop'),
    ]
    cb.reset_mock()

    s.emit('foo', 123, this='that')
    s.emit('bar', 456, hey='ho')
    s.emit('baz', 789, zip='zop')
    assert cb.mock_calls == [
        call.foo(123, this='that'),
        call.bar(456, hey='ho'),
        call.baz(789, zip='zop'),
    ]