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_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_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_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_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_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_invulnerable(self): """ You can't shoot something that is invulnerable. """ world = World(MagicMock()) thing = world.create('foo') target = world.create('foo') self.assertRaises(Invulnerable, Shoot(thing['id'], target['id'], 500).execute, world) world.setAttr(target['id'], 'hp', None) self.assertRaises(Invulnerable, Shoot(thing['id'], target['id'], 500).execute, world)
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_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_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_onNextChange_cancel(self): """ You can cancel the deferred returned by onNextChange """ world = World(MagicMock()) obj = world.create('foo') d = world.onNextChange(obj['id'], 'hey') d.cancel() world.setAttr(obj['id'], 'hey', 3) self.assertFailure(d, defer.CancelledError)
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_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_onBecome(self): """ You can get a Deferred which will fire when an attribute becomes a particular value. """ world = World(MagicMock()) obj = world.create('foo') d = world.onBecome(obj['id'], 'hey', 3) self.assertFalse(d.called) world.setAttr(obj['id'], 'hey', 3) self.assertEqual(self.successResultOf(d), 3) # make sure it isn't called again world.setAttr(obj['id'], 'hey', 2) world.setAttr(obj['id'], 'hey', 3)
def test_onNextChange(self): """ You can get a Deferred which will fire when an attribute changes """ world = World(MagicMock()) obj = world.create('foo') d = world.onNextChange(obj['id'], 'hey') self.assertFalse(d.called) world.setAttr(obj['id'], 'ho', 8) self.assertFalse(d.called) world.setAttr(obj['id'], 'hey', 3) self.assertEqual(self.successResultOf(d), 3) # make sure it isn't called again world.setAttr(obj['id'], 'hey', 2)
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")