policySemantics.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, (hr, desc, exc, arg):
  44. assert hr==winerror.S_FALSE, "Bad result at end of enum"
  45. break
  46. dispids.sort()
  47. if dispids != [pythoncom.DISPID_EVALUATE, pythoncom.DISPID_NEWENUM, 10, 11, 1000]:
  48. raise Error("Got back the wrong dispids: %s" % dispids)
  49. def SemanticTest(ob):
  50. # First just check our object "generally" as expected.
  51. ob.Add(1)
  52. ob.Add(2)
  53. ob.Add(3)
  54. # invoke _value_
  55. if ob() != (1,2,3):
  56. raise Error("Bad result - got %s" % (repr(ob())))
  57. dispob = ob._oleobj_
  58. rc = dispob.Invoke(pythoncom.DISPID_EVALUATE, 0, pythoncom.DISPATCH_METHOD|pythoncom.DISPATCH_PROPERTYGET, 1)
  59. if rc != 6:
  60. raise Error("Evaluate returned %d" % rc)
  61. class Tester(win32com.test.util.TestCase):
  62. def setUp(self):
  63. debug=0
  64. import win32com.server.dispatcher
  65. if debug:
  66. dispatcher=win32com.server.dispatcher.DefaultDebugDispatcher
  67. else:
  68. dispatcher=None
  69. disp = win32com.server.util.wrap(PythonSemanticClass(), useDispatcher=dispatcher)
  70. self.ob = win32com.client.Dispatch(disp)
  71. def tearDown(self):
  72. self.ob = None
  73. def testSemantics(self):
  74. SemanticTest(self.ob)
  75. def testIDispatchEx(self):
  76. dispexob = self.ob._oleobj_.QueryInterface(pythoncom.IID_IDispatchEx)
  77. DispExTest(dispexob)
  78. if __name__=='__main__':
  79. unittest.main()