policySemantics.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import win32com.server.util
  2. import win32com.client
  3. import pythoncom
  4. import winerror
  5. import win32com.test.util
  6. import unittest
  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__: print("WARNING: Tests dressed up as assertions are being skipped!")
  31. assert ob.GetDispID("Add", 0)==10, "Policy did not honour the dispid"
  32. # Not impl
  33. # assert ob.GetMemberName(10, 0)=="add", "Policy did not give me the correct function for the dispid"
  34. assert ob.GetDispID("Remove", 0)==11, "Policy did not honour the dispid"
  35. assert ob.GetDispID("In", 0)==1000, "Allocated dispid unexpected value"
  36. assert ob.GetDispID("_NewEnum", 0)==pythoncom.DISPID_NEWENUM, "_NewEnum() got unexpected DISPID"
  37. dispids = []
  38. dispid = -1
  39. while 1:
  40. try:
  41. dispid = ob.GetNextDispID(0, dispid)
  42. dispids.append(dispid)
  43. except pythoncom.com_error as xxx_todo_changeme:
  44. (hr, desc, exc, arg) = xxx_todo_changeme.args
  45. assert hr==winerror.S_FALSE, "Bad result at end of enum"
  46. break
  47. dispids.sort()
  48. if dispids != [pythoncom.DISPID_EVALUATE, pythoncom.DISPID_NEWENUM, 10, 11, 1000]:
  49. raise Error("Got back the wrong dispids: %s" % dispids)
  50. def SemanticTest(ob):
  51. # First just check our object "generally" as expected.
  52. ob.Add(1)
  53. ob.Add(2)
  54. ob.Add(3)
  55. # invoke _value_
  56. if ob() != (1,2,3):
  57. raise Error("Bad result - got %s" % (repr(ob())))
  58. dispob = ob._oleobj_
  59. rc = dispob.Invoke(pythoncom.DISPID_EVALUATE, 0, pythoncom.DISPATCH_METHOD|pythoncom.DISPATCH_PROPERTYGET, 1)
  60. if rc != 6:
  61. raise Error("Evaluate returned %d" % rc)
  62. class Tester(win32com.test.util.TestCase):
  63. def setUp(self):
  64. debug=0
  65. import win32com.server.dispatcher
  66. if debug:
  67. dispatcher=win32com.server.dispatcher.DefaultDebugDispatcher
  68. else:
  69. dispatcher=None
  70. disp = win32com.server.util.wrap(PythonSemanticClass(), useDispatcher=dispatcher)
  71. self.ob = win32com.client.Dispatch(disp)
  72. def tearDown(self):
  73. self.ob = None
  74. def testSemantics(self):
  75. SemanticTest(self.ob)
  76. def testIDispatchEx(self):
  77. dispexob = self.ob._oleobj_.QueryInterface(pythoncom.IID_IDispatchEx)
  78. DispExTest(dispexob)
  79. if __name__=='__main__':
  80. unittest.main()