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_testDictionary.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  1.9 KB  |  75 lines

  1. # testDictionary.py
  2. #
  3.  
  4. import win32com.server.util
  5. import win32com.client
  6. import traceback
  7. import pythoncom
  8. import pywintypes
  9. import winerror
  10. L=pywintypes.Unicode
  11.  
  12.  
  13. error = "dictionary test error"
  14.  
  15. def MakeTestDictionary():
  16.     return win32com.client.Dispatch("Python.Dictionary")
  17.  
  18. def TestDictAgainst(dict,check):
  19.     for key, value in check.items():
  20.         if dict(key) != value:
  21.             raise error, "Indexing for '%s' gave the incorrect value - %s/%s" % (`key`, `dict[key]`, `check[key]`)
  22.  
  23.  
  24. def TestDict(quiet=0):
  25.     if not quiet: print "Simple enum test"
  26.     dict = MakeTestDictionary()
  27.     checkDict = {}
  28.     TestDictAgainst(dict, checkDict)
  29.  
  30.     dict["NewKey"] = "NewValue"
  31.     checkDict["NewKey"] = "NewValue"
  32.     TestDictAgainst(dict, checkDict)
  33.  
  34.     dict["NewKey"] = None
  35.     del checkDict["NewKey"]
  36.     TestDictAgainst(dict, checkDict)
  37.  
  38.     if not quiet:
  39.         print "Failure tests"
  40.     try:
  41.         dict()
  42.         raise error, "default method with no args worked when it shouldnt have!"
  43.     except pythoncom.com_error, (hr, desc, exc, argErr):
  44.         if hr != winerror.DISP_E_BADPARAMCOUNT:
  45.             raise error, "Expected DISP_E_BADPARAMCOUNT - got %d (%s)" % (hr, desc)
  46.  
  47.     try:
  48.         dict("hi", "there")
  49.         raise error, "multiple args worked when it shouldnt have!"
  50.     except pythoncom.com_error, (hr, desc, exc, argErr):
  51.         if hr != winerror.DISP_E_BADPARAMCOUNT:
  52.             raise error, "Expected DISP_E_BADPARAMCOUNT - got %d (%s)" % (hr, desc)
  53.  
  54.     try:
  55.         dict(0)
  56.         raise error, "int key worked when it shouldnt have!"
  57.     except pythoncom.com_error, (hr, desc, exc, argErr):
  58.         if hr != winerror.DISP_E_TYPEMISMATCH:
  59.             raise error, "Expected DISP_E_TYPEMISMATCH - got %d (%s)" % (hr, desc)
  60.  
  61.     if not quiet:
  62.         print "Python.Dictionary tests complete."
  63.  
  64.  
  65. def doit():
  66.     try:
  67.         TestDict()
  68.     except:
  69.         traceback.print_exc()
  70.         
  71. if __name__=='__main__':
  72.     doit()
  73.     print "Worked OK with %d/%d" % (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())
  74.  
  75.