Exemplo n.º 1
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)
Exemplo n.º 2
0
    def test_custom_accumulator (self):

        class CustomAccumulator (AbstractSignal.AbstractAccumulator):

            def get_initial_value (self):
                return 10

            def accumulate_value (self, accumulated_value, value_to_add):
                return accumulated_value + value_to_add

            def should_continue (self, accumulated_value):
                return accumulated_value <= 50

            def post_process_value (self, accumulated_value):
                return -accumulated_value


        signal = Signal (CustomAccumulator ())
        self.assertEqual (signal.emit (), -10)

        signal.connect (lambda: 15)
        self.assertEqual (signal.emit (), -25)

        signal.connect (lambda: 20)
        self.assertEqual (signal.emit (), -45)

        signal.connect (lambda: 30)
        self.assertEqual (signal.emit (), -75)

        # This handler should never be invoked.
        signal.connect (lambda: 50)
        self.assertEqual (signal.emit (), -75)
Exemplo n.º 3
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)
Exemplo n.º 4
0
    def test_custom_accumulator(self):
        class CustomAccumulator(AbstractSignal.AbstractAccumulator):
            def get_initial_value(self):
                return 10

            def accumulate_value(self, accumulated_value, value_to_add):
                return accumulated_value + value_to_add

            def should_continue(self, accumulated_value):
                return accumulated_value <= 50

            def post_process_value(self, accumulated_value):
                return -accumulated_value

        signal = Signal(CustomAccumulator())
        self.assertEqual(signal.emit(), -10)

        signal.connect(lambda: 15)
        self.assertEqual(signal.emit(), -25)

        signal.connect(lambda: 20)
        self.assertEqual(signal.emit(), -45)

        signal.connect(lambda: 30)
        self.assertEqual(signal.emit(), -75)

        # This handler should never be invoked.
        signal.connect(lambda: 50)
        self.assertEqual(signal.emit(), -75)
Exemplo n.º 5
0
    def test_argument_passing (self):
        test   = NotifyTestObject ()
        signal = Signal ()

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

        test.assert_results ((45, 'abc'))
Exemplo n.º 6
0
    def test_argument_passing(self):
        test = NotifyTestObject()
        signal = Signal()

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

        test.assert_results((45, 'abc'))
Exemplo n.º 7
0
    def test_last_value_accumulator (self):
        signal = Signal (AbstractSignal.LAST_VALUE)
        self.assertEqual (signal.emit (), None)

        signal.connect (lambda: 15)
        self.assertEqual (signal.emit (), 15)

        signal.connect (lambda: 'abc')
        self.assertEqual (signal.emit (), 'abc')
Exemplo n.º 8
0
    def test_last_value_accumulator(self):
        signal = Signal(AbstractSignal.LAST_VALUE)
        self.assertEqual(signal.emit(), None)

        signal.connect(lambda: 15)
        self.assertEqual(signal.emit(), 15)

        signal.connect(lambda: 'abc')
        self.assertEqual(signal.emit(), 'abc')
Exemplo n.º 9
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 (())
Exemplo n.º 10
0
    def test_value_list_accumulator(self):
        signal = Signal(AbstractSignal.VALUE_LIST)
        self.assertEqual(signal.emit(), [])

        signal.connect(lambda: 50)
        self.assertEqual(signal.emit(), [50])

        signal.connect(lambda: None)
        signal.connect(lambda: ())
        self.assertEqual(signal.emit(), [50, None, ()])
Exemplo n.º 11
0
    def test_value_list_accumulator (self):
        signal = Signal (AbstractSignal.VALUE_LIST)
        self.assertEqual (signal.emit (), [])

        signal.connect (lambda: 50)
        self.assertEqual (signal.emit (), [50])

        signal.connect (lambda: None)
        signal.connect (lambda: ())
        self.assertEqual (signal.emit (), [50, None, ()])
Exemplo n.º 12
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(())
Exemplo n.º 13
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)
Exemplo n.º 14
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)
Exemplo n.º 15
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(())
Exemplo n.º 16
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 (())
Exemplo n.º 17
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 ()
Exemplo n.º 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()
Exemplo n.º 19
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'))
Exemplo n.º 20
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)
Exemplo n.º 21
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'))
Exemplo n.º 22
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)
Exemplo n.º 23
0
    def test_any_accepts_accumulator (self):
        signal = Signal (AbstractSignal.ANY_ACCEPTS)
        self.assertEqual (signal.emit (), False)

        signal.connect (lambda: False)
        self.assertEqual (signal.emit (), False)

        signal.connect (lambda: ())
        self.assertEqual (signal.emit (), ())

        signal.connect (lambda: 'I accept')
        self.assertEqual (signal.emit (), 'I accept')

        signal.connect (lambda: ())
        self.assertEqual (signal.emit (), 'I accept')
