PyXR

c:\python24\lib\site-packages\win32 \ com \ test \ testCollections.py



0001 # testCollections.py
0002 #
0003 # This code tests both the client and server side of collections
0004 # and enumerators.
0005 #
0006 # Also has the side effect of testing some of the PythonCOM error semantics.
0007 import sys
0008 import win32com.server.util
0009 import win32com.test.util
0010 import win32com.client
0011 import traceback
0012 import pythoncom
0013 import pywintypes
0014 import winerror
0015 L=pywintypes.Unicode
0016 
0017 import unittest
0018 
0019 error = "collection test error"
0020 
0021 def MakeEmptyEnum():
0022     # create the Python enumerator object as a real COM object
0023     o = win32com.server.util.wrap( win32com.server.util.Collection() )
0024     return win32com.client.Dispatch(o)
0025 
0026 def MakeTestEnum():
0027     # create a sub-collection, just to make sure it works :-)
0028     sub = win32com.server.util.wrap( win32com.server.util.Collection( ['Sub1', 2, 'Sub3']) )
0029     # create the Python enumerator object as a real COM object
0030     o = win32com.server.util.wrap( win32com.server.util.Collection( [1,'Two',3, sub]))
0031     return win32com.client.Dispatch(o)
0032 
0033 def TestEnumAgainst(o,check):
0034     for i in range(len(check)):
0035         if o(i) != check[i]:
0036             raise error, "Using default method gave the incorrect value - %s/%s" % (`o(i)`, `check[i]`)
0037 
0038     for i in range(len(check)):
0039         if o.Item(i) != check[i]:
0040             raise error, "Using Item method gave the incorrect value - %s/%s" % (`o(i)`, `check[i]`)
0041 
0042     # First try looping.
0043     cmp = []
0044     for s in o:
0045         cmp.append(s)
0046 
0047     if cmp[:len(check)] != check:
0048         raise error, "Result after looping isnt correct - %s/%s" % (`cmp[:len(check)]`, `check`)
0049 
0050     for i in range(len(check)):
0051         if o[i] != check[i]:
0052             raise error, "Using indexing gave the incorrect value"
0053 
0054 
0055 def TestEnum(quiet=None):
0056     if quiet is None:
0057         quiet = not "-v" in sys.argv
0058     if not quiet: print "Simple enum test"
0059     o = MakeTestEnum()
0060     check = [1,'Two',3]
0061     TestEnumAgainst(o, check)
0062 
0063     if not quiet: print "sub-collection test"
0064     sub = o[3]
0065     TestEnumAgainst(sub ,['Sub1', 2, 'Sub3'])
0066 
0067     # Remove the sublist for this test!
0068     o.Remove(o.Count()-1)
0069 
0070     if not quiet: print "Remove item test"
0071     del check[1]
0072     o.Remove(1)
0073     TestEnumAgainst(o, check)
0074 
0075     if not quiet: print "Add item test"
0076     o.Add('New Item')
0077     check.append('New Item')
0078     TestEnumAgainst(o, check)
0079 
0080     if not quiet: print "Insert item test"
0081     o.Insert(2, -1)
0082     check.insert(2, -1)
0083     TestEnumAgainst(o, check)
0084 
0085 ### This does not work!
0086 #       if not quiet: print "Indexed replace item test"
0087 #       o[2] = 'Replaced Item'
0088 #       check[2] = 'Replaced Item'
0089 #       TestEnumAgainst(o, check)
0090 
0091     try:
0092         o()
0093         raise error, "default method with no args worked when it shouldnt have!"
0094     except pythoncom.com_error, (hr, desc, exc, argErr):
0095         if hr != winerror.DISP_E_BADPARAMCOUNT:
0096             raise error, "Expected DISP_E_BADPARAMCOUNT - got %d (%s)" % (hr, desc)
0097 
0098     try:
0099         o.Insert("foo", 2)
0100         raise error, "Insert worked when it shouldnt have!"
0101     except pythoncom.com_error, (hr, desc, exc, argErr):
0102         if hr != winerror.DISP_E_TYPEMISMATCH:
0103             raise error, "Expected DISP_E_TYPEMISMATCH - got %d (%s)" % (hr, desc)
0104 
0105     # Remove the sublist for this test!
0106     try:
0107         o.Remove(o.Count())
0108         raise error, "Remove worked when it shouldnt have!"
0109     except pythoncom.com_error, (hr, desc, exc, argErr):
0110         if hr != winerror.DISP_E_BADINDEX:
0111             raise error, "Expected DISP_E_BADINDEX - got %d (%s)" % (hr, desc)
0112 
0113     # Test an empty collection
0114     if not quiet: print "Empty collection test"
0115     o = MakeEmptyEnum()
0116     for item in o:
0117         raise error, "Empty list performed an iteration"
0118 
0119     try:
0120         ob = o[1]
0121         raise error, "Empty list could be indexed"
0122     except IndexError:
0123         pass
0124 
0125     try:
0126         ob = o[0]
0127         raise error, "Empty list could be indexed"
0128     except IndexError:
0129         pass
0130 
0131     try:
0132         ob = o(0)
0133         raise error, "Empty list could be indexed"
0134     except pythoncom.com_error, (hr, fn, desc, arg):
0135         if hr != winerror.DISP_E_BADINDEX:
0136             raise error, "Expected DISP_E_BADINDEX - got %d (%s)" % (hr, desc)
0137 
0138 class TestCase(win32com.test.util.TestCase):
0139     def testEnum(self):
0140         TestEnum()
0141 
0142 if __name__=='__main__':
0143     unittest.main()
0144 

Generated by PyXR 0.9.4
SourceForge.net Logo