home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_win32com_test_testCollections.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  3.9 KB  |  145 lines

  1. # testCollections.py
  2. #
  3. # This code tests both the client and server side of collections
  4. # and enumerators.
  5. #
  6. # Also has the side effect of testing some of the PythonCOM error semantics.
  7.  
  8. import win32com.server.util
  9. import win32com.client
  10. import traceback
  11. import pythoncom
  12. import pywintypes
  13. import winerror
  14. L=pywintypes.Unicode
  15.  
  16.  
  17. error = "collection test error"
  18.  
  19. def MakeEmptyEnum():
  20.     # create the Python enumerator object as a real COM object
  21.     o = win32com.server.util.wrap( win32com.server.util.Collection() )
  22.     return win32com.client.Dispatch(o)
  23.  
  24. def MakeTestEnum():
  25.     # create a sub-collection, just to make sure it works :-)
  26.     sub = win32com.server.util.wrap( win32com.server.util.Collection( ['Sub1', 2, 'Sub3']) )
  27.     # create the Python enumerator object as a real COM object
  28.     o = win32com.server.util.wrap( win32com.server.util.Collection( [1,'Two',3, sub]))
  29.     return win32com.client.Dispatch(o)
  30.  
  31. def TestEnumAgainst(o,check):
  32.     for i in range(len(check)):
  33.         if o(i) != check[i]:
  34.             raise error, "Using default method gave the incorrect value - %s/%s" % (`o(i)`, `check[i]`)
  35.  
  36.     for i in range(len(check)):
  37.         if o.Item(i) != check[i]:
  38.             raise error, "Using Item method gave the incorrect value - %s/%s" % (`o(i)`, `check[i]`)
  39.         
  40.     # First try looping.
  41.     cmp = []
  42.     for s in o:
  43.         cmp.append(s)
  44.     
  45.     if cmp[:len(check)] != check:
  46.         raise error, "Result after looping isnt correct - %s/%s" % (`cmp[:len(check)]`, `check`)
  47.         
  48.     for i in range(len(check)):
  49.         if o[i] != check[i]:
  50.             raise error, "Using indexing gave the incorrect value"
  51.  
  52.  
  53. def TestEnum(quiet=0):
  54.     if not quiet: print "Simple enum test"
  55.     o = MakeTestEnum()
  56.     check = [1,'Two',3]
  57.     TestEnumAgainst(o, check)
  58.  
  59.     if not quiet: print "sub-collection test"
  60.     sub = o[3]
  61.     TestEnumAgainst(sub ,['Sub1', 2, 'Sub3'])
  62.     
  63.     # Remove the sublist for this test!
  64.     o.Remove(o.Count()-1)
  65.     
  66.     if not quiet: print "Remove item test"
  67.     del check[1]
  68.     o.Remove(1)
  69.     TestEnumAgainst(o, check)
  70.     
  71.     if not quiet: print "Add item test"
  72.     o.Add('New Item')
  73.     check.append('New Item')
  74.     TestEnumAgainst(o, check)
  75.     
  76.     if not quiet: print "Insert item test"
  77.     o.Insert(2, -1)
  78.     check.insert(2, -1)
  79.     TestEnumAgainst(o, check)
  80.  
  81. ### This does not work!
  82. #    if not quiet: print "Indexed replace item test"
  83. #    o[2] = 'Replaced Item'
  84. #    check[2] = 'Replaced Item'
  85. #    TestEnumAgainst(o, check)
  86.  
  87.     try:
  88.         o()
  89.         raise error, "default method with no args worked when it shouldnt have!"
  90.     except pythoncom.com_error, (hr, desc, exc, argErr):
  91.         if hr != winerror.DISP_E_BADPARAMCOUNT:
  92.             raise error, "Expected DISP_E_BADPARAMCOUNT - got %d (%s)" % (hr, desc)
  93.  
  94.     try:
  95.         o.Insert("foo", 2)
  96.         raise error, "Insert worked when it shouldnt have!"
  97.     except pythoncom.com_error, (hr, desc, exc, argErr):
  98.         if hr != winerror.DISP_E_TYPEMISMATCH:
  99.             raise error, "Expected DISP_E_TYPEMISMATCH - got %d (%s)" % (hr, desc)
  100.  
  101.     # Remove the sublist for this test!
  102.     try:
  103.         o.Remove(o.Count())
  104.         raise error, "Remove worked when it shouldnt have!"
  105.     except pythoncom.com_error, (hr, desc, exc, argErr):
  106.         if hr != winerror.DISP_E_BADINDEX:
  107.             raise error, "Expected DISP_E_BADINDEX - got %d (%s)" % (hr, desc)
  108.  
  109.     # Test an empty collection
  110.     print "Empty collection test"
  111.     o = MakeEmptyEnum()
  112.     for item in o:
  113.         raise error, "Empty list performed an iteration"
  114.  
  115.     try:
  116.         ob = o[1]
  117.         raise error, "Empty list could be indexed"
  118.     except IndexError:
  119.         pass
  120.  
  121.     try:
  122.         ob = o[0]
  123.         raise error, "Empty list could be indexed"
  124.     except IndexError:
  125.         pass
  126.  
  127.     try:
  128.         ob = o(0)
  129.         raise error, "Empty list could be indexed"
  130.     except pythoncom.com_error, (hr, fn, desc, arg):
  131.         if hr != winerror.DISP_E_BADINDEX:
  132.             raise error, "Expected DISP_E_BADINDEX - got %d (%s)" % (hr, desc)
  133.  
  134.  
  135.  
  136. def doit():
  137.     try:
  138.         TestEnum()
  139.     except:
  140.         traceback.print_exc()
  141.         
  142. if __name__=='__main__':
  143.     doit()
  144.     print "Worked OK with %d/%d" % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())
  145.