Exemple #1
0
    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)
Exemple #2
0
    def test_connect_with_keywords(self):
        test = NotifyTestObject()
        signal = Signal()

        signal.connect_safe(test.simple_keywords_handler, a=1)
        signal.connect_safe(test.simple_keywords_handler, a=2, b=3)

        signal.emit()
        signal.emit(b=42)
        signal.emit('ham')

        test.assert_results(
            {'a': 1},
            {
                'a': 2,
                'b': 3
            },
            # Note that emission keyword arguments must override
            # connection-time keyword arguments.
            {
                'a': 1,
                'b': 42
            },
            {
                'a': 2,
                'b': 42
            },
            ('ham', {
                'a': 1
            }),
            ('ham', {
                'a': 2,
                'b': 3
            }))
Exemple #3
0
    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)
Exemple #4
0
    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)
Exemple #5
0
    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)
Exemple #6
0
    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)
Exemple #7
0
    def test_argument_passing(self):
        test = NotifyTestObject()
        signal = Signal()

        signal.connect(test.simple_handler)
        signal.emit(45, 'abc')

        test.assert_results((45, 'abc'))
Exemple #8
0
    def test_predicate_condition_2(self):
        test = NotifyTestObject()

        predicate = PredicateCondition(bool, None)
        predicate.store(test.simple_handler)

        predicate.update(False)

        test.assert_results(False)
Exemple #9
0
    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()
Exemple #10
0
    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)
Exemple #11
0
    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(())
Exemple #12
0
    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)
Exemple #13
0
    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)
Exemple #14
0
    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(())
Exemple #15
0
    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'
        }))
Exemple #16
0
    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()
Exemple #17
0
    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)
Exemple #18
0
    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()
Exemple #19
0
    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)
Exemple #20
0
    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)
Exemple #21
0
    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)
Exemple #22
0
    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)
Exemple #23
0
    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)
Exemple #24
0
    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'))
Exemple #25
0
    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)
Exemple #26
0
    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)
Exemple #27
0
    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)
Exemple #28
0
    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)
Exemple #29
0
    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)
Exemple #30
0
    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)