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.

policySemantics.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import unittest
  2. import pythoncom
  3. import win32com.client
  4. import win32com.server.util
  5. import win32com.test.util
  6. import winerror
  7. class Error(Exception):
  8. pass
  9. # An object representing a list of numbers
  10. class PythonSemanticClass:
  11. _public_methods_ = ["In"] # DISPIDs are allocated.
  12. _dispid_to_func_ = {10: "Add", 11: "Remove"} # DISPIDs specified by the object.
  13. def __init__(self):
  14. self.list = []
  15. def _NewEnum(self):
  16. return win32com.server.util.NewEnum(self.list)
  17. def _value_(self):
  18. # should return an array.
  19. return self.list
  20. def _Evaluate(self):
  21. # return the sum
  22. return sum(self.list)
  23. def In(self, value):
  24. return value in self.list
  25. def Add(self, value):
  26. self.list.append(value)
  27. def Remove(self, value):
  28. self.list.remove(value)
  29. def DispExTest(ob):
  30. if not __debug__:
  31. print("WARNING: Tests dressed up as assertions are being skipped!")
  32. assert ob.GetDispID("Add", 0) == 10, "Policy did not honour the dispid"
  33. # Not impl
  34. # assert ob.GetMemberName(10, 0)=="add", "Policy did not give me the correct function for the dispid"
  35. assert ob.GetDispID("Remove", 0) == 11, "Policy did not honour the dispid"
  36. assert ob.GetDispID("In", 0) == 1000, "Allocated dispid unexpected value"
  37. assert (
  38. ob.GetDispID("_NewEnum", 0) == pythoncom.DISPID_NEWENUM
  39. ), "_NewEnum() got unexpected DISPID"
  40. dispids = []
  41. dispid = -1
  42. while 1:
  43. try:
  44. dispid = ob.GetNextDispID(0, dispid)
  45. dispids.append(dispid)
  46. except pythoncom.com_error as xxx_todo_changeme:
  47. (hr, desc, exc, arg) = xxx_todo_changeme.args
  48. assert hr == winerror.S_FALSE, "Bad result at end of enum"
  49. break
  50. dispids.sort()
  51. if dispids != [pythoncom.DISPID_EVALUATE, pythoncom.DISPID_NEWENUM, 10, 11, 1000]:
  52. raise Error("Got back the wrong dispids: %s" % dispids)
  53. def SemanticTest(ob):
  54. # First just check our object "generally" as expected.
  55. ob.Add(1)
  56. ob.Add(2)
  57. ob.Add(3)
  58. # invoke _value_
  59. if ob() != (1, 2, 3):
  60. raise Error("Bad result - got %s" % (repr(ob())))
  61. dispob = ob._oleobj_
  62. rc = dispob.Invoke(
  63. pythoncom.DISPID_EVALUATE,
  64. 0,
  65. pythoncom.DISPATCH_METHOD | pythoncom.DISPATCH_PROPERTYGET,
  66. 1,
  67. )
  68. if rc != 6:
  69. raise Error("Evaluate returned %d" % rc)
  70. class Tester(win32com.test.util.TestCase):
  71. def setUp(self):
  72. debug = 0
  73. import win32com.server.dispatcher
  74. if debug:
  75. dispatcher = win32com.server.dispatcher.DefaultDebugDispatcher
  76. else:
  77. dispatcher = None
  78. disp = win32com.server.util.wrap(
  79. PythonSemanticClass(), useDispatcher=dispatcher
  80. )
  81. self.ob = win32com.client.Dispatch(disp)
  82. def tearDown(self):
  83. self.ob = None
  84. def testSemantics(self):
  85. SemanticTest(self.ob)
  86. def testIDispatchEx(self):
  87. dispexob = self.ob._oleobj_.QueryInterface(pythoncom.IID_IDispatchEx)
  88. DispExTest(dispexob)
  89. if __name__ == "__main__":
  90. unittest.main()