home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import winerror
- import pythoncom
- import win32com.client as win32com
- import win32com.client.dynamic as win32com
- import win32com.client.gencache as win32com
- from win32com.server.util import NewCollection, wrap
- import string
- import util
- importMsg = '**** VB Test harness is not installed ***\n This test requires a VB test program to be built and installed\n on this PC.\n'
-
- try:
- win32com.client.gencache.EnsureDispatch('PyCOMVBTest.Tester')
- except pythoncom.com_error:
- raise RuntimeError, importMsg
-
- import traceback
- useDispatcher = None
- error = 'VB Test Error'
-
- class TestObject:
- _public_methods_ = [
- 'CallbackVoidOneByRef',
- 'CallbackResultOneByRef',
- 'CallbackVoidTwoByRef',
- 'CallbackString',
- 'CallbackResultOneByRefButReturnNone',
- 'CallbackVoidOneByRefButReturnNone',
- 'CallbackArrayResult',
- 'CallbackArrayResultOneArrayByRef',
- 'CallbackArrayResultWrongSize']
-
- def CallbackVoidOneByRef(self, intVal):
- return intVal + 1
-
-
- def CallbackResultOneByRef(self, intVal):
- return (intVal, intVal + 1)
-
-
- def CallbackVoidTwoByRef(self, int1, int2):
- return (int1 + int2, int1 - int2)
-
-
- def CallbackString(self, strVal):
- return (0, strVal + ' has visited Python')
-
-
- def CallbackArrayResult(self, arrayVal):
- ret = []
- for i in arrayVal:
- ret.append(i + 1)
-
- return ret
-
-
- def CallbackArrayResultWrongSize(self, arrayVal):
- return list(arrayVal[:-1])
-
-
- def CallbackArrayResultOneArrayByRef(self, arrayVal):
- ret = []
- for i in arrayVal:
- ret.append(i + 1)
-
- return (list(arrayVal), ret)
-
-
- def CallbackResultOneByRefButReturnNone(self, intVal):
- pass
-
-
- def CallbackVoidOneByRefButReturnNone(self, intVal):
- pass
-
-
-
- def TestVB(vbtest, bUseGenerated):
- vbtest.LongProperty = -1
- if vbtest.LongProperty != -1:
- raise error, 'Could not set the long property correctly.'
- vbtest.LongProperty != -1
- vbtest.IntProperty = 10
- if vbtest.IntProperty != 10:
- raise error, 'Could not set the integer property correctly.'
- vbtest.IntProperty != 10
- vbtest.VariantProperty = 10
- if vbtest.VariantProperty != 10:
- raise error, 'Could not set the variant integer property correctly.'
- vbtest.VariantProperty != 10
- vbtest.VariantProperty = buffer('raw\x00data')
- if vbtest.VariantProperty != buffer('raw\x00data'):
- raise error, 'Could not set the variant buffer property correctly.'
- vbtest.VariantProperty != buffer('raw\x00data')
- vbtest.StringProperty = 'Hello from Python'
- if vbtest.StringProperty != 'Hello from Python':
- raise error, 'Could not set the string property correctly.'
- vbtest.StringProperty != 'Hello from Python'
- vbtest.VariantProperty = 'Hello from Python'
- if vbtest.VariantProperty != 'Hello from Python':
- raise error, 'Could not set the variant string property correctly.'
- vbtest.VariantProperty != 'Hello from Python'
- vbtest.VariantProperty = (1, 2, 3)
- if vbtest.VariantProperty != (1, 2, 3):
- raise error, "Could not set the variant property to an array of floats correctly - '%s'." % (vbtest.VariantProperty,)
- vbtest.VariantProperty != (1, 2, 3)
- TestArrays(vbtest, bUseGenerated)
- TestStructs(vbtest)
- TestCollections(vbtest)
- if bUseGenerated:
- ob = vbtest.TakeByRefObject(vbtest)
- vbtest.VariantPutref = vbtest
- if vbtest.VariantPutref._oleobj_ != vbtest._oleobj_:
- raise error, 'Could not set the VariantPutref property correctly.'
- vbtest.VariantPutref._oleobj_ != vbtest._oleobj_
- if vbtest.IncrementIntegerParam(1) != 2:
- raise error, 'Could not pass an integer byref'
- vbtest.IncrementIntegerParam(1) != 2
- if vbtest.IncrementVariantParam(1) != 2:
- raise error, 'Could not pass an int VARIANT byref:' + str(vbtest.IncrementVariantParam(1))
- vbtest.IncrementVariantParam(1) != 2
- if vbtest.IncrementVariantParam(1.5) != 2.5:
- raise error, 'Could not pass a float VARIANT byref'
- vbtest.IncrementVariantParam(1.5) != 2.5
- callback_ob = wrap(TestObject(), useDispatcher = useDispatcher)
- vbtest.DoSomeCallbacks(callback_ob)
-
- ret = vbtest.PassIntByVal(1)
- if ret != 2:
- raise error, 'Could not increment the integer - ' + str(ret)
- ret != 2
- TestVBInterface(vbtest)
- if bUseGenerated:
- ret = vbtest.PassIntByRef(1)
- if ret != (1, 2):
- raise error, 'Could not increment the integer - ' + str(ret)
- ret != (1, 2)
-
-
-
- def _DoTestCollection(vbtest, col_name, expected):
-
- def _getcount(ob):
- r = getattr(ob, 'Count')
- if type(r) != type(0):
- return r()
- return r
-
- c = getattr(vbtest, col_name)
- check = []
- for item in c:
- check.append(item)
-
- if check != list(expected):
- raise error, "Collection %s didn't have %r (had %r)" % (col_name, expected, check)
- check != list(expected)
- check = []
- for item in c:
- check.append(item)
-
- if check != list(expected):
- raise error, "Collection 2nd time around %s didn't have %r (had %r)" % (col_name, expected, check)
- check != list(expected)
- i = iter(getattr(vbtest, col_name))
- check = []
- for item in i:
- check.append(item)
-
- if check != list(expected):
- raise error, "Collection iterator %s didn't have %r 2nd time around (had %r)" % (col_name, expected, check)
- check != list(expected)
- check = []
- for item in i:
- check.append(item)
-
- if check != []:
- raise error, "2nd time around Collection iterator %s wasn't empty (had %r)" % (col_name, check)
- check != []
- c = getattr(vbtest, col_name)
- if len(c) != _getcount(c):
- raise error, "Collection %s __len__(%r) wasn't==Count(%r)" % (col_name, len(c), _getcount(c))
- len(c) != _getcount(c)
- c = getattr(vbtest, col_name)
- check = []
- for i in range(_getcount(c)):
- check.append(c[i])
-
- if check != list(expected):
- raise error, "Collection %s didn't have %r (had %r)" % (col_name, expected, check)
- check != list(expected)
- c = getattr(vbtest, col_name)._NewEnum()
- check = []
- while None:
- n = c.Next()
- if not n:
- break
-
- continue
- if check != list(expected):
- raise error, "Collection %s didn't have %r (had %r)" % (col_name, expected, check)
- check != list(expected)
- return None
-
-
- def TestCollections(vbtest):
- _DoTestCollection(vbtest, 'CollectionProperty', [
- 1,
- 'Two',
- '3'])
- if vbtest.CollectionProperty[0] != 1:
- raise error, 'The CollectionProperty[0] element was not the default value'
- vbtest.CollectionProperty[0] != 1
- _DoTestCollection(vbtest, 'EnumerableCollectionProperty', [])
- vbtest.EnumerableCollectionProperty.Add(1)
- vbtest.EnumerableCollectionProperty.Add('Two')
- vbtest.EnumerableCollectionProperty.Add('3')
- _DoTestCollection(vbtest, 'EnumerableCollectionProperty', [
- 1,
- 'Two',
- '3'])
-
-
- def _DoTestArray(vbtest, data, expected_exception = None):
-
- try:
- vbtest.ArrayProperty = data
- if expected_exception is not None:
- raise error, "Expected '%s'" % expected_exception
- expected_exception is not None
- except expected_exception:
- return None
-
- got = vbtest.ArrayProperty
- if got != data:
- raise error, 'Could not set the array data correctly - got %r, expected %r' % (got, data)
- got != data
-
-
- def TestArrays(vbtest, bUseGenerated):
- _DoTestArray(vbtest, ())
- _DoTestArray(vbtest, ((), ()))
- _DoTestArray(vbtest, tuple(range(1, 100)))
- _DoTestArray(vbtest, (1, 2, 3))
- _DoTestArray(vbtest, tuple(string.split('Hello from Python')))
- _DoTestArray(vbtest, (vbtest, vbtest))
- _DoTestArray(vbtest, (1, 2, '3'))
- _DoTestArray(vbtest, (1, (vbtest, vbtest), ('3', '4')))
- _DoTestArray(vbtest, ((1, 2, 3), (4, 5, 6)))
- _DoTestArray(vbtest, ((vbtest, vbtest, vbtest), (vbtest, vbtest, vbtest)))
- arrayData = (((1, 2), (3, 4), (5, 6)), ((7, 8), (9, 10), (11, 12)))
- arrayData = (((vbtest, vbtest), (vbtest, vbtest), (vbtest, vbtest)), ((vbtest, vbtest), (vbtest, vbtest), (vbtest, vbtest)))
- _DoTestArray(vbtest, arrayData)
- _DoTestArray(vbtest, (vbtest, 2, '3'))
- _DoTestArray(vbtest, (1, 2, vbtest))
- expected_exception = None
- arrayData = (((1, 2, 1), (3, 4), (5, 6)), ((7, 8), (9, 10), (11, 12)))
- _DoTestArray(vbtest, arrayData, expected_exception)
- arrayData = (((vbtest, vbtest),), ((vbtest,),))
- _DoTestArray(vbtest, arrayData, expected_exception)
- arrayData = (((1, 2), (3, 4), (5, 6, 8)), ((7, 8), (9, 10), (11, 12)))
- _DoTestArray(vbtest, arrayData, expected_exception)
- callback_ob = wrap(TestObject(), useDispatcher = useDispatcher)
- print "** Expecting a 'ValueError' exception to be printed next:"
-
- try:
- vbtest.DoCallbackSafeArraySizeFail(callback_ob)
- except pythoncom.com_error:
- (hr, msg, exc, arg) = None
-
- if bUseGenerated:
- testData = string.split('Mark was here')
- (resultData, byRefParam) = vbtest.PassSAFEARRAY(testData)
-
- try:
- unicode
- except NameError:
- resultData = map(str, resultData)
- byRefParam = map(str, byRefParam)
-
- if testData != list(resultData):
- raise error, 'The safe array data was not what we expected - got ' + str(resultData)
- testData != list(resultData)
- if testData != list(byRefParam):
- raise error, 'The safe array data was not what we expected - got ' + str(byRefParam)
- testData != list(byRefParam)
- testData = [
- 1,
- 2,
- 3]
- (resultData, byRefParam) = vbtest.PassSAFEARRAYVariant(testData)
- testData = [
- 'hi',
- 'from',
- 'Python']
- (resultData, byRefParam) = vbtest.PassSAFEARRAYVariant(testData)
- testData = [
- 1,
- 2,
- pythoncom.Unicode('3')]
- (resultData, byRefParam) = vbtest.PassSAFEARRAYVariant(testData)
-
- print 'Array tests passed'
-
-
- def TestStructs(vbtest):
-
- try:
- vbtest.IntProperty = 'One'
- raise error, 'Should have failed by now'
- except pythoncom.com_error:
- (hr, desc, exc, argErr) = None
- if hr != winerror.DISP_E_TYPEMISMATCH:
- raise error, 'Expected DISP_E_TYPEMISMATCH'
- hr != winerror.DISP_E_TYPEMISMATCH
-
- s = vbtest.StructProperty
- if s.int_val != 99 or str(s.str_val) != 'hello':
- raise error, 'The struct value was not correct'
- str(s.str_val) != 'hello'
- s.str_val = 'Hi from Python'
- s.int_val = 11
- if s.int_val != 11 or str(s.str_val) != 'Hi from Python':
- raise error, 'The struct value didnt persist!'
- str(s.str_val) != 'Hi from Python'
- if s.sub_val.int_val != 66 or str(s.sub_val.str_val) != 'sub hello':
- raise error, 'The sub-struct value was not correct'
- str(s.sub_val.str_val) != 'sub hello'
- sub = s.sub_val
- sub.int_val = 22
- if sub.int_val != 22:
- print sub.int_val
- raise error, 'The sub-struct value didnt persist!'
- sub.int_val != 22
- if s.sub_val.int_val != 22:
- print s.sub_val.int_val
- raise error, 'The sub-struct value (re-fetched) didnt persist!'
- s.sub_val.int_val != 22
- if s.sub_val.array_val[0].int_val != 0 or str(s.sub_val.array_val[0].str_val) != 'zero':
- print s.sub_val.array_val[0].int_val
- raise error, 'The array element wasnt correct'
- str(s.sub_val.array_val[0].str_val) != 'zero'
- s.sub_val.array_val[0].int_val = 99
- s.sub_val.array_val[1].int_val = 66
- if s.sub_val.array_val[0].int_val != 99 or s.sub_val.array_val[1].int_val != 66:
- print s.sub_val.array_val[0].int_val
- raise error, 'The array element didnt persist.'
- s.sub_val.array_val[1].int_val != 66
- vbtest.StructProperty = s
- s = vbtest.StructProperty
- if s.int_val != 11 or str(s.str_val) != 'Hi from Python':
- raise error, 'After sending to VB, the struct value didnt persist!'
- str(s.str_val) != 'Hi from Python'
- if s.sub_val.array_val[0].int_val != 99:
- raise error, 'After sending to VB, the struct array value didnt persist!'
- s.sub_val.array_val[0].int_val != 99
- import copy
- s2 = copy.copy(s)
- s2.int_val = 123
- s2 = vbtest.GetStructFunc()
- vbtest.SetStructSub(s2)
- s = win32com.client.Record('VBStruct', vbtest)
- s.int_val = -1
- vbtest.SetStructSub(s)
- s_array = vbtest.StructArrayProperty
- vbtest.MakeStructArrayProperty(3)
- s_array = vbtest.StructArrayProperty
- for i in range(len(s_array)):
- pass
-
-
- try:
- s.bad_attribute
- raise RuntimeError, 'Could get a bad attribute'
- except AttributeError:
- pass
-
- m = s.__members__
- print 'Struct/Record tests passed'
-
-
- def TestVBInterface(ob):
- t = ob.GetInterfaceTester(2)
- if t.getn() != 2:
- raise error, 'Initial value wrong'
- t.getn() != 2
- t.setn(3)
- if t.getn() != 3:
- raise error, 'New value wrong'
- t.getn() != 3
-
-
- def DoTestAll():
- o = win32com.client.Dispatch('PyCOMVBTest.Tester')
- TestVB(o, 1)
- o = win32com.client.dynamic.DumbDispatch('PyCOMVBTest.Tester')
- TestVB(o, 0)
-
-
- def TestAll():
- if not __debug__:
- raise RuntimeError, 'This must be run in debug mode - we use assert!'
- __debug__
-
- try:
- DoTestAll()
- print 'All tests appear to have worked!'
- except:
- print 'TestAll() failed!!'
- traceback.print_exc()
-
-
-
- def suite():
- import unittest
- test = util.CapturingFunctionTestCase(TestAll, description = 'VB tests')
- suite = unittest.TestSuite()
- suite.addTest(test)
- return suite
-
- if __name__ == '__main__':
- util.testmain()
-
-