def test_emit_receiveOnce(self): """ Events should only be received by each object once. """ world = World(MagicMock()) obj1 = world.create('foo')['id'] obj2 = world.create('foo')['id'] obj1_received = [] world.receiveFor(obj1, obj1_received.append) obj2_received = [] world.receiveFor(obj2, obj2_received.append) # obj1 emits everything it receives world.receiveFor(obj1, world.emitterFor(obj1)) # obj2 emits everything it receives world.receiveFor(obj2, world.emitterFor(obj2)) # obj2 receives emissions from obj1 world.subscribeTo(obj1, world.receiverFor(obj2)) # obj1 receives emissions from obj2 world.subscribeTo(obj2, world.receiverFor(obj1)) # we have a nice loop set up. When this test fails, it is likely to # continue spinning forever. world.emit('echo', obj1) self.assertEqual(obj1_received, ['echo'], "Should have received the " "message once") self.assertEqual(obj2_received, ['echo'], "Should have received the " "message once")
def test_emit(self): """ All emissions are sent to my event_receiver """ ev = MagicMock() world = World(ev) world.emit('something', 'foo') ev.assert_called_once_with('something')
def test_emit_toEngine(self): """ All emissions are sent to the engine. """ ev = MagicMock() engine = MagicMock() world = World(ev, engine) world.emit('foo', 'object_id') engine.worldEventReceived.assert_called_once_with(world, 'foo')
def test_disable_selfReceiving(self): """ You can disable self-receipt by creating things with a special arg. """ world = World(MagicMock()) thing = world.create('foo', receive_emissions=False) called = [] world.receiveFor(thing['id'], called.append) world.emit('foo', thing['id']) self.assertEqual(called, [], "Should not receive because it was " "disabled on creation.")
def test_selfReceiving(self): """ All things should receive their own events by default. """ world = World(MagicMock()) thing = world.create('foo') called = [] world.receiveFor(thing['id'], called.append) world.emit('foo', thing['id']) self.assertEqual(called, ['foo'], "Things should receive their own " "emissions")
def test_emit_oneEventAtATime(self): """ Only one event should be emitted at a time. If an event emission causes another event to be emitted, it should be appended to a queue and be emitted after the current event is finished. """ world = World(MagicMock()) obj1 = world.create('foo')['id'] obj2 = world.create('foo')['id'] obj3 = world.create('foo')['id'] obj1_received = [] world.receiveFor(obj1, obj1_received.append) obj2_received = [] world.receiveFor(obj2, obj2_received.append) obj3_received = [] world.receiveFor(obj3, obj3_received.append) # obj1 will emit to # obj2 and obj3 world.subscribeTo(obj1, world.receiverFor(obj2)) world.subscribeTo(obj1, world.receiverFor(obj3)) # obj2 will emit to # obj1 and obj3 world.subscribeTo(obj2, world.receiverFor(obj1)) world.subscribeTo(obj2, world.receiverFor(obj3)) # 1_ # /|\ # / \ # |/_ _\| # 3 <---- 2 # obj2 will emit "obj2" every time he receives an event def noisy(_): world.emit('obj2', obj2) world.receiveFor(obj2, noisy) # If things are working properly, then obj3 will receive # the 'obj2' emissions AFTER it receives the event from obj1 world.emit('obj1', obj1) self.assertEqual(obj1_received, ['obj1', 'obj2'], "obj1 should receive obj1 from self, then obj2") self.assertEqual(obj2_received, ['obj1', 'obj2'], "obj2 should receive obj1, then obj2 from self") self.assertEqual(obj3_received, ['obj1', 'obj2'], "obj3 should receiver obj1 first, then obj2")
def test_execute_inRoom(self): """ If an object is already located in another container, then moving it should: - Unsubscribe from the location's events - Unsubscribe the location from the thing's events - Cause C{contents} to be updated. """ world = World(MagicMock()) thing = world.create('thing')['id'] room1 = world.create('room1')['id'] room2 = world.create('room2')['id'] move1 = Move(thing, room1) move1.execute(world) move2 = Move(thing, room2) move2.execute(world) room1_called = [] world.receiveFor(room1, room1_called.append) room2_called = [] world.receiveFor(room2, room2_called.append) thing_called = [] world.receiveFor(thing, thing_called.append) # emit from room1 world.emit('foo', room1) self.assertEqual(thing_called, [], "Thing should detach from previous " "room events") room1_called.pop() # emit from thing world.emit('bar', thing) self.assertEqual(thing_called, ['bar'], "Thing should see own events") self.assertEqual(room1_called, [], "Room1 should detach from thing" " events") # room1 contents room1_obj = world.get(room1) self.assertEqual(room1_obj['contents'], [], "Should remove from " "contents of room1") location = world.get(thing)['location'] self.assertEqual( location, room2, "Should update the " "location of the thing. This is mostly to confirm " "that the default moving behavior tested in the other " "test happened.")
def test_onEvent_cancel(self): """ You can cancel the deferred returned by onEvent """ world = World(MagicMock()) obj = world.create('foo') d = world.onEvent(obj['id'], 'hey') d.cancel() world.emit('hey', obj['id']) self.assertFailure(d, defer.CancelledError)
def test_disable_selfReceiving(self): """ You can disable self-receipt by creating things with a special arg. """ world = World(MagicMock()) thing = world.create('foo', receive_emissions=False) called = [] world.receiveFor(thing['id'], called.append) world.emit('foo', thing['id']) self.assertEqual( called, [], "Should not receive because it was " "disabled on creation.")
def test_execute_inRoom(self): """ If an object is already located in another container, then moving it should: - Unsubscribe from the location's events - Unsubscribe the location from the thing's events - Cause C{contents} to be updated. """ world = World(MagicMock()) thing = world.create('thing')['id'] room1 = world.create('room1')['id'] room2 = world.create('room2')['id'] move1 = Move(thing, room1) move1.execute(world) move2 = Move(thing, room2) move2.execute(world) room1_called = [] world.receiveFor(room1, room1_called.append) room2_called = [] world.receiveFor(room2, room2_called.append) thing_called = [] world.receiveFor(thing, thing_called.append) # emit from room1 world.emit('foo', room1) self.assertEqual(thing_called, [], "Thing should detach from previous " "room events") room1_called.pop() # emit from thing world.emit('bar', thing) self.assertEqual(thing_called, ['bar'], "Thing should see own events") self.assertEqual(room1_called, [], "Room1 should detach from thing" " events") # room1 contents room1_obj = world.get(room1) self.assertEqual(room1_obj['contents'], [], "Should remove from " "contents of room1") location = world.get(thing)['location'] self.assertEqual(location, room2, "Should update the " "location of the thing. This is mostly to confirm " "that the default moving behavior tested in the other " "test happened.")
def test_onEvent(self): """ You can be notified when a certain event happens. """ world = World(MagicMock()) obj = world.create('foo')['id'] d = world.onEvent(obj, 'event') self.assertEqual(d.called, False) world.emit('event', obj) self.assertEqual(self.successResultOf(d), 'event') # shouldn't die calling again world.emit('event', obj)
def test_execute_Deferred(self): """ If a command returns a successful deferred, wait to emit. """ engine = MagicMock() d = defer.Deferred() engine.execute.return_value = d world = World(MagicMock(), engine) world.emit = MagicMock() action = MagicMock() action.emitters.return_value = ['I did it'] r = world.execute(action) self.assertEqual(r, d, "Should return the result of the execution") engine.execute.assert_called_once_with(world, action) self.assertEqual(world.emit.call_count, 0, "Should not have emitted " "the ActionPerformed event yet, because it hasn't " "finished") d.callback('foo') self.assertEqual(self.successResultOf(r), 'foo', "Should return the result of execution") world.emit.assert_called_once_with(ActionPerformed(action), 'I did it')
def test_execute_Deferred(self): """ If a command returns a successful deferred, wait to emit. """ engine = MagicMock() d = defer.Deferred() engine.execute.return_value = d world = World(MagicMock(), engine) world.emit = MagicMock() action = MagicMock() action.emitters.return_value = ['I did it'] r = world.execute(action) self.assertEqual(r, d, "Should return the result of the execution") engine.execute.assert_called_once_with(world, action) self.assertEqual( world.emit.call_count, 0, "Should not have emitted " "the ActionPerformed event yet, because it hasn't " "finished") d.callback('foo') self.assertEqual(self.successResultOf(r), 'foo', "Should return the result of execution") world.emit.assert_called_once_with(ActionPerformed(action), 'I did it')
def test_thingsInTheSameRoom(self): """ Things in the same location should see events emitted by each other. """ world = World(MagicMock()) room = world.create('a')['id'] thing1 = world.create('thing')['id'] thing2 = world.create('thing')['id'] Move(thing1, room).execute(world) Move(thing2, room).execute(world) c1 = [] world.receiveFor(thing1, c1.append) c2 = [] world.receiveFor(thing2, c2.append) world.emit('foo', thing1) self.assertEqual(c1, ['foo']) self.assertEqual(c2, ['foo'])
def test_emit_Exception(self): """ Exceptions caused by event receivers should not prevent other event receivers from receiving the events. """ ev1 = MagicMock() ev1.side_effect = Exception() world = World(ev1) ev2 = MagicMock() ev2.side_effect = Exception() world.subscribeTo('1234', ev2) ev3 = MagicMock() world.subscribeTo('1234', ev3) world.emit('hey', '1234') ev1.assert_called_once_with('hey') ev2.assert_called_once_with('hey') ev3.assert_called_once_with('hey')
def test_subscribeTo(self): """ You can subscribe to the events that are emitted by a particular object. """ ev = MagicMock() world = World(ev) obj = world.create('foo') called = [] world.subscribeTo(obj['id'], called.append) ev.reset_mock() world.emit('event', obj['id']) self.assertEqual(called, ['event']) ev.assert_called_once_with('event') ev.reset_mock() world.unsubscribeFrom(obj['id'], called.append) world.emit('event', obj['id']) self.assertEqual(called, ['event'], "Should not have changed") ev.assert_called_once_with('event')
def test_execute(self): """ Moving to a new location should: - Cause C{location} to be updated - Cause C{contents} to be updated - Subscribe the thing to the location's events - Subscribe the location to the thing's events """ world = World(MagicMock()) thing = world.create('thing') room = world.create('room') move = Move(thing['id'], room['id']) move.execute(world) self.assertEqual(thing['location'], room['id'], "Should set location") self.assertEqual(room['contents'], [thing['id']], "Should set contents") room_called = [] world.receiveFor(room['id'], room_called.append) thing_called = [] world.receiveFor(thing['id'], thing_called.append) world.emit('foo', room['id']) self.assertEqual(room_called, ['foo'], "Room should receive room events") self.assertEqual(thing_called, ['foo'], "Thing should receive room events") room_called.pop() thing_called.pop() world.emit('foo', thing['id']) self.assertEqual(room_called, ['foo'], "Room should receive thing events") self.assertEqual(thing_called, ['foo'], "Thing should receive thing events")
def test_moveToNone(self): """ Moving something to nowhere should result in the location being None. """ world = World(MagicMock()) thing = world.create('thing')['id'] room1 = world.create('room1')['id'] move1 = Move(thing, room1) move1.execute(world) move2 = Move(thing, None) move2.execute(world) room1_called = [] world.receiveFor(room1, room1_called.append) thing_called = [] world.receiveFor(thing, thing_called.append) # emit from room1 world.emit('foo', room1) self.assertEqual(thing_called, [], "Thing should detach from previous " "room events") room1_called.pop() # emit from thing world.emit('bar', thing) self.assertEqual(thing_called, ['bar'], "Thing should see own events") self.assertEqual(room1_called, [], "Room1 should detach from thing" " events") # room1 contents room1_obj = world.get(room1) self.assertEqual(room1_obj['contents'], [], "Should remove from " "contents of room1") location = world.get(thing)['location'] self.assertEqual(location, None, "Should update the location")
def test_setGamePiece(self): """ Connecting an avatar to a game piece will make the avatar subscribe to the events of that game piece. """ world = World(MagicMock()) a = Avatar(world) a.eventReceived = MagicMock() piece = world.create('foo', receive_emissions=False) a.setGamePiece(piece['id']) self.assertEqual(a._game_piece, piece['id']) world.eventReceived('hey', piece['id']) a.eventReceived.assert_called_once_with('hey') a.eventReceived.reset_mock() world.emit('foo', piece['id']) self.assertEqual( a.eventReceived.call_count, 0, "Should only receive " "things the game piece receives, not subscribe to " "things the game piece emits.")
def test_execute(self): """ Asking the world to execute a command will ask the (game) engine to execute the command. """ engine = MagicMock() engine.execute.return_value = 'response' world = World(MagicMock(), engine) world.emit = MagicMock() action = MagicMock() action.emitters.return_value = ['I did it'] r = world.execute(action) engine.execute.assert_called_once_with(world, action) self.assertEqual(self.successResultOf(r), 'response', "Should return the result of execution") world.emit.assert_called_once_with(ActionPerformed(action), 'I did it')