testCollections.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. import sys
  8. import win32com.server.util
  9. import win32com.test.util
  10. import win32com.client
  11. import traceback
  12. import pythoncom
  13. import pywintypes
  14. import winerror
  15. L=pywintypes.Unicode
  16. import unittest
  17. error = "collection test error"
  18. def MakeEmptyEnum():
  19. # create the Python enumerator object as a real COM object
  20. o = win32com.server.util.wrap( win32com.server.util.Collection() )
  21. return win32com.client.Dispatch(o)
  22. def MakeTestEnum():
  23. # create a sub-collection, just to make sure it works :-)
  24. sub = win32com.server.util.wrap( win32com.server.util.Collection( ['Sub1', 2, 'Sub3']) )
  25. # create the Python enumerator object as a real COM object
  26. o = win32com.server.util.wrap( win32com.server.util.Collection( [1,'Two',3, sub]))
  27. return win32com.client.Dispatch(o)
  28. def TestEnumAgainst(o,check):
  29. for i in range(len(check)):
  30. if o(i) != check[i]:
  31. raise error("Using default method gave the incorrect value - %s/%s" % (repr(o(i)), repr(check[i])))
  32. for i in range(len(check)):
  33. if o.Item(i) != check[i]:
  34. raise error("Using Item method gave the incorrect value - %s/%s" % (repr(o(i)), repr(check[i])))
  35. # First try looping.
  36. cmp = []
  37. for s in o:
  38. cmp.append(s)
  39. if cmp[:len(check)] != check:
  40. raise error("Result after looping isnt correct - %s/%s" % (repr(cmp[:len(check)]), repr(check)))
  41. for i in range(len(check)):
  42. if o[i] != check[i]:
  43. raise error("Using indexing gave the incorrect value")
  44. def TestEnum(quiet=None):
  45. if quiet is None:
  46. quiet = not "-v" in sys.argv
  47. if not quiet: print("Simple enum test")
  48. o = MakeTestEnum()
  49. check = [1,'Two',3]
  50. TestEnumAgainst(o, check)
  51. if not quiet: print("sub-collection test")
  52. sub = o[3]
  53. TestEnumAgainst(sub ,['Sub1', 2, 'Sub3'])
  54. # Remove the sublist for this test!
  55. o.Remove(o.Count()-1)
  56. if not quiet: print("Remove item test")
  57. del check[1]
  58. o.Remove(1)
  59. TestEnumAgainst(o, check)
  60. if not quiet: print("Add item test")
  61. o.Add('New Item')
  62. check.append('New Item')
  63. TestEnumAgainst(o, check)
  64. if not quiet: print("Insert item test")
  65. o.Insert(2, -1)
  66. check.insert(2, -1)
  67. TestEnumAgainst(o, check)
  68. ### This does not work!
  69. # if not quiet: print "Indexed replace item test"
  70. # o[2] = 'Replaced Item'
  71. # check[2] = 'Replaced Item'
  72. # TestEnumAgainst(o, check)
  73. try:
  74. o()
  75. raise error("default method with no args worked when it shouldnt have!")
  76. except pythoncom.com_error as exc:
  77. if exc.hresult != winerror.DISP_E_BADPARAMCOUNT:
  78. raise error("Expected DISP_E_BADPARAMCOUNT - got %s" % (exc,))
  79. try:
  80. o.Insert("foo", 2)
  81. raise error("Insert worked when it shouldnt have!")
  82. except pythoncom.com_error as exc:
  83. if exc.hresult != winerror.DISP_E_TYPEMISMATCH:
  84. raise error("Expected DISP_E_TYPEMISMATCH - got %s" % (exc,))
  85. # Remove the sublist for this test!
  86. try:
  87. o.Remove(o.Count())
  88. raise error("Remove worked when it shouldnt have!")
  89. except pythoncom.com_error as exc:
  90. if exc.hresult != winerror.DISP_E_BADINDEX:
  91. raise error("Expected DISP_E_BADINDEX - got %s" % (exc,))
  92. # Test an empty collection
  93. if not quiet: print("Empty collection test")
  94. o = MakeEmptyEnum()
  95. for item in o:
  96. raise error("Empty list performed an iteration")
  97. try:
  98. ob = o[1]
  99. raise error("Empty list could be indexed")
  100. except IndexError:
  101. pass
  102. try:
  103. ob = o[0]
  104. raise error("Empty list could be indexed")
  105. except IndexError:
  106. pass
  107. try:
  108. ob = o(0)
  109. raise error("Empty list could be indexed")
  110. except pythoncom.com_error as exc:
  111. if exc.hresult != winerror.DISP_E_BADINDEX:
  112. raise error("Expected DISP_E_BADINDEX - got %s" % (exc,))
  113. class TestCase(win32com.test.util.TestCase):
  114. def testEnum(self):
  115. TestEnum()
  116. if __name__=='__main__':
  117. unittest.main()