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 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_move_get_drop_container(self): with locks.authority_of(locks.SYSTEM): magician = db.Object("magician") rabbit = db.Object("stubborn rabbit") db.store(magician) db.store(rabbit) with locks.authority_of(rabbit): rabbit.locks.take = locks.Fail() rabbit.locks.drop = locks.Fail() rabbit.locks.insert = locks.Is(magician) with locks.authority_of(magician): carrot = db.Object("carrot", magician) celery = db.Object("celery", magician) hat = db.Container("hat", magician) db.store(carrot) db.store(celery) db.store(hat) try: rabbit.location = magician except locks.LockFailedError as e: self.assertEqual(str(e), "You cannot take stubborn rabbit.") else: self.fail() carrot.location = rabbit with locks.authority_of(rabbit): rabbit.locks.take = locks.Is(magician) rabbit.location = magician try: rabbit.location = hat except locks.LockFailedError as e: self.assertEqual(str(e), "You cannot drop stubborn rabbit.") else: self.fail() celery.location = rabbit with locks.authority_of(rabbit): rabbit.locks.drop = locks.Is(magician) rabbit.location = hat rabbit.location = magician
def __init__(self, name, location=None, owner=None): """ Create a brand-new object and add it to the database. Args: name, location (default None), owner (defaults to current authority) as described in the class docstring """ lock = locks.AttributeLock(locks.SYSTEM, locks.Fail(), locks.Fail()) super(Object, self).__setattr__("attr_locks", {"attr_locks": lock}) if owner is not None: owner_ = owner else: if locks.authority() is not None: owner_ = locks.authority() else: raise locks.MissingAuthorityError("Object created with no " "owner and no authority.") with locks.authority_of(locks.SYSTEM): self.uid = None # This will be assigned when we call store() self.type = 'thing' self.owner = owner_ self.name = name self.lock_attr("name", set_lock=locks.Owns(self)) self.lock_attr("owner", set_lock=locks.Owns(self)) self.locks = Locks() self.lock_attr("locks", set_lock=locks.Fail()) self._location = None with locks.authority_of(self.owner): self.description = "You see nothing special." self.locks.take = locks.Pass() self.locks.drop = locks.Pass() self.locks.insert = locks.Is(self) self.locks.remove = locks.Is(self) self.locks.destroy = locks.Owns(self) if location: self.location = location
def test_is(self): lock = locks.Is(self.player) self.assertTrue(lock(self.player)) self.assertFalse(lock(self.player2))