Exemplo n.º 24
0
    def test_any_accepts_accumulator(self):
        signal = Signal(AbstractSignal.ANY_ACCEPTS)
        self.assertEqual(signal.emit(), False)

        signal.connect(lambda: False)
        self.assertEqual(signal.emit(), False)

        signal.connect(lambda: ())
        self.assertEqual(signal.emit(), ())

        signal.connect(lambda: 'I accept')
        self.assertEqual(signal.emit(), 'I accept')

        signal.connect(lambda: ())
        self.assertEqual(signal.emit(), 'I accept')
Exemplo n.º 25
0
    def test_all_accept_accumulator(self):
        signal = Signal(AbstractSignal.ALL_ACCEPT)
        self.assertEqual(signal.emit(), True)

        signal.connect(lambda: True)
        self.assertEqual(signal.emit(), True)

        signal.connect(lambda: 'I accept')
        self.assertEqual(signal.emit(), 'I accept')

        signal.connect(lambda: [])
        self.assertEqual(signal.emit(), [])

        signal.connect(lambda: True)
        self.assertEqual(signal.emit(), [])
Exemplo n.º 26
0
    def test_all_accept_accumulator (self):
        signal = Signal (AbstractSignal.ALL_ACCEPT)
        self.assertEqual (signal.emit (), True)

        signal.connect (lambda: True)
        self.assertEqual (signal.emit (), True)

        signal.connect (lambda: 'I accept')
        self.assertEqual (signal.emit (), 'I accept')

        signal.connect (lambda: [])
        self.assertEqual (signal.emit (), [])

        signal.connect (lambda: True)
        self.assertEqual (signal.emit (), [])
Exemplo n.º 27
0
    def test_connecting_safely_1 (self):
        test   = NotifyTestObject ()
        signal = Signal ()

        signal.emit (1)

        with signal.connecting_safely (test.simple_handler):
            signal.emit (2)

            with signal.connecting_safely (test.simple_handler):
                signal.emit (3)

            signal.emit (4)

        signal.emit (5)

        test.assert_results (2, 3, 4)
Exemplo n.º 28
0
    def test_connecting_safely_1(self):
        test = NotifyTestObject()
        signal = Signal()

        signal.emit(1)

        with signal.connecting_safely(test.simple_handler):
            signal.emit(2)

            with signal.connecting_safely(test.simple_handler):
                signal.emit(3)

            signal.emit(4)

        signal.emit(5)

        test.assert_results(2, 3, 4)
Exemplo n.º 29
0
    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)
Exemplo n.º 30
0
    def test_emission_level_1(self):
        signal = Signal(Signal.VALUE_LIST)

        self.assertEqual(signal.emission_level, 0)

        signal.connect(lambda: signal.emission_level)

        self.assertEqual(signal.emit(), [1])

        signal = Signal(Signal.VALUE_LIST)

        def stop_emission_and_get_level():
            signal.stop_emission()
            return signal.emission_level

        signal.connect(stop_emission_and_get_level)

        self.assertEqual(signal.emit(), [1])
Exemplo n.º 31
0
    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)
Exemplo n.º 32
0
    def test_emission_level_1 (self):
        signal = Signal (Signal.VALUE_LIST)

        self.assertEqual (signal.emission_level, 0)

        signal.connect (lambda: signal.emission_level)

        self.assertEqual (signal.emit (), [1])

        signal = Signal (Signal.VALUE_LIST)

        def stop_emission_and_get_level ():
            signal.stop_emission ()
            return signal.emission_level

        signal.connect (stop_emission_and_get_level)

        self.assertEqual (signal.emit (), [1])
Exemplo n.º 33
0
    def test_emission_stop_3 (self):
        def stop_emission ():
            was_stopped = signal.emission_stopped
            signal.stop_emission ()
            return was_stopped, signal.emission_stopped

        signal = Signal (Signal.VALUE_LIST)
        signal.connect (stop_emission)

        self.assertEqual (signal.emit (), [(False, True)])
Exemplo n.º 34
0
    def test_emission_stop_3(self):
        def stop_emission():
            was_stopped = signal.emission_stopped
            signal.stop_emission()
            return was_stopped, signal.emission_stopped

        signal = Signal(Signal.VALUE_LIST)
        signal.connect(stop_emission)

        self.assertEqual(signal.emit(), [(False, True)])
Exemplo n.º 35
0
    def test_handler_garbage_collection_1 (self):
        test   = NotifyTestObject ()
        signal = Signal ()

        handler = HandlerGarbageCollectionTestCase.HandlerObject (test)
        signal.connect (handler.simple_handler)

        self.assert_(signal._handlers is not None)

        signal.emit (1)

        del handler
        self.collect_garbage ()

        self.assert_(signal._handlers is not None)

        signal.emit (2)

        self.assert_(signal._handlers is None)
        test.assert_results (1)
