def test_destroy_disableSubscribers(self): """ When an object is destroyed, things subscribed to its events will no longer receive events. """ world = World(MagicMock()) thing = world.create('foo') received = [] world.receiveFor(thing['id'], received.append) emitted = [] world.subscribeTo(thing['id'], emitted.append) receiver = world.receiverFor(thing['id']) emitter = world.emitterFor(thing['id']) world.destroy(thing['id']) received.pop() emitted.pop() receiver('foo') self.assertEqual(received, []) self.assertEqual(emitted, []) emitter('foo') self.assertEqual(received, []) self.assertEqual(emitted, [])
def test_destroy_disableSubscribers(self): """ When an object is destroyed, things subscribed to its events will no longer receive events. """ world = World(MagicMock()) thing = world.create('foo') received = [] world.receiveFor(thing['id'], received.append) emitted = [] world.subscribeTo(thing['id'], emitted.append) receiver = world.receiverFor(thing['id']) emitter = world.emitterFor(thing['id']) world.destroy(thing['id']) received.pop() emitted.pop() receiver('foo') self.assertEqual(received, []) self.assertEqual(emitted, []) emitter('foo') self.assertEqual(received, []) self.assertEqual(emitted, [])
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_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_receiverFor(self): """ You can get a function that will call eventReceived for a given object. """ world = World(MagicMock()) obj = world.create('foo') called = [] world.receiveFor(obj['id'], called.append) receiver = world.receiverFor(obj['id']) receiver('hey') self.assertEqual(called, ['hey'])
def test_receiverFor(self): """ You can get a function that will call eventReceived for a given object. """ world = World(MagicMock()) obj = world.create('foo') called = [] world.receiveFor(obj['id'], called.append) receiver = world.receiverFor(obj['id']) receiver('hey') self.assertEqual(called, ['hey'])
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_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_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_destroy(self): """ You can destroy an object, and be notified about it. """ ev = MagicMock() world = World(ev) obj = world.create('foo') ev.reset_mock() called = [] world.receiveFor(obj['id'], called.append) world.destroy(obj['id']) ev.assert_any_call(Destroyed(obj['id'])) self.assertEqual(called, [Destroyed(obj['id'])], "Should notify things" " receiving events for the object") self.assertNotIn(obj['id'], world.objects)
def test_destroy(self): """ You can destroy an object, and be notified about it. """ ev = MagicMock() world = World(ev) obj = world.create('foo') ev.reset_mock() called = [] world.receiveFor(obj['id'], called.append) world.destroy(obj['id']) ev.assert_any_call(Destroyed(obj['id'])) self.assertEqual( called, [Destroyed(obj['id'])], "Should notify things" " receiving events for the object") self.assertNotIn(obj['id'], world.objects)
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_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_receiveFor(self): """ You can subscribe to the events that are received by a particular object. """ ev = MagicMock() world = World(ev) obj = world.create('foo') called = [] world.receiveFor(obj['id'], called.append) world.eventReceived('event', obj['id']) self.assertEqual(called, ['event']) called.pop() world.stopReceivingFor(obj['id'], called.append) world.eventReceived('foo', obj['id']) self.assertEqual(called, [], "Should not receive")
def test_receiveFor(self): """ You can subscribe to the events that are received by a particular object. """ ev = MagicMock() world = World(ev) obj = world.create('foo') called = [] world.receiveFor(obj['id'], called.append) world.eventReceived('event', obj['id']) self.assertEqual(called, ['event']) called.pop() world.stopReceivingFor(obj['id'], called.append) world.eventReceived('foo', obj['id']) self.assertEqual(called, [], "Should not receive")
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_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(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_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_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_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_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.")