home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_win32com_test_policySemantics.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  2.6 KB  |  84 lines

  1. import win32com.server.util
  2. import win32com.client
  3. import pythoncom
  4. import winerror
  5.  
  6. class Error(Exception):
  7.     pass
  8.     
  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 reduce(lambda a,b: a+b, self.list, 0)
  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.  
  30. def DispExTest(ob):
  31.     if not __debug__: 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 ob.GetDispID("_NewEnum", 0)==pythoncom.DISPID_NEWENUM, "_NewEnum() got unexpected DISPID"
  38.     dispids = []
  39.     dispid = -1
  40.     while 1:
  41.         try:
  42.             dispid = ob.GetNextDispID(0, dispid)
  43.             dispids.append(dispid)
  44.         except pythoncom.com_error, (hr, desc, exc, arg):
  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.     print "IDispatchEx semantics worked"
  51.         
  52. def SemanticTest(ob):
  53.     # First just check our object "generally" as expected.
  54.     ob.Add(1)
  55.     ob.Add(2)
  56.     ob.Add(3)
  57.     # invoke _value_
  58.     if ob() != (1,2,3):
  59.         raise Error, "Bad result - got %s" % (`ob()`)
  60.  
  61.     dispob = ob._oleobj_
  62.     
  63.     rc = dispob.Invoke(pythoncom.DISPID_EVALUATE, 0, pythoncom.DISPATCH_METHOD|pythoncom.DISPATCH_PROPERTYGET, 1)
  64.     if rc != 6:
  65.         raise Error, "Evaluate returned", rc
  66.  
  67.     dispexob = dispob.QueryInterface(pythoncom.IID_IDispatchEx)
  68.     DispExTest(dispexob)
  69.     print "Python policy semantics worked."
  70.     
  71. def TestAll():
  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(PythonSemanticClass(), useDispatcher=dispatcher)
  79.     ob = win32com.client.Dispatch(disp)
  80.     SemanticTest(ob)
  81.     
  82. if __name__=='__main__':
  83.     TestAll()
  84.