def test_garbage_collection_3(self): test = NotifyTestObject() variable = Variable() condition1 = variable.is_true() condition2 = ~condition1 condition2.store(test.simple_handler) condition1 = weakref.ref(condition1) condition2 = weakref.ref(condition2) self.collect_garbage() self.assertNotEqual(condition1(), None) self.assertNotEqual(condition2(), None) self.collect_garbage() variable.value = 10 condition2().changed.disconnect(test.simple_handler) self.collect_garbage() self.assertEqual(condition1(), None) self.assertEqual(condition2(), None) variable = weakref.ref(variable) self.collect_garbage() self.assertEqual(variable(), None) test.assert_results(True, False)
def test_garbage_collection_3 (self): test = NotifyTestObject () variable = Variable () condition1 = variable.is_true () condition2 = ~condition1 condition2.store (test.simple_handler) condition1 = weakref.ref (condition1) condition2 = weakref.ref (condition2) self.collect_garbage () self.assertNotEqual (condition1 (), None) self.assertNotEqual (condition2 (), None) self.collect_garbage () variable.value = 10 condition2 ().changed.disconnect (test.simple_handler) self.collect_garbage () self.assertEqual (condition1 (), None) self.assertEqual (condition2 (), None) variable = weakref.ref (variable) self.collect_garbage () self.assertEqual (variable (), None) test.assert_results (True, False)
def test_with_changes_frozen_1 (self): test = NotifyTestObject () variable = Variable () variable.changed.connect (test.simple_handler) variable.with_changes_frozen (lambda: None) # Must not emit `changed' signal: no changes at all. test.assert_results ()
def test_with_changes_frozen_1(self): test = NotifyTestObject() variable = Variable() variable.changed.connect(test.simple_handler) variable.with_changes_frozen(lambda: None) # Must not emit `changed' signal: no changes at all. test.assert_results()
def test_changes_frozen_2(self): test = NotifyTestObject() variable = Variable() variable.changed.connect(test.simple_handler) with variable.changes_frozen(): variable.value = 1 test.assert_results(1)
def test_changes_frozen_2 (self): test = NotifyTestObject () variable = Variable () variable.changed.connect (test.simple_handler) with variable.changes_frozen (): variable.value = 1 test.assert_results (1)
def test_changes_frozen_4(self): test = NotifyTestObject() variable = Variable() variable.changed.connect(test.simple_handler) with variable.changes_frozen(): variable.value = 1 variable.value = None # Must not emit: value returned to original. test.assert_results()
def test_changes_frozen_4 (self): test = NotifyTestObject () variable = Variable () variable.changed.connect (test.simple_handler) with variable.changes_frozen (): variable.value = 1 variable.value = None # Must not emit: value returned to original. test.assert_results ()
def test_storing_1 (self): test = NotifyTestObject () variable = Variable () variable.value = 100 with variable.storing (test.simple_handler): variable.value = 200 variable.value = 300 test.assert_results (100, 200)
def test_transformation_1 (self): variable = Variable (0) floor = variable.transform (math.floor) self.assertEqual (floor.value, 0) self.assert_(not floor.mutable) variable.value = 10.5 self.assertEqual (floor.value, 10) variable.value = 15 self.assertEqual (floor.value, 15)
def test_with_changes_frozen_3(self): test = NotifyTestObject() variable = Variable() variable.changed.connect(test.simple_handler) def do_changes(): variable.value = 1 variable.value = 2 variable.with_changes_frozen(do_changes) test.assert_results(2)
def test_storing_2 (self): test = NotifyTestObject () variable = Variable () variable.value = 100 with nested (ignoring_exceptions (), variable.storing (test.simple_handler)): variable.value = 200 raise Exception variable.value = 300 test.assert_results (100, 200)
def test_with_changes_frozen_3 (self): test = NotifyTestObject () variable = Variable () variable.changed.connect (test.simple_handler) def do_changes (): variable.value = 1 variable.value = 2 variable.with_changes_frozen (do_changes) test.assert_results (2)
def test_watcher_variable_1 (self): test = NotifyTestObject () watcher = WatcherVariable () watcher.store (test.simple_handler) variable = Variable ('abc') watcher.watch (variable) self.assert_(watcher.watched_variable is variable) variable.value = 60 test.assert_results (None, 'abc', 60)
def test_predicate_1 (self): variable = Variable (0) is_single_digit = variable.predicate (lambda value: 0 <= value < 10) self.assert_(is_single_digit) self.assert_(not is_single_digit.mutable) variable.value = -5 self.assert_(not is_single_digit) variable.value = 9 self.assert_(is_single_digit) variable.value = 100 self.assert_(not is_single_digit)
def test_derivation_6 (self): StringVariable = Variable.derive_type ('StringVariable', allowed_value_types = StringType, setter = lambda variable, value: None) variable = StringVariable ('') self.assertRaises (ValueError, lambda: variable.set (None))
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_with_changes_frozen_7 (self): test = NotifyTestObject () variable = Variable () variable.changed.connect (test.simple_handler) def do_changes_1 (): def do_changes_2 (): variable.value = 1 variable.with_changes_frozen (do_changes_2) variable.value = None variable.with_changes_frozen (do_changes_1) # Must not emit: value returned to original. test.assert_results ()
def test_with_changes_frozen_7(self): test = NotifyTestObject() variable = Variable() variable.changed.connect(test.simple_handler) def do_changes_1(): def do_changes_2(): variable.value = 1 variable.with_changes_frozen(do_changes_2) variable.value = None variable.with_changes_frozen(do_changes_1) # Must not emit: value returned to original. test.assert_results()
def test_derivation_5 (self): IntVariable = Variable.derive_type ('IntVariable', allowed_value_types = int, default_value = 10) variable = IntVariable () self.assertEqual (variable.value, 10) variable = IntVariable (30) self.assertEqual (variable.value, 30) self.assertRaises (ValueError, lambda: variable.set ('string'))
def test_multiple_derivation (self): # Derive two types and make sure they don't spoil each other's is_allowed_value() # method. IntVariable = Variable.derive_type ('IntVariable', allowed_value_types = int) StrVariable = Variable.derive_type ('StrVariable', allowed_value_types = str) integer = IntVariable (10) string = StrVariable ('test') integer.value = 20 self.assertEqual (integer.value, 20) string.value = 'string' self.assertEqual (string.value, 'string') self.assertRaises (ValueError, lambda: integer.set ('foo')) self.assertRaises (ValueError, lambda: string .set (-1000)) self.assertRaises (ValueError, lambda: integer.set ('')) self.assertRaises (ValueError, lambda: string .set (0))
def test_storing_safely_1(self): test = NotifyTestObject() variable = Variable() variable.value = 100 with variable.storing_safely(test.simple_handler): variable.value = 200 with variable.storing_safely(test.simple_handler): variable.value = 300 variable.value = 400 variable.value = 500 test.assert_results(100, 200, 300, 400)
def test_synchronizing_1(self): test = NotifyTestObject() variable1 = Variable() variable2 = Variable() variable1.value = 100 variable2.value = 200 variable1.changed.connect(test.simple_handler) with variable1.synchronizing(variable2): variable2.value = 300 variable2.value = 400 test.assert_results(200, 300)
def test_synchronizing_2(self): test = NotifyTestObject() variable1 = Variable() variable2 = Variable() variable1.value = 100 variable2.value = 200 variable1.changed.connect(test.simple_handler) with nested(ignoring_exceptions(), variable1.synchronizing(variable2)): variable2.value = 300 raise Exception variable2.value = 400 test.assert_results(200, 300)
def test_derivation_2 (self): EnumVariable = Variable.derive_type ('EnumVariable', allowed_values = (None, 'a', 'b', 'c')) variable = EnumVariable () self.assertEqual (variable.value, None) self.assertEqual (variable.mutable, True) variable.value = 'b' self.assertEqual (variable.value, 'b') self.assertRaises (ValueError, lambda: variable.set (15)) self.assertRaises (ValueError, lambda: variable.set ('d'))
def test_synchronizing_safely_1 (self): test = NotifyTestObject () variable1 = Variable () variable2 = Variable () variable1.value = 100 variable2.value = 200 variable1.changed.connect (test.simple_handler) variable1.synchronize (variable2) with variable1.synchronizing_safely (variable2): variable2.value = 300 variable2.value = 400 test.assert_results (200, 300, 400)
def test_derivation_1 (self): IntVariable = Variable.derive_type ('IntVariable', allowed_value_types = int) # Since None is not an allowed value, there must be no default constructor. self.assertRaises (TypeError, lambda: IntVariable ()) count = IntVariable (10) self.assertEqual (count.value, 10) self.assertEqual (count.mutable, True) count.value = 30 self.assertEqual (count.value, 30) self.assertRaises (ValueError, lambda: count.set ('invalid'))
def test_synchronizing_safely_2 (self): test = NotifyTestObject () variable1 = Variable () variable2 = Variable () variable1.value = 100 variable2.value = 200 variable1.changed.connect (test.simple_handler) variable1.synchronize (variable2) with nested (ignoring_exceptions (), variable1.synchronizing_safely (variable2)): variable2.value = 300 raise Exception variable2.value = 400 test.assert_results (200, 300, 400)
def test_storing_safely_2(self): test = NotifyTestObject() variable = Variable() variable.value = 100 with nested(ignoring_exceptions(), variable.storing_safely(test.simple_handler)): variable.value = 200 with nested(ignoring_exceptions(), variable.storing_safely(test.simple_handler)): variable.value = 300 variable.value = 400 raise Exception variable.value = 500 test.assert_results(100, 200, 300, 400)
def test_garbage_collection_1(self): test = NotifyTestObject() variable = Variable() condition = variable.is_true() condition.store(test.simple_handler) condition = weakref.ref(condition) # This must not collect the `is_true' condition, even though it is not directly # referenced at all. self.collect_garbage() self.assertNotEqual(condition(), None) self.collect_garbage() variable.value = 10 # This makes condition `unused' and it must become available to garbage collector # again. del variable self.collect_garbage() self.assertEqual(condition(), None) test.assert_results(False, True)
def test_garbage_collection_1 (self): test = NotifyTestObject () variable = Variable () condition = variable.is_true () condition.store (test.simple_handler) condition = weakref.ref (condition) # This must not collect the `is_true' condition, even though it is not directly # referenced at all. self.collect_garbage () self.assertNotEqual (condition (), None) self.collect_garbage () variable.value = 10 # This makes condition `unused' and it must become available to garbage collector # again. del variable self.collect_garbage () self.assertEqual (condition (), None) test.assert_results (False, True)
def test_predicate_2 (self): test = NotifyTestObject () variable = Variable (0) variable.predicate (lambda value: 0 <= value < 10).store (test.simple_handler) variable.value = 5 variable.value = 15 variable.value = -1 variable.value = 9 variable.value = 3 test.assert_results (True, False, True)
def test_derivation_4 (self): NumericVariable = Variable.derive_type ('NumericVariable', allowed_value_types = (int, float, complex)) self.assertRaises (TypeError, lambda: NumericVariable ()) variable = NumericVariable (0) variable.value = 15 self.assertEqual (variable.value, 15) variable.value = -2.5 self.assertEqual (variable.value, -2.5) variable.value = 1j self.assertEqual (variable.value, 1j) self.assertRaises (ValueError, lambda: variable.set ('string')) self.assertRaises (ValueError, lambda: variable.set ([]))
def test_transformation_2 (self): test = NotifyTestObject () variable = Variable (0) variable.transform (math.floor).store (test.simple_handler) variable.value = 5 variable.value = 5.6 variable.value = 15.7 variable.value = 16 variable.value = 16.5 variable.value = 16.2 test.assert_results (0, 5, 15, 16)
def test_is_true (self): variable = Variable (0) is_true = variable.is_true () self.assert_(not is_true) variable.value = 'string' self.assert_(is_true) variable.value = [] self.assert_(not is_true) variable.value = None self.assert_(not is_true) variable.value = 25 self.assert_(is_true)