Exemplo n.º 36
0
    def test_handler_garbage_collection_1(self):
        test = NotifyTestObject()
        signal = Signal()

        handler = HandlerGarbageCollectionTestCase.HandlerObject(test)
        signal.connect(handler.simple_handler)

        self.assert_(signal._handlers is not None)

        signal.emit(1)

        del handler
        self.collect_garbage()

        self.assert_(signal._handlers is not None)

        signal.emit(2)

        self.assert_(signal._handlers is None)
        test.assert_results(1)
Exemplo n.º 37
0
    def test_disconnect_blocked_handler_3 (self):
        test   = NotifyTestObject ()
        signal = Signal ()

        signal.connect (test.simple_handler)
        signal.connect (test.simple_handler)
        signal.emit (1)

        signal.block (test.simple_handler)
        signal.emit (2)

        signal.disconnect_all (test.simple_handler)
        signal.emit (3)

        signal.connect (test.simple_handler)
        signal.emit (4)

        signal.unblock (test.simple_handler)
        signal.emit (5)

        test.assert_results (1, 1, 4, 5)
Exemplo n.º 38
0
    def test_disconnect_blocked_handler_3(self):
        test = NotifyTestObject()
        signal = Signal()

        signal.connect(test.simple_handler)
        signal.connect(test.simple_handler)
        signal.emit(1)

        signal.block(test.simple_handler)
        signal.emit(2)

        signal.disconnect_all(test.simple_handler)
        signal.emit(3)

        signal.connect(test.simple_handler)
        signal.emit(4)

        signal.unblock(test.simple_handler)
        signal.emit(5)

        test.assert_results(1, 1, 4, 5)
Exemplo n.º 39
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
            }))
Exemplo n.º 40
0
    def test_handler_garbage_collection_2 (self):
        test   = NotifyTestObject ()
        signal = Signal ()

        handler = HandlerGarbageCollectionTestCase.HandlerObject (test)

        signal.connect (lambda *ignored: signal.stop_emission ())
        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)

        # Even though emission is stopped by the first handler, signal must still notice
        # that it should remove the second one.
        self.assertEqual (len (signal._handlers), 1)
        test.assert_results ()
Exemplo n.º 41
0
    def test_handler_garbage_collection_2(self):
        test = NotifyTestObject()
        signal = Signal()

        handler = HandlerGarbageCollectionTestCase.HandlerObject(test)

        signal.connect(lambda *ignored: signal.stop_emission())
        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)

        # Even though emission is stopped by the first handler, signal must still notice
        # that it should remove the second one.
        self.assertEqual(len(signal._handlers), 1)
        test.assert_results()
Exemplo n.º 42
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' }))
Exemplo n.º 43
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'
        }))
Exemplo n.º 44
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)
Exemplo n.º 45
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)
Exemplo n.º 46
0
    def test_blocking_1(self):
        test = NotifyTestObject()
        signal = Signal()

        signal.connect(test.simple_handler)

        signal.emit(1)

        with signal.blocking(test.simple_handler):
            signal.emit(2)

        signal.emit(3)

        test.assert_results(1, 3)
Exemplo n.º 47
0
    def test_blocking_1 (self):
        test   = NotifyTestObject ()
        signal = Signal ()

        signal.connect (test.simple_handler)

        signal.emit (1)

        with signal.blocking (test.simple_handler):
            signal.emit (2)

        signal.emit (3)

        test.assert_results (1, 3)
Exemplo n.º 48
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 }))
Exemplo n.º 49
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)
Exemplo n.º 50
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)
Exemplo n.º 51
0
    def test_connect_disconnect(self):
        test = NotifyTestObject()
        signal = Signal()

        signal.connect(test.simple_handler)
        signal.connect(test.simple_handler_100)

        signal.emit(1)

        # This must be a no-op.
        signal.connect(test.simple_handler)
        signal.disconnect(test.simple_handler)

        signal.emit(2)

        # This must be a no-op.
        signal.connect(test.simple_handler_100)
        signal.disconnect(test.simple_handler_100)

        signal.emit(3)

        test.assert_results(1, 101, 2, 102, 3, 103)
Exemplo n.º 52
0
    def test_connect_disconnect (self):
        test   = NotifyTestObject ()
        signal = Signal ()

        signal.connect (test.simple_handler)
        signal.connect (test.simple_handler_100)

        signal.emit (1)

        # This must be a no-op.
        signal.connect    (test.simple_handler)
        signal.disconnect (test.simple_handler)

        signal.emit (2)

        # This must be a no-op.
        signal.connect    (test.simple_handler_100)
        signal.disconnect (test.simple_handler_100)

        signal.emit (3)

        test.assert_results (1, 101, 2, 102, 3, 103)