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'), ]
def test_replay_emits(): emissions = ( ('foo', { 'args': (123, ), 'kwargs': {} }), ('bar', { 'args': ('hello', 'world'), 'kwargs': {} }), ('foo', { 'args': ('and', ), 'kwargs': { 'this': 'that' } }), ('bar', { 'args': (), 'kwargs': { 'four': 56 } }), ) s = Signal('foo', 'bar', 'baz') cb = Mock() s.register('foo', cb.foo) s.register('bar', cb.bar) s.register('baz', cb.baz) s.replay(emissions) assert cb.mock_calls == [ call.foo(123), call.bar('hello', 'world'), call.foo('and', this='that'), call.bar(four=56), ]
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)]
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(), ]
def test_registering_noncallable(): s = Signal('foo') with pytest.raises(TypeError, match=r"^Not a callable: 'bar'$"): s.register('foo', 'bar')
def test_registering_for_unknown_signal(): s = Signal('foo') with pytest.raises(ValueError, match=r"^Unknown signal: 'bar'$"): s.register('bar', lambda: None)