Пример #1
0
    def testInstanceOverride(self):
        callback.Before(self.a.foo, self.before)
        callback.After(self.a.foo, self.after)

        self.a.foo(1)
        assert self.foo_called == (self.a, 1)
        assert self.after_called == (1,)
        assert self.before_called == (1,)
        assert self.after_count == 1
        assert self.before_count == 1

        self.b.foo(2)
        assert self.foo_called == (self.b, 2)
        assert self.after_called == (1,)
        assert self.before_called == (1,)
        assert self.after_count == 1
        assert self.before_count == 1

        assert callback.Remove(self.a.foo, self.before) == True

        self.a.foo(2)
        assert self.foo_called == (self.a, 2)
        assert self.after_called == (2,)
        assert self.before_called == (1,)
        assert self.after_count == 2
        assert self.before_count == 1

        callback.Before(self.a.foo, self.before)
        callback.Before(self.a.foo, self.before)  # Registering twice has no effect the 2nd time

        self.a.foo(5)
        assert self.before_called == (5,)
        assert self.before_count == 2
Пример #2
0
    def testSenderDies(self):

        class Sender(object):
            def foo(s, *args):  # @NoSelf
                s.args = args
            def __del__(dummy):  # @NoSelf
                self.sender_died = True

        self.sender_died = False
        s = Sender()
        w = weakref.ref(s)
        callback.Before(s.foo, self.before)
        s.foo(10)
        f = s.foo  # hold a strong reference to s
        assert self.before_count == 1
        assert self.before_called == (10,)

        assert not self.sender_died
        del s
        assert self.sender_died

        with pytest.raises(ReferenceError):
            f(10)  # must have already died: we don't have a strong reference

        assert w() is None
Пример #3
0
    def testClassOverride(self):
        callback.Before(self.C.foo, self.before)
        callback.After(self.C.foo, self.after)

        self.a.foo(1)
        assert self.foo_called == (self.a, 1)
        assert self.after_called == (self.a, 1)
        assert self.after_count == 1
        assert self.before_called == (self.a, 1)
        assert self.before_count == 1

        self.b.foo(2)
        assert self.foo_called == (self.b, 2)
        assert self.after_called == (self.b, 2)
        assert self.after_count == 2
        assert self.before_called == (self.b, 2)
        assert self.before_count == 2

        callback.Remove(self.C.foo, self.before)

        self.a.foo(3)
        assert self.foo_called == (self.a, 3)
        assert self.after_called == (self.a, 3)
        assert self.after_count == 3
        assert self.before_called == (self.b, 2)
        assert self.before_count == 2
Пример #4
0
    def testBoundMethodsRight(self):
        foo = self.a.foo
        foo = callback.Before(foo, self.before)
        foo = callback.After(foo, self.after)

        foo(10)
        assert self.before_count == 1
        assert self.after_count == 1
Пример #5
0
    def testBoundMethodsWrong(self):
        foo = self.a.foo
        callback.Before(foo, self.before)
        callback.After(foo, self.after)

        foo(10)
        assert 0 == self.before_count
        assert 0 == self.after_count
Пример #6
0
    def testSenderAsParameter(self):
        self.zulu_calls = []

        def zulu_one(*args):
            self.zulu_calls.append(args)

        def zulu_two(*args):
            self.zulu_calls.append(args)

        callback.Before(self.a.foo, zulu_one, sender_as_parameter=True)

        assert self.zulu_calls == []
        self.a.foo(0)
        assert self.zulu_calls == [(self.a, 0)]

        # The second method registered with the sender_as_parameter on did not receive it.
        callback.Before(self.a.foo, zulu_two, sender_as_parameter=True)

        self.zulu_calls = []
        self.a.foo(1)
        assert self.zulu_calls == [(self.a, 1), (self.a, 1)]
Пример #7
0
    def testAfterBeforeHandleError(self, monkeypatch):

        class C(object):
            def Method(self, x):
                return x * 2

        def AfterMethod(*args):
            self.before_called += 1
            raise RuntimeError

        def BeforeMethod(*args):
            self.after_called += 1
            raise RuntimeError

        self.before_called = 0
        self.after_called = 0

        c = C()
        callback.Before(c.Method, BeforeMethod)
        callback.After(c.Method, AfterMethod)

        # handled_errors = []
        # def HandleErrorOnCallback(func, *args, **kwargs):
        #    handled_errors.append(func)
#
        # monkeypatch.setattr(callback, 'HandleErrorOnCallback', HandleErrorOnCallback)

        handled_errors = []
        def HandleException(func, *args, **kwargs):
            handled_errors.append(func)
        monkeypatch.setattr(handle_exception, 'HandleException', HandleException)

        assert c.Method(10) == 20
        assert self.before_called == 1
        assert self.after_called == 1
        assert len(handled_errors) == 2

        assert c.Method(20) == 40
        assert self.before_called == 2
        assert self.after_called == 2
        assert len(handled_errors) == 4

        # Testing with a non-function.
        class Alpha:
            pass

        callback.After(c.Method, Alpha())
        assert c.Method(20) == 40
        assert len(handled_errors) == 7
Пример #8
0
    def test_sender_as_parameter_after_and_before(self):
        self.zulu_calls = []

        def zulu_one(*args):
            self.zulu_calls.append((1, args))

        def zulu_too(*args):
            self.zulu_calls.append((2, args))

        callback.Before(self.a.foo, zulu_one, sender_as_parameter=True)
        callback.After(self.a.foo, zulu_too)

        assert self.zulu_calls == []
        self.a.foo(0)
        assert self.zulu_calls == [(1, (self.a, 0)), (2, (0,))]
Пример #9
0
    def testReferenceDies(self):
        class Receiver(object):

            def before(dummy, *args):  # @NoSelf
                self.before_count += 1
                self.before_args = args

        rec = Receiver()
        self.before_count = 0
        self.before_args = None

        foo = self.a.foo
        foo = callback.Before(foo, rec.before)

        foo(10)
        assert self.before_args == (10,)
        assert self.before_count == 1

        del rec  # kill the receiver

        foo(20)
        assert self.before_args == (10,)
        assert self.before_count == 1