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_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_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(self): """ Listing squares should return a list of all the things in the world which are squares. It should include their coordinates and number of each kind of thing inside them. """ world = World(MagicMock()) s1 = world.create('square')['id'] s2 = world.create('square')['id'] world.setAttr(s2, 'coordinates', (0, 1)) thing1 = world.create('thing')['id'] Move(thing1, s2).execute(world) output = ListSquares(thing1).execute(world) self.assertIn({ 'id': s1, 'kind': 'square', 'coordinates': None, 'contents': {}, }, output) self.assertIn({ 'id': s2, 'kind': 'square', 'coordinates': (0, 1), 'contents': { 'thing': 1, } }, output) self.assertEqual(len(output), 2)
def test_receiverFor_same(self): """ You should get the same function each time you ask for a receiver for the same object. """ world = World(MagicMock()) self.assertEqual(world.receiverFor('foo'), world.receiverFor('foo'))
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_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_receiverFor_same(self): """ You should get the same function each time you ask for a receiver for the same object. """ world = World(MagicMock()) self.assertEqual(world.receiverFor('foo'), world.receiverFor('foo'))
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_failIfNotOnBoard(self): """ The following actions require a bot to be on the board. """ world = World(MagicMock()) rules = StandardRules() bot = world.create('bot')['id'] actions = [ action.Charge(bot), action.ShareEnergy(bot, 'foo', 2), action.ConsumeEnergy(bot, 2), action.Shoot(bot, 'foo', 1), action.Repair(bot, 'foo', 1), action.MakeTool(bot, 'foo', 'foo'), action.OpenPortal(bot, 'foo', 'foo'), action.AddLock(bot, 'foo'), action.BreakLock(bot, 'foo'), action.LookAt(bot, 'foo'), ] for a in actions: try: rules.isAllowed(world, a) except NotAllowed: pass else: self.fail("You must be on a square to do %r" % (a,))
def test_failIfNotOnBoard(self): """ The following actions require a bot to be on the board. """ world = World(MagicMock()) rules = StandardRules() bot = world.create('bot')['id'] actions = [ action.Charge(bot), action.ShareEnergy(bot, 'foo', 2), action.ConsumeEnergy(bot, 2), action.Shoot(bot, 'foo', 1), action.Repair(bot, 'foo', 1), action.MakeTool(bot, 'foo', 'foo'), action.OpenPortal(bot, 'foo', 'foo'), action.AddLock(bot, 'foo'), action.BreakLock(bot, 'foo'), action.LookAt(bot, 'foo'), ] for a in actions: try: rules.isAllowed(world, a) except NotAllowed: pass else: self.fail("You must be on a square to do %r" % (a, ))
def test_makeSecondTool(self): """ If you make a tool from a different piece of ore, your existing tool is unequipped and the lifesource it was made from is reverted to ore. """ world = World(MagicMock()) ore1 = world.create('ore') ore2 = world.create('ore') bot = world.create('bot') MakeTool(bot['id'], ore1['id'], 'knife').execute(world) MakeTool(bot['id'], ore2['id'], 'butterfly net').execute(world) self.assertEqual(bot['tool'], 'butterfly net', "Should equip the new tool") self.assertEqual(ore1['kind'], 'ore', "Should revert to ore") self.assertEqual(ore2['kind'], 'lifesource') # kill original world.setAttr(ore1['id'], 'hp', 0) self.assertEqual(bot['tool'], 'butterfly net', "should not change tool" " when the original ore dies") self.assertEqual(ore2['kind'], 'lifesource')
def test_invalidLocation(self): """ It is an error to move to a non-entity. """ world = World(MagicMock()) thing = world.create('thing')['id'] self.assertRaises(NotAllowed, Move(thing, 4).execute, world)
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_badPassword(self): auth = FileStoredPasswords(self.mktemp()) world = World(MagicMock(), auth=auth) thing = world.create('thing') yield CreateTeam(thing['id'], 'teamA', 'password').execute(world) self.assertFailure(JoinTeam(thing['id'], 'teamA', 'not password').execute(world), BadPassword)
def test_create_uniqueId(self): """ The id of an object should be unique """ world = World(MagicMock()) o1 = world.create('foo') o2 = world.create('bar') self.assertNotEqual(o1['id'], o2['id'])
def test_create_uniqueId(self): """ The id of an object should be unique """ world = World(MagicMock()) o1 = world.create('foo') o2 = world.create('bar') self.assertNotEqual(o1['id'], o2['id'])
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_get(self): """ You can get objects. """ world = World(MagicMock()) obj = world.create('foo') obj2 = world.get(obj['id']) self.assertEqual(obj, obj2)
def test_nowhere(self): """ If you are nowhere, return an empty list. """ world = World(MagicMock()) thing = world.create('thing') self.assertEqual(Look(thing['id']).execute(world), [])
def test_nowhere(self): """ If you are nowhere, return an empty list. """ world = World(MagicMock()) thing = world.create('thing') self.assertEqual(Look(thing['id']).execute(world), [])
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_invalidLocation(self): """ It is an error to move to a non-entity. """ world = World(MagicMock()) thing = world.create('thing')['id'] self.assertRaises(NotAllowed, Move(thing, 4).execute, world)
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_badPassword(self): auth = FileStoredPasswords(self.mktemp()) world = World(MagicMock(), auth=auth) thing = world.create('thing') yield CreateTeam(thing['id'], 'teamA', 'password').execute(world) self.assertFailure( JoinTeam(thing['id'], 'teamA', 'not password').execute(world), BadPassword)
def test_envelope(self): """ You can read/write on the envelope of objects. """ world = World(MagicMock()) obj = MagicMock() env = world.envelope(obj) self.assertTrue(isinstance(env, dict)) env['foo'] = 'bar'
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_envelope(self): """ You can read/write on the envelope of objects. """ world = World(MagicMock()) obj = MagicMock() env = world.envelope(obj) self.assertTrue(isinstance(env, dict)) env['foo'] = 'bar'
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_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_execute(self): """ Locking something should increase the number of 'locks' on the thing. """ world = World(MagicMock()) thing = world.create('thing')['id'] box = world.create('box') AddLock(thing, box['id']).execute(world) self.assertEqual(box['locks'], 1)
def test_onlyOre(self): """ Only ore can be turned into tools. """ world = World(MagicMock()) ore = world.create('flower') bot = world.create('bot') self.assertRaises(NotAllowed, MakeTool(bot['id'], ore['id'], 'knife').execute, world)
def test_execute(self): """ Locking something should increase the number of 'locks' on the thing. """ world = World(MagicMock()) thing = world.create('thing')['id'] box = world.create('box') AddLock(thing, box['id']).execute(world) self.assertEqual(box['locks'], 1)
def test_execute(self): """ Should set the password for a team. """ auth = FileStoredPasswords(self.mktemp()) world = World(MagicMock(), auth=auth) thing = world.create('thing') d = CreateTeam(thing['id'], 'teamA', 'password').execute(world) def check(r): self.assertEqual(r, 'teamA') return d.addCallback(check)
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_onlyOre(self): """ Only ore can be turned into tools. """ world = World(MagicMock()) ore = world.create('flower') bot = world.create('bot') self.assertRaises(NotAllowed, MakeTool(bot['id'], ore['id'], 'knife').execute, world)
def test_execute(self): """ Shooting something should reduce its hitpoints by the given amount. """ world = World(MagicMock()) thing = world.create('foo') target = world.create('foo') world.setAttr(target['id'], 'hp', 30) Shoot(thing['id'], target['id'], 10).execute(world) self.assertEqual(target['hp'], 20, "Should reduce the hitpoints")
def test_open(self): """ You can open a portal on from some ore. """ world = World(MagicMock()) ore = world.create('ore') bot = world.create('bot') OpenPortal(bot['id'], ore['id'], 'user').execute(world) self.assertEqual(ore['portal_user'], 'user', "Should set the portal " "user to the id of the user that can use the portal") self.assertEqual(ore['kind'], 'portal')
def test_execute(self): """ Should set the team name if the password matches. """ auth = FileStoredPasswords(self.mktemp()) world = World(MagicMock(), auth=auth) thing = world.create('thing') yield CreateTeam(thing['id'], 'teamA', 'password').execute(world) yield JoinTeam(thing['id'], 'teamA', 'password').execute(world) self.assertEqual(thing['team'], 'teamA')
def test_stayAbove0(self): """ You can't damage something below 0 by shooting. """ world = World(MagicMock()) thing = world.create('foo') target = world.create('foo') world.setAttr(target['id'], 'hp', 30) Shoot(thing['id'], target['id'], 500).execute(world) self.assertEqual(target['hp'], 0, "Should reduce the hitpoints to 0")
def test_execute(self): """ Should set the team name if the password matches. """ auth = FileStoredPasswords(self.mktemp()) world = World(MagicMock(), auth=auth) thing = world.create('thing') yield CreateTeam(thing['id'], 'teamA', 'password').execute(world) yield JoinTeam(thing['id'], 'teamA', 'password').execute(world) self.assertEqual(thing['team'], 'teamA')
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_execute_workRequired_succeed(self): """ If work is required, succeed if the solution is good. """ e = self.friendlyEngine() e.engine.workRequirement.return_value = Work(0, 'bar') world = World(MagicMock()) action = MagicMock() world.envelope(action)['work_solution'] = 'anything will work' yield e.execute(world, action) action.execute.assert_called_once_with(world)
def test_onlyBotsCanExecute(self): """ Only bots can execute commands. """ world = World(MagicMock()) rules = StandardRules() bot = world.create('bot')['id'] rules.isAllowed(world, action.ListSquares(bot)) nonbot = world.create('foo')['id'] self.assertRaises(NotAllowed, rules.isAllowed, world, action.ListSquares(nonbot))
def test_invulnerable(self): """ You can't repair something that is invulnerable. """ world = World(MagicMock()) thing = world.create('foo') target = world.create('foo') self.assertRaises(Invulnerable, Repair(thing['id'], target['id'], 500).execute, world) world.setAttr(target['id'], 'hp', None) self.assertRaises(Invulnerable, Repair(thing['id'], target['id'], 500).execute, world)
def test_execute(self): """ Should set the password for a team. """ auth = FileStoredPasswords(self.mktemp()) world = World(MagicMock(), auth=auth) thing = world.create('thing') d = CreateTeam(thing['id'], 'teamA', 'password').execute(world) def check(r): self.assertEqual(r, 'teamA') return d.addCallback(check)
def test_revertWhenKilled(self): """ If the lifesource is shot to death, then revert to ore and unequip. """ world = World(MagicMock()) ore = world.create('ore') bot = world.create('bot') MakeTool(bot['id'], ore['id'], 'knife').execute(world) world.setAttr(ore['id'], 'hp', 0) self.assertNotIn('tool', bot, "Should unequip the tool") self.assertEqual(ore['kind'], 'ore', "Should revert to ore")
def test_revertWhenKilled(self): """ If the lifesource is shot to death, then revert to ore and unequip. """ world = World(MagicMock()) ore = world.create('ore') bot = world.create('bot') MakeTool(bot['id'], ore['id'], 'knife').execute(world) world.setAttr(ore['id'], 'hp', 0) self.assertNotIn('tool', bot, "Should unequip the tool") self.assertEqual(ore['kind'], 'ore', "Should revert to ore")
def test_open(self): """ You can open a portal on from some ore. """ world = World(MagicMock()) ore = world.create('ore') bot = world.create('bot') OpenPortal(bot['id'], ore['id'], 'user').execute(world) self.assertEqual( ore['portal_user'], 'user', "Should set the portal " "user to the id of the user that can use the portal") self.assertEqual(ore['kind'], 'portal')
def test_onlyBotsCanExecute(self): """ Only bots can execute commands. """ world = World(MagicMock()) rules = StandardRules() bot = world.create('bot')['id'] rules.isAllowed(world, action.ListSquares(bot)) nonbot = world.create('foo')['id'] self.assertRaises(NotAllowed, rules.isAllowed, world, action.ListSquares(nonbot))
def test_execute_workRequired_succeed(self): """ If work is required, succeed if the solution is good. """ e = self.friendlyEngine() e.engine.workRequirement.return_value = Work(0, 'bar') world = World(MagicMock()) action = MagicMock() world.envelope(action)['work_solution'] = 'anything will work' yield e.execute(world, action) action.execute.assert_called_once_with(world)
def test_execute(self): """ Repairing something should increas the number of hitpoints on that thing. """ world = World(MagicMock()) thing = world.create('foo') target = world.create('foo') world.setAttr(target['id'], 'hp', 30) Repair(thing['id'], target['id'], 10).execute(world) self.assertEqual(target['hp'], 40, "Should increase the hitpoints")
def test_makeTool(self): """ Making a tool will destroy the ore, make a lifesource in its place, and equip the maker with the tool. """ world = World(MagicMock()) ore = world.create('ore') bot = world.create('bot') MakeTool(bot['id'], ore['id'], 'knife').execute(world) self.assertEqual(ore['kind'], 'lifesource', "Should have converted " "the ore into a lifesource") self.assertEqual(bot['tool'], 'knife', "Should have given the bot " "the tool")
def test_inLocation(self): """ Should list the things in my location """ world = World(MagicMock()) room = world.create('foo') thing = world.create('thing') other = world.create('thing') Move(thing['id'], room['id']).execute(world) Move(other['id'], room['id']).execute(world) r = Look(thing['id']).execute(world) self.assertEqual(set(r), set([thing['id'], other['id']]))
def test_create(self): """ You can create objects. """ ev = MagicMock() world = World(ev) obj = world.create('foo') self.assertIn('id', obj) self.assertEqual(obj['kind'], 'foo') self.assertEqual(obj, world.objects[obj['id']], "Should be in the " "objects list") ev.assert_any_call(Created(obj['id'])) ev.assert_any_call(AttrSet(obj['id'], 'kind', 'foo'))
def test_openerDies(self): """ If the opener dies before the portal is used, the portal reverts to ore. """ world = World(MagicMock()) ore = world.create('ore') bot = world.create('bot') OpenPortal(bot['id'], ore['id'], 'user').execute(world) # it is death to move to the void Move(bot['id'], None).execute(world) self.assertEqual(ore['kind'], 'ore') self.assertNotIn('portal_user', ore)
def test_revertToOreWhenBotDies(self): """ If the thing dies that made the tool, revert to ore and set the tool to None """ world = World(MagicMock()) ore = world.create('ore') bot = world.create('bot') MakeTool(bot['id'], ore['id'], 'knife').execute(world) # moving to None is death Move(bot['id'], None).execute(world) self.assertNotIn('tool', bot, "Should unequip the tool") self.assertEqual(ore['kind'], 'ore', "Should revert to ore")
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_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")