Beispiel #1
0
    def testHandlerCallbackFailure(self):
        """Test failure mode for inappropriate handlers."""
        class BadHandler:
            def handler(self, one):
                return 'too many'

        object = EventTest()
        handler = BadHandler()

        def test():
            object.PublicEvent += handler.handler
            object.OnPublicEvent(TestEventArgs(10))

        self.failUnlessRaises(TypeError, test)

        object.PublicEvent -= handler.handler

        class BadHandler:
            def handler(self, one, two, three, four, five):
                return 'not enough'

        object = EventTest()
        handler = BadHandler()

        def test():
            object.PublicEvent += handler.handler
            object.OnPublicEvent(TestEventArgs(10))

        self.failUnlessRaises(TypeError, test)

        object.PublicEvent -= handler.handler
def test_multicast_event():
    """Test multicast events."""
    ob = EventTest()

    handler1 = GenericHandler()
    handler2 = GenericHandler()
    handler3 = GenericHandler()

    ob.PublicEvent += handler1.handler
    ob.PublicEvent += handler2.handler
    ob.PublicEvent += handler3.handler

    ob.OnPublicEvent(EventArgsTest(10))

    assert handler1.value == 10
    assert handler2.value == 10
    assert handler3.value == 10

    ob.OnPublicEvent(EventArgsTest(20))

    assert handler1.value == 20
    assert handler2.value == 20
    assert handler3.value == 20

    ob.PublicEvent -= handler1.handler
    ob.PublicEvent -= handler2.handler
    ob.PublicEvent -= handler3.handler
def test_implicit_cls_event_registration():
    """Test implicit CLS event registration."""

    with pytest.raises(TypeError):
        ob = EventTest()
        handler = GenericHandler()
        ob.add_PublicEvent(handler.handler)
Beispiel #4
0
    def testMulticastEvent(self):
        """Test multicast events."""
        object = EventTest()

        handler1 = GenericHandler()
        handler2 = GenericHandler()
        handler3 = GenericHandler()

        object.PublicEvent += handler1.handler
        object.PublicEvent += handler2.handler
        object.PublicEvent += handler3.handler

        object.OnPublicEvent(TestEventArgs(10))

        self.failUnless(handler1.value == 10)
        self.failUnless(handler2.value == 10)
        self.failUnless(handler3.value == 10)

        object.OnPublicEvent(TestEventArgs(20))

        self.failUnless(handler1.value == 20)
        self.failUnless(handler2.value == 20)
        self.failUnless(handler3.value == 20)

        object.PublicEvent -= handler1.handler
        object.PublicEvent -= handler2.handler
        object.PublicEvent -= handler3.handler
Beispiel #5
0
    def test_implicit_cls_event_registration(self):
        """Test implicit CLS event registration."""

        with self.assertRaises(TypeError):
            ob = EventTest()
            handler = GenericHandler()
            ob.add_PublicEvent(handler.handler)
def test_unbound_method_handler():
    """Test failure mode for unbound method handlers."""
    ob = EventTest()
    ob.PublicEvent += GenericHandler.handler

    with pytest.raises(TypeError):
        ob.OnPublicEvent(EventArgsTest(10))

    ob.PublicEvent -= GenericHandler.handler
def test_internal_events():
    """Test internal events."""

    with pytest.raises(AttributeError):
        _ = EventTest().InternalEvent

    with pytest.raises(AttributeError):
        _ = EventTest().InternalStaticEvent

    with pytest.raises(AttributeError):
        _ = EventTest.InternalStaticEvent
Beispiel #8
0
    def testUnboundMethodHandler(self):
        """Test failure mode for unbound method handlers."""
        object = EventTest()
        object.PublicEvent += GenericHandler.handler
        try:
            object.OnPublicEvent(TestEventArgs(10))
        except TypeError:
            object.PublicEvent -= GenericHandler.handler
            return

        raise TypeError("should have raised a TypeError")
def test_private_events():
    """Test private events."""

    with pytest.raises(AttributeError):
        _ = EventTest().PrivateEvent

    with pytest.raises(AttributeError):
        _ = EventTest().PrivateStaticEvent

    with pytest.raises(AttributeError):
        _ = EventTest.PrivateStaticEvent
Beispiel #10
0
    def testProtectedInstanceEvent(self):
        """Test protected instance events."""
        object = EventTest()

        handler = GenericHandler()
        self.failUnless(handler.value == None)

        object.ProtectedEvent += handler.handler

        object.OnProtectedEvent(TestEventArgs(10))
        self.failUnless(handler.value == 10)

        object.ProtectedEvent -= handler.handler
