testDictionary.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 list(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. now = win32timezone.now()
  41. # We want to keep the milliseconds but discard microseconds as they
  42. # don't survive the conversion.
  43. now = now.replace(microsecond = round(now.microsecond / 1000) * 1000)
  44. dict["Now"] = now
  45. checkDict["Now"] = now
  46. TestDictAgainst(dict, checkDict)
  47. if not quiet:
  48. print("Failure tests")
  49. try:
  50. dict()
  51. raise Exception("default method with no args worked when it shouldnt have!")
  52. except pythoncom.com_error as xxx_todo_changeme:
  53. (hr, desc, exc, argErr) = xxx_todo_changeme.args
  54. if hr != winerror.DISP_E_BADPARAMCOUNT:
  55. raise Exception("Expected DISP_E_BADPARAMCOUNT - got %d (%s)" % (hr, desc))
  56. try:
  57. dict("hi", "there")
  58. raise Exception("multiple args worked when it shouldnt have!")
  59. except pythoncom.com_error as xxx_todo_changeme1:
  60. (hr, desc, exc, argErr) = xxx_todo_changeme1.args
  61. if hr != winerror.DISP_E_BADPARAMCOUNT:
  62. raise Exception("Expected DISP_E_BADPARAMCOUNT - got %d (%s)" % (hr, desc))
  63. try:
  64. dict(0)
  65. raise Exception("int key worked when it shouldnt have!")
  66. except pythoncom.com_error as xxx_todo_changeme2:
  67. (hr, desc, exc, argErr) = xxx_todo_changeme2.args
  68. if hr != winerror.DISP_E_TYPEMISMATCH:
  69. raise Exception("Expected DISP_E_TYPEMISMATCH - got %d (%s)" % (hr, desc))
  70. if not quiet:
  71. print("Python.Dictionary tests complete.")
  72. class TestCase(win32com.test.util.TestCase):
  73. def testDict(self):
  74. TestDict()
  75. if __name__=='__main__':
  76. unittest.main()