0001 from __future__ import generators 0002 0003 # Some raw iter tests. Some "high-level" iterator tests can be found in 0004 # testvb.py and testOutlook.py 0005 import sys 0006 import unittest 0007 0008 from win32com.client.gencache import EnsureDispatch 0009 from win32com.client import Dispatch 0010 import win32com.server.util 0011 import win32com.test.util 0012 import pythoncom 0013 0014 def yield_iter(iter): 0015 while 1: 0016 yield iter.next() 0017 0018 class _BaseTestCase(win32com.test.util.TestCase): 0019 def test_enumvariant_vb(self): 0020 ob, iter = self.iter_factory() 0021 got=[] 0022 for v in iter: 0023 got.append(v) 0024 self.assertEquals(got, self.expected_data) 0025 def test_yield(self): 0026 ob, i = self.iter_factory() 0027 got=[] 0028 for v in yield_iter(iter(i)): 0029 got.append(v) 0030 self.assertEquals(got, self.expected_data) 0031 0032 def _do_test_nonenum(self, object): 0033 try: 0034 for i in object: 0035 pass 0036 self.fail("Could iterate over a non-iterable object") 0037 except TypeError: 0038 pass # this is expected. 0039 self.assertRaises(TypeError, iter, object) 0040 self.assertRaises(AttributeError, getattr, object, "next") 0041 0042 def test_nonenum_wrapper(self): 0043 # Check our raw PyIDispatch 0044 ob = self.object._oleobj_ 0045 try: 0046 for i in ob: 0047 pass 0048 self.fail("Could iterate over a non-iterable object") 0049 except TypeError: 0050 pass # this is expected. 0051 self.assertRaises(TypeError, iter, ob) 0052 self.assertRaises(AttributeError, getattr, ob, "next") 0053 0054 # And our Dispatch wrapper 0055 ob = self.object 0056 try: 0057 for i in ob: 0058 pass 0059 self.fail("Could iterate over a non-iterable object") 0060 except TypeError: 0061 pass # this is expected. 0062 # Note that as our object may be dynamic, we *do* have a __getitem__ 0063 # method, meaning we *can* call iter() on the object. In this case 0064 # actual iteration is what fails. 0065 # So either the 'iter(); will raise a type error, or an attempt to 0066 # fetch it 0067 try: 0068 iter(ob).next() 0069 self.fail("Expected a TypeError fetching this iterator") 0070 except TypeError: 0071 pass 0072 # And it should never have a 'next' method 0073 self.assertRaises(AttributeError, getattr, ob, "next") 0074 0075 class VBTestCase(_BaseTestCase): 0076 def setUp(self): 0077 def factory(): 0078 # Our VB test harness exposes a property with IEnumVariant. 0079 ob = self.object.EnumerableCollectionProperty 0080 for i in self.expected_data: 0081 ob.Add(i) 0082 # Get the raw IEnumVARIANT. 0083 invkind = pythoncom.DISPATCH_METHOD | pythoncom.DISPATCH_PROPERTYGET 0084 iter = ob._oleobj_.InvokeTypes(pythoncom.DISPID_NEWENUM,0,invkind,(13, 10),()) 0085 return ob, iter.QueryInterface(pythoncom.IID_IEnumVARIANT) 0086 # We *need* generated dispatch semantics, so dynamic __getitem__ etc 0087 # don't get in the way of our tests. 0088 self.object = EnsureDispatch("PyCOMVBTest.Tester") 0089 self.expected_data = [1, "Two", "3"] 0090 self.iter_factory = factory 0091 0092 def tearDown(self): 0093 self.object = None 0094 0095 # Test our client semantics, but using a wrapped Python list object. 0096 # This has the effect of re-using our client specific tests, but in this 0097 # case is exercising the server side. 0098 class SomeObject: 0099 _public_methods_ = ["GetCollection"] 0100 def __init__(self, data): 0101 self.data = data 0102 def GetCollection(self): 0103 return win32com.server.util.NewCollection(self.data) 0104 0105 class WrappedPythonCOMServerTestCase(_BaseTestCase): 0106 def setUp(self): 0107 def factory(): 0108 ob = self.object.GetCollection() 0109 flags = pythoncom.DISPATCH_METHOD | pythoncom.DISPATCH_PROPERTYGET 0110 enum = ob._oleobj_.Invoke(pythoncom.DISPID_NEWENUM, 0, flags, 1) 0111 return ob, enum.QueryInterface(pythoncom.IID_IEnumVARIANT) 0112 0113 self.expected_data = [1,'Two',3] 0114 sv = win32com.server.util.wrap(SomeObject(self.expected_data)) 0115 self.object = Dispatch(sv) 0116 self.iter_factory = factory 0117 0118 def tearDown(self): 0119 self.object = None 0120 0121 def suite(): 0122 # We dont want our base class run 0123 suite = unittest.TestSuite() 0124 for item in globals().values(): 0125 if type(item)==type(unittest.TestCase) and \ 0126 issubclass(item, unittest.TestCase) and \ 0127 item != _BaseTestCase: 0128 suite.addTest(unittest.makeSuite(item)) 0129 return suite 0130 0131 if __name__=='__main__': 0132 unittest.main(argv=sys.argv + ['suite']) 0133
Generated by PyXR 0.9.4