def test_object_derivation_2 (self): class MainObject (object): def __init__(self, x): self.__x = x self.__x_variable = XVariable (self) def get_x (self): return self.__x def _set_x (self, x): self.__x = x x = property (lambda self: self.__x_variable) XVariable = AbstractValueTrackingVariable.derive_type ('XVariable', object = '__main', property = 'main', getter = MainObject.get_x, setter = MainObject._set_x) main = MainObject (100) self.assert_(main.x.main is main) self.assert_(main.x.value is main.get_x ()) main.x.value = 200 self.assert_(main.x.value is main.get_x ()) def set_main_x (): main.x = None self.assertRaises (AttributeError, set_main_x)
def test_derivation_12 (self): DerivedVariable = AbstractValueTrackingVariable.derive_type ('DerivedVariable', object = '__list', property = 'list') variable = DerivedVariable ([1, 2, 3], 200) self.assertEqual (variable.list, [1, 2, 3]) self.assertEqual (variable.value, 200)
def test_derivation_slots (self): DerivedVariable = AbstractVariable.derive_type ('DerivedVariable') self.assertRaises (AttributeError, self.non_existing_attribute_setter (DerivedVariable ())) DerivedVariable = AbstractValueTrackingVariable.derive_type ('DerivedVariable') self.assertRaises (AttributeError, self.non_existing_attribute_setter (DerivedVariable ())) DerivedVariable = Variable.derive_type ('DerivedVariable') self.assertRaises (AttributeError, self.non_existing_attribute_setter (DerivedVariable ()))
def test_derivation_7 (self): DerivedVariable = \ AbstractValueTrackingVariable.derive_type ('DerivedVariable', setter = lambda variable, value: None) variable = DerivedVariable () self.assert_(variable.value is None) variable.set (100) self.assert_(variable.value == 100) variable.value = 'abc' self.assert_(variable.value == 'abc')
def test_derivation_9 (self): test = NotifyTestObject () DerivedVariable = \ AbstractValueTrackingVariable.derive_type ('DerivedVariable', setter = (lambda variable, value: test.simple_handler (value))) variable = DerivedVariable () variable.set (100) variable.value = 'abc' # There is no getter at all, so setter must be called during variable creation. test.assert_results (None, 100, 'abc')
def test_derivation_8 (self): test = NotifyTestObject () DerivedVariable = \ AbstractValueTrackingVariable.derive_type ('DerivedVariable', getter = lambda variable: None, setter = (lambda variable, value: test.simple_handler (value))) variable = DerivedVariable () variable.set (100) variable.value = 'abc' # The default value is retrieved with the getter function, so the setter must not # be called during variable creation. test.assert_results (100, 'abc')
def test_object_derivation_1 (self): class MainObject (object): def __init__(self, x): self.__x = x def get_x (self): return self.__x XVariable = AbstractValueTrackingVariable.derive_type ('XVariable', object = 'main', getter = MainObject.get_x) main = MainObject (100) variable = XVariable (main) self.assert_(variable.main is main) self.assert_(variable.value is main.get_x ()) main.x = 200 self.assert_(variable.value is main.get_x ())
def test_derivation_3 (self): AbstractIntVariable = AbstractValueTrackingVariable.derive_type ( 'AbstractIntVariable', allowed_value_types = int) self.assertEqual (AbstractIntVariable (-5).mutable, False)