def test_execute_energyRequired_command_fails(self): """ If there is energy required and the command fails to run, the energy should not be consumed. """ e = self.friendlyEngine() # require some energy e.engine.energyRequirement.return_value = 2 # make some energy world = World(MagicMock()) actor = world.create('thing') yield Charge(actor['id']).execute(world) yield Charge(actor['id']).execute(world) self.assertEqual(len(actor['energy']), 2) # do the action and consume the energy action = MagicMock() action.subject.return_value = actor['id'] # synchronous action.execute.side_effect = NotAllowed('foo') self.assertFailure(e.execute(world, action), NotAllowed) self.assertEqual(len(actor['energy']), 2, "Should not have consumed " "the energy") # asynchronous action.execute.side_effect = None action.execute.return_value = defer.fail(NotAllowed('foo')) self.assertFailure(e.execute(world, action), NotAllowed) self.assertEqual(len(actor['energy']), 2, "Should not have consumed " "the energy")
def test_execute_energyRequired_succeed(self): """ If energy is required and the actor has enough energy, do the action. """ e = self.friendlyEngine() # require some energy e.engine.energyRequirement.return_value = 2 # make some energy world = World(MagicMock()) actor = world.create('thing') yield Charge(actor['id']).execute(world) yield Charge(actor['id']).execute(world) self.assertEqual(len(actor['energy']), 2) # do the action and consume the energy action = MagicMock() action.subject.return_value = actor['id'] action.execute.return_value = 'foo' ret = yield e.execute(world, action) self.assertEqual(ret, 'foo', "Should have executed the action") self.assertEqual(len(actor['energy']), 0, "Should have consumed " "the energy")
def test_execute(self): """ Consuming energy should simply destroy the energy. """ world = World(MagicMock()) thing = world.create('foo') Charge(thing['id']).execute(world) Charge(thing['id']).execute(world) energies = list(thing['energy']) ConsumeEnergy(thing['id'], 2).execute(world) self.assertNotIn(energies[0], world.objects) self.assertNotIn(energies[1], world.objects)
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_notEnough(self): """ It is an error to consume more energy than you have. """ world = World(MagicMock()) thing = world.create('foo') Charge(thing['id']).execute(world) energies = list(thing['energy']) self.assertRaises(NotEnoughEnergy, ConsumeEnergy(thing['id'], 2).execute, world) self.assertIn(energies[0], world.objects, "Should not have consumed " "the energy")
def test_share(self): """ Sharing energy should result in the energy being removed from the giver's energy pool and added to the receiver's energy pool. """ world = World(MagicMock()) giver = world.create('thing') receiver = world.create('thing') Charge(giver['id']).execute(world) ShareEnergy(giver['id'], receiver['id'], 1).execute(world) self.assertEqual(len(giver['energy']), 0, "Should deplete giver's energy") self.assertEqual(len(receiver['energy']), 1, "Should increase receiver's energy")
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_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")
def test_creator_dead(self): """ When the creator of energy is dead, the energy is also dead. XXX I'm not sure if this belongs in the game engine or not. Seems like a rule that could be changed. """ world = World(MagicMock()) thing = world.create('thing') Charge(thing['id']).execute(world) energy = thing['energy'][0] d = world.onEvent(energy, Destroyed(energy)) # to die means to move to None Move(thing['id'], None).execute(world) self.assertEqual( d.called, True, "Energy should be destroyed because " "the creator of the energy died.")
def test_subject(self): self.assertEqual(Charge('foo').subject(), 'foo')
def test_emitters(self): self.assertEqual(Charge('foo').emitters(), ['foo'])
def test_IAction(self): verifyObject(IAction, Charge('foo'))
def test_Charge(self): self.assertSimple(Charge('foo'), 'foo charged')
def test_Charge(self): self.assertSimple(Charge('foo'), { 'action': 'charge', 'subject': 'foo' })
def test_ActionPerformed(self): transformer = DictTransformer() self.assertSimple(ActionPerformed(Charge('foo')), { 'ev': 'action', 'action': transformer.transform(Charge('foo')) })