PyXR

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



0001 # testDictionary.py
0002 #
0003 import sys
0004 import win32com.server.util
0005 import win32com.test.util
0006 import win32com.client
0007 import traceback
0008 import pythoncom
0009 import pywintypes
0010 import winerror
0011 L=pywintypes.Unicode
0012 
0013 import unittest
0014 
0015 error = "dictionary test error"
0016 
0017 def MakeTestDictionary():
0018     return win32com.client.Dispatch("Python.Dictionary")
0019 
0020 def TestDictAgainst(dict,check):
0021     for key, value in check.items():
0022         if dict(key) != value:
0023             raise error, "Indexing for '%s' gave the incorrect value - %s/%s" % (`key`, `dict[key]`, `check[key]`)
0024 
0025 # Ensure we have the correct version registered.
0026 def Register(quiet):
0027     import win32com.server.register
0028     from win32com.servers.dictionary import DictionaryPolicy
0029     win32com.server.register.RegisterClasses(DictionaryPolicy, quiet=quiet)
0030 
0031 def TestDict(quiet=None):
0032     if quiet is None:
0033         quiet = not "-v" in sys.argv
0034     Register(quiet)
0035 
0036     if not quiet: print "Simple enum test"
0037     dict = MakeTestDictionary()
0038     checkDict = {}
0039     TestDictAgainst(dict, checkDict)
0040 
0041     dict["NewKey"] = "NewValue"
0042     checkDict["NewKey"] = "NewValue"
0043     TestDictAgainst(dict, checkDict)
0044 
0045     dict["NewKey"] = None
0046     del checkDict["NewKey"]
0047     TestDictAgainst(dict, checkDict)
0048 
0049     if not quiet:
0050         print "Failure tests"
0051     try:
0052         dict()
0053         raise error, "default method with no args worked when it shouldnt have!"
0054     except pythoncom.com_error, (hr, desc, exc, argErr):
0055         if hr != winerror.DISP_E_BADPARAMCOUNT:
0056             raise error, "Expected DISP_E_BADPARAMCOUNT - got %d (%s)" % (hr, desc)
0057 
0058     try:
0059         dict("hi", "there")
0060         raise error, "multiple args worked when it shouldnt have!"
0061     except pythoncom.com_error, (hr, desc, exc, argErr):
0062         if hr != winerror.DISP_E_BADPARAMCOUNT:
0063             raise error, "Expected DISP_E_BADPARAMCOUNT - got %d (%s)" % (hr, desc)
0064 
0065     try:
0066         dict(0)
0067         raise error, "int key worked when it shouldnt have!"
0068     except pythoncom.com_error, (hr, desc, exc, argErr):
0069         if hr != winerror.DISP_E_TYPEMISMATCH:
0070             raise error, "Expected DISP_E_TYPEMISMATCH - got %d (%s)" % (hr, desc)
0071 
0072     if not quiet:
0073         print "Python.Dictionary tests complete."
0074 
0075 class TestCase(win32com.test.util.TestCase):
0076     def testDict(self):
0077         TestDict()
0078 
0079 if __name__=='__main__':
0080     unittest.main()
0081 

Generated by PyXR 0.9.4
SourceForge.net Logo