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_init_inventory(self): rat = NPC("rat", "n", race="rodent") rat.aggressive = True with self.assertRaises(ActionRefused): rat.insert(Item("thing"), None) rat.insert(Item("thing"), rat) wizz = Player("wizard", "f") wizz.privileges.add("wizard") rat.insert(Item("thing2"), wizz) self.assertEqual(2, rat.inventory_size) stuff = [Item("thing")] with self.assertRaises(AssertionError): rat.init_inventory(stuff) rat = NPC("rat", "n", race="rodent") rat.aggressive = True rat.init_inventory(stuff) self.assertEqual(1, rat.inventory_size)