本文整理汇总了Python中tale.base.Location.init_inventory方法的典型用法代码示例。如果您正苦于以下问题:Python Location.init_inventory方法的具体用法?Python Location.init_inventory怎么用?Python Location.init_inventory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tale.base.Location
的用法示例。
在下文中一共展示了Location.init_inventory方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_move
# 需要导入模块: from tale.base import Location [as 别名]
# 或者: from tale.base.Location import init_inventory [as 别名]
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
示例2: test_move
# 需要导入模块: from tale.base import Location [as 别名]
# 或者: from tale.base.Location import init_inventory [as 别名]
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)
示例3: test_custom_verbs
# 需要导入模块: from tale.base import Location [as 别名]
# 或者: from tale.base.Location import init_inventory [as 别名]
def test_custom_verbs(self):
player = Player("julie", "f")
player.verbs["xywobble"] = "p1"
room = Location("room")
chair1 = Item("chair1")
chair1.verbs["frobnitz"] = "c1"
chair2 = Item("chair2")
chair2.verbs["frobnitz"] = "c2"
chair_in_inventory = Item("chair3")
chair_in_inventory.verbs["kowabooga"] = "c3"
room.init_inventory([chair1, player, chair2])
# check inventory NOT affecting player custom verbs, but DOES affect location verbs
self.assertEqual({"xywobble": "p1"}, player.verbs)
self.assertEqual({"frobnitz": "c2", "xywobble": "p1"}, room.verbs)
player.insert(chair_in_inventory, player)
self.assertEqual({"xywobble": "p1"}, player.verbs)
self.assertEqual({"frobnitz": "c2", "xywobble": "p1", "kowabooga": "c3"}, room.verbs)
player.remove(chair_in_inventory, player)
self.assertEqual({"frobnitz": "c2", "xywobble": "p1"}, room.verbs)
player.insert(chair_in_inventory, player)
self.assertEqual({"frobnitz": "c2", "xywobble": "p1", "kowabooga": "c3" }, room.verbs)
room2 = Location("room2")
self.assertEqual({}, room2.verbs)
chair1.move(room2, player)
self.assertEqual({"xywobble": "p1", "kowabooga": "c3" }, room.verbs)
self.assertEqual({"frobnitz": "c1"}, room2.verbs)
chair2.move(room2, player)
self.assertEqual({"xywobble": "p1", "kowabooga": "c3"}, room.verbs)
self.assertEqual({"frobnitz": "c2"}, room2.verbs)
player.move(room2)
self.assertEqual({}, room.verbs)
self.assertEqual({"frobnitz": "c2", "xywobble": "p1", "kowabooga": "c3"}, room2.verbs)
示例4: test_destroy_player
# 需要导入模块: from tale.base import Location [as 别名]
# 或者: from tale.base.Location import init_inventory [as 别名]
def test_destroy_player(self):
ctx = Context()
loc = Location("loc")
player = Player("julie", "f")
player.privileges = {"wizard"}
player.create_wiretap(loc)
player.insert(Item("key"), player)
loc.init_inventory([player])
self.assertEqual(loc, player.location)
self.assertTrue(len(player.inventory) > 0)
self.assertTrue(player in loc.livings)
player.destroy(ctx)
import gc
gc.collect()
self.assertTrue(len(player.inventory) == 0)
self.assertFalse(player in loc.livings)
self.assertIsNone(player.location, "destroyed player should end up nowhere (None)")
示例5: test_destroy_loc
# 需要导入模块: from tale.base import Location [as 别名]
# 或者: from tale.base.Location import init_inventory [as 别名]
def test_destroy_loc(self):
ctx = Context()
loc = Location("loc")
i = Item("item")
liv = Living("rat", "n", race="rodent")
loc.add_exits([Exit("north", "somewhere", "exit to somewhere")])
player = Player("julie", "f")
player.privileges = {"wizard"}
player.create_wiretap(loc)
loc.init_inventory([i, liv, player])
self.assertTrue(len(loc.exits) > 0)
self.assertTrue(len(loc.items) > 0)
self.assertTrue(len(loc.livings) > 0)
self.assertEqual(loc, player.location)
self.assertEqual(loc, liv.location)
loc.destroy(ctx)
self.assertTrue(len(loc.exits) == 0)
self.assertTrue(len(loc.items) == 0)
self.assertTrue(len(loc.livings) == 0)
self.assertEqual(_Limbo, player.location)
self.assertEqual(_Limbo, liv.location)
示例6: test_custom_verbs
# 需要导入模块: from tale.base import Location [as 别名]
# 或者: from tale.base.Location import init_inventory [as 别名]
def test_custom_verbs(self):
player = Player("julie", "f")
player.verbs["xywobble"] = "p1"
monster = NPC("snake", "f")
monster.verbs["snakeverb"] = "s1"
room = Location("room")
chair1 = Item("chair1")
chair1.verbs["frobnitz"] = "c1"
chair2 = Item("chair2")
chair2.verbs["frobnitz"] = "c2"
chair_in_inventory = Item("chair3")
chair_in_inventory.verbs["kowabooga"] = "c3"
box_in_inventory = Item("box")
box_in_inventory.verbs["boxverb"] = "c4"
player.init_inventory([box_in_inventory, chair_in_inventory])
exit = Exit("e", "dummy", None, None)
exit.verbs["exitverb"] = "c5"
room.init_inventory([chair1, player, chair2, monster])
room.add_exits([exit])
custom_verbs = mud_context.driver.current_custom_verbs(player)
all_verbs = mud_context.driver.current_verbs(player)
self.assertEqual({"xywobble", "snakeverb", "frobnitz", "kowabooga", "boxverb", "exitverb"}, set(custom_verbs))
self.assertEqual(set(), set(custom_verbs) - set(all_verbs))
示例7: test_location
# 需要导入模块: from tale.base import Location [as 别名]
# 或者: from tale.base.Location import init_inventory [as 别名]
def test_location(self):
thingy = Item("thing")
with self.assertRaises(TypeError):
thingy.location = "foobar"
hall = Location("hall")
thingy.location = hall
self.assertEqual(hall, thingy.contained_in)
self.assertEqual(hall, thingy.location)
person = Living("person", "m", race="human")
key = Item("key")
backpack = Container("backpack")
person.insert(backpack, person)
self.assertIsNone(key.contained_in)
self.assertIsNone(key.location)
self.assertTrue(backpack in person)
self.assertEqual(person, backpack.contained_in)
self.assertEqual(_Limbo, backpack.location)
hall.init_inventory([person, key])
self.assertEqual(hall, key.contained_in)
self.assertEqual(hall, key.location)
self.assertEqual(hall, backpack.location)
key.move(backpack, person)
self.assertEqual(backpack, key.contained_in)
self.assertEqual(hall, key.location)
示例8: test_handle_and_notify_action
# 需要导入模块: from tale.base import Location [as 别名]
# 或者: from tale.base.Location import init_inventory [as 别名]
def test_handle_and_notify_action(self):
class SpecialPlayer(Player):
def init(self):
self.handled = False
self.handle_verb_called = False
self.notify_called = False
def handle_verb(self, parsed, actor):
self.handle_verb_called = True
if parsed.verb in self.verbs:
self.handled = True
return True
else:
return False
def notify_action(self, parsed, actor):
self.notify_called = True
player = SpecialPlayer("julie", "f")
player.verbs["xywobble"] = ""
room = Location("room")
class Chair(Item):
def init(self):
self.handled = False
self.handle_verb_called = False
self.notify_called = False
def handle_verb(self, parsed, actor):
self.handle_verb_called = True
if parsed.verb in self.verbs:
self.handled = True
return True
else:
return False
def notify_action(self, parsed, actor):
self.notify_called = True
chair_in_inventory = Chair("littlechair")
chair_in_inventory.verbs["kerwaffle"] = ""
player.insert(chair_in_inventory, player)
chair = Chair("chair")
chair.verbs["frobnitz"] = ""
room.init_inventory([player, chair])
# first check if the handle_verb passes to all objects including inventory
parsed = ParseResult("kowabungaa12345")
handled = room.handle_verb(parsed, player)
self.assertFalse(handled)
self.assertTrue(chair.handle_verb_called)
self.assertTrue(player.handle_verb_called)
self.assertTrue(chair_in_inventory.handle_verb_called)
self.assertFalse(chair.handled)
self.assertFalse(player.handled)
self.assertFalse(chair_in_inventory.handled)
# check item handling
player.init()
chair.init()
chair_in_inventory.init()
parsed = ParseResult("frobnitz")
handled = room.handle_verb(parsed, player)
self.assertTrue(handled)
self.assertTrue(chair.handled)
self.assertFalse(player.handled)
self.assertFalse(chair_in_inventory.handled)
# check living handling
player.init()
chair.init()
chair_in_inventory.init()
parsed = ParseResult("xywobble")
handled = room.handle_verb(parsed, player)
self.assertTrue(handled)
self.assertFalse(chair.handled)
self.assertTrue(player.handled)
self.assertFalse(chair_in_inventory.handled)
# check inventory handling
player.init()
chair.init()
chair_in_inventory.init()
parsed = ParseResult("kerwaffle")
handled = room.handle_verb(parsed, player)
self.assertTrue(handled)
self.assertFalse(chair.handled)
self.assertFalse(player.handled)
self.assertTrue(chair_in_inventory.handled)
# check notify_action
player.init()
chair.init()
chair_in_inventory.init()
room.notify_action(parsed, player)
self.assertTrue(chair.notify_called)
self.assertTrue(player.notify_called)
self.assertTrue(chair_in_inventory.notify_called)
示例9: heartbeat
# 需要导入模块: from tale.base import Location [as 别名]
# 或者: from tale.base.Location import init_inventory [as 别名]
def heartbeat(self, ctx):
rand = random.random()
if rand < 0.07:
self.do_socialize("twitch erra")
elif rand < 0.14:
self.do_socialize("rotate random")
elif rand < 0.21:
self.location.tell("%s hums softly." % tale.lang.capital(self.title))
drone = Drone("drone", "n", race="bot", title="mindless drone",
description="A stupid metallic drone. It just hovers here with no apparent reason. It has a little label attached to it.")
drone.add_extradesc({"label"}, "The label reads: \"Wall-E was my cousin\".")
drone.aggressive = True
hall.init_inventory([table, key, drone])
attic = Location("Tower attic",
"""
The dark and dusty attic of the wizard tower.
There are piles of old scrolls and assorted stuff here of which you assume
it once held great magical power. All of it is now covered with a thick
layer of dust.
""")
attic.add_extradesc({"dust", "stuff", "scrolls"}, "The scrolls look ancient. One looks like a spell!")
attic.add_extradesc({"spell"}, "The scroll looks like it contains a spell, but on closer inspection, it has become too faded to be understood anymore.")
kitchen = Location("Tower kitchen",
"""
A cozy little kitchen for hungry wizards.
示例10: Librarian
# 需要导入模块: from tale.base import Location [as 别名]
# 或者: from tale.base.Location import init_inventory [as 别名]
"The book's title is Cryptography for Dummies. You remember skimming it during detention yesterday but that was a long time ago.",
)
book.aliases = {"library book", "cryptography book", "cryptography for dummies", "dog-eared book"}
librarian = Librarian(
"School Librarian",
"f",
race="human",
title="Ms. Smith, the School Librarian",
description="Ms. Smith, the school librarian.",
)
librarian.add_extradesc(
{"librarian", "school librarian"}, "Female, elderly, hair in a bun. Could she be more stereotypical?"
)
library.init_inventory([cubby, book, librarian])
# class GameEnd(Location):
# def init(self):
# pass
# def insert(self, obj, actor):
# # Normally you would use notify_player_arrived() to trigger an action.
# # but for the game ending, we require an immediate response.
# # So instead we hook into the direct arrival of something in this location.
# super(GameEnd, self).insert(obj, actor)
# try:
# obj.story_completed() # player arrived! Great Success!
# except AttributeError:
# pass
示例11: TestLocations
# 需要导入模块: from tale.base import Location [as 别名]
# 或者: from tale.base.Location import init_inventory [as 别名]
class TestLocations(unittest.TestCase):
def setUp(self):
mud_context.driver = DummyDriver()
self.hall = Location("Main hall", "A very large hall.")
self.attic = Location("Attic", "A dark attic.")
self.street = Location("Street", "An endless street.")
e1 = Exit("up", self.attic, "A ladder leads up.")
e2 = Exit(["door", "east"], self.street, "A heavy wooden door to the east blocks the noises from the street outside.")
self.hall.add_exits([e1, e2])
self.table = Item("table", "oak table", "a large dark table with a lot of cracks in its surface")
self.key = Item("key", "rusty key", "an old rusty key without a label", short_description="Someone forgot a key.")
self.magazine = Item("magazine", "university magazine")
self.rat = NPC("rat", "n", race="rodent")
self.fly = NPC("fly", "n", race="insect", short_description="A fly buzzes around your head.")
self.julie = NPC("julie", "f", title="attractive Julie", description="She's quite the looker.")
self.julie.aliases = {"chick"}
self.player = Player("player", "m")
self.pencil = Item("pencil", title="fountain pen")
self.pencil.aliases = {"pen"}
self.bag = Container("bag")
self.notebook_in_bag = Item("notebook")
self.bag.insert(self.notebook_in_bag, self.player)
self.player.insert(self.pencil, self.player)
self.player.insert(self.bag, self.player)
self.hall.init_inventory([self.table, self.key, self.magazine, self.rat, self.julie, self.player, self.fly])
def test_names(self):
loc = Location("The Attic", "A dusty attic.")
self.assertEqual("The Attic", loc.name)
self.assertEqual("A dusty attic.", loc.description)
def test_contains(self):
self.assertTrue(self.julie in self.hall)
self.assertTrue(self.magazine in self.hall)
self.assertFalse(self.pencil in self.hall)
self.assertFalse(self.magazine in self.attic)
self.assertFalse(self.julie in self.attic)
def test_look(self):
expected = ["[Main hall]", "A very large hall.",
"A heavy wooden door to the east blocks the noises from the street outside. A ladder leads up.",
"Someone forgot a key. You see a university magazine and an oak table. Player, attractive Julie, and rat are here. A fly buzzes around your head."]
self.assertEqual(expected, strip_text_styles(self.hall.look()))
expected = ["[Main hall]", "A very large hall.",
"A heavy wooden door to the east blocks the noises from the street outside. A ladder leads up.",
"Someone forgot a key. You see a university magazine and an oak table. Attractive Julie and rat are here. A fly buzzes around your head."]
self.assertEqual(expected, strip_text_styles(self.hall.look(exclude_living=self.player)))
expected = ["[Attic]", "A dark attic."]
self.assertEqual(expected, strip_text_styles(self.attic.look()))
def test_look_short(self):
expected = ["[Attic]"]
self.assertEqual(expected, strip_text_styles(self.attic.look(short=True)))
expected = ["[Main hall]", "Exits: door, east, up", "You see: key, magazine, table", "Present: fly, julie, player, rat"]
self.assertEqual(expected, strip_text_styles(self.hall.look(short=True)))
expected = ["[Main hall]", "Exits: door, east, up", "You see: key, magazine, table", "Present: fly, julie, rat"]
self.assertEqual(expected, strip_text_styles(self.hall.look(exclude_living=self.player, short=True)))
def test_search_living(self):
self.assertEqual(None, self.hall.search_living("<notexisting>"))
self.assertEqual(None, self.attic.search_living("<notexisting>"))
self.assertEqual(self.rat, self.hall.search_living("rat"))
self.assertEqual(self.julie, self.hall.search_living("Julie"))
self.assertEqual(self.julie, self.hall.search_living("attractive julie"))
self.assertEqual(self.julie, self.hall.search_living("chick"))
self.assertEqual(None, self.hall.search_living("bloke"))
def test_search_item(self):
# almost identical to locate_item so only do a few basic tests
self.assertEqual(None, self.player.search_item("<notexisting>"))
self.assertEqual(self.pencil, self.player.search_item("pencil"))
def test_locate_item(self):
item, container = self.player.locate_item("<notexisting>")
self.assertEqual(None, item)
self.assertEqual(None, container)
item, container = self.player.locate_item("pencil")
self.assertEqual(self.pencil, item)
self.assertEqual(self.player, container)
item, container = self.player.locate_item("fountain pen")
self.assertEqual(self.pencil, item, "need to find the title")
item, container = self.player.locate_item("pen")
self.assertEqual(self.pencil, item, "need to find the alias")
item, container = self.player.locate_item("pencil", include_inventory=False)
self.assertEqual(None, item)
self.assertEqual(None, container)
item, container = self.player.locate_item("key")
self.assertEqual(self.key, item)
self.assertEqual(self.hall, container)
item, container = self.player.locate_item("key", include_location=False)
self.assertEqual(None, item)
self.assertEqual(None, container)
item, container = self.player.locate_item("KEY")
self.assertEqual(self.key, item, "should work case-insensitive")
self.assertEqual(self.hall, container, "should work case-insensitive")
item, container = self.player.locate_item("notebook")
self.assertEqual(self.notebook_in_bag, item)
self.assertEqual(self.bag, container, "should search in bags in inventory")
item, container = self.player.locate_item("notebook", include_containers_in_inventory=False)
self.assertEqual(None, item)
#.........这里部分代码省略.........
示例12: vision_problems
# 需要导入模块: from tale.base import Location [as 别名]
# 或者: from tale.base.Location import init_inventory [as 别名]
while self.wearing:
pass
def vision_problems(self):
pass
glasses = Item("glasses", "glasses", "pair of ladies eyeglasses.")
glasses.aliases = {"glasses"}
glasses.add_extradesc({"glasses"}, "The horn-rimmed glasses aren't really your style.")
glasses_case = GlassesCase("case", "clamshell case for eyeglasses")
glasses_case.aliases = {"case", "clamshell case", "eyeglasses case"}
glasses_case.init_inventory([glasses])
gym.init_inventory([glasses_case])
freshman = NPC("freshman", "m", title="freshman", description="It's a freshman. They all look the same.")
# sawdust = Item("sawdust", "sawdust")
# sawdust.add_extradesc({"sawdust", "pink sawdust"}, "It's pink sawdust, the kind used to soak up spills and messes.")
# bucket = Container("bucket", "bucket")
# bucket.add_extradesc({"bucket"}, "You see an ordinary blue plastic bucket.")
# bucket.init_inventory([sawdust])
trash_can = UntakableContainer("trash can", "large grey trash can")
trash_can.aliases = {"trash can", "trashcan", "trash"}
trash_can.init_inventory([freshman])
# broom = Item("broom", "broom")
示例13: VillageIdiot
# 需要导入模块: from tale.base import Location [as 别名]
# 或者: from tale.base.Location import init_inventory [as 别名]
towncrier.aliases = {"crier", "town crier"}
idiot = VillageIdiot("idiot", "m", title="blubbering idiot", description="""
This person's engine is running but there is nobody behind the wheel.
He is a few beers short of a six-pack. Three ice bricks shy of an igloo.
Not the sharpest knife in the drawer. Anyway you get the idea: it's an idiot.
""")
rat = WalkingRat("rat", "n", race="rodent", description="A filthy looking rat. Its whiskers tremble slightly as it peers back at you.")
ant = NPC("ant", "n", race="insect", short_description="A single ant seems to have lost its way.")
clock = clone(gameclock)
clock.short_description = "On the pavement lies a clock, it seems to be working still."
square.init_inventory([cursed_gem, normal_gem, paper, trashcan, pouch, insertonly_box, removeonly_box, clock, towncrier, idiot, rat, ant])
class AlleyOfDoors(Location):
def notify_player_arrived(self, player, previous_location):
if previous_location is self:
player.tell("...Weird... The door you just entered seems to go back to the same place you came from...")
alley = AlleyOfDoors("Alley of doors", "An alley filled with doors.")
descr = "The doors seem to be connected to the computer nearby."
door1 = Door(["first door", "door one"], alley, "There's a door marked 'door one'.", long_description=descr, locked=False, opened=True)
door2 = Door(["second door", "door two"], alley, "There's a door marked 'door two'.", long_description=descr, locked=True, opened=False)
door3 = Door(["third door", "door three"], alley, "There's a door marked 'door three'.", long_description=descr, locked=False, opened=False)
door4 = Door(["fourth door", "door four"], alley, "There's a door marked 'door four'.", long_description=descr, locked=True, opened=False)
alley.add_exits([
door1, door2, door3, door4,
示例14: Locker
# 需要导入模块: from tale.base import Location [as 别名]
# 或者: from tale.base.Location import init_inventory [as 别名]
)
vomit.verbs = {
# register some custom verbs. You can redefine existing verbs, so be careful.
"clean": "Clean up the puke.",
"cover": "Clean up the puke."
# "put": "Put something on something else.",
}
vomit.add_extradesc(
{"puddle", "sick", "vomit", "puke"},
"""
Someone appears to have had tummy troubles; a puddle of sick lies in the middle of the corridor.
The puddle is . . . kinda gross. It was Salisbury Steak Day.
""",
)
south_hallway.init_inventory([vomit])
english_paper = Item("english homework", "english paper")
english_paper.aliases = {"english paper", "paper", "homework"}
english_paper.add_extradesc({"paper", "english paper", "homework"}, "You paid good money for this!")
class Locker(Boxlike):
def init(self, locked=True, opened=False, combo="8-16-32"):
super(Boxlike, self).init()
self.txt_title_closed = self._title
self.txt_title_open_filled = self._title
self.txt_title_open_empty = "empty " + self._title
self.txt_descr_closed = "The locker is closed."
self.txt_descr_open_filled = "There's something inside {}.".format(self._title)
self.txt_descr_open_empty = "There's nothing inside {}.".format(self._title)
示例15: TestLocations
# 需要导入模块: from tale.base import Location [as 别名]
# 或者: from tale.base.Location import init_inventory [as 别名]
class TestLocations(unittest.TestCase):
def setUp(self):
mud_context.driver = TestDriver()
mud_context.config = DemoStory()._get_config()
self.hall = Location("Main hall", "A very large hall.")
self.attic = Location("Attic", "A dark attic.")
self.street = Location("Street", "An endless street.")
e1 = Exit("up", self.attic, "A ladder leads up.")
e2 = Exit(["door", "east"], self.street, "A heavy wooden door to the east blocks the noises from the street outside.")
self.hall.add_exits([e1, e2])
self.table = Item("table", "oak table", "a large dark table with a lot of cracks in its surface")
self.key = Item("key", "rusty key", "an old rusty key without a label", short_description="Someone forgot a key.")
self.magazine = Item("magazine", "university magazine")
self.magazine2 = Item("magazine", "university magazine")
self.rat = NPC("rat", "n", race="rodent")
self.rat2 = NPC("rat", "n", race="rodent")
self.fly = NPC("fly", "n", race="insect", short_description="A fly buzzes around your head.")
self.julie = NPC("julie", "f", title="attractive Julie", description="She's quite the looker.")
self.julie.aliases = {"chick"}
self.player = Player("player", "m")
self.pencil = Item("pencil", title="fountain pen")
self.pencil.aliases = {"pen"}
self.bag = Container("bag")
self.notebook_in_bag = Item("notebook")
self.bag.insert(self.notebook_in_bag, self.player)
self.player.insert(self.pencil, self.player)
self.player.insert(self.bag, self.player)
self.hall.init_inventory([self.table, self.key, self.magazine, self.magazine2, self.rat, self.rat2, self.julie, self.player, self.fly])
def test_names(self):
loc = Location("The Attic", "A dusty attic.")
self.assertEqual("The Attic", loc.name)
self.assertEqual("A dusty attic.", loc.description)
def test_contains(self):
self.assertTrue(self.julie in self.hall)
self.assertTrue(self.magazine in self.hall)
self.assertFalse(self.pencil in self.hall)
self.assertFalse(self.magazine in self.attic)
self.assertFalse(self.julie in self.attic)
def test_look(self):
expected = ["[Main hall]", "A very large hall.",
"A heavy wooden door to the east blocks the noises from the street outside. A ladder leads up.",
"Someone forgot a key. You see two university magazines and an oak table. Player, attractive Julie, and two rats are here. A fly buzzes around your head."]
self.assertEqual(expected, strip_text_styles(self.hall.look()))
expected = ["[Main hall]", "A very large hall.",
"A heavy wooden door to the east blocks the noises from the street outside. A ladder leads up.",
"Someone forgot a key. You see two university magazines and an oak table. Attractive Julie and two rats are here. A fly buzzes around your head."]
self.assertEqual(expected, strip_text_styles(self.hall.look(exclude_living=self.player)))
expected = ["[Attic]", "A dark attic."]
self.assertEqual(expected, strip_text_styles(self.attic.look()))
def test_look_short(self):
expected = ["[Attic]"]
self.assertEqual(expected, strip_text_styles(self.attic.look(short=True)))
expected = ["[Main hall]", "Exits: door, east, up", "You see: key, two magazines, and table", "Present: fly, julie, player, and two rats"]
self.assertEqual(expected, strip_text_styles(self.hall.look(short=True)))
expected = ["[Main hall]", "Exits: door, east, up", "You see: key, two magazines, and table", "Present: fly, julie, and two rats"]
self.assertEqual(expected, strip_text_styles(self.hall.look(exclude_living=self.player, short=True)))
def test_search_living(self):
self.assertEqual(None, self.hall.search_living("<notexisting>"))
self.assertEqual(None, self.attic.search_living("<notexisting>"))
self.assertEqual(self.fly, self.hall.search_living("fly"))
self.assertEqual(self.julie, self.hall.search_living("Julie"))
self.assertEqual(self.julie, self.hall.search_living("attractive julie"))
self.assertEqual(self.julie, self.hall.search_living("chick"))
self.assertEqual(None, self.hall.search_living("bloke"))
def test_search_item(self):
# almost identical to locate_item so only do a few basic tests
self.assertEqual(None, self.player.search_item("<notexisting>"))
self.assertEqual(self.pencil, self.player.search_item("pencil"))
def test_locate_item(self):
item, container = self.player.locate_item("<notexisting>")
self.assertEqual(None, item)
self.assertEqual(None, container)
item, container = self.player.locate_item("pencil")
self.assertEqual(self.pencil, item)
self.assertEqual(self.player, container)
item, container = self.player.locate_item("fountain pen")
self.assertEqual(self.pencil, item, "need to find the title")
item, container = self.player.locate_item("pen")
self.assertEqual(self.pencil, item, "need to find the alias")
item, container = self.player.locate_item("pencil", include_inventory=False)
self.assertEqual(None, item)
self.assertEqual(None, container)
item, container = self.player.locate_item("key")
self.assertEqual(self.key, item)
self.assertEqual(self.hall, container)
item, container = self.player.locate_item("key", include_location=False)
self.assertEqual(None, item)
self.assertEqual(None, container)
item, container = self.player.locate_item("KEY")
self.assertEqual(self.key, item, "should work case-insensitive")
self.assertEqual(self.hall, container, "should work case-insensitive")
item, container = self.player.locate_item("notebook")
self.assertEqual(self.notebook_in_bag, item)
#.........这里部分代码省略.........