def test_public_instance_event():
    """Test public instance events."""
    ob = EventTest()

    handler = GenericHandler()
    assert handler.value is None

    ob.PublicEvent += handler.handler

    ob.OnPublicEvent(EventArgsTest(10))
    assert handler.value == 10

    ob.PublicEvent -= handler.handler
def test_protected_instance_event():
    """Test protected instance events."""
    ob = EventTest()

    handler = GenericHandler()
    assert handler.value is None

    ob.ProtectedEvent += handler.handler

    ob.OnProtectedEvent(EventArgsTest(10))
    assert handler.value == 10

    ob.ProtectedEvent -= handler.handler
    def testPublicInstanceEvent(self):
        """Test public instance events."""
        object = EventTest()

        handler = GenericHandler()
        self.assertTrue(handler.value == None)

        object.PublicEvent += handler.handler

        object.OnPublicEvent(TestEventArgs(10))
        self.assertTrue(handler.value == 10)

        object.PublicEvent -= handler.handler
def test_incorrect_invokation():
    """Test incorrect invocation of events."""
    ob = EventTest()

    handler = GenericHandler()
    ob.PublicEvent += handler.handler

    with pytest.raises(TypeError):
        ob.OnPublicEvent()

    with pytest.raises(TypeError):
        ob.OnPublicEvent(32)

    ob.PublicEvent -= handler.handler
def test_managed_instance_method_handler():
    """Test managed instance method handlers."""
    ob = EventTest()

    ob.PublicEvent += ob.GenericHandler
    assert ob.value == 0

    ob.OnPublicEvent(EventArgsTest(10))
    assert ob.value == 10

    ob.PublicEvent -= ob.GenericHandler
    assert ob.value == 10

    ob.OnPublicEvent(EventArgsTest(20))
    assert ob.value == 10
Beispiel #16
0
    def testManagedInstanceMethodHandler(self):
        """Test managed instance method handlers."""
        object = EventTest()

        object.PublicEvent += object.GenericHandler
        self.failUnless(object.value == 0)

        object.OnPublicEvent(TestEventArgs(10))
        self.failUnless(object.value == 10)

        object.PublicEvent -= object.GenericHandler
        self.failUnless(object.value == 10)

        object.OnPublicEvent(TestEventArgs(20))
        self.failUnless(object.value == 10)
Beispiel #17
0
    def testInstanceMethodHandler(self):
        """Test instance method handlers."""
        object = EventTest()
        handler = GenericHandler()

        object.PublicEvent += handler.handler
        self.failUnless(handler.value == None)

        object.OnPublicEvent(TestEventArgs(10))
        self.failUnless(handler.value == 10)

        object.PublicEvent -= handler.handler
        self.failUnless(handler.value == 10)

        object.OnPublicEvent(TestEventArgs(20))
        self.failUnless(handler.value == 10)
def test_managed_static_method_handler():
    """Test managed static method handlers."""
    ob = EventTest()
    EventTest.s_value = 0

    ob.PublicEvent += ob.StaticHandler
    assert EventTest.s_value == 0

    ob.OnPublicEvent(EventArgsTest(10))
    assert EventTest.s_value == 10

    ob.PublicEvent -= ob.StaticHandler
    assert EventTest.s_value == 10

    ob.OnPublicEvent(EventArgsTest(20))
    assert EventTest.s_value == 10
def test_var_args_callable_handler():
    """Test varargs callable handlers."""
    ob = EventTest()
    handler = VarCallableHandler()

    ob.PublicEvent += handler
    assert handler.value is None

    ob.OnPublicEvent(EventArgsTest(10))
    assert handler.value == 10

    ob.PublicEvent -= handler
    assert handler.value == 10

    ob.OnPublicEvent(EventArgsTest(20))
    assert handler.value == 10
Beispiel #20
0
    def testManagedStaticMethodHandler(self):
        """Test managed static method handlers."""
        object = EventTest()
        EventTest.s_value = 0

        object.PublicEvent += object.StaticHandler
        self.failUnless(EventTest.s_value == 0)

        object.OnPublicEvent(TestEventArgs(10))
        self.failUnless(EventTest.s_value == 10)

        object.PublicEvent -= object.StaticHandler
        self.failUnless(EventTest.s_value == 10)

        object.OnPublicEvent(TestEventArgs(20))
        self.failUnless(EventTest.s_value == 10)
