[Zope-CVS] CVS: PythonNet/tests/python - test_indexer.py:1.1 runtests.py:1.2 test_exceptions.py:1.2

Brian Lloyd brian@zope.com
Thu, 24 Jul 2003 19:55:17 -0400


Update of /cvs-repository/PythonNet/tests/python
In directory cvs.zope.org:/tmp/cvs-serv24649/tests/python

Modified Files:
	runtests.py test_exceptions.py 
Added Files:
	test_indexer.py 
Log Message:
commit refactorings

=== Added File PythonNet/tests/python/test_indexer.py ===
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
#
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.

import sys, os, string, unittest, types
import CLR.Python.Test as Test

class IndexerTests(unittest.TestCase):
    """Test support for indexer properties."""

    def testPublicIndexer(self):
        """Test public indexers."""
        object = Test.PublicIndexerTest()

        object[0] = "zero"
        self.failUnless(object[0] == "zero")
        
        object[1] = "one"
        self.failUnless(object[1] == "one")

        def test():
            value = object[10]

        self.failUnlessRaises(IndexError, test)


    def testProtectedIndexer(self):
        """Test protected indexers."""
        object = Test.ProtectedIndexerTest()

        object[0] = "zero"
        self.failUnless(object[0] == "zero")
        
        object[1] = "one"
        self.failUnless(object[1] == "one")

        def test():
            value = object[10]

        self.failUnlessRaises(IndexError, test)


    def testInternalIndexer(self):
        """Test internal indexers."""
        object = Test.InternalIndexerTest()

        # think about this!
        
        def test():
            object[0] = "zero"

        self.failUnlessRaises(TypeError, test)

        def test():
            Test.InternalIndexerTest.__getitem__(object, 0)

        self.failUnlessRaises(AttributeError, test)

        def test():
            object.__getitem__(0)

        self.failUnlessRaises(AttributeError, test)


    def testPrivateIndexer(self):
        """Test private indexers."""
        object = Test.PrivateIndexerTest()
        
        def test():
            object[0] = "zero"

        self.failUnlessRaises(TypeError, test)

        def test():
            Test.InternalIndexerTest.__getitem__(object, 0)

        self.failUnlessRaises(AttributeError, test)

        def test():
            object.__getitem__(0)

        self.failUnlessRaises(AttributeError, test)



    # add typed indexer tests here.


    def testIndexerWrongKeyType(self):
        """Test calling an indexer using a key of the wrong type."""
        def test():
            object = Test.PublicIndexerTest()
            object["wrong"] = "spam"

        self.failUnlessRaises(TypeError, test)


    def testIndexerWrongValueType(self):
        """Test calling an indexer using a value of the wrong type."""
        def test():
            object = Test.PublicIndexerTest()
            object[1] = 9993.9

        self.failUnlessRaises(TypeError, test)


    def testIndexerAbuse(self):
        """Test indexer abuse."""
        _class = Test.PublicIndexerTest
        object = Test.PublicIndexerTest()

        def test():
            del _class.__getitem__

        self.failUnlessRaises(TypeError, test)

        def test():
            del object.__getitem__

        self.failUnlessRaises(TypeError, test)



def test_suite():
    return unittest.makeSuite(IndexerTests)

def main():
    unittest.TextTestRunner().run(test_suite())

if __name__ == '__main__':
    testcase.setup()
    main()



=== PythonNet/tests/python/runtests.py 1.1 => 1.2 ===
--- PythonNet/tests/python/runtests.py:1.1	Mon Feb 17 22:44:38 2003
+++ PythonNet/tests/python/runtests.py	Thu Jul 24 19:55:11 2003
@@ -10,6 +10,7 @@
         'test_enum',
         'test_field',
         'test_property',
+        'test_indexer',
         'test_event',
         'test_method',
         'test_delegate',


=== PythonNet/tests/python/test_exceptions.py 1.1 => 1.2 ===
--- PythonNet/tests/python/test_exceptions.py:1.1	Mon Feb 17 22:44:38 2003
+++ PythonNet/tests/python/test_exceptions.py	Thu Jul 24 19:55:11 2003
@@ -10,14 +10,85 @@
 # FOR A PARTICULAR PURPOSE.
 
 import sys, os, string, unittest, types
-from CLR.Python.Test import ConversionTest
 from CLR import System
 
+# Note: all of these tests are known to fail because Python currently
+# doesn't allow new-style classes to be used as exceptions. I'm leaving
+# the tests in place in to document 'how it ought to work' in the hopes
+# that they'll all pass one day...
+
 class ExceptionTests(unittest.TestCase):
     """Test exception support."""
 
-    def testOverflowException(self):
-        """Test overflow exception propagation."""
+    def testRaiseClassException(self):
+        """Test class exception propagation."""
+        from CLR.System import NullReferenceException
+
+        def test():
+            raise NullReferenceException
+
+        self.failUnlessRaises(NullReferenceException, test)
+
+        try:
+            raise NullReferenceException
+        except:
+            type, value, tb = sys.exc_info()
+            self.failUnless(type is NullReferenceException)
+            self.failUnless(isinstance(value, NullReferenceException))
+
+    def testRaiseClassExceptionWithValue(self):
+        """Test class exception propagation with associated value."""
+        from CLR.System import NullReferenceException
+
+        def test():
+            raise NullReferenceException, 'Aiiieee!'
+
+        self.failUnlessRaises(NullReferenceException, test)
+
+        try:
+            raise NullReferenceException, 'Aiiieee!'
+        except:
+            type, value, tb = sys.exc_info()
+            self.failUnless(type is NullReferenceException)
+            self.failUnless(isinstance(value, NullReferenceException))
+            self.failUnless(value.Message == 'Aiiieee!')
+
+    def testRaiseInstanceException(self):
+        """Test instance exception propagation."""
+        from CLR.System import NullReferenceException
+
+        def test():
+            raise NullReferenceException()
+
+        self.failUnlessRaises(NullReferenceException, test)
+
+        try:
+            raise NullReferenceException()
+        except:
+            type, value, tb = sys.exc_info()
+            self.failUnless(type is NullReferenceException)
+            self.failUnless(isinstance(value, NullReferenceException))
+            self.failUnless(value.Message == '')
+
+    def testRaiseInstanceExceptionWithArgs(self):
+        """Test instance exception propagation with args."""
+        from CLR.System import NullReferenceException
+
+        def test():
+            raise NullReferenceException("Aiieeee!")
+
+        self.failUnlessRaises(NullReferenceException, test)
+
+        try:
+            raise NullReferenceException('Aiiieee!')
+        except:
+            type, value, tb = sys.exc_info()
+            self.failUnless(type is NullReferenceException)
+            self.failUnless(isinstance(value, NullReferenceException))
+            self.failUnless(value.Message == 'Aiiieee!')
+
+    def testManagedExceptionPropagation(self):
+        """Test propagation of exceptions raised in managed code."""
         from CLR.System import Decimal, OverflowException
 
         def test():