def setUp(self): super(AttrLockTestCase, self).setUp() self.obj_owner = db.Player("Objowner", "password") db.store(self.obj_owner) self.attr_owner = db.Player("Attrowner", "password") db.store(self.attr_owner) self.setter = db.Player("Setter", "password") db.store(self.setter) self.getter = db.Player("Getter", "password") db.store(self.getter) self.players = [self.obj_owner, self.attr_owner, self.getter, self.setter] with locks.authority_of(self.obj_owner): self.obj = db.Object("Object") db.store(self.obj) with locks.authority_of(self.attr_owner): self.obj.attr = "value" self.obj.lock_attr("attr", get_lock=locks.Is(self.getter), set_lock=locks.Is(self.setter))
def __init__(self, name, password): """ Create a brand-new player and add it to the database. Args: name: The player's name. password: The player's password, in plaintext, to be discarded forever after this method call. """ Object.__init__(self, name, location=get(0), owner=self) with locks.authority_of(locks.SYSTEM): self.type = 'player' self.lock_attr("name", set_lock=locks.Fail()) self.lock_attr("owner", set_lock=locks.Fail()) self.password = self.hash(password) self.textwrapper = textwrap.TextWrapper() # Initialize the mode stack empty, but enter_mode() must be called # before any input is handled. self.mode_stack = [] self.lock_attr("mode_stack", set_lock=locks.Owns(self)) self.last_told = None with locks.authority_of(self): self.locks.take = locks.Fail() self.locks.destroy = locks.Fail() # While we're under development, let's assume everybody wants debug # information. self.debug = True # Until there's a command to join a channel, do it automatically. channels._channels['Public'].join(self)
def test_exit(self): with locks.authority_of(locks.SYSTEM): owner = db.Object("owner") db.store(owner) with locks.authority_of(owner): source = db.Room("Source") dest = db.Room("Dest") db.store(source) db.store(dest) player = db.Object("Player", location=source) player.send = mock.MagicMock() db.store(player) exit = db.Exit("Exit", source, dest) exit.go_message = ("You, {player}, go from {source} to " "{destination} via {exit}.") db.store(exit) sourceBystander = db.Object("source bystander", location=source) sourceBystander.send = mock.MagicMock() db.store(sourceBystander) destBystander = db.Object("dest bystander", location=dest) destBystander.send = mock.MagicMock() db.store(destBystander) self.assertIs(player.location, source) exit.go(player) self.assertIs(player.location, dest) self.assertEqual(sourceBystander.send.call_count, 1) self.assertEqual(destBystander.send.call_count, 1) sourceBystander.send.assert_called_once_with( "Player leaves through Exit.") destBystander.send.assert_called_once_with("Player arrives.") player.send.assert_called_with("You, Player, go from Source to " "Dest via Exit.")
def test_move_insert_remove(self): with locks.authority_of(locks.SYSTEM): hat = db.Object("hat") magician = db.Object("magician") db.store(hat) db.store(magician) try: with locks.authority_of(magician): rabbit = db.Object("rabbit", hat) except locks.LockFailedError as e: self.assertEqual(str(e), "You can't put that in hat.") else: self.fail() with locks.authority_of(hat): hat.locks.insert = locks.Is(magician) with locks.authority_of(magician): rabbit = db.Object("rabbit", hat) db.store(rabbit) try: rabbit.location = magician except locks.LockFailedError as e: self.assertEqual(str(e), "You can't remove that from hat.") else: self.fail() with locks.authority_of(hat): hat.locks.remove = locks.Is(magician) rabbit.location = magician self.assertEqual(rabbit.location, magician)
def test_contextmanager_nested(self): self.assertIs(locks._authority, None) with locks.authority_of(self.player): self.assertIs(locks._authority, self.player) with locks.authority_of(self.player2): self.assertIs(locks._authority, self.player2) self.assertIs(locks._authority, self.player) self.assertIs(locks._authority, None)
def setUp(self): super(LockTestCase, self).setUp() with locks.authority_of(locks.SYSTEM): self.player = db.Player("Player", "password") db.store(self.player) self.player2 = db.Player("PlayerTwo", "password") db.store(self.player2) self.obj = db.Object("object", owner=self.player) with locks.authority_of(self.player2): self.obj.foreign_attr = 0 db.store(self.obj)
def setup_objects(self): """ Generates the following clutter: OBJECTS IN THE LOBBY: abacus, ant, balloon, Bucket, cat, frog, Fodor's Guide, horse IN PLAYER'S INVENTORY: Anabot doll, ape plushie, apple, cat, cherry, cheese, horse figurine, monster mask, monocle, moose, millipede IN NEIGHBOR'S INVENTORY: apple IN FROG'S INVENTORY: hat All of these are stored in self.objects[name], EXCEPT: * the cat in the room is objects["room_cat"] * the cat in player's inventory is objects["inv_cat"] * the apple in neighbor's inventory is ["neighbor_apple"] All are plain db.Objects, EXCEPT: * monocle and monster mask are equipment.Equipment * Bucket is a db.Container The player owns all the objects in its inventory. SYSTEM owns the rest. """ self.objects = {} with locks.authority_of(self.player): for inv_object in [ "apple", "horse figurine", "ape plushie", "Anabot doll", "cherry", "cheese", "moose", "millipede" ]: self.objects[inv_object] = db.Object(inv_object, self.player) self.objects["monocle"] = equipment.Equipment( "monocle", self.player) self.objects["monster mask"] = equipment.Equipment( "monster mask", self.player) with locks.authority_of(locks.SYSTEM): for room_object in [ "frog", "ant", "horse", "Fodor's Guide", "abacus", "balloon" ]: self.objects[room_object] = db.Object(room_object, location=self.lobby) self.objects["Bucket"] = db.Container("Bucket", self.lobby) self.objects["room_cat"] = db.Object("cat", self.lobby) self.objects["inv_cat"] = db.Object("cat", self.player) self.objects["neighbor_apple"] = db.Object("apple", self.neighbor) self.objects["hat"] = db.Object("hat", self.objects["frog"]) for key in self.objects: db.store(self.objects[key])
def __init__(self, name, owner=None): super(Room, self).__init__(name, None, owner) self.locks.insert = locks.Pass() self.locks.remove = locks.Pass() self.locks.take = locks.Fail() with locks.authority_of(locks.SYSTEM): self.type = "room"
def lineReceived(self, line): """ Respond to a received line by passing to whatever mode is current. Args: line: The line received, without a trailing delimiter. """ try: with locks.authority_of(self.player): self.player.mode.handle(self.player, line) except Exception: # Exceptions are supposed to be caught somewhere lower down and # handled specifically. If we catch one here, it's a code error. log.err() if hasattr(self.player, "debug") and self.player.debug: for line in traceback.format_exc().split("\n"): self.player.send(line) else: self.player.send( "Sorry! Something went wrong. We'll look into " "it.") if self.player.mode.blank_line: self.player.send("")
def test_position_string(self): with locks.authority_of(locks.SYSTEM): model = db.Object("model") db.store(model) model.position = "vogueing for the camera" self.assertEqual(model.position_string(), "model (vogueing for the camera)")
def test_retrieve_none(self): with locks.authority_of(locks.SYSTEM): foo = db.Object("foo") self.assertRaises(KeyError, db.find, lambda obj: obj.name == "bar") found = db.find_all(lambda obj: obj.name == "bar") self.assertEqual(len(found), 0)
def setUp(self): super(SocialTestCase, self).setUp() self.neighbor = self.new_player("Neighbor") self.otherneighbor = self.new_player("OtherNeighbor") self.notconnected = self.new_player("NotConnected") with locks.authority_of(locks.SYSTEM): self.notconnected.mode_stack = []
def test_destroy_emit_elsewhere(self): with locks.authority_of(self.player): new_room = db.Room("a room") db.store(new_room) self.player.send_line("destroy #{}".format(new_room.uid)) self.assertNotEqual(self.neighbor.last_response(), "Player destroys a room.")
def execute(self, player, args): if getattr(player, "debug"): line = args["line"] with locks.authority_of(locks.SYSTEM): player.mode.handle(player, line) else: player.send("You're not set for debugging!")
def test_neighbors(self): with locks.authority_of(locks.SYSTEM): container = db.Object("container") foo = db.Object("foo", location=container) neighbor = db.Object("neighbor", location=container) containee = db.Object("containee", location=foo) distant = db.Object("distant") inside_neighbor = db.Object("inside neighbor", location=neighbor) inside_containee = db.Object("inside containee", location=containee) db.store(container) db.store(foo) db.store(neighbor) db.store(containee) db.store(distant) db.store(inside_neighbor) db.store(inside_containee) neighbors = foo.neighbors() self.assertIn(container, neighbors) self.assertIn(foo, neighbors) self.assertIn(neighbor, neighbors) self.assertIn(containee, neighbors) self.assertNotIn(distant, neighbors) self.assertNotIn(inside_neighbor, neighbors) self.assertNotIn(inside_containee, neighbors) self.assertEqual(len(neighbors), 4)
def send_line(self, command): """ Send a string to this player's current mode, as if they'd typed it in at the console. """ with locks.authority_of(self): self.mode.handle(self, command)
def test_exit_nospace(self): with locks.authority_of(locks.SYSTEM): self.foyer = db.Room("foyer") self.zzzfoo = db.Exit("zzzfoo", self.lobby, self.foyer) db.store(self.foyer) db.store(self.zzzfoo) self.assert_response("zzzfoo", "Spaaaaaaaaaaaaaace. (foo).")
def test_many_exits_one_command(self): with locks.authority_of(locks.SYSTEM): self.exit_h1 = db.Exit("h1", self.lobby, self.lobby) self.exit_h2 = db.Exit("h2", self.lobby, self.lobby) db.store(self.exit_h1) db.store(self.exit_h2) self.assert_response("h", startswith="Available commands:")
def test_many_exits_and_commands(self): with locks.authority_of(locks.SYSTEM): self.exit_s1 = db.Exit("s1", self.lobby, self.lobby) self.exit_s2 = db.Exit("s2", self.lobby, self.lobby) db.store(self.exit_s1) db.store(self.exit_s2) self.assert_response("s", startswith="Which command do you mean")
def test_many_exits_one_nospace(self): with locks.authority_of(locks.SYSTEM): self.exit_zzza = db.Exit("zzza", self.lobby, self.lobby) self.exit_zzzb = db.Exit("zzzb", self.lobby, self.lobby) db.store(self.exit_zzza) db.store(self.exit_zzzb) self.assert_response("zzz foo", "Spaaaaaaaaaaaaaace. (foo).")
def run_command(self, command, string): """ Parse the string against the command's argument pattern, then execute the command on behalf of the player. """ args = command.args(self.player).parseString(string) with locks.authority_of(self.player): command().execute(self.player, args)
def setUp(self): self.patch(db, "_objects", {}) self.patch(db, "_nextUid", 0) with locks.authority_of(locks.SYSTEM): self.lobby = db.Room("lobby") db.store(self.lobby) self.player = self.new_player("Player") self.neighbor = self.new_player("PlayersNeighbor")
def test_ambiguous_exit(self): with locks.authority_of(locks.SYSTEM): self.foyer = db.Room("foyer") self.exit_ju = db.Exit("jump", self.lobby, self.foyer) self.exit_jo = db.Exit("joust", self.lobby, self.foyer) for obj in self.foyer, self.exit_ju, self.exit_jo: db.store(obj) self.assert_response("j", "Which exit do you mean? (joust, jump)")
def test_exit_permissions(self): with locks.authority_of(locks.SYSTEM): self.foyer = db.Room("foyer") self.exit = db.Exit("exit", self.lobby, self.foyer) self.exit.locks.go = locks.Fail() db.store(self.foyer) db.store(self.exit) self.assert_response("exit", "You can't go through exit.")
def test_nearbyobject_here(self): pattern = parser.NearbyObject(self.player) self.assert_parse(pattern, "here", self.lobby) with locks.authority_of(locks.SYSTEM): here = db.Object("here", self.lobby) db.store(here) pattern = parser.NearbyObject(self.player) self.assert_parse(pattern, "here", here)
def test_retell_failure(self): self.assert_response("retell to nowhere", "You haven't sent a tell to anyone yet.") self.assert_response("tell o hi", "You tell OtherNeighbor: hi") with locks.authority_of(locks.SYSTEM): self.otherneighbor.mode_stack = [] self.assert_response("retell to nowhere", "OtherNeighbor is not connected.")
def test_create(self): expected_uid = db._nextUid with locks.authority_of(locks.SYSTEM): obj = db.Object("foo") self.assertEqual(obj.uid, None) db.store(obj) self.assertEqual(obj.uid, expected_uid) self.assertEqual(db._nextUid, expected_uid + 1)
def setup_objects(self): """ Generates the following clutter: OBJECTS IN THE LOBBY: abacus, ant, balloon, Bucket, cat, frog, Fodor's Guide, horse IN PLAYER'S INVENTORY: Anabot doll, ape plushie, apple, cat, cherry, cheese, horse figurine, monster mask, monocle, moose, millipede IN NEIGHBOR'S INVENTORY: apple IN FROG'S INVENTORY: hat All of these are stored in self.objects[name], EXCEPT: * the cat in the room is objects["room_cat"] * the cat in player's inventory is objects["inv_cat"] * the apple in neighbor's inventory is ["neighbor_apple"] All are plain db.Objects, EXCEPT: * monocle and monster mask are equipment.Equipment * Bucket is a db.Container The player owns all the objects in its inventory. SYSTEM owns the rest. """ self.objects = {} with locks.authority_of(self.player): for inv_object in ["apple", "horse figurine", "ape plushie", "Anabot doll", "cherry", "cheese", "moose", "millipede"]: self.objects[inv_object] = db.Object(inv_object, self.player) self.objects["monocle"] = equipment.Equipment("monocle", self.player) self.objects["monster mask"] = equipment.Equipment("monster mask", self.player) with locks.authority_of(locks.SYSTEM): for room_object in ["frog", "ant", "horse", "Fodor's Guide", "abacus", "balloon"]: self.objects[room_object] = db.Object(room_object, location=self.lobby) self.objects["Bucket"] = db.Container("Bucket", self.lobby) self.objects["room_cat"] = db.Object("cat", self.lobby) self.objects["inv_cat"] = db.Object("cat", self.player) self.objects["neighbor_apple"] = db.Object("apple", self.neighbor) self.objects["hat"] = db.Object("hat", self.objects["frog"]) for key in self.objects: db.store(self.objects[key])
def test_nearbyobject_me(self): pattern = parser.NearbyObject(self.player) self.assert_parse(pattern, "me", self.player) with locks.authority_of(locks.SYSTEM): me = db.Object("me", self.lobby) db.store(me) pattern = parser.NearbyObject(self.player) self.assert_parse(pattern, "me", me)
def test_others_cannot_set(self): with locks.authority_of(self.getter): try: self.set() except locks.LockFailedError: pass else: self.fail("Expected LockFailedError when setting attribute")
def test_others_cannot_delete(self): with locks.authority_of(self.obj_owner): try: self.delete() except locks.LockFailedError: pass else: self.fail("Expected LockFailedError when deleting attribute")
def test_others_can_create(self): # This is redundant with the setup, but might as well test explicitly # in case that changes with locks.authority_of(self.attr_owner): try: self.create() except locks.LockFailedError as e: self.fail(e)