0001 import win32com.server.util 0002 import win32com.client 0003 import pythoncom 0004 import winerror 0005 import win32com.test.util 0006 0007 import unittest 0008 0009 class Error(Exception): 0010 pass 0011 0012 # An object representing a list of numbers 0013 class PythonSemanticClass: 0014 _public_methods_ = ["In"] # DISPIDs are allocated. 0015 _dispid_to_func_ = { 10: 'Add', 11:'Remove'} # DISPIDs specified by the object. 0016 def __init__(self): 0017 self.list = [] 0018 def _NewEnum(self): 0019 return win32com.server.util.NewEnum(self.list) 0020 def _value_(self): 0021 # should return an array. 0022 return self.list 0023 def _Evaluate(self): 0024 # return the sum 0025 return reduce(lambda a,b: a+b, self.list, 0) 0026 def In(self, value): 0027 return value in self.list 0028 def Add(self, value): 0029 self.list.append(value) 0030 def Remove(self, value): 0031 self.list.remove(value) 0032 0033 def DispExTest(ob): 0034 if not __debug__: print "WARNING: Tests dressed up as assertions are being skipped!" 0035 assert ob.GetDispID("Add", 0)==10, "Policy did not honour the dispid" 0036 # Not impl 0037 # assert ob.GetMemberName(10, 0)=="add", "Policy did not give me the correct function for the dispid" 0038 assert ob.GetDispID("Remove", 0)==11, "Policy did not honour the dispid" 0039 assert ob.GetDispID("In", 0)==1000, "Allocated dispid unexpected value" 0040 assert ob.GetDispID("_NewEnum", 0)==pythoncom.DISPID_NEWENUM, "_NewEnum() got unexpected DISPID" 0041 dispids = [] 0042 dispid = -1 0043 while 1: 0044 try: 0045 dispid = ob.GetNextDispID(0, dispid) 0046 dispids.append(dispid) 0047 except pythoncom.com_error, (hr, desc, exc, arg): 0048 assert hr==winerror.S_FALSE, "Bad result at end of enum" 0049 break 0050 dispids.sort() 0051 if dispids <> [pythoncom.DISPID_EVALUATE, pythoncom.DISPID_NEWENUM, 10, 11, 1000]: 0052 raise Error, "Got back the wrong dispids: %s" % dispids 0053 0054 def SemanticTest(ob): 0055 # First just check our object "generally" as expected. 0056 ob.Add(1) 0057 ob.Add(2) 0058 ob.Add(3) 0059 # invoke _value_ 0060 if ob() != (1,2,3): 0061 raise Error, "Bad result - got %s" % (`ob()`) 0062 0063 dispob = ob._oleobj_ 0064 0065 rc = dispob.Invoke(pythoncom.DISPID_EVALUATE, 0, pythoncom.DISPATCH_METHOD|pythoncom.DISPATCH_PROPERTYGET, 1) 0066 if rc != 6: 0067 raise Error, "Evaluate returned", rc 0068 0069 0070 class Tester(win32com.test.util.TestCase): 0071 def setUp(self): 0072 debug=0 0073 import win32com.server.dispatcher 0074 if debug: 0075 dispatcher=win32com.server.dispatcher.DefaultDebugDispatcher 0076 else: 0077 dispatcher=None 0078 disp = win32com.server.util.wrap(PythonSemanticClass(), useDispatcher=dispatcher) 0079 self.ob = win32com.client.Dispatch(disp) 0080 def tearDown(self): 0081 self.ob = None 0082 def testSemantics(self): 0083 SemanticTest(self.ob) 0084 def testIDispatchEx(self): 0085 dispexob = self.ob._oleobj_.QueryInterface(pythoncom.IID_IDispatchEx) 0086 DispExTest(dispexob) 0087 0088 if __name__=='__main__': 0089 unittest.main() 0090
Generated by PyXR 0.9.4