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_envelope_ids(self): """ You can read/write on the envelope of things in the world (using their id). """ world = World(MagicMock()) obj_id = world.create('foo')['id'] env = world.envelope(obj_id) self.assertTrue(isinstance(env, dict)) env['foo'] = 'bar' # destruction should destroy the envelope too world.destroy(obj_id) self.assertRaises(KeyError, world.envelope, obj_id)
def test_energyDestroyed(self): """ When energy is destroyed, it should decrement the creator's created_energy amount. """ world = World(MagicMock()) thing = world.create('thing') Charge(thing['id']).execute(world) energy = thing['energy'][0] world.destroy(energy) self.assertEqual(thing['energy'], [], "Should remove the energy from " "the energy list of the user") self.assertEqual(thing['created_energy'], 0, "Should decrement " "the created_energy attribute")
def test_energyDestroyed(self): """ When energy is destroyed, it should decrement the creator's created_energy amount. """ world = World(MagicMock()) thing = world.create('thing') Charge(thing['id']).execute(world) energy = thing['energy'][0] world.destroy(energy) self.assertEqual( thing['energy'], [], "Should remove the energy from " "the energy list of the user") self.assertEqual(thing['created_energy'], 0, "Should decrement " "the created_energy attribute")
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_sharedEnergy_destroyed(self): """ When shared energy is destroyed, it should be removed from the energy pool of whoever has it and still decrement the creator's created_energy amount. """ world = World(MagicMock()) giver = world.create('thing') receiver = world.create('thing') Charge(giver['id']).execute(world) ShareEnergy(giver['id'], receiver['id'], 1).execute(world) e = receiver['energy'][0] world.destroy(e) self.assertEqual(giver['created_energy'], 0, "Should decrement creator's created count") self.assertEqual(len(receiver['energy']), 0, "Should deplete receiver's energy")
class UsePortalTest(TestCase): def test_IAction(self): verifyObject(IAction, UsePortal('me', 'portal_id')) def test_emitters(self): self.assertEqual(UsePortal('me', 'portal_id').emitters(), ['me']) def test_subject(self): self.assertEqual(UsePortal('foo', 'bar').subject(), 'foo') def usedPortal(self): self.world = World(MagicMock()) self.place = self.world.create('place') self.ore = self.world.create('ore') self.bot = self.world.create('bot') self.lander = self.world.create('lander') Move(self.ore['id'], self.place['id']).execute(self.world) OpenPortal(self.bot['id'], self.ore['id'], self.lander['id']).execute(self.world) UsePortal(self.lander['id'], self.ore['id']).execute(self.world) def test_use(self): """ Using a portal will cause the thing that used it to be moved to the location where the portal is, remove the portal_user attribute from the portal. """ self.usedPortal() lander = self.lander ore = self.ore self.assertEqual(lander['location'], ore['location'], "Should move " "the lander into the location") def test_portalDestroyed(self): """ If a portal is destroyed, the user of the portal is sent to the void. """ self.usedPortal() # destroy the portal self.world.destroy(self.ore['id']) self.assertEqual(self.lander['location'], None, "Should send lander " "to the void") def test_portalKilled(self): """ If a portal is killed (by hp reaching 0) the user of the portal is sent to the void and the portal reverts to ore. """ self.usedPortal() # kill the portal self.world.setAttr(self.ore['id'], 'hp', 0) self.assertEqual(self.lander['location'], None, "Should send lander " "to the void") self.assertEqual(self.ore['kind'], 'ore', "Should revert to ore") self.assertNotIn('portal_user', self.ore, "Should delete portal_user " "attribute") def test_portal_user_noMatch(self): """ It is NotAllowed to use a portal with a portal_user different than the thing trying to use the portal. """ world = World(MagicMock()) place = world.create('place') ore = world.create('ore') bot = world.create('bot') lander = world.create('lander') imposter = world.create('imposter') Move(ore['id'], place['id']).execute(world) OpenPortal(bot['id'], ore['id'], lander['id']).execute(world) self.assertRaises(NotAllowed, UsePortal(imposter['id'], ore['id']).execute, world) def test_openerDiesAfterUse(self): """ If the opener dies or is destroyed AFTER a portal is used, it should not affect the portal. """ self.usedPortal() # kill the opener (send them to the void) Move(self.bot['id'], None).execute(self.world) # destroy the opener self.world.destroy(self.bot['id']) self.assertEqual(self.ore['kind'], 'portal', "Should still be a portal") self.assertEqual(self.ore['portal_user'], self.lander['id'], "Should still be tied to the lander")