def test_garbage_collection_binary (self): for _operator in (operator.__and__, operator.__or__, operator.__xor__): test = NotifyTestObject () condition1 = Condition (True) condition2 = Condition (False) binary_condition = _operator (condition1, condition2) binary_condition.store (test.simple_handler) binary_condition = weakref.ref (binary_condition) del condition1 self.collect_garbage () self.assertNotEqual (binary_condition (), None) condition2.state = True del condition2 self.collect_garbage () self.assertEqual (binary_condition (), None) expected_results = [] for state1, state2 in ((True, False), (True, True)): if not expected_results or expected_results[-1] != _operator (state1, state2): expected_results.append (_operator (state1, state2)) test.assert_results (*expected_results)
def test_garbage_collection_if_else (self): test = NotifyTestObject () condition1 = Condition (False) condition2 = Condition (False) condition3 = Condition (True) if_else_condition = condition1.if_else (condition2, condition3) if_else_condition.store (test.simple_handler) if_else_condition = weakref.ref (if_else_condition) del condition2 self.collect_garbage () self.assertNotEqual (if_else_condition (), None) condition3.state = False del condition1 self.collect_garbage () self.assertNotEqual (if_else_condition (), None) condition3.state = True del condition3 self.collect_garbage () self.assertEqual (if_else_condition (), None) test.assert_results (True, False, True)
def test_internals_1 (self): test = NotifyTestObject () condition = Condition (False) not_condition = ~condition self.assert_(not not_condition._has_signal ()) not_condition.changed.connect (test.simple_handler) self.assert_(not_condition._has_signal ()) def set_state_true (): condition.state = True condition.with_changes_frozen (set_state_true) condition.state = False not_condition.changed.disconnect (test.simple_handler) self.collect_garbage () self.assert_(not not_condition._has_signal ()) not_condition.changed.connect (test.simple_handler) self.assert_(not_condition._has_signal ()) condition.state = True test.assert_results (False, 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_handler_garbage_collection_3 (self): test = NotifyTestObject () signal = Signal (AbstractSignal.ANY_ACCEPTS) handler = HandlerGarbageCollectionTestCase.HandlerObject (test) def accepting_handler (*arguments): test.simple_handler_100 (*arguments) return arguments[0] signal.connect (accepting_handler) signal.connect (handler.simple_handler) self.assertEqual (len (signal._handlers), 2) signal.emit (1) del handler self.collect_garbage () self.assertEqual (len (signal._handlers), 2) signal.emit (2) # This time emission is stopped by accumulator, but still the gc-collected handler # must be removed. self.assertEqual (len (signal._handlers), 1) test.assert_results (101, 102)
def test_argument_passing (self): test = NotifyTestObject () signal = Signal () signal.connect (test.simple_handler) signal.emit (45, 'abc') test.assert_results ((45, 'abc'))
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_predicate_condition_2 (self): test = NotifyTestObject () predicate = PredicateCondition (bool, None) predicate.store (test.simple_handler) predicate.update (False) test.assert_results (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_predicate_condition_1(self): test = NotifyTestObject() predicate = PredicateCondition(bool, None) predicate.store(test.simple_handler) predicate.update(10) test.assert_results(False, True)
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_connect(self): test = NotifyTestObject() signal = Signal() signal.connect(test.simple_handler) signal.emit() self.assert_(signal.has_handlers()) self.assert_(signal) test.assert_results(())
def test_connect (self): test = NotifyTestObject () signal = Signal () signal.connect (test.simple_handler) signal.emit () self.assert_ (signal.has_handlers ()) self.assert_ (signal) test.assert_results (())
def test_predicate_condition_3 (self): test = NotifyTestObject () predicate = PredicateCondition (lambda x: x > 10, 0) predicate.store (test.simple_handler) predicate.update (10) predicate.update (20) predicate.update (-5) test.assert_results (False, True, False)
def test_predicate_condition_3(self): test = NotifyTestObject() predicate = PredicateCondition(lambda x: x > 10, 0) predicate.store(test.simple_handler) predicate.update(10) predicate.update(20) predicate.update(-5) test.assert_results(False, True, False)
def test_block (self): test = NotifyTestObject () signal = Signal () signal.connect (test.simple_handler) signal.emit (1) signal.block (test.simple_handler) signal.emit (2) test.assert_results (1)
def test_disconnect (self): test = NotifyTestObject () signal = Signal () signal.connect (test.simple_handler) signal.emit () signal.disconnect (test.simple_handler) signal.emit () test.assert_results (())
def test_disconnect(self): test = NotifyTestObject() signal = Signal() signal.connect(test.simple_handler) signal.emit() signal.disconnect(test.simple_handler) signal.emit() test.assert_results(())
def test_block(self): test = NotifyTestObject() signal = Signal() signal.connect(test.simple_handler) signal.emit(1) signal.block(test.simple_handler) signal.emit(2) test.assert_results(1)
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_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_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_mixed_argument_passing (self): test = NotifyTestObject () signal = Signal () signal.connect (test.simple_keywords_handler) signal.emit (ham = 'spam') signal.emit (42) signal.emit (1, 2, 3, foo = 'bar') test.assert_results ({ 'ham': 'spam' }, (42, { }), (1, 2, 3, { 'foo': 'bar' }))
def test_connect_with_arguments (self): test = NotifyTestObject () signal = Signal () signal.connect_safe (test.simple_handler, 'one argument') signal.connect_safe (test.simple_handler, 'first', 'second', 3) signal.emit () signal.emit ('a', 'b') test.assert_results ('one argument', ('first', 'second', 3), ('one argument', 'a', 'b'), ('first', 'second', 3, 'a', 'b'))
def test_connecting_1 (self): test = NotifyTestObject () signal = Signal () signal.emit (1) with signal.connecting (test.simple_handler): signal.emit (2) signal.emit (3) test.assert_results (2)
def test_emission_stop_1 (self): def stop_emission (): signal.stop_emission () test = NotifyTestObject () signal = Signal () signal.connect (stop_emission) signal.connect (test.simple_handler) signal.emit () test.assert_results ()
def test_connecting_1(self): test = NotifyTestObject() signal = Signal() signal.emit(1) with signal.connecting(test.simple_handler): signal.emit(2) signal.emit(3) test.assert_results(2)
def test_mixed_argument_passing(self): test = NotifyTestObject() signal = Signal() signal.connect(test.simple_keywords_handler) signal.emit(ham='spam') signal.emit(42) signal.emit(1, 2, 3, foo='bar') test.assert_results({'ham': 'spam'}, (42, {}), (1, 2, 3, { 'foo': 'bar' }))
def test_emission_stop_1(self): def stop_emission(): signal.stop_emission() test = NotifyTestObject() signal = Signal() signal.connect(stop_emission) signal.connect(test.simple_handler) signal.emit() test.assert_results()
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_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_connecting_safely_2 (self): test = NotifyTestObject () signal = Signal () signal.emit (1) with nested (ignoring_exceptions (), signal.connecting_safely (test.simple_handler)): signal.emit (2) raise Exception signal.emit (3) test.assert_results (2)
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_not (self): test = NotifyTestObject () condition = Condition (False) not_condition = ~condition not_condition.store (test.simple_handler) self.assertEqual (not_condition.state, True) condition.state = True self.assertEqual (not_condition.state, False) test.assert_results (True, False)
def test_if_else_2 (self): test = NotifyTestObject () condition1 = Condition (False) condition2 = Condition (False) condition3 = Condition (True) if_else_condition = condition1.if_else (condition2, condition3) if_else_condition.store (test.simple_handler) condition1.state = True test.assert_results (True, False)
def test_not(self): test = NotifyTestObject() condition = Condition(False) not_condition = ~condition not_condition.store(test.simple_handler) self.assertEqual(not_condition.state, True) condition.state = True self.assertEqual(not_condition.state, False) test.assert_results(True, False)
def test_if_else_2(self): test = NotifyTestObject() condition1 = Condition(False) condition2 = Condition(False) condition3 = Condition(True) if_else_condition = condition1.if_else(condition2, condition3) if_else_condition.store(test.simple_handler) condition1.state = True test.assert_results(True, False)
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_emission_level_2 (self): test = NotifyTestObject () signal = Signal () def reemit_if_shallow (): test.results.append (signal.emission_level) if signal.emission_level < 3: signal.emit () signal.connect (reemit_if_shallow) signal.emit () test.assert_results (1, 2, 3)
def test_emission_level_2(self): test = NotifyTestObject() signal = Signal() def reemit_if_shallow(): test.results.append(signal.emission_level) if signal.emission_level < 3: signal.emit() signal.connect(reemit_if_shallow) signal.emit() test.assert_results(1, 2, 3)
def test_connect_with_arguments(self): test = NotifyTestObject() signal = Signal() signal.connect_safe(test.simple_handler, 'one argument') signal.connect_safe(test.simple_handler, 'first', 'second', 3) signal.emit() signal.emit('a', 'b') test.assert_results('one argument', ('first', 'second', 3), ('one argument', 'a', 'b'), ('first', 'second', 3, 'a', 'b'))
def test_watcher_condition_1 (self): test = NotifyTestObject () watcher = WatcherCondition () watcher.store (test.simple_handler) condition = Condition (True) watcher.watch (condition) self.assert_(watcher.watched_condition is condition) condition.state = False test.assert_results (False, True, False)
def test_derivation_3(self): test = NotifyTestObject() DerivedCondition = \ AbstractStateTrackingCondition.derive_type ('DerivedCondition', setter = (lambda condition, state: test.simple_handler (state))) condition = DerivedCondition(False) condition.set(True) condition.state = False # There is no getter at all, so setter must be called during condition creation. test.assert_results(False, True, False)
def test_watcher_condition_1(self): test = NotifyTestObject() watcher = WatcherCondition() watcher.store(test.simple_handler) condition = Condition(True) watcher.watch(condition) self.assert_(watcher.watched_condition is condition) condition.state = False test.assert_results(False, True, False)
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_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_connecting_safely_2(self): test = NotifyTestObject() signal = Signal() signal.emit(1) with nested(ignoring_exceptions(), signal.connecting_safely(test.simple_handler)): signal.emit(2) raise Exception signal.emit(3) test.assert_results(2)
def test_derivation_3 (self): test = NotifyTestObject () DerivedCondition = \ AbstractStateTrackingCondition.derive_type ('DerivedCondition', setter = (lambda condition, state: test.simple_handler (state))) condition = DerivedCondition (False) condition.set (True) condition.state = False # There is no getter at all, so setter must be called during condition creation. test.assert_results (False, True, False)
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_derived_variable_changes_frozen_1(self): DerivedVariable = AbstractVariable.derive_type( 'DerivedVariable', getter=lambda variable: x) test = NotifyTestObject() x = 1 variable = DerivedVariable() variable.store(test.simple_handler) with variable.changes_frozen(): x = 2 # Though we never call _value_changed(), changes_frozen() promises to call it # itself in such cases. test.assert_results(1, 2)
def test_derivation_2(self): test = NotifyTestObject() DerivedCondition = \ AbstractStateTrackingCondition.derive_type ('DerivedCondition', getter = lambda condition: False, setter = (lambda condition, state: test.simple_handler (state))) condition = DerivedCondition() condition.set(True) condition.state = False # The default state is retrieved with the getter function, so the setter must not # be called during condition creation. test.assert_results(True, False)
def test_disconnect_all(self): test = NotifyTestObject() signal = Signal() signal.connect(test.simple_handler) signal.connect(test.simple_handler) signal.connect(test.simple_handler) signal.emit(1) signal.disconnect(test.simple_handler) signal.emit(2) signal.disconnect_all(test.simple_handler) signal.emit(3) test.assert_results(1, 1, 1, 2, 2)
def test_if_else_1(self): test = NotifyTestObject() condition1 = Condition(False) condition2 = Condition(False) condition3 = Condition(False) if_else_condition = condition1.if_else(condition2, condition3) if_else_condition.store(test.simple_handler) self.assertEqual(if_else_condition.state, False) condition1.state = False condition2.state = False condition3.state = True self.assertEqual(if_else_condition.state, True) condition1.state = False condition2.state = True condition3.state = False self.assertEqual(if_else_condition.state, False) condition1.state = False condition2.state = True condition3.state = True self.assertEqual(if_else_condition.state, True) condition1.state = True condition2.state = False condition3.state = False self.assertEqual(if_else_condition.state, False) condition1.state = True condition2.state = False condition3.state = True self.assertEqual(if_else_condition.state, False) condition1.state = True condition2.state = True condition3.state = False self.assertEqual(if_else_condition.state, True) condition1.state = True condition2.state = True condition3.state = True self.assertEqual(if_else_condition.state, True) test.assert_results(False, True, False, True, False, True)
def test_with_derived_variable_changes_frozen_1(self): DerivedVariable = AbstractVariable.derive_type( 'DerivedVariable', getter=lambda variable: values['x']) test = NotifyTestObject() values = {'x': 1} variable = DerivedVariable() variable.store(test.simple_handler) def do_changes(values): values['x'] = 2 variable.with_changes_frozen(do_changes, values) # Though we never call _value_changed(), with_changes_frozen() promises to call it # itself in such cases. test.assert_results(1, 2)
def test_referenced_signal(self): test = NotifyTestObject() condition = Condition(False) signal = (~condition).changed signal.connect(test.simple_handler) condition.state = True # This must not change anything. But current (at the time of test addition) # implementation destroys the `not' condition despite the reference to its signal. signal.disconnect(test.simple_handler) signal.connect(test.simple_handler) condition.state = False test.assert_results(False, True)
def test_disconnect_blocked_handler_1(self): test = NotifyTestObject() signal = Signal() signal.connect(test.simple_handler) signal.emit(1) signal.block(test.simple_handler) signal.emit(2) signal.disconnect(test.simple_handler) signal.emit(3) signal.connect(test.simple_handler) signal.emit(4) test.assert_results(1, 4)
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_emission_stop_2(self): def reemit_signal(number): signal.stop_emission() if number < 10: signal(number + 1) test = NotifyTestObject() signal = Signal() signal.connect(test.simple_handler) signal.connect(reemit_signal) # This must never be called since emission is stopped by the previous handler. signal.connect(test.simple_handler) signal.emit(0) test.assert_results(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)