Esempio n. 1
0
    def test_notify(self):
        """
        Check notify.
        """
        observer = ObserverMixin()
        self.signals_called = []

        def one_callback(signal):
            self.signals_called.append({
                'source': u'one_callback',
                'signal': signal,
                })

        def another_callback(signal):
            self.signals_called.append({
                'source': u'another_callback',
                'signal': signal,
                })

        signal = Signal(source=self)
        observer.subscribe('signal-name', one_callback)
        observer.notify('signal-name', signal)
        self.assertEqual(1, len(self.signals_called))
        self.assertEqual(u'one_callback', self.signals_called[0]['source'])
        self.assertEqual(signal, self.signals_called[0]['signal'])

        self.signals_called = []
        observer.subscribe('signal-name', another_callback)
        observer.notify('signal-name', signal)
        self.assertEqual(2, len(self.signals_called))
        self.assertEqual(u'one_callback', self.signals_called[0]['source'])
        self.assertEqual(signal, self.signals_called[0]['signal'])
        self.assertEqual(
            u'another_callback', self.signals_called[1]['source'])
        self.assertEqual(signal, self.signals_called[1]['signal'])
Esempio n. 2
0
    def test_unsubscribe_all(self):
        """
        Check unsubscribe.
        """
        observer = ObserverMixin()

        def one_callback(signal):
            pass

        def another_callback(signal):
            pass

        observer.subscribe('signal1', one_callback)
        observer.subscribe('signal2', one_callback)
        self.assertEqual(2, len(observer._subscribers))
        observer.unsubscribe()
        self.assertEqual(0, len(observer._subscribers))
Esempio n. 3
0
    def test_subscribe(self):
        """
        Check subscribe.
        """
        observer = ObserverMixin()

        def one_callback(signal):
            pass

        def another_callback(signal):
            pass

        observer.subscribe('signal', one_callback)
        self.assertEqual(1, len(observer._subscribers))
        self.assertEqual(1, len(observer._subscribers['signal']))
        self.assertTrue(one_callback in observer._subscribers['signal'])

        # Adding the same callback more than once will only add it once.
        observer.subscribe('signal', another_callback)
        observer.subscribe('signal', another_callback)
        self.assertEqual(1, len(observer._subscribers))
        self.assertEqual(2, len(observer._subscribers['signal']))
        self.assertTrue(another_callback in observer._subscribers['signal'])

        observer.subscribe('another_signal', another_callback)
        self.assertEqual(2, len(observer._subscribers))
        self.assertEqual(1, len(observer._subscribers['another_signal']))
        self.assertTrue(
            another_callback in observer._subscribers['another_signal'])
Esempio n. 4
0
    def test_unsubscribe_all_signal(self):
        """
        Check unsubscribe for a specific signal.
        """
        observer = ObserverMixin()

        def one_callback(signal):
            pass

        def another_callback(signal):
            pass

        observer.subscribe('signal1', one_callback)
        observer.subscribe('signal1', another_callback)
        observer.subscribe('signal2', one_callback)
        self.assertEqual(2, len(observer._subscribers))
        observer.unsubscribe(name='signal1')
        self.assertEqual(2, len(observer._subscribers))
        self.assertEqual(0, len(observer._subscribers['signal1']))
        self.assertEqual(1, len(observer._subscribers['signal2']))
Esempio n. 5
0
    def test_unsubscribe_callback(self):
        """
        Check unsubscribe for a specific callback.
        """
        observer = ObserverMixin()

        def one_callback(signal):
            pass

        def another_callback(signal):
            pass

        observer.subscribe('signal1', one_callback)
        observer.subscribe('signal1', another_callback)
        observer.subscribe('signal2', one_callback)
        self.assertEqual(2, len(observer._subscribers))
        observer.unsubscribe(name='signal1', callback=another_callback)
        self.assertEqual(2, len(observer._subscribers))
        self.assertEqual(1, len(observer._subscribers['signal1']))
        self.assertTrue(one_callback in observer._subscribers['signal1'])
        self.assertFalse(another_callback in observer._subscribers['signal1'])
        self.assertEqual(1, len(observer._subscribers['signal2']))