testDictionary.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # testDictionary.py
  2. #
  3. import sys
  4. import datetime
  5. import time
  6. import win32com.server.util
  7. import win32com.test.util
  8. import win32com.client
  9. import traceback
  10. import pythoncom
  11. import pywintypes
  12. import winerror
  13. import win32timezone
  14. import unittest
  15. def MakeTestDictionary():
  16. return win32com.client.Dispatch("Python.Dictionary")
  17. def TestDictAgainst(dict,check):
  18. for key, value in check.items():
  19. if dict(key) != value:
  20. raise Exception("Indexing for '%s' gave the incorrect value - %s/%s" % (repr(key), repr(dict[key]), repr(check[key])))
  21. # Ensure we have the correct version registered.
  22. def Register(quiet):
  23. import win32com.servers.dictionary
  24. from win32com.test.util import RegisterPythonServer
  25. RegisterPythonServer(win32com.servers.dictionary.__file__, 'Python.Dictionary')
  26. def TestDict(quiet=None):
  27. if quiet is None:
  28. quiet = not "-v" in sys.argv
  29. Register(quiet)
  30. if not quiet: print("Simple enum test")
  31. dict = MakeTestDictionary()
  32. checkDict = {}
  33. TestDictAgainst(dict, checkDict)
  34. dict["NewKey"] = "NewValue"
  35. checkDict["NewKey"] = "NewValue"
  36. TestDictAgainst(dict, checkDict)
  37. dict["NewKey"] = None
  38. del checkDict["NewKey"]
  39. TestDictAgainst(dict, checkDict)
  40. if issubclass(pywintypes.TimeType, datetime.datetime):
  41. now = win32timezone.now()
  42. # We want to keep the milliseconds but discard microseconds as they
  43. # don't survive the conversion.
  44. now = now.replace(microsecond = round(now.microsecond / 1000) * 1000)
  45. else:
  46. now = pythoncom.MakeTime(time.gmtime(time.time()))
  47. dict["Now"] = now
  48. checkDict["Now"] = now
  49. TestDictAgainst(dict, checkDict)
  50. if not quiet:
  51. print("Failure tests")
  52. try:
  53. dict()
  54. raise Exception("default method with no args worked when it shouldnt have!")
  55. except pythoncom.com_error as xxx_todo_changeme:
  56. (hr, desc, exc, argErr) = xxx_todo_changeme.args
  57. if hr != winerror.DISP_E_BADPARAMCOUNT:
  58. raise Exception("Expected DISP_E_BADPARAMCOUNT - got %d (%s)" % (hr, desc))
  59. try:
  60. dict("hi", "there")
  61. raise Exception("multiple args worked when it shouldnt have!")
  62. except pythoncom.com_error as xxx_todo_changeme1:
  63. (hr, desc, exc, argErr) = xxx_todo_changeme1.args
  64. if hr != winerror.DISP_E_BADPARAMCOUNT:
  65. raise Exception("Expected DISP_E_BADPARAMCOUNT - got %d (%s)" % (hr, desc))
  66. try:
  67. dict(0)
  68. raise Exception("int key worked when it shouldnt have!")
  69. except pythoncom.com_error as xxx_todo_changeme2:
  70. (hr, desc, exc, argErr) = xxx_todo_changeme2.args
  71. if hr != winerror.DISP_E_TYPEMISMATCH:
  72. raise Exception("Expected DISP_E_TYPEMISMATCH - got %d (%s)" % (hr, desc))
  73. if not quiet:
  74. print("Python.Dictionary tests complete.")
  75. class TestCase(win32com.test.util.TestCase):
  76. def testDict(self):
  77. TestDict()
  78. if __name__=='__main__':
  79. unittest.main()