Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testCollections.py 4.3KB

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