home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_2906 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  12.9 KB  |  425 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import winerror
  5. import pythoncom
  6. import win32com.client as win32com
  7. import win32com.client.dynamic as win32com
  8. import win32com.client.gencache as win32com
  9. from win32com.server.util import NewCollection, wrap
  10. import string
  11. import util
  12. 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'
  13.  
  14. try:
  15.     win32com.client.gencache.EnsureDispatch('PyCOMVBTest.Tester')
  16. except pythoncom.com_error:
  17.     raise RuntimeError, importMsg
  18.  
  19. import traceback
  20. useDispatcher = None
  21. error = 'VB Test Error'
  22.  
  23. class TestObject:
  24.     _public_methods_ = [
  25.         'CallbackVoidOneByRef',
  26.         'CallbackResultOneByRef',
  27.         'CallbackVoidTwoByRef',
  28.         'CallbackString',
  29.         'CallbackResultOneByRefButReturnNone',
  30.         'CallbackVoidOneByRefButReturnNone',
  31.         'CallbackArrayResult',
  32.         'CallbackArrayResultOneArrayByRef',
  33.         'CallbackArrayResultWrongSize']
  34.     
  35.     def CallbackVoidOneByRef(self, intVal):
  36.         return intVal + 1
  37.  
  38.     
  39.     def CallbackResultOneByRef(self, intVal):
  40.         return (intVal, intVal + 1)
  41.  
  42.     
  43.     def CallbackVoidTwoByRef(self, int1, int2):
  44.         return (int1 + int2, int1 - int2)
  45.  
  46.     
  47.     def CallbackString(self, strVal):
  48.         return (0, strVal + ' has visited Python')
  49.  
  50.     
  51.     def CallbackArrayResult(self, arrayVal):
  52.         ret = []
  53.         for i in arrayVal:
  54.             ret.append(i + 1)
  55.         
  56.         return ret
  57.  
  58.     
  59.     def CallbackArrayResultWrongSize(self, arrayVal):
  60.         return list(arrayVal[:-1])
  61.  
  62.     
  63.     def CallbackArrayResultOneArrayByRef(self, arrayVal):
  64.         ret = []
  65.         for i in arrayVal:
  66.             ret.append(i + 1)
  67.         
  68.         return (list(arrayVal), ret)
  69.  
  70.     
  71.     def CallbackResultOneByRefButReturnNone(self, intVal):
  72.         pass
  73.  
  74.     
  75.     def CallbackVoidOneByRefButReturnNone(self, intVal):
  76.         pass
  77.  
  78.  
  79.  
  80. def TestVB(vbtest, bUseGenerated):
  81.     vbtest.LongProperty = -1
  82.     if vbtest.LongProperty != -1:
  83.         raise error, 'Could not set the long property correctly.'
  84.     vbtest.LongProperty != -1
  85.     vbtest.IntProperty = 10
  86.     if vbtest.IntProperty != 10:
  87.         raise error, 'Could not set the integer property correctly.'
  88.     vbtest.IntProperty != 10
  89.     vbtest.VariantProperty = 10
  90.     if vbtest.VariantProperty != 10:
  91.         raise error, 'Could not set the variant integer property correctly.'
  92.     vbtest.VariantProperty != 10
  93.     vbtest.VariantProperty = buffer('raw\x00data')
  94.     if vbtest.VariantProperty != buffer('raw\x00data'):
  95.         raise error, 'Could not set the variant buffer property correctly.'
  96.     vbtest.VariantProperty != buffer('raw\x00data')
  97.     vbtest.StringProperty = 'Hello from Python'
  98.     if vbtest.StringProperty != 'Hello from Python':
  99.         raise error, 'Could not set the string property correctly.'
  100.     vbtest.StringProperty != 'Hello from Python'
  101.     vbtest.VariantProperty = 'Hello from Python'
  102.     if vbtest.VariantProperty != 'Hello from Python':
  103.         raise error, 'Could not set the variant string property correctly.'
  104.     vbtest.VariantProperty != 'Hello from Python'
  105.     vbtest.VariantProperty = (1, 2, 3)
  106.     if vbtest.VariantProperty != (1, 2, 3):
  107.         raise error, "Could not set the variant property to an array of floats correctly - '%s'." % (vbtest.VariantProperty,)
  108.     vbtest.VariantProperty != (1, 2, 3)
  109.     TestArrays(vbtest, bUseGenerated)
  110.     TestStructs(vbtest)
  111.     TestCollections(vbtest)
  112.     if bUseGenerated:
  113.         ob = vbtest.TakeByRefObject(vbtest)
  114.         vbtest.VariantPutref = vbtest
  115.         if vbtest.VariantPutref._oleobj_ != vbtest._oleobj_:
  116.             raise error, 'Could not set the VariantPutref property correctly.'
  117.         vbtest.VariantPutref._oleobj_ != vbtest._oleobj_
  118.         if vbtest.IncrementIntegerParam(1) != 2:
  119.             raise error, 'Could not pass an integer byref'
  120.         vbtest.IncrementIntegerParam(1) != 2
  121.         if vbtest.IncrementVariantParam(1) != 2:
  122.             raise error, 'Could not pass an int VARIANT byref:' + str(vbtest.IncrementVariantParam(1))
  123.         vbtest.IncrementVariantParam(1) != 2
  124.         if vbtest.IncrementVariantParam(1.5) != 2.5:
  125.             raise error, 'Could not pass a float VARIANT byref'
  126.         vbtest.IncrementVariantParam(1.5) != 2.5
  127.         callback_ob = wrap(TestObject(), useDispatcher = useDispatcher)
  128.         vbtest.DoSomeCallbacks(callback_ob)
  129.     
  130.     ret = vbtest.PassIntByVal(1)
  131.     if ret != 2:
  132.         raise error, 'Could not increment the integer - ' + str(ret)
  133.     ret != 2
  134.     TestVBInterface(vbtest)
  135.     if bUseGenerated:
  136.         ret = vbtest.PassIntByRef(1)
  137.         if ret != (1, 2):
  138.             raise error, 'Could not increment the integer - ' + str(ret)
  139.         ret != (1, 2)
  140.     
  141.  
  142.  
  143. def _DoTestCollection(vbtest, col_name, expected):
  144.     
  145.     def _getcount(ob):
  146.         r = getattr(ob, 'Count')
  147.         if type(r) != type(0):
  148.             return r()
  149.         return r
  150.  
  151.     c = getattr(vbtest, col_name)
  152.     check = []
  153.     for item in c:
  154.         check.append(item)
  155.     
  156.     if check != list(expected):
  157.         raise error, "Collection %s didn't have %r (had %r)" % (col_name, expected, check)
  158.     check != list(expected)
  159.     check = []
  160.     for item in c:
  161.         check.append(item)
  162.     
  163.     if check != list(expected):
  164.         raise error, "Collection 2nd time around %s didn't have %r (had %r)" % (col_name, expected, check)
  165.     check != list(expected)
  166.     i = iter(getattr(vbtest, col_name))
  167.     check = []
  168.     for item in i:
  169.         check.append(item)
  170.     
  171.     if check != list(expected):
  172.         raise error, "Collection iterator %s didn't have %r 2nd time around (had %r)" % (col_name, expected, check)
  173.     check != list(expected)
  174.     check = []
  175.     for item in i:
  176.         check.append(item)
  177.     
  178.     if check != []:
  179.         raise error, "2nd time around Collection iterator %s wasn't empty (had %r)" % (col_name, check)
  180.     check != []
  181.     c = getattr(vbtest, col_name)
  182.     if len(c) != _getcount(c):
  183.         raise error, "Collection %s __len__(%r) wasn't==Count(%r)" % (col_name, len(c), _getcount(c))
  184.     len(c) != _getcount(c)
  185.     c = getattr(vbtest, col_name)
  186.     check = []
  187.     for i in range(_getcount(c)):
  188.         check.append(c[i])
  189.     
  190.     if check != list(expected):
  191.         raise error, "Collection %s didn't have %r (had %r)" % (col_name, expected, check)
  192.     check != list(expected)
  193.     c = getattr(vbtest, col_name)._NewEnum()
  194.     check = []
  195.     while None:
  196.         n = c.Next()
  197.         if not n:
  198.             break
  199.         
  200.         continue
  201.         if check != list(expected):
  202.             raise error, "Collection %s didn't have %r (had %r)" % (col_name, expected, check)
  203.         check != list(expected)
  204.         return None
  205.  
  206.  
  207. def TestCollections(vbtest):
  208.     _DoTestCollection(vbtest, 'CollectionProperty', [
  209.         1,
  210.         'Two',
  211.         '3'])
  212.     if vbtest.CollectionProperty[0] != 1:
  213.         raise error, 'The CollectionProperty[0] element was not the default value'
  214.     vbtest.CollectionProperty[0] != 1
  215.     _DoTestCollection(vbtest, 'EnumerableCollectionProperty', [])
  216.     vbtest.EnumerableCollectionProperty.Add(1)
  217.     vbtest.EnumerableCollectionProperty.Add('Two')
  218.     vbtest.EnumerableCollectionProperty.Add('3')
  219.     _DoTestCollection(vbtest, 'EnumerableCollectionProperty', [
  220.         1,
  221.         'Two',
  222.         '3'])
  223.  
  224.  
  225. def _DoTestArray(vbtest, data, expected_exception = None):
  226.     
  227.     try:
  228.         vbtest.ArrayProperty = data
  229.         if expected_exception is not None:
  230.             raise error, "Expected '%s'" % expected_exception
  231.         expected_exception is not None
  232.     except expected_exception:
  233.         return None
  234.  
  235.     got = vbtest.ArrayProperty
  236.     if got != data:
  237.         raise error, 'Could not set the array data correctly - got %r, expected %r' % (got, data)
  238.     got != data
  239.  
  240.  
  241. def TestArrays(vbtest, bUseGenerated):
  242.     _DoTestArray(vbtest, ())
  243.     _DoTestArray(vbtest, ((), ()))
  244.     _DoTestArray(vbtest, tuple(range(1, 100)))
  245.     _DoTestArray(vbtest, (1, 2, 3))
  246.     _DoTestArray(vbtest, tuple(string.split('Hello from Python')))
  247.     _DoTestArray(vbtest, (vbtest, vbtest))
  248.     _DoTestArray(vbtest, (1, 2, '3'))
  249.     _DoTestArray(vbtest, (1, (vbtest, vbtest), ('3', '4')))
  250.     _DoTestArray(vbtest, ((1, 2, 3), (4, 5, 6)))
  251.     _DoTestArray(vbtest, ((vbtest, vbtest, vbtest), (vbtest, vbtest, vbtest)))
  252.     arrayData = (((1, 2), (3, 4), (5, 6)), ((7, 8), (9, 10), (11, 12)))
  253.     arrayData = (((vbtest, vbtest), (vbtest, vbtest), (vbtest, vbtest)), ((vbtest, vbtest), (vbtest, vbtest), (vbtest, vbtest)))
  254.     _DoTestArray(vbtest, arrayData)
  255.     _DoTestArray(vbtest, (vbtest, 2, '3'))
  256.     _DoTestArray(vbtest, (1, 2, vbtest))
  257.     expected_exception = None
  258.     arrayData = (((1, 2, 1), (3, 4), (5, 6)), ((7, 8), (9, 10), (11, 12)))
  259.     _DoTestArray(vbtest, arrayData, expected_exception)
  260.     arrayData = (((vbtest, vbtest),), ((vbtest,),))
  261.     _DoTestArray(vbtest, arrayData, expected_exception)
  262.     arrayData = (((1, 2), (3, 4), (5, 6, 8)), ((7, 8), (9, 10), (11, 12)))
  263.     _DoTestArray(vbtest, arrayData, expected_exception)
  264.     callback_ob = wrap(TestObject(), useDispatcher = useDispatcher)
  265.     print "** Expecting a 'ValueError' exception to be printed next:"
  266.     
  267.     try:
  268.         vbtest.DoCallbackSafeArraySizeFail(callback_ob)
  269.     except pythoncom.com_error:
  270.         (hr, msg, exc, arg) = None
  271.  
  272.     if bUseGenerated:
  273.         testData = string.split('Mark was here')
  274.         (resultData, byRefParam) = vbtest.PassSAFEARRAY(testData)
  275.         
  276.         try:
  277.             unicode
  278.         except NameError:
  279.             resultData = map(str, resultData)
  280.             byRefParam = map(str, byRefParam)
  281.  
  282.         if testData != list(resultData):
  283.             raise error, 'The safe array data was not what we expected - got ' + str(resultData)
  284.         testData != list(resultData)
  285.         if testData != list(byRefParam):
  286.             raise error, 'The safe array data was not what we expected - got ' + str(byRefParam)
  287.         testData != list(byRefParam)
  288.         testData = [
  289.             1,
  290.             2,
  291.             3]
  292.         (resultData, byRefParam) = vbtest.PassSAFEARRAYVariant(testData)
  293.         testData = [
  294.             'hi',
  295.             'from',
  296.             'Python']
  297.         (resultData, byRefParam) = vbtest.PassSAFEARRAYVariant(testData)
  298.         testData = [
  299.             1,
  300.             2,
  301.             pythoncom.Unicode('3')]
  302.         (resultData, byRefParam) = vbtest.PassSAFEARRAYVariant(testData)
  303.     
  304.     print 'Array tests passed'
  305.  
  306.  
  307. def TestStructs(vbtest):
  308.     
  309.     try:
  310.         vbtest.IntProperty = 'One'
  311.         raise error, 'Should have failed by now'
  312.     except pythoncom.com_error:
  313.         (hr, desc, exc, argErr) = None
  314.         if hr != winerror.DISP_E_TYPEMISMATCH:
  315.             raise error, 'Expected DISP_E_TYPEMISMATCH'
  316.         hr != winerror.DISP_E_TYPEMISMATCH
  317.  
  318.     s = vbtest.StructProperty
  319.     if s.int_val != 99 or str(s.str_val) != 'hello':
  320.         raise error, 'The struct value was not correct'
  321.     str(s.str_val) != 'hello'
  322.     s.str_val = 'Hi from Python'
  323.     s.int_val = 11
  324.     if s.int_val != 11 or str(s.str_val) != 'Hi from Python':
  325.         raise error, 'The struct value didnt persist!'
  326.     str(s.str_val) != 'Hi from Python'
  327.     if s.sub_val.int_val != 66 or str(s.sub_val.str_val) != 'sub hello':
  328.         raise error, 'The sub-struct value was not correct'
  329.     str(s.sub_val.str_val) != 'sub hello'
  330.     sub = s.sub_val
  331.     sub.int_val = 22
  332.     if sub.int_val != 22:
  333.         print sub.int_val
  334.         raise error, 'The sub-struct value didnt persist!'
  335.     sub.int_val != 22
  336.     if s.sub_val.int_val != 22:
  337.         print s.sub_val.int_val
  338.         raise error, 'The sub-struct value (re-fetched) didnt persist!'
  339.     s.sub_val.int_val != 22
  340.     if s.sub_val.array_val[0].int_val != 0 or str(s.sub_val.array_val[0].str_val) != 'zero':
  341.         print s.sub_val.array_val[0].int_val
  342.         raise error, 'The array element wasnt correct'
  343.     str(s.sub_val.array_val[0].str_val) != 'zero'
  344.     s.sub_val.array_val[0].int_val = 99
  345.     s.sub_val.array_val[1].int_val = 66
  346.     if s.sub_val.array_val[0].int_val != 99 or s.sub_val.array_val[1].int_val != 66:
  347.         print s.sub_val.array_val[0].int_val
  348.         raise error, 'The array element didnt persist.'
  349.     s.sub_val.array_val[1].int_val != 66
  350.     vbtest.StructProperty = s
  351.     s = vbtest.StructProperty
  352.     if s.int_val != 11 or str(s.str_val) != 'Hi from Python':
  353.         raise error, 'After sending to VB, the struct value didnt persist!'
  354.     str(s.str_val) != 'Hi from Python'
  355.     if s.sub_val.array_val[0].int_val != 99:
  356.         raise error, 'After sending to VB, the struct array value didnt persist!'
  357.     s.sub_val.array_val[0].int_val != 99
  358.     import copy
  359.     s2 = copy.copy(s)
  360.     s2.int_val = 123
  361.     s2 = vbtest.GetStructFunc()
  362.     vbtest.SetStructSub(s2)
  363.     s = win32com.client.Record('VBStruct', vbtest)
  364.     s.int_val = -1
  365.     vbtest.SetStructSub(s)
  366.     s_array = vbtest.StructArrayProperty
  367.     vbtest.MakeStructArrayProperty(3)
  368.     s_array = vbtest.StructArrayProperty
  369.     for i in range(len(s_array)):
  370.         pass
  371.     
  372.     
  373.     try:
  374.         s.bad_attribute
  375.         raise RuntimeError, 'Could get a bad attribute'
  376.     except AttributeError:
  377.         pass
  378.  
  379.     m = s.__members__
  380.     print 'Struct/Record tests passed'
  381.  
  382.  
  383. def TestVBInterface(ob):
  384.     t = ob.GetInterfaceTester(2)
  385.     if t.getn() != 2:
  386.         raise error, 'Initial value wrong'
  387.     t.getn() != 2
  388.     t.setn(3)
  389.     if t.getn() != 3:
  390.         raise error, 'New value wrong'
  391.     t.getn() != 3
  392.  
  393.  
  394. def DoTestAll():
  395.     o = win32com.client.Dispatch('PyCOMVBTest.Tester')
  396.     TestVB(o, 1)
  397.     o = win32com.client.dynamic.DumbDispatch('PyCOMVBTest.Tester')
  398.     TestVB(o, 0)
  399.  
  400.  
  401. def TestAll():
  402.     if not __debug__:
  403.         raise RuntimeError, 'This must be run in debug mode - we use assert!'
  404.     __debug__
  405.     
  406.     try:
  407.         DoTestAll()
  408.         print 'All tests appear to have worked!'
  409.     except:
  410.         print 'TestAll() failed!!'
  411.         traceback.print_exc()
  412.  
  413.  
  414.  
  415. def suite():
  416.     import unittest
  417.     test = util.CapturingFunctionTestCase(TestAll, description = 'VB tests')
  418.     suite = unittest.TestSuite()
  419.     suite.addTest(test)
  420.     return suite
  421.  
  422. if __name__ == '__main__':
  423.     util.testmain()
  424.  
  425.