def test_var_args_instance_method_handler():
    """Test vararg instance method handlers."""
    ob = EventTest()
    handler = VariableArgsHandler()

    ob.PublicEvent += handler.handler
    assert handler.value is None

    ob.OnPublicEvent(EventArgsTest(10))
    assert handler.value == 10

    ob.PublicEvent -= handler.handler
    assert handler.value == 10

    ob.OnPublicEvent(EventArgsTest(20))
    assert handler.value == 10
def test_instance_method_handler():
    """Test instance method handlers."""
    ob = EventTest()
    handler = GenericHandler()

    ob.PublicEvent += handler.handler
    assert handler.value is None

    ob.OnPublicEvent(EventArgsTest(10))
    assert handler.value == 10

    ob.PublicEvent -= handler.handler
    assert handler.value == 10

    ob.OnPublicEvent(EventArgsTest(20))
    assert handler.value == 10
Beispiel #23
0
    def testVarArgsInstanceMethodHandler(self):
        """Test vararg instance method handlers."""
        object = EventTest()
        handler = VariableArgsHandler()

        object.PublicEvent += handler.handler
        self.failUnless(handler.value == None)

        object.OnPublicEvent(TestEventArgs(10))
        self.failUnless(handler.value == 10)

        object.PublicEvent -= handler.handler
        self.failUnless(handler.value == 10)

        object.OnPublicEvent(TestEventArgs(20))
        self.failUnless(handler.value == 10)
Beispiel #24
0
    def testCallableObjectHandler(self):
        """Test callable object handlers."""
        object = EventTest()
        handler = CallableHandler()

        object.PublicEvent += handler
        self.failUnless(handler.value == None)

        object.OnPublicEvent(TestEventArgs(10))
        self.failUnless(handler.value == 10)

        object.PublicEvent -= handler
        self.failUnless(handler.value == 10)

        object.OnPublicEvent(TestEventArgs(20))
        self.failUnless(handler.value == 10)
Beispiel #25
0
    def testVarArgsCallableHandler(self):
        """Test varargs callable handlers."""
        object = EventTest()
        handler = VarCallableHandler()

        object.PublicEvent += handler
        self.failUnless(handler.value == None)

        object.OnPublicEvent(TestEventArgs(10))
        self.failUnless(handler.value == 10)

        object.PublicEvent -= handler
        self.failUnless(handler.value == 10)

        object.OnPublicEvent(TestEventArgs(20))
        self.failUnless(handler.value == 10)
Beispiel #26
0
        def test():

            class spam:
                pass
            
            object = EventTest()
            object.PublicEvent += spam()
def test_callableob_handler():
    """Test callable ob handlers."""
    ob = EventTest()
    handler = CallableHandler()

    ob.PublicEvent += handler
    assert handler.value is None

    ob.OnPublicEvent(EventArgsTest(10))
    assert handler.value == 10

    ob.PublicEvent -= handler
    assert handler.value == 10

    ob.OnPublicEvent(EventArgsTest(20))
    assert handler.value == 10
def test_remove_multiple_static_handlers():
    """Test removing multiple instances of a static handler."""
    ob = EventTest()
    handler = MultipleHandler()

    h1 = handler.handler
    ob.PublicStaticEvent += h1

    h2 = handler.handler
    ob.PublicStaticEvent += h2

    ob.OnPublicStaticEvent(EventArgsTest(10))
    assert handler.value == 20

    ob.PublicStaticEvent -= h1

    ob.OnPublicStaticEvent(EventArgsTest(10))
    assert handler.value == 30

    ob.PublicStaticEvent -= h2

    ob.OnPublicStaticEvent(EventArgsTest(10))
    assert handler.value == 30

    # try again, removing in a different order.

    ob = EventTest()
    handler = MultipleHandler()

    h1 = handler.handler
    ob.PublicStaticEvent += h1

    h2 = handler.handler
    ob.PublicStaticEvent += h2

    ob.OnPublicStaticEvent(EventArgsTest(10))
    assert handler.value == 20

    ob.PublicStaticEvent -= h2

    ob.OnPublicStaticEvent(EventArgsTest(10))
    assert handler.value == 30

    ob.PublicStaticEvent -= h1

    ob.OnPublicStaticEvent(EventArgsTest(10))
    assert handler.value == 30
    def testRemoveMultipleHandlers(self):
        """Test removing multiple instances of the same handler."""
        object = EventTest()
        handler = MultipleHandler()

        h1 = handler.handler
        object.PublicEvent += h1

        h2 = handler.handler
        object.PublicEvent += h2

        object.OnPublicEvent(TestEventArgs(10))
        self.assertTrue(handler.value == 20)

        object.PublicEvent -= h1

        object.OnPublicEvent(TestEventArgs(10))
        self.assertTrue(handler.value == 30)

        object.PublicEvent -= h2

        object.OnPublicEvent(TestEventArgs(10))
        self.assertTrue(handler.value == 30)

        # try again, removing in a different order.

        object = EventTest()
        handler = MultipleHandler()

        h1 = handler.handler
        object.PublicEvent += h1

        h2 = handler.handler
        object.PublicEvent += h2

        object.OnPublicEvent(TestEventArgs(10))
        self.assertTrue(handler.value == 20)

        object.PublicEvent -= h2

        object.OnPublicEvent(TestEventArgs(10))
        self.assertTrue(handler.value == 30)

        object.PublicEvent -= h1

        object.OnPublicEvent(TestEventArgs(10))
        self.assertTrue(handler.value == 30)
