def test_any(self): '''A callback bound to sa.ANY is triggered by any signal.''' actor = ActorAny() director = sa.Director() actor.emit('foobar') director.run() self.assertEqual([sa.INITIATE, 'foobar'], actor.messages)
def test_halt(self): '''Halting the directory process FINISH and reset the event queue.''' director = sa.Director() sa.global_event_queue.append('foo') # If processed --> ValueError with mock.patch.object(director, 'process_event') as mock_process: director.halt(sa.HALT, self) self.assertEqual([], list(sa.global_event_queue)) mock_process.assert_called_once_with((sa.FINISH, director, (), {}))
def test_kill(self): '''Killing an actor removes it completely from the registries.''' actor = ActorSingle() director = sa.Director() actor.emit(sa.KILL, target=actor) director.run() self.assertNotIn(actor, sa.global_actors) self.assertNotIn(actor.echo_callback, sa.global_callbacks['echo'])
def test_run(self): '''A callback is triggered on emitting the bound signal.''' actor = ActorSingle() director = sa.Director() a_dictionary = {} actor.emit('echo', a_dictionary=a_dictionary) director.run() self.assertEqual({'emitter': actor}, a_dictionary)
def test_actors(self): '''Director.actors return all actors withouth the director.''' first = ActorSingle() second = ActorSingle() third = ActorDouble() director = sa.Director() expected = set([first, second, third]) self.assertEqual(expected, director.actors)
def test_run_send_initiate_event(self): '''Running the Dirctor emit the INITIATE signal.''' director = sa.Director() with mock.patch.object(director, 'process_event') as mock_process: director.emit('second') director.run() expected = [sa.INITIATE, 'second'] actual = [c[0][0][0] for c in mock_process.call_args_list] self.assertEqual(expected, actual)
def test_process_event(self): '''Processing an events means triggering all callbacks.''' mock_cb_one = mock.MagicMock(name='one') mock_cb_two = mock.MagicMock(name='two') mock_cb_three = mock.MagicMock(name='three') sa.global_callbacks['foo'] = set([mock_cb_one, mock_cb_two]) sa.global_callbacks['bar'] = set([mock_cb_three]) director = sa.Director() event = ('foo', None, (1, 2), {'spam': 42}) director.process_event(event) mock_cb_one.assert_called_once_with('foo', None, 1, 2, spam=42) mock_cb_two.assert_called_once_with('foo', None, 1, 2, spam=42) self.assertFalse(mock_cb_three.called)
def test_run_effect_on_registries(self): '''Registries are left untouched by a simulation run.''' actor = ActorSingle() director = sa.Director() director.run() expected = { 'echo': set([actor.echo_callback]), sa.KILL: set([director.kill]), sa.HALT: set([director.halt]), sa.INITIATE: set(), sa.ANY: set() } self.assertEqual(expected, sa.global_callbacks) self.assertEqual(set([actor, director]), sa.global_actors) self.assertEqual(0, len(sa.global_event_queue))