def test_deferreds(self): target = Thing() item = base.Item("key") now = datetime.datetime.now() deferreds = [ driver.Deferred(now, target.append, [1, 2, 3], {"kwarg": 42}), driver.Deferred(now, os.getcwd, [], None), driver.Deferred(now, module_level_func, [], None), driver.Deferred(now, item.init, [], None, periodical=(11.1, 22.2)) ] x1, x2, x3, x4 = serializecycle(deferreds) assert x1["__class__"] == "tale.driver.Deferred" assert x1["action"] == "append" assert x1["vargs"] == [1, 2, 3] assert x1["kwargs"] == {"kwarg": 42} assert x1["periodical"] is None assert x1["owner"] == "class:tests.supportstuff.Thing" assert x1["due_gametime"] == now.isoformat() assert x2["action"] == "getcwd" assert x2["owner"] in ("module:os", "module:nt", "module:posix") assert x3["action"] == "module_level_func" assert x3["owner"] == "module:tests.test_serialize" assert x4["action"] == "init" assert len(x4["owner"]) == 4 assert x4["owner"][1] == "key" assert x4["periodical"] == (11.1, 22.2)
def test_takability(self): p = base.Living("living", "m") item = base.Item("item") self.assertTrue(item.takeable) item.move(p, p) brd = board.BulletinBoard("board") self.assertFalse(brd.takeable) with self.assertRaises(ActionRefused) as x: brd.move(p, p, verb="frob") self.assertEqual("You can't frob board.", str(x.exception)) bx = basic.Boxlike("box") self.assertTrue(bx.takeable) bx.move(p, p) bnk = bank.Bank("bank") self.assertFalse(bnk.takeable) with self.assertRaises(ActionRefused) as x: bnk.move(p, p, verb="frob") self.assertEqual("The bank won't budge.", str(x.exception)) with self.assertRaises(ActionRefused) as x: bnk.allow_item_move(p, verb="frob") self.assertEqual("The bank won't budge.", str(x.exception)) # now flip the flags around bnk.takeable = True bnk.allow_item_move(p) bnk.move(p, p) bx.takeable = False with self.assertRaises(ActionRefused) as x: bx.move(p, p, verb="frob") self.assertEqual("You can't frob box.", str(x.exception)) brd.takeable = True brd.move(p, p) item.takeable = False with self.assertRaises(ActionRefused) as x: item.move(p, p, verb="frob") self.assertEqual("You can't frob item.", str(x.exception))
def test_make_catapult(self): stick = basic.woodenYstick elastic = basic.elastic_band thing = base.Item("thing") self.assertIsNone(stick.combine([thing], self.actor)) self.assertIsNone(stick.combine([elastic, thing], self.actor)) catapult = stick.combine([elastic], self.actor) self.assertIsInstance(catapult, basic.Catapult)
def test_pouch(self): p = basic.pouch thing = base.Item("thing") self.assertEqual(0, p.inventory_size) p.insert(thing, self.actor) self.assertTrue(thing in p) self.assertEqual(1, p.inventory_size) p.remove(thing, self.actor) self.assertFalse(thing in p)
def test_living_player(self): thing = base.Item("thing") p = player.Player("playername", "n", descr="description") p.insert(thing, None) p.title = "title" p.money = 42 p.brief = True p.story_data = {"data": 42} p.privileges.add("wizard") o = base.Living("name", "f", title="title", descr="description", race="dragon") o.aggressive = True o.following = p o.is_pet = True o.stats.attack_dice = "2d8" o.stats.level = 12 o.stats.hp = 100 x = serializecycle(o) assert x["__class__"] == "tale.base.Living" assert x["aggressive"] == True assert len(x["following"]) == 4 assert x["following"][1] == "playername" assert x["is_pet"] == True assert x["location"][1] == "Limbo" assert x["race"] == "dragon" assert len(x["privileges"]) == 0 assert "soul" not in x assert "teleported_from" not in x s = x["stats"] assert s["gender"] == "f" assert s["race"] == "dragon" assert s["attack_dice"] == "2d8" assert s["level"] == 12 assert s["hp"] == 100 x = serializecycle(p) assert x["__class__"] == x["__base_class__"] == "tale.player.Player" assert x["brief"] == True assert x["location"][1] == "Limbo" assert x["money"] == 42 assert x["name"] == "playername" assert x["story_data"]["data"] == 42 assert x["turns"] == 0 assert x["screen_width"] == p.screen_width assert x["hints"]["__class__"] == "tale.hints.HintSystem" assert x["hints"]["checkpoints"] == [None] assert x["stats"]["race"] == x["race"] == "human" assert x["stats"]["xp"] == 0 assert len(x["inventory"]) == 1 inv = x["inventory"].pop() assert inv[1] == "thing"
def test_trashcan(self): t = basic.trashcan thing = base.Item("thing") with self.assertRaises(ActionRefused): t.insert(thing, self.actor) t.open(self.actor) t.insert(thing, self.actor) self.assertTrue(thing in t) t.close(self.actor) with self.assertRaises(ActionRefused): t.remove(thing, self.actor) t.open(self.actor) t.remove(thing, self.actor) self.assertFalse(thing in t)
def test_items_and_container(self): o = base.Item("name", "title", "description") o.aliases = ["alias"] bag = base.Container("name", "title", "description") bag.insert(o, None) x = serializecycle(bag) self.assert_base_attrs(x) self.assertEqual(1, x.inventory_size) y = list(x.inventory)[0] self.assertEqual(x, y.contained_in) o = base.Weapon("w") x = serializecycle(o) self.assertEqual("w", x.name) o = base.Armour("a") x = serializecycle(o) self.assertEqual("a", x.name)
def test_location(self): room = base.Location("room", "description") thing = base.Item("thing") room.insert(thing, None) npc = base.Living("dog", "m") room.insert(npc, None) x = serializecycle(room) assert x["__class__"] == "tale.base.Location" assert x["name"] == "room" assert x["descr"] == "description" assert x["exits"] == () assert len(x["items"]) == 1 assert len(x["livings"]) == 1 assert isinstance(x["items"], set) assert isinstance(x["livings"], set) x_item = x["items"].pop() x_living = x["livings"].pop() assert len(x_item) == 4 assert x_item[0] > 0 assert x_item[1] == "thing" assert len(x_living) == 4 assert x_living[0] > 0 assert x_living[1] == "dog" # now add some exits and a second location, and try again room2 = base.Location("room2", "description") exit1 = base.Exit("room2", room2, "to room2") exit2 = base.Exit("room", room, "back to room") room.add_exits([exit1]) room2.add_exits([exit2]) x1, x2 = serializecycle([room, room2]) assert len(x1["exits"]) == 1 assert isinstance(x1["exits"], set) assert len(x2["exits"]) == 1 assert isinstance(x2["exits"], set) x_exit = x1["exits"].pop() assert len(x_exit) == 4 assert x_exit[0] > 0 assert x_exit[1] == "room2" assert x_exit[2] == x_exit[3] == "tale.base.Exit" assert x2["name"] == "room2" x_exit = x2["exits"].pop() assert len(x_exit) == 4 assert x_exit[0] > 0 assert x_exit[1] == "room"
def test_items_and_container(self): o = base.Item("name1", "title1", descr="description1") o.aliases = ["alias1"] bag = base.Container("name2", "title2", descr="description2") bag.insert(o, None) x = serializecycle(bag) x_inv = x["inventory"] assert isinstance(x_inv, set) assert len(x_inv) == 1 x_contained = x_inv.pop() assert len(x_contained) == 4 assert x_contained[0] > 1 assert x_contained[1] == 'name1' assert x_contained[2] == 'tale.base.Item' assert x_contained[3] == 'tale.base.Item' x = serializecycle(o) assert "inventory" not in x assert "location" not in x and "contained_in" not in x, "item is referenced from its location instead" o = base.Armour("a") x = serializecycle(o) assert x["__class__"] == "tale.base.Armour" assert x["__base_class__"] == "tale.base.Item"
def test_fundamentals(self): o = serializecycle(races.races) assert len(races.races) == len(o) assert "golem" in o o = base.Item("name", "title", descr="description", short_descr="short description") o.aliases = ["alias"] o.default_verb = "push" o.extra_desc = {"thing": "there's a thing"} o.rent = 99 o.value = 88 o.story_data["data"] = 42 o.weight = 123.0 loc = base.Location("location") loc.insert(o, None) x = serializecycle(o) assert isinstance(x, dict) assert x["__base_class__"] == "tale.base.Item" assert x["__class__"] == "tale.base.Item" assert x["aliases"] == ["alias"] assert x["default_verb"] == "push" assert x["descr"] == "description" assert x["extra_desc"] == {"thing": "there's a thing"} assert x["name"] == "name" assert x["rent"] == 99 assert x["value"] == 88 assert x["short_descr"] == "short description" assert x["story_data"] == {"data": 42} assert x["takeable"] == True assert x["title"] == "title" assert x["vnum"] > 0 assert x["weight"] == 123.0 assert "inventory" not in x assert "location" not in x and "contained_in" not in x, "item is referenced from its location instead"