[Zope-CVS] CVS: PythonNet/tests/python - test_event.py:1.2
Brian Lloyd
brian at zope.com
Thu Oct 2 23:06:22 EDT 2003
Update of /cvs-repository/PythonNet/tests/python
In directory cvs.zope.org:/tmp/cvs-serv5985/tests/python
Modified Files:
test_event.py
Log Message:
Checkin partial event fixes and refactoring
=== PythonNet/tests/python/test_event.py 1.1 => 1.2 ===
--- PythonNet/tests/python/test_event.py:1.1 Mon Feb 17 22:44:38 2003
+++ PythonNet/tests/python/test_event.py Thu Oct 2 23:06:20 2003
@@ -10,6 +10,7 @@
# FOR A PARTICULAR PURPOSE.
import sys, os, string, unittest, types
+from CLR.Python.Test import TestEventArgs
from CLR.Python.Test import EventTest
class EventTests(unittest.TestCase):
@@ -17,213 +18,256 @@
def testPublicInstanceEvent(self):
"""Test public instance events."""
- pass
+ object = EventTest()
+ handler = GenericHandler()
+ self.failUnless(handler.value == None)
-## def testPublicStaticField(self):
-## """Test public static fields."""
-## object = TestClass();
+ object.PublicEvent += handler.handler
-## self.failUnless(TestClass.PublicStaticField == 'public')
-## TestClass.PublicStaticField = 'changed'
-## self.failUnless(TestClass.PublicStaticField == 'changed')
+ object.PublicEvent(object, TestEventArgs(10))
+ self.failUnless(handler.value == 10)
-## self.failUnless(object.PublicStaticField == 'changed')
-## object.PublicStaticField = 'public'
-## self.failUnless(object.PublicStaticField == 'public')
-## def test():
-## del TestClass.PublicStaticField
+ def testPublicStaticEvent(self):
+ """Test public static events."""
+ handler = GenericHandler()
+ self.failUnless(handler.value == None)
-## self.failUnlessRaises(TypeError, test)
+ EventTest.PublicStaticEvent += handler.handler
-## def test():
-## del TestClass().PublicStaticField
+ EventTest.PublicStaticEvent(None, TestEventArgs(10))
+ self.failUnless(handler.value == 10)
-## self.failUnlessRaises(TypeError, test)
-## def testProtectedInstanceField(self):
-## """Test protected instance fields."""
-## object = TestClass();
+ def testProtectedInstanceEvent(self):
+ """Test protected instance events."""
+ object = EventTest()
-## self.failUnless(object.ProtectedField == 'protected')
-## object.ProtectedField = 'changed'
-## self.failUnless(object.ProtectedField == 'changed')
+ handler = GenericHandler()
+ self.failUnless(handler.value == None)
-## def test():
-## del TestClass().ProtectedField
+ object.ProtectedEvent += handler.handler
-## self.failUnlessRaises(TypeError, test)
+ object.ProtectedEvent(object, TestEventArgs(10))
+ self.failUnless(handler.value == 10)
-## def testProtectedStaticField(self):
-## """Test protected static fields."""
-## object = TestClass();
-## self.failUnless(TestClass.ProtectedStaticField == 'protected')
-## TestClass.ProtectedStaticField = 'changed'
-## self.failUnless(TestClass.ProtectedStaticField == 'changed')
+ def testProtectedStaticEvent(self):
+ """Test protected static events."""
+ object = EventTest
-## self.failUnless(object.ProtectedStaticField == 'changed')
-## object.ProtectedStaticField = 'protected'
-## self.failUnless(object.ProtectedStaticField == 'protected')
+ handler = GenericHandler()
+ self.failUnless(handler.value == None)
-## def test():
-## del TestClass.ProtectedStaticField
+ EventTest.ProtectedStaticEvent += handler.handler
-## self.failUnlessRaises(TypeError, test)
+ EventTest.ProtectedStaticEvent(None, TestEventArgs(10))
+ self.failUnless(handler.value == 10)
-## def test():
-## del TestClass().ProtectedStaticField
-## self.failUnlessRaises(TypeError, test)
+ def testInternalEvents(self):
+ """Test internal events."""
-## def testReadOnlyInstanceField(self):
-## """Test readonly instance fields."""
-## self.failUnless(TestClass().ReadOnlyField == 'readonly')
+ def test():
+ f = EventTest().InternalEvent
-## def test():
-## TestClass().ReadOnlyField = 'changed'
+ self.failUnlessRaises(AttributeError, test)
-## self.failUnlessRaises(TypeError, test)
+ def test():
+ f = EventTest().InternalStaticEvent
-## def test():
-## del TestClass().ReadOnlyField
+ self.failUnlessRaises(AttributeError, test)
-## self.failUnlessRaises(TypeError, test)
+ def test():
+ f = EventTest.InternalStaticEvent
-## def testReadOnlyStaticField(self):
-## """Test readonly static fields."""
-## object = TestClass();
+ self.failUnlessRaises(AttributeError, test)
-## self.failUnless(TestClass.ReadOnlyStaticField == 'readonly')
-## self.failUnless(object.ReadOnlyStaticField == 'readonly')
-## def test():
-## TestClass.ReadOnlyStaticField = 'changed'
+ def testPrivateEvents(self):
+ """Test private events."""
-## self.failUnlessRaises(TypeError, test)
+ def test():
+ f = EventTest().PrivateEvent
-## def test():
-## TestClass().ReadOnlyStaticField = 'changed'
+ self.failUnlessRaises(AttributeError, test)
-## self.failUnlessRaises(TypeError, test)
+ def test():
+ f = EventTest().PrivateStaticEvent
-## def test():
-## del TestClass.ReadOnlyStaticField
+ self.failUnlessRaises(AttributeError, test)
-## self.failUnlessRaises(TypeError, test)
+ def test():
+ f = EventTest.PrivateStaticEvent
-## def test():
-## del TestClass().ReadOnlyStaticField
+ self.failUnlessRaises(AttributeError, test)
-## self.failUnlessRaises(TypeError, test)
-## def testConstantField(self):
-## """Test const fields."""
-## object = TestClass();
+ def testMulticastEvent(self):
+ """Test multicast events."""
+ object = EventTest()
-## self.failUnless(TestClass.ConstField == 'const')
-## self.failUnless(object.ConstField == 'const')
+ handler1 = GenericHandler()
+ handler2 = GenericHandler()
+ handler3 = GenericHandler()
-## def test():
-## TestClass().ConstField = 'changed'
+ object.PublicEvent += handler1.handler
+ object.PublicEvent += handler2.handler
+ object.PublicEvent += handler3.handler
+
+ object.FireEvent(object, TestEventArgs(10))
+
+ self.failUnless(handler1.value == 10)
+ self.failUnless(handler2.value == 10)
+ self.failUnless(handler3.value == 10)
-## self.failUnlessRaises(TypeError, test)
+ object.FireEvent(object, TestEventArgs(20))
+
+ self.failUnless(handler1.value == 20)
+ self.failUnless(handler2.value == 20)
+ self.failUnless(handler3.value == 20)
-## def test():
-## TestClass.ConstField = 'changed'
-## self.failUnlessRaises(TypeError, test)
+ def testConditionalFireEvent(self):
+ """Test conditional firing of events."""
+ object = EventTest()
-## def test():
-## del TestClass().ConstField
+ if (object.PublicEvent != None):
+ empty = 0
+ else:
+ empty = 1
-## self.failUnlessRaises(TypeError, test)
+ self.failUnless(empty == 1)
-## def test():
-## del TestClass.ConstField
-## self.failUnlessRaises(TypeError, test)
+ def testIndirectFireEvent(self):
+ """Test indirect firing of events."""
+ object = EventTest()
-## def testInternalField(self):
-## """Test internal fields."""
+ handler = GenericHandler()
+ self.failUnless(handler.value == None)
-## def test():
-## f = TestClass().InternalField
+ object.PublicEvent += handler.handler
-## self.failUnlessRaises(AttributeError, test)
+ object.FireEvent(TestEventArgs(10))
+ self.failUnless(handler.value == 10)
-## def test():
-## f = TestClass().InternalStaticField
+ object = EventTest
-## self.failUnlessRaises(AttributeError, test)
+ handler = GenericHandler()
+ self.failUnless(handler.value == None)
-## def test():
-## f = TestClass.InternalStaticField
+ object.PublicStaticEvent += handler.handler
-## self.failUnlessRaises(AttributeError, test)
+ object.FireStatic(TestEventArgs(10))
+ self.failUnless(handler.value == 10)
-## def testPrivateField(self):
-## """Test private fields."""
-## def test():
-## f = TestClass().PrivateField
+ def testFireEmptyEvent(self):
+ """Test behavior of invoking an event with no subscribers."""
-## self.failUnlessRaises(AttributeError, test)
+ # 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.
-## def test():
-## f = TestClass().PrivateStaticField
+ # This check just ensures that the runtime is effectively turning
+ # an invokation of a null event into a no-op - if it is not handled
+ # correctly, the process should bail with a NullReferenceException.
-## self.failUnlessRaises(AttributeError, test)
+ object = EventTest()
+ object.PublicEvent(object, TestEventArgs(10))
-## def test():
-## f = TestClass.PrivateStaticField
-## self.failUnlessRaises(AttributeError, test)
+ def testIncorrectInvokation(self):
+ """Test incorrect invokation of events."""
+ object = EventTest()
-## def testFieldDescriptorGetSet(self):
-## """Test field descriptor get / set."""
+ handler = GenericHandler()
+ object.PublicEvent += handler.handler
-## # This test ensures that setting an attribute implemented with
-## # a descript actually goes through the descriptor (rather than
-## # silently replacing the descriptor in the instance or type dict.
+ object.PublicEvent(32)
-## object = TestClass()
+ def test():
+ object.PublicEvent()
-## self.failUnless(TestClass.PublicStaticField == 'public')
-## self.failUnless(object.PublicStaticField == 'public')
+ self.failUnlessRaises(ValueError, test)
-## descriptor = TestClass.__dict__['PublicStaticField']
-## self.failUnless(type(descriptor) != types.StringType)
+ def test():
+ EventTest().PublicEvent(32)
-## object.PublicStaticField = 'public'
-## descriptor = TestClass.__dict__['PublicStaticField']
-## self.failUnless(type(descriptor) != types.StringType)
+ self.failUnlessRaises(ValueError, test)
-## TestClass.PublicStaticField = 'public'
-## descriptor = TestClass.__dict__['PublicStaticField']
-## self.failUnless(type(descriptor) != types.StringType)
-## def testFieldDescriptorWrongType(self):
-## """Test setting a field using a value of the wrong type."""
-## def test():
-## TestClass().PublicField = 0
+ def testRemoveEventHandlers(self):
+ """Test removal of event handlers."""
+ object = EventTest()
-## self.failUnlessRaises(TypeError, test)
+ handler1 = GenericHandler()
+ handler2 = GenericHandler()
-## def testFieldDescriptorAbuse(self):
-## """Test field descriptor abuse."""
-## desc = TestClass.__dict__['PublicField']
+ object.PublicEvent += handler1.handler
+ object.PublicEvent += handler2.handler
+
+ object.FireEvent(object, TestEventArgs(10))
+
+ self.failUnless(handler1.value == 10)
+ self.failUnless(handler2.value == 10)
-## def test():
-## desc.__get__(0, 0)
+ object.PublicEvent -= handler2.handler
-## self.failUnlessRaises(TypeError, test)
+ object.PublicEvent(TestEventArgs(20))
+
+ self.failUnless(handler1.value == 20)
+ self.failUnless(handler2.value == 10)
-## def test():
-## desc.__set__(0, 0)
-## self.failUnlessRaises(TypeError, test)
+
+# test different kinds of callables
+
+ def testEventDescriptorAbuse(self):
+ """Test event descriptor abuse."""
+ desc = EventTest.__dict__['PublicEvent']
+
+ def test():
+ desc.__get__(0, 0)
+
+ self.failUnlessRaises(TypeError, test)
+
+ def test():
+ desc.__set__(0, 0)
+
+ self.failUnlessRaises(TypeError, test)
+
+ def test():
+ object = EventTest()
+ object.PublicEvent = 0
+
+ self.failUnlessRaises(TypeError, test)
+
+ def test():
+ EventTest.PublicStaticEvent = 0
+
+ self.failUnlessRaises(TypeError, test)
+
+
+ def testCrash(self):
+ """Test crash."""
+ desc = EventTest.__dict__['PublicEvent']
+ evil = desc.__get__(0, 0)
+ evil()
+
+
+
+class GenericHandler:
+ """A generic handler to test event callbacks."""
+ def __init__(self):
+ self.value = None
+
+ def handler(self, sender, args):
+ self.value = args.value
More information about the Zope-CVS
mailing list