def test_property(self):
    this = Obj()
    prop = Property(this)
    
    # No setter and no getter has been set, this should raise exception
    self.assertRaises(WriteOnlyException, prop.get)
    self.assertRaises(ReadOnlyException, prop.set, 2)
    
    # Set a value on the this object
    setattr(this, "val", 2)
    
    # Add the getter and setter
    prop.getter = lambda this: getattr(this, "val") + 1
    prop.setter = lambda this, val: setattr(this, "val", val - 1)
    
    # Check that the get function is working
    self.assertEqual(prop.get(), 3)

    # Check that the set function is working
    prop.set(4)
    self.assertEqual(prop.get(), 4)
    self.assertEqual(getattr(this, "val"), 3)
    
    # Check that the clone function is working
    prop2 = prop.clone()
    self.assertEqual(prop2.get(), 4)
    this2 = Obj()
    setattr(this2, "val", 4)
    prop2.this = this2
    self.assertEqual(prop.get(), 4)
    self.assertEqual(prop2.get(), 5)
    prop2.set(3)
    self.assertEqual(prop.get(), 4)
    self.assertEqual(prop2.get(), 3)
 def defineProperty(self, this, obj, name, param):
   prop = Property(this)
   if(hasattr(param, 'get')):
     prop.getter = param.get
   if(hasattr(param, 'set')):
     prop.setter = param.set
   setattr(obj, name, prop)
示例#3
0
 def defineProperty(self, this, obj, name, param):
     prop = Property(obj)
     if (hasattr(param, 'get')):
         prop.getter = list(param.get)[1]
     if (hasattr(param, 'set')):
         prop.setter = list(param.set)[1]
     setattr(obj, name, prop)
示例#4
0
 def execute_MAKE_SETTER(self):
     name = self.stack.pop()
     func = self.stack.pop()
     obj = self.stack.pop()
     prop = Property(obj)
     prop.setter = func
     if hasattr(obj, name):
         prop.merge(getattr(obj, name))
     else:
         setattr(prop, 'getter', None)
     setattr(obj, name, prop)
     self.stack.push(obj)
 def execute_MAKE_SETTER(self):
   name = self.stack.pop()
   func = self.stack.pop()
   obj  = self.stack.pop()
   prop = Property(obj)
   prop.setter = func
   if hasattr(obj, name):
     prop.merge(getattr(obj, name))
   else:
     setattr(prop, 'getter', None)
   setattr(obj, name, prop)
   self.stack.push(obj)
示例#6
0
 def defineProperty(self, this, obj, name, param):
     #prop = Property(obj, param.getter, param.setter)
     prop = Property(obj)
     #print("IN DEFINE PROPERTY")
     #print(name)
     #print(type(param))
     if (hasattr(param, 'get')):
         #print("¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤")
         #prop.getter = list(param.get)[1]
         prop.getter = param.get
     if (hasattr(param, 'set')):
         #prop.setter = list(param.set)[1]
         prop.setter = param.set
     #prop.getter = lambda this: getattr(this, str(param))
     #prop.setter = lambda this, val: setattr(this, param, val)
     setattr(obj[1], name, prop)
    def execute_make_setter(self):
        '''
    Execute the MAKE_SETTER instruction
    '''
        name = self.stack.pop()
        func = self.stack.pop()
        obj = self.stack.pop()

        if hasattr(obj, name):
            prop = getattr(obj, name)
            prop.setter = func
        else:
            prop = Property(obj)
            prop.setter = func
            setattr(obj, name, prop)
        self.stack.push(obj)
示例#8
0
    def visitObjectLiteralExpression(self, ctx):
        obj = Object()

        # res is a list of tuples
        res = ctx.children[0].accept(self)

        for value in res:
            if len(value) == 3:
                prop = Property(obj)
                if value[0] == 'get':
                    prop.getter = value[-1]
                else:
                    prop.setter = value[-1]
                setattr(obj, str(value[1]), prop)
            else:
                setattr(obj, str(value[0]), value[1])
        return obj
  def test_property_merge(self):
    # Check that the merge function is working
    this = Obj()
    prop  = Property(this)
    prop2 = Property(this)
    
    setattr(this, "val", 2)
    prop.getter = lambda this: getattr(this, "val") + 1
    prop2.setter = lambda this, val: setattr(this, "val", val - 1)

    self.assertEqual(prop.get(), 3)
    prop2.set(4)
    self.assertEqual(prop.get(), 4)
    self.assertEqual(getattr(this, "val"), 3)
    
    prop.merge(prop2)
    prop.set(3)
    self.assertEqual(prop.get(), 3)
    self.assertEqual(getattr(this, "val"), 2)
    def test_property_merge(self):
        # Check that the merge function is working
        this = Obj()
        prop = Property(this)
        prop2 = Property(this)

        setattr(this, "val", 2)
        prop.getter = lambda this: getattr(this, "val") + 1
        prop2.setter = lambda this, val: setattr(this, "val", val - 1)

        self.assertEqual(prop.get(), 3)
        prop2.set(4)
        self.assertEqual(prop.get(), 4)
        self.assertEqual(getattr(this, "val"), 3)

        prop.merge(prop2)
        prop.set(3)
        self.assertEqual(prop.get(), 3)
        self.assertEqual(getattr(this, "val"), 2)
    def test_property(self):
        this = Obj()
        prop = Property(this)

        # No setter and no getter has been set, this should raise exception
        self.assertRaises(WriteOnlyException, prop.get)
        self.assertRaises(ReadOnlyException, prop.set, 2)

        # Set a value on the this object
        setattr(this, "val", 2)

        # Add the getter and setter
        prop.getter = lambda this: getattr(this, "val") + 1
        prop.setter = lambda this, val: setattr(this, "val", val - 1)

        # Check that the get function is working
        self.assertEqual(prop.get(), 3)

        # Check that the set function is working
        prop.set(4)
        self.assertEqual(prop.get(), 4)
        self.assertEqual(getattr(this, "val"), 3)

        # Check that the clone function is working
        prop2 = prop.clone()
        self.assertEqual(prop2.get(), 4)
        this2 = Obj()
        setattr(this2, "val", 4)
        prop2.this = this2
        self.assertEqual(prop.get(), 4)
        self.assertEqual(prop2.get(), 5)
        prop2.set(3)
        self.assertEqual(prop.get(), 4)
        self.assertEqual(prop2.get(), 3)