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.")
def test_get(self): """ You can get objects. """ world = World(MagicMock()) obj = world.create('foo') obj2 = world.get(obj['id']) self.assertEqual(obj, obj2)
def test_get(self): """ You can get objects. """ world = World(MagicMock()) obj = world.create('foo') obj2 = world.get(obj['id']) self.assertEqual(obj, obj2)
def test_basic(self): """ Should return information about the object. """ world = World(MagicMock()) thing = world.create('thing') r = LookAt(thing['id'], thing['id']).execute(world) self.assertEqual(r, world.get(thing['id']))
def test_basic(self): """ Should return information about the object. """ world = World(MagicMock()) thing = world.create('thing') r = LookAt(thing['id'], thing['id']).execute(world) self.assertEqual(r, world.get(thing['id']))
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_delAttr(self): """ You can delete attributes. """ ev = MagicMock() world = World(ev) obj = world.create('foo') world.setAttr(obj['id'], 'foo', 'bar') ev.reset_mock() world.delAttr(obj['id'], 'foo') ev.assert_any_call(AttrDel(obj['id'], 'foo')) self.assertNotIn('foo', world.get(obj['id']))
def test_delAttr(self): """ You can delete attributes. """ ev = MagicMock() world = World(ev) obj = world.create('foo') world.setAttr(obj['id'], 'foo', 'bar') ev.reset_mock() world.delAttr(obj['id'], 'foo') ev.assert_any_call(AttrDel(obj['id'], 'foo')) self.assertNotIn('foo', world.get(obj['id']))
def test_removeItem(self): """ You can remove items from a list """ ev = MagicMock() world = World(ev) obj = world.create('foo') world.addItem(obj['id'], 'foo', 'bar') ev.reset_mock() world.removeItem(obj['id'], 'foo', 'bar') ev.assert_any_call(ItemRemoved(obj['id'], 'foo', 'bar')) self.assertEqual(world.get(obj['id'])['foo'], [])
def test_setAttr(self): """ You can set the value of an attribute. """ ev = MagicMock() world = World(ev) obj = world.create('foo') ev.reset_mock() world.setAttr(obj['id'], 'foo', 'bar') ev.assert_any_call(AttrSet(obj['id'], 'foo', 'bar')) obj = world.get(obj['id']) self.assertEqual(obj['foo'], 'bar')
def test_setAttr(self): """ You can set the value of an attribute. """ ev = MagicMock() world = World(ev) obj = world.create('foo') ev.reset_mock() world.setAttr(obj['id'], 'foo', 'bar') ev.assert_any_call(AttrSet(obj['id'], 'foo', 'bar')) obj = world.get(obj['id']) self.assertEqual(obj['foo'], 'bar')
def test_removeItem(self): """ You can remove items from a list """ ev = MagicMock() world = World(ev) obj = world.create('foo') world.addItem(obj['id'], 'foo', 'bar') ev.reset_mock() world.removeItem(obj['id'], 'foo', 'bar') ev.assert_any_call(ItemRemoved(obj['id'], 'foo', 'bar')) self.assertEqual(world.get(obj['id'])['foo'], [])
def test_addItem(self): """ You can add items to a list """ ev = MagicMock() world = World(ev) obj = world.create('foo') ev.reset_mock() world.addItem(obj['id'], 'foo', 'bar') ev.assert_any_call(ItemAdded(obj['id'], 'foo', 'bar')) obj = world.get(obj['id']) self.assertEqual(obj['foo'], ['bar'])
def test_addItem(self): """ You can add items to a list """ ev = MagicMock() world = World(ev) obj = world.create('foo') ev.reset_mock() world.addItem(obj['id'], 'foo', 'bar') ev.assert_any_call(ItemAdded(obj['id'], 'foo', 'bar')) obj = world.get(obj['id']) self.assertEqual(obj['foo'], ['bar'])
def test_charge(self): """ Charging should add energy to the thing's energy pool and to the thing's created energy pool. """ world = World(MagicMock()) thing = world.create('thing') charge = Charge(thing['id']) charge.execute(world) self.assertEqual(len(thing['energy']), 1) self.assertEqual(thing['created_energy'], 1, "Should keep track " "of the energy it created") e = world.get(thing['energy'][0]) self.assertEqual(e['kind'], 'energy')
def test_charge(self): """ Charging should add energy to the thing's energy pool and to the thing's created energy pool. """ world = World(MagicMock()) thing = world.create('thing') charge = Charge(thing['id']) charge.execute(world) self.assertEqual(len(thing['energy']), 1) self.assertEqual(thing['created_energy'], 1, "Should keep track " "of the energy it created") e = world.get(thing['energy'][0]) self.assertEqual(e['kind'], 'energy')
def test_buildProtocol(self): """ When a protocol is made, the protocol should be given an Avatar that is connected to a 'bot' in the world. Also, the avatar should be given a copy of the available commands. """ world = World(MagicMock()) f = BotFactory(world, {'foo': 'bar'}) proto = f.buildProtocol(None) self.assertEqual(proto.factory, f) self.assertTrue(isinstance(proto, BotLineProtocol)) self.assertTrue(isinstance(proto.avatar, Avatar)) self.assertEqual(proto.avatar._world, world, "Should be connected to " "the world") self.assertNotEqual(proto.avatar._game_piece, None, "Should have a game" " piece") self.assertEqual(proto.avatar.availableCommands(), {'foo': 'bar'}) obj = world.get(proto.avatar._game_piece) self.assertEqual(obj['kind'], 'bot', "Should make a bot in the world") self.assertTrue(isinstance(proto.event_transformer, ToStringTransformer))