[Zope-CVS] CVS: PythonNet/tests/python - test_event.py:1.7
Brian Lloyd
cvs-admin at zope.org
Wed Nov 5 22:35:13 EST 2003
Update of /cvs-repository/PythonNet/tests/python
In directory cvs.zope.org:/tmp/cvs-serv19508/tests/python
Modified Files:
test_event.py
Log Message:
Fixed event support; added keyfile to buildout; fixed exception obsfucation
in property accessors.
=== PythonNet/tests/python/test_event.py 1.6 => 1.7 ===
--- PythonNet/tests/python/test_event.py:1.6 Mon Oct 27 21:07:03 2003
+++ PythonNet/tests/python/test_event.py Wed Nov 5 22:35:11 2003
@@ -26,7 +26,7 @@
object.PublicEvent += handler.handler
- object.PublicEvent(object, TestEventArgs(10))
+ object.OnPublicEvent(TestEventArgs(10))
self.failUnless(handler.value == 10)
object.PublicEvent -= handler.handler
@@ -39,7 +39,7 @@
EventTest.PublicStaticEvent += handler.handler
- EventTest.PublicStaticEvent(None, TestEventArgs(10))
+ EventTest.OnPublicStaticEvent(TestEventArgs(10))
self.failUnless(handler.value == 10)
@@ -52,7 +52,7 @@
object.ProtectedEvent += handler.handler
- object.ProtectedEvent(object, TestEventArgs(10))
+ object.OnProtectedEvent(TestEventArgs(10))
self.failUnless(handler.value == 10)
object.ProtectedEvent -= handler.handler
@@ -67,7 +67,7 @@
EventTest.ProtectedStaticEvent += handler.handler
- EventTest.ProtectedStaticEvent(None, TestEventArgs(10))
+ EventTest.OnProtectedStaticEvent(TestEventArgs(10))
self.failUnless(handler.value == 10)
EventTest.ProtectedStaticEvent -= handler.handler
@@ -123,13 +123,13 @@
object.PublicEvent += handler2.handler
object.PublicEvent += handler3.handler
- object.PublicEvent(object, TestEventArgs(10))
+ object.OnPublicEvent(TestEventArgs(10))
self.failUnless(handler1.value == 10)
self.failUnless(handler2.value == 10)
self.failUnless(handler3.value == 10)
- object.PublicEvent(object, TestEventArgs(20))
+ object.OnPublicEvent(TestEventArgs(20))
self.failUnless(handler1.value == 20)
self.failUnless(handler2.value == 20)
@@ -140,90 +140,6 @@
object.PublicEvent -= handler3.handler
- def testConditionalFireEvent(self):
- """Test conditional firing of events."""
- object = EventTest()
-
- self.failIf(object.PublicEvent)
-
- handler = GenericHandler()
-
- object.PublicEvent += handler.handler
-
- self.failUnless(object.PublicEvent)
-
- object.PublicEvent -= handler.handler
-
-
- def testIndirectFireEvent(self):
- """Test indirect firing of events."""
- object = EventTest()
-
- handler = GenericHandler()
- self.failUnless(handler.value == None)
-
- object.PublicEvent += handler.handler
-
- object.FireEvent(TestEventArgs(10))
- self.failUnless(handler.value == 10)
-
- object.PublicEvent -= handler.handler
-
- object = EventTest
-
- handler = GenericHandler()
- self.failUnless(handler.value == None)
-
- object.PublicStaticEvent += handler.handler
-
- object.FireStatic(TestEventArgs(10))
- self.failUnless(handler.value == 10)
-
- object.PublicStaticEvent -= handler.handler
-
-
- def testFireEmptyEvent(self):
- """Test behavior of invoking an event with no subscribers."""
-
- # Calling an uninitialized event from Python should be harmless
- # and essentially do nothing. This is slightly different than the
- # managed code idiom where you have to check if the event is null
- # before invoking it. From Python, an event never appears to be
- # null, because we create descriptors to represent declared events.
-
- # Event descriptors do support __nonzero__, which allows you to
- # check for an empty event from Python using 'if event:'.
-
- object = EventTest()
- object.PublicEvent(object, TestEventArgs(10))
-
- if object.PublicEvent:
- raise ValueError, 'Event descriptor should evaluate false'
-
- handler = GenericHandler()
- object.PublicEvent += handler.handler
-
- if not object.PublicEvent:
- raise ValueError, 'Event descriptor should evaluate true'
-
- object.PublicEvent -= handler.handler
-
-
- def testFireUnboundEvent(self):
- """Test calling an unbound event."""
- object = EventTest()
-
- handler = GenericHandler()
- self.failUnless(handler.value == None)
-
- object.PublicEvent += handler.handler
-
- EventTest.PublicEvent(object, object, TestEventArgs(10))
- self.failUnless(handler.value == 10)
-
- object.PublicEvent -= handler.handler
-
-
def testInstanceMethodHandler(self):
"""Test instance method handlers."""
object = EventTest()
@@ -232,13 +148,13 @@
object.PublicEvent += handler.handler
self.failUnless(handler.value == None)
- object.PublicEvent(object, TestEventArgs(10))
+ object.OnPublicEvent(TestEventArgs(10))
self.failUnless(handler.value == 10)
object.PublicEvent -= handler.handler
self.failUnless(handler.value == 10)
- object.PublicEvent(object, TestEventArgs(20))
+ object.OnPublicEvent(TestEventArgs(20))
self.failUnless(handler.value == 10)
@@ -250,13 +166,13 @@
object.PublicEvent += handler.handler
self.failUnless(handler.value == None)
- object.PublicEvent(object, TestEventArgs(10))
+ object.OnPublicEvent(TestEventArgs(10))
self.failUnless(handler.value == 10)
object.PublicEvent -= handler.handler
self.failUnless(handler.value == 10)
- object.PublicEvent(object, TestEventArgs(20))
+ object.OnPublicEvent(TestEventArgs(20))
self.failUnless(handler.value == 10)
@@ -268,13 +184,13 @@
object.PublicEvent += handler
self.failUnless(handler.value == None)
- object.PublicEvent(object, TestEventArgs(10))
+ object.OnPublicEvent(TestEventArgs(10))
self.failUnless(handler.value == 10)
object.PublicEvent -= handler
self.failUnless(handler.value == 10)
- object.PublicEvent(object, TestEventArgs(20))
+ object.OnPublicEvent(TestEventArgs(20))
self.failUnless(handler.value == 10)
@@ -286,13 +202,13 @@
object.PublicEvent += handler
self.failUnless(handler.value == None)
- object.PublicEvent(object, TestEventArgs(10))
+ object.OnPublicEvent(TestEventArgs(10))
self.failUnless(handler.value == 10)
object.PublicEvent -= handler
self.failUnless(handler.value == 10)
- object.PublicEvent(object, TestEventArgs(20))
+ object.OnPublicEvent(TestEventArgs(20))
self.failUnless(handler.value == 10)
@@ -305,13 +221,13 @@
object.PublicEvent += handler.handler
self.failUnless(handler.value == None)
- object.PublicEvent(object, TestEventArgs(10))
+ object.OnPublicEvent(TestEventArgs(10))
self.failUnless(handler.value == 10)
object.PublicEvent -= handler.handler
self.failUnless(handler.value == 10)
- object.PublicEvent(object, TestEventArgs(20))
+ object.OnPublicEvent(TestEventArgs(20))
self.failUnless(handler.value == 10)
@@ -324,13 +240,13 @@
object.PublicEvent += handler.handler
self.failUnless(handler.value == None)
- object.PublicEvent(object, TestEventArgs(10))
+ object.OnPublicEvent(TestEventArgs(10))
self.failUnless(handler.value == 10)
object.PublicEvent -= handler.handler
self.failUnless(handler.value == 10)
- object.PublicEvent(object, TestEventArgs(20))
+ object.OnPublicEvent(TestEventArgs(20))
self.failUnless(handler.value == 10)
@@ -341,13 +257,13 @@
object.PublicEvent += object.GenericHandler
self.failUnless(object.value == 0)
- object.PublicEvent(object, TestEventArgs(10))
+ object.OnPublicEvent(TestEventArgs(10))
self.failUnless(object.value == 10)
object.PublicEvent -= object.GenericHandler
self.failUnless(object.value == 10)
- object.PublicEvent(object, TestEventArgs(20))
+ object.OnPublicEvent(TestEventArgs(20))
self.failUnless(object.value == 10)
@@ -359,24 +275,22 @@
object.PublicEvent += object.StaticHandler
self.failUnless(EventTest.s_value == 0)
- object.PublicEvent(object, TestEventArgs(10))
+ object.OnPublicEvent(TestEventArgs(10))
self.failUnless(EventTest.s_value == 10)
object.PublicEvent -= object.StaticHandler
self.failUnless(EventTest.s_value == 10)
- object.PublicEvent(object, TestEventArgs(20))
+ object.OnPublicEvent(TestEventArgs(20))
self.failUnless(EventTest.s_value == 10)
def testUnboundMethodHandler(self):
"""Test failure mode for unbound method handlers."""
-
-
object = EventTest()
object.PublicEvent += GenericHandler.handler
try:
- object.PublicEvent(object, TestEventArgs(10))
+ object.OnPublicEvent(TestEventArgs(10))
except TypeError:
object.PublicEvent -= GenericHandler.handler
return
@@ -395,13 +309,13 @@
object.PublicEvent += handler
self.failUnless(dict['value'] == None)
- object.PublicEvent(object, TestEventArgs(10))
+ object.OnPublicEvent(TestEventArgs(10))
self.failUnless(dict['value'] == 10)
object.PublicEvent -= handler
self.failUnless(dict['value'] == 10)
- object.PublicEvent(object, TestEventArgs(20))
+ object.OnPublicEvent(TestEventArgs(20))
self.failUnless(dict['value'] == 10)
@@ -431,6 +345,169 @@
self.failUnlessRaises(TypeError, test)
+ 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.failUnless(handler.value == 20)
+
+ object.PublicEvent -= h1
+
+ object.OnPublicEvent(TestEventArgs(10))
+ self.failUnless(handler.value == 30)
+
+ object.PublicEvent -= h2
+
+ object.OnPublicEvent(TestEventArgs(10))
+ self.failUnless(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.failUnless(handler.value == 20)
+
+ object.PublicEvent -= h2
+
+ object.OnPublicEvent(TestEventArgs(10))
+ self.failUnless(handler.value == 30)
+
+ object.PublicEvent -= h1
+
+ object.OnPublicEvent(TestEventArgs(10))
+ self.failUnless(handler.value == 30)
+
+
+ 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)
+
+
+ def testRandomMultipleHandlers(self):
+ """Test random subscribe / unsubscribe of the same handlers."""
+ import whrandom
+ object = EventTest()
+ handler = MultipleHandler()
+ handler2 = MultipleHandler()
+
+ object.PublicEvent += handler2.handler
+ object.PublicEvent += handler2.handler
+
+ handlers = []
+ for i in range(30):
+ method = handler.handler
+ object.PublicEvent += method
+ handlers.append(method)
+
+ object.OnPublicEvent(TestEventArgs(10))
+ self.failUnless(handler.value == 300)
+ self.failUnless(handler2.value == 20)
+ handler.value = 0
+ handler2.value = 0
+
+ for i in range(30):
+ item = whrandom.choice(handlers)
+ handlers.remove(item)
+ object.PublicEvent -= item
+ handler.value = 0
+ object.OnPublicEvent(TestEventArgs(10))
+ self.failUnless(handler.value == (len(handlers) * 10))
+ self.failUnless(handler2.value == ((i + 1) * 20))
+
+ handler2.value = 0
+ object.OnPublicEvent(TestEventArgs(10))
+ self.failUnless(handler2.value == 20)
+
+ object.PublicEvent -= handler2.handler
+
+ handler2.value = 0
+ object.OnPublicEvent(TestEventArgs(10))
+ self.failUnless(handler2.value == 10)
+
+ object.PublicEvent -= handler2.handler
+
+ handler2.value = 0
+ object.OnPublicEvent(TestEventArgs(10))
+ self.failUnless(handler2.value == 0)
+
+
+ def testRemoveInternalCallHandler(self):
+ """Test remove on an event sink implemented w/internalcall."""
+ object = EventTest()
+
+ def h(sender, args):
+ pass
+
+ object.PublicEvent += h
+ object.PublicEvent -= h
+
+ from CLR.System.Windows.Forms import Form
+ f = Form()
+ f.Click += h
+ f.Click -= h
+ f.Dispose()
+
+
def testRemoveUnknownHandler(self):
"""Test removing an event handler that was never added."""
def test():
@@ -454,7 +531,7 @@
def test():
object.PublicEvent += handler.handler
- object.PublicEvent(object, TestEventArgs(10))
+ object.OnPublicEvent(TestEventArgs(10))
self.failUnlessRaises(TypeError, test)
@@ -469,7 +546,7 @@
def test():
object.PublicEvent += handler.handler
- object.PublicEvent(object, TestEventArgs(10))
+ object.OnPublicEvent(TestEventArgs(10))
self.failUnlessRaises(TypeError, test)
@@ -484,12 +561,12 @@
object.PublicEvent += handler.handler
def test():
- object.PublicEvent()
+ object.OnPublicEvent()
self.failUnlessRaises(TypeError, test)
def test():
- object.PublicEvent(32)
+ object.OnPublicEvent(32)
self.failUnlessRaises(TypeError, test)
@@ -505,13 +582,13 @@
object.add_PublicEvent(delegate)
self.failUnless(handler.value == None)
- object.PublicEvent(object, TestEventArgs(10))
+ object.OnPublicEvent(TestEventArgs(10))
self.failUnless(handler.value == 10)
object.remove_PublicEvent(delegate)
self.failUnless(handler.value == 10)
- object.PublicEvent(object, TestEventArgs(20))
+ object.OnPublicEvent(TestEventArgs(20))
self.failUnless(handler.value == 10)
@@ -624,6 +701,13 @@
handler = classmethod(handler)
+class MultipleHandler:
+ """A generic handler to test multiple callbacks."""
+ def __init__(self):
+ self.value = 0
+
+ def handler(self, sender, args):
+ self.value += args.value
More information about the Zope-CVS
mailing list