def test_enter_leave(self): hall = Location("hall") rat1 = NPC("rat1", "n") rat2 = NPC("rat2", "n") julie = NPC("julie", "f") with self.assertRaises(TypeError): hall.insert(12345, julie) self.assertEqual(_limbo, rat1.location) self.assertFalse(rat1 in hall.livings) wiretap = Wiretap(hall) hall.insert(rat1, julie) self.assertEqual(hall, rat1.location) self.assertTrue(rat1 in hall.livings) self.assertEqual([], wiretap.msgs, "insert shouldn't produce arrival messages") hall.insert(rat2, julie) self.assertTrue(rat2 in hall.livings) self.assertEqual([], wiretap.msgs, "insert shouldn't produce arrival messages") # now test leave wiretap.clear() hall.remove(rat1, julie) self.assertFalse(rat1 in hall.livings) self.assertIsNone(rat1.location) self.assertEqual([], wiretap.msgs, "remove shouldn't produce exit message") hall.remove(rat2, julie) self.assertFalse(rat2 in hall.livings) self.assertEqual([], wiretap.msgs, "remove shouldn't produce exit message") # test random leave hall.remove(rat1, julie) hall.remove(12345, julie)
def test_message_nearby_location(self): plaza = Location("plaza") road = Location("road") house = Location("house") attic = Location("attic") plaza.add_exits([ Exit("north", road, "road leads north"), Exit("door", house, "door to a house") ]) road.add_exits([Exit("south", plaza, "plaza to the south")]) house.add_exits([ Exit("door", plaza, "door to the plaza"), Exit("ladder", attic, "dusty attic") ]) attic.add_exits([Exit("ladder", house, "the house")]) wiretap_plaza = Wiretap(plaza) wiretap_road = Wiretap(road) wiretap_house = Wiretap(house) wiretap_attic = Wiretap(attic) plaza.message_nearby_locations("boing") pubsub.sync() self.assertEqual([], wiretap_plaza.msgs, "the plaza doesnt receive tells") self.assertEqual([], wiretap_attic.msgs, "the attic is too far away to receive msgs") self.assertTrue(("road", "boing") in wiretap_road.msgs) self.assertTrue(("road", "The sound is coming from the south.") in wiretap_road.msgs, "road should give sound direction") self.assertTrue(("house", "boing") in wiretap_house.msgs) self.assertTrue( ("house", "You can't hear where the sound is coming from.") in wiretap_house.msgs, "in the house you can't locate the sound direction")
def test_enter_leave(self): hall = Location("hall") rat1 = NPC("rat1", "n") rat2 = NPC("rat2", "n") julie = NPC("julie", "f") with self.assertRaises(TypeError): hall.insert(12345, julie) self.assertEqual(_Limbo, rat1.location) self.assertFalse(rat1 in hall.livings) wiretap = Wiretap(hall) hall.insert(rat1, julie) self.assertEqual(hall, rat1.location) self.assertTrue(rat1 in hall.livings) self.assertEqual([], wiretap.msgs, "insert shouldn't produce arrival messages") hall.insert(rat2, julie) self.assertTrue(rat2 in hall.livings) self.assertEqual([], wiretap.msgs, "insert shouldn't produce arrival messages") # now test leave wiretap.clear() hall.remove(rat1, julie) self.assertFalse(rat1 in hall.livings) self.assertIsNone(rat1.location) self.assertEqual([], wiretap.msgs, "remove shouldn't produce exit message") hall.remove(rat2, julie) self.assertFalse(rat2 in hall.livings) self.assertEqual([], wiretap.msgs, "remove shouldn't produce exit message") # test random leave hall.remove(rat1, julie) hall.remove(12345, julie)
def test_move(self): hall = Location("hall") attic = Location("attic") rat = Living("rat", "n", race="rodent") hall.init_inventory([rat]) wiretap_hall = Wiretap(hall) wiretap_attic = Wiretap(attic) self.assertTrue(rat in hall.livings) self.assertFalse(rat in attic.livings) self.assertEqual(hall, rat.location) rat.move(attic) self.assertTrue(rat in attic.livings) self.assertFalse(rat in hall.livings) self.assertEqual(attic, rat.location) pubsub.sync() self.assertEqual([("hall", "Rat leaves.")], wiretap_hall.msgs) self.assertEqual([("attic", "Rat arrives.")], wiretap_attic.msgs) # now try silent wiretap_hall.clear() wiretap_attic.clear() rat.move(hall, silent=True) pubsub.sync() self.assertTrue(rat in hall.livings) self.assertFalse(rat in attic.livings) self.assertEqual(hall, rat.location) self.assertEqual([], wiretap_hall.msgs) self.assertEqual([], wiretap_attic.msgs)
def test_move(self): hall = Location("hall") person = Living("person", "m", race="human") monster = NPC("dragon", "f", race="dragon") monster.aggressive = True key = Item("key") stone = Item("stone") hall.init_inventory([person, key]) stone.move(hall, person) wiretap = Wiretap(hall) self.assertTrue(person in hall) self.assertTrue(key in hall) key.contained_in = person # hack to force move to actually check the source container with self.assertRaises(KeyError): key.move(person, person) key.contained_in = hall # put it back as it was key.move(person, person) self.assertFalse(key in hall) self.assertTrue(key in person) self.assertEqual([], wiretap.msgs, "item.move() should be silent") with self.assertRaises(ActionRefused) as x: key.move(monster, person) # aggressive monster should fail self.assertTrue("not a good idea" in str(x.exception)) monster.aggressive = False key.move(monster, person) # non-aggressive should be ok
def test_move(self): hall = Location("hall") attic = Location("attic") rat = Living("rat", "n", race="rodent") hall.init_inventory([rat]) wiretap_hall = Wiretap(hall) wiretap_attic = Wiretap(attic) self.assertTrue(rat in hall.livings) self.assertFalse(rat in attic.livings) self.assertEqual(hall, rat.location) rat.move(attic) self.assertTrue(rat in attic.livings) self.assertFalse(rat in hall.livings) self.assertEqual(attic, rat.location) self.assertEqual([("hall", "Rat leaves.")], wiretap_hall.msgs) self.assertEqual([("attic", "Rat arrives.")], wiretap_attic.msgs) # now try silent wiretap_hall.clear() wiretap_attic.clear() rat.move(hall, silent=True) self.assertTrue(rat in hall.livings) self.assertFalse(rat in attic.livings) self.assertEqual(hall, rat.location) self.assertEqual([], wiretap_hall.msgs) self.assertEqual([], wiretap_attic.msgs)