Beispiel #30
0
    def testRemoveMultipleStaticHandlers(self):
        """Test removing multiple instances of a static handler."""
        object = EventTest()
        handler = MultipleHandler()

        h1 = handler.handler
        object.PublicStaticEvent += h1

        h2 = handler.handler
        object.PublicStaticEvent += h2

        object.OnPublicStaticEvent(TestEventArgs(10))
        self.failUnless(handler.value == 20)

        object.PublicStaticEvent -= h1

        object.OnPublicStaticEvent(TestEventArgs(10))
        self.failUnless(handler.value == 30)

        object.PublicStaticEvent -= h2

        object.OnPublicStaticEvent(TestEventArgs(10))
        self.failUnless(handler.value == 30)

        # try again, removing in a different order.

        object = EventTest()
        handler = MultipleHandler()

        h1 = handler.handler
        object.PublicStaticEvent += h1

        h2 = handler.handler
        object.PublicStaticEvent += h2

        object.OnPublicStaticEvent(TestEventArgs(10))
        self.failUnless(handler.value == 20)

        object.PublicStaticEvent -= h2

        object.OnPublicStaticEvent(TestEventArgs(10))
        self.failUnless(handler.value == 30)

        object.PublicStaticEvent -= h1

        object.OnPublicStaticEvent(TestEventArgs(10))
        self.failUnless(handler.value == 30)
Beispiel #31
0
    def testExplicitCLSEventRegistration(self):
        """Test explicit CLS event registration."""
        object = EventTest()
        handler = GenericHandler()

        delegate = TestEventHandler(handler.handler)
        object.add_PublicEvent(delegate)
        self.failUnless(handler.value == None)

        object.OnPublicEvent(TestEventArgs(10))        
        self.failUnless(handler.value == 10)

        object.remove_PublicEvent(delegate)
        self.failUnless(handler.value == 10)

        object.OnPublicEvent(TestEventArgs(20))        
        self.failUnless(handler.value == 10)
def test_remove_unknown_handler():
    """Test removing an event handler that was never added."""

    with pytest.raises(ValueError):
        ob = EventTest()
        handler = GenericHandler()

        ob.PublicEvent -= handler.handler
Beispiel #33
0
    def test_explicit_cls_event_registration(self):
        """Test explicit CLS event registration."""
        from Python.Test import EventHandlerTest

        ob = EventTest()
        handler = GenericHandler()

        delegate = EventHandlerTest(handler.handler)
        ob.add_PublicEvent(delegate)
        self.assertTrue(handler.value is None)

        ob.OnPublicEvent(EventArgsTest(10))
        self.assertTrue(handler.value == 10)

        ob.remove_PublicEvent(delegate)
        self.assertTrue(handler.value == 10)

        ob.OnPublicEvent(EventArgsTest(20))
        self.assertTrue(handler.value == 10)
Beispiel #34
0
    def test_event_descriptor_abuse(self):
        """Test event descriptor abuse."""

        with self.assertRaises(TypeError):
            del EventTest.PublicEvent

        with self.assertRaises(TypeError):
            del EventTest.__dict__['PublicEvent']

        desc = EventTest.__dict__['PublicEvent']

        with self.assertRaises(TypeError):
            desc.__get__(0, 0)

        with self.assertRaises(TypeError):
            desc.__set__(0, 0)

        with self.assertRaises(TypeError):
            ob = EventTest()
            ob.PublicEvent = 0

        with self.assertRaises(TypeError):
            EventTest.PublicStaticEvent = 0
Beispiel #35
0
 def test():
     object = EventTest()
     handler = GenericHandler()
     object.add_PublicEvent(handler.handler)
Beispiel #36
0
 def test():
     object = EventTest()
     object.PublicEvent = 0