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.

testIterators.py 4.5KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # Some raw iter tests. Some "high-level" iterator tests can be found in
  2. # testvb.py and testOutlook.py
  3. import sys
  4. import unittest
  5. import pythoncom
  6. import win32com.server.util
  7. import win32com.test.util
  8. from win32com.client import Dispatch
  9. from win32com.client.gencache import EnsureDispatch
  10. class _BaseTestCase(win32com.test.util.TestCase):
  11. def test_enumvariant_vb(self):
  12. ob, iter = self.iter_factory()
  13. got = []
  14. for v in iter:
  15. got.append(v)
  16. self.assertEqual(got, self.expected_data)
  17. def test_yield(self):
  18. ob, i = self.iter_factory()
  19. got = []
  20. for v in iter(i):
  21. got.append(v)
  22. self.assertEqual(got, self.expected_data)
  23. def _do_test_nonenum(self, object):
  24. try:
  25. for i in object:
  26. pass
  27. self.fail("Could iterate over a non-iterable object")
  28. except TypeError:
  29. pass # this is expected.
  30. self.assertRaises(TypeError, iter, object)
  31. self.assertRaises(AttributeError, getattr, object, "next")
  32. def test_nonenum_wrapper(self):
  33. # Check our raw PyIDispatch
  34. ob = self.object._oleobj_
  35. try:
  36. for i in ob:
  37. pass
  38. self.fail("Could iterate over a non-iterable object")
  39. except TypeError:
  40. pass # this is expected.
  41. self.assertRaises(TypeError, iter, ob)
  42. self.assertRaises(AttributeError, getattr, ob, "next")
  43. # And our Dispatch wrapper
  44. ob = self.object
  45. try:
  46. for i in ob:
  47. pass
  48. self.fail("Could iterate over a non-iterable object")
  49. except TypeError:
  50. pass # this is expected.
  51. # Note that as our object may be dynamic, we *do* have a __getitem__
  52. # method, meaning we *can* call iter() on the object. In this case
  53. # actual iteration is what fails.
  54. # So either the 'iter(); will raise a type error, or an attempt to
  55. # fetch it
  56. try:
  57. next(iter(ob))
  58. self.fail("Expected a TypeError fetching this iterator")
  59. except TypeError:
  60. pass
  61. # And it should never have a 'next' method
  62. self.assertRaises(AttributeError, getattr, ob, "next")
  63. class VBTestCase(_BaseTestCase):
  64. def setUp(self):
  65. def factory():
  66. # Our VB test harness exposes a property with IEnumVariant.
  67. ob = self.object.EnumerableCollectionProperty
  68. for i in self.expected_data:
  69. ob.Add(i)
  70. # Get the raw IEnumVARIANT.
  71. invkind = pythoncom.DISPATCH_METHOD | pythoncom.DISPATCH_PROPERTYGET
  72. iter = ob._oleobj_.InvokeTypes(
  73. pythoncom.DISPID_NEWENUM, 0, invkind, (13, 10), ()
  74. )
  75. return ob, iter.QueryInterface(pythoncom.IID_IEnumVARIANT)
  76. # We *need* generated dispatch semantics, so dynamic __getitem__ etc
  77. # don't get in the way of our tests.
  78. self.object = EnsureDispatch("PyCOMVBTest.Tester")
  79. self.expected_data = [1, "Two", "3"]
  80. self.iter_factory = factory
  81. def tearDown(self):
  82. self.object = None
  83. # Test our client semantics, but using a wrapped Python list object.
  84. # This has the effect of re-using our client specific tests, but in this
  85. # case is exercising the server side.
  86. class SomeObject:
  87. _public_methods_ = ["GetCollection"]
  88. def __init__(self, data):
  89. self.data = data
  90. def GetCollection(self):
  91. return win32com.server.util.NewCollection(self.data)
  92. class WrappedPythonCOMServerTestCase(_BaseTestCase):
  93. def setUp(self):
  94. def factory():
  95. ob = self.object.GetCollection()
  96. flags = pythoncom.DISPATCH_METHOD | pythoncom.DISPATCH_PROPERTYGET
  97. enum = ob._oleobj_.Invoke(pythoncom.DISPID_NEWENUM, 0, flags, 1)
  98. return ob, enum.QueryInterface(pythoncom.IID_IEnumVARIANT)
  99. self.expected_data = [1, "Two", 3]
  100. sv = win32com.server.util.wrap(SomeObject(self.expected_data))
  101. self.object = Dispatch(sv)
  102. self.iter_factory = factory
  103. def tearDown(self):
  104. self.object = None
  105. def suite():
  106. # We dont want our base class run
  107. suite = unittest.TestSuite()
  108. for item in list(globals().values()):
  109. if (
  110. type(item) == type(unittest.TestCase)
  111. and issubclass(item, unittest.TestCase)
  112. and item != _BaseTestCase
  113. ):
  114. suite.addTest(unittest.makeSuite(item))
  115. return suite
  116. if __name__ == "__main__":
  117. unittest.main(argv=sys.argv + ["suite"])