PageRenderTime 57ms CodeModel.GetById 2ms app.highlight 49ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Languages/IronPython/Tests/interop/com/dlrcomlib/paramsinretval.py

#
Python | 455 lines | 349 code | 47 blank | 59 comment | 61 complexity | df28e3dc280c2631fcf1abef1d70b758 MD5 | raw file
  1#####################################################################################
  2#
  3#  Copyright (c) Microsoft Corporation. All rights reserved.
  4#
  5# This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
  6# copy of the license can be found in the License.html file at the root of this distribution. If 
  7# you cannot locate the  Apache License, Version 2.0, please send an email to 
  8# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
  9# by the terms of the Apache License, Version 2.0.
 10#
 11# You must not remove this notice, or any other, from this software.
 12#
 13#
 14#####################################################################################
 15
 16# COM Interop tests for IronPython
 17from iptest.assert_util import skiptest
 18skiptest("silverlight")
 19from iptest.cominterop_util import *
 20from clr import StrongBox
 21from System.Runtime.InteropServices import ErrorWrapper
 22
 23if is_cli:
 24    from System import DateTime, TimeSpan, Reflection, Int32
 25    from System.Runtime.InteropServices import COMException
 26
 27###############################################################################
 28##GLOBALS######################################################################
 29###############################################################################
 30
 31com_type_name = "DlrComLibrary.ParamsInRetval"
 32com_obj = getRCWFromProgID(com_type_name)
 33
 34
 35###############################################################################
 36##HELPERS######################################################################
 37###############################################################################
 38
 39def testhelper(function, values, equality_func=AreEqual):
 40    for i in xrange(len(values)):
 41        try:
 42            t_val = function(values[i])
 43        except Exception, e:
 44            print "FAILED trying to pass", values[i], "of type", type(values[i]) ,"to", function#.__name__
 45            raise e
 46        
 47        for j in xrange(i, len(values)):
 48            equality_func(values[i], values[j]) #Make sure no test issues first
 49        
 50            try:
 51                t_val2 = function(values[j])
 52            except Exception, e:
 53                print "FAILED trying to pass", values[j], "of type", type(values[j]) ,"to", function#.__name__
 54                raise e
 55            
 56            equality_func(t_val, t_val2)
 57
 58
 59###############################################################################
 60##SANITY CHECKS################################################################
 61###############################################################################
 62def test_sanity():
 63    #Integer types
 64    AreEqual(com_obj.mVariantBool(True), True)
 65    AreEqual(com_obj.mByte(System.Byte.MinValue), System.Byte.MinValue)
 66    AreEqual(com_obj.mChar(System.SByte.MinValue), System.SByte.MinValue)
 67    AreEqual(com_obj.mShort(System.Int16.MinValue), System.Int16.MinValue)
 68    AreEqual(com_obj.mUShort(System.UInt16.MinValue), System.UInt16.MinValue)
 69    AreEqual(com_obj.mLong(System.Int32.MinValue), System.Int32.MinValue)
 70    AreEqual(com_obj.mUlong(System.UInt32.MinValue), System.UInt32.MinValue)
 71    AreEqual(com_obj.mLongLong(System.Int64.MinValue), System.Int64.MinValue)
 72    AreEqual(com_obj.mULongLong(System.UInt64.MinValue), System.UInt64.MinValue)
 73    
 74    #Float types
 75    AreEqual(com_obj.mDouble(System.Double(3.14)), 3.14)
 76    AreEqual(com_obj.mFloat(System.Single(2.0)), 2.0)     
 77    
 78    #Complex types
 79    AreEqual(com_obj.mCy(System.Decimal(3)), 3)
 80    tempDate = System.DateTime.Now
 81    AreEqual(str(com_obj.mDate(tempDate)), str(tempDate))
 82    AreEqual(com_obj.mVariant(System.Single(4.0)), 4.0)   
 83
 84    #Ole types
 85    #mOleTristate
 86    #mOleColor
 87    #mOleXposHimetric
 88    #mOleYposHimetric
 89    #mOleXsizeHimetric
 90    #mOleYsizeHimetric
 91    #mOleXposPixels
 92    #mOleYposPixels
 93    #mOleXsizePixels
 94    #mOleYsizePixels
 95    #mOleHandle
 96    #mOleOptExclusive
 97
 98    
 99    
100def test_will_not_fix():
101    '''
102    The following probably should not work but does and 
103    won't be fixed by the DLR.
104    '''
105    AreEqual(com_obj.mByte("123"),  System.Byte(123))
106    AreEqual(com_obj.mChar("123"),  System.SByte(123))
107    AreEqual(com_obj.mFloat("123"), 123.0)
108    AreEqual(com_obj.mBstr(object()), 'System.Object')
109    AreEqual(type(com_obj.mIDispatch(object())), object)
110    AreEqual(com_obj.mByte(None), System.Byte(0))
111    AreEqual(com_obj.mFloat(None), 0.0)
112    AreEqual(com_obj.mBstr(3.14), "3.14")
113    
114def test_sanity_int_types_broken():
115    a = StrongBox[ErrorWrapper](ErrorWrapper(System.Int32.MinValue))
116    AreEqual(com_obj.mScode(a), System.Int32.MinValue)
117    
118    a = ErrorWrapper(5)
119    AreEqual(com_obj.mScode(a), 5)
120    
121    AssertErrorWithMessage(ValueError, "Could not convert argument 0 for call to mScode.",
122                           com_obj.mScode, 0)
123    
124        
125###############################################################################
126##SIMPLE TYPES#################################################################
127'''
128X    [id(32), helpstring("method mVariantBool")] HRESULT mVariantBool([in] VARIANT_BOOL a, [out,retval] VARIANT_BOOL* b);
129X    [id(1), helpstring("method mBstr")] HRESULT mBstr([in] BSTR a, [out,retval] BSTR* b);
130X    [id(2), helpstring("method mByte")] HRESULT mByte([in] BYTE a, [out,retval] BYTE* b);
131X    [id(3), helpstring("method mChar")] HRESULT mChar([in] CHAR a, [out,retval] CHAR* b);
132X    [id(6), helpstring("method mDouble")] HRESULT mDouble([in] DOUBLE a, [out,retval] DOUBLE* b);
133X    [id(7), helpstring("method mFloat")] HRESULT mFloat([in] FLOAT a, [out,retval] FLOAT* b);
134
135X    [id(30), helpstring("method mUShort")] HRESULT mUShort([in] USHORT a, [out,retval] USHORT* b);
136X    [id(28), helpstring("method mUlong")] HRESULT mUlong([in] ULONG a, [out,retval] ULONG* b);
137X    [id(29), helpstring("method mULongLong")] HRESULT mULongLong([in] ULONGLONG a, [out,retval] ULONGLONG* b);
138
139X    [id(27), helpstring("method mShort")] HRESULT mShort([in] SHORT a, [out,retval] SHORT* b);
140X    [id(12), helpstring("method mLong")] HRESULT mLong([in] LONG a, [out,retval] LONG* b);
141X    [id(13), helpstring("method mLongLong")] HRESULT mLongLong([in] LONGLONG a, [out,retval] LONGLONG* b);   
142'''
143
144#------------------------------------------------------------------------------
145def test_variant_bool():
146    for test_list in pythonToCOM("VARIANT_BOOL"):
147        testhelper(com_obj.mVariantBool, test_list)
148
149def test_variant_bool_typeerrror():
150    for val in typeErrorTrigger("VARIANT_BOOL"):
151        AssertError(TypeError, com_obj.mVariantBool, val)
152
153def test_variant_bool_valueerrror():
154    for val in valueErrorTrigger("VARIANT_BOOL"):
155        AssertError(ValueError, com_obj.mVariantBool, val)
156        
157def test_variant_bool_overflowerror():
158    for val in overflowErrorTrigger("VARIANT_BOOL"):
159        AssertError(OverflowError, com_obj.mVariantBool, val)
160        
161#------------------------------------------------------------------------------
162def test_byte():
163    for test_list in pythonToCOM("BYTE"):
164        testhelper(com_obj.mByte, test_list)
165
166def test_byte_typeerrror():
167    for val in typeErrorTrigger("BYTE"):
168        AssertError(TypeError, com_obj.mByte, val)
169
170def test_byte_valueerrror():
171    for val in valueErrorTrigger("BYTE"):
172        AssertError(ValueError, com_obj.mByte, val)
173        
174def test_byte_overflowerror():
175    for val in overflowErrorTrigger("BYTE"):
176        AssertError(OverflowError, com_obj.mByte, val)
177
178#------------------------------------------------------------------------------
179def test_bstr():
180    for test_list in pythonToCOM("BSTR"):
181        testhelper(com_obj.mBstr, test_list)
182
183def test_bstr_typeerrror():
184    for val in typeErrorTrigger("BSTR"):
185        AssertError(TypeError, com_obj.mBstr, val)
186
187def test_bstr_valueerrror():
188    for val in valueErrorTrigger("BSTR"):
189        AssertError(ValueError, com_obj.mBstr, val)
190
191def test_bstr_overflowerror():
192    for val in overflowErrorTrigger("BSTR"):
193        AssertError(OverflowError, com_obj.mBstr, val)
194
195#------------------------------------------------------------------------------
196def test_char():
197    for test_list in pythonToCOM("CHAR"):
198        testhelper(com_obj.mChar, test_list)
199
200def test_char_typeerrror():
201    for val in typeErrorTrigger("CHAR"):
202        AssertError(TypeError, com_obj.mChar, val)
203
204def test_char_valueerrror():
205    for val in valueErrorTrigger("CHAR"):
206        AssertError(ValueError, com_obj.mChar, val)
207
208def test_char_overflowerror():
209    for val in overflowErrorTrigger("CHAR"):
210        AssertError(OverflowError, com_obj.mChar, val)
211
212#------------------------------------------------------------------------------
213def test_float():
214    for test_list in pythonToCOM("FLOAT"):
215        testhelper(com_obj.mFloat, test_list, equality_func=AlmostEqual)
216
217    #Min/Max float values
218	AssertError(OverflowError,com_obj.mFloat, -3.402823e+039) 
219	AssertError(OverflowError,com_obj.mFloat, 3.402823e+039) 
220
221def test_float_typeerrror():
222    for val in typeErrorTrigger("FLOAT"):
223        AssertError(TypeError, com_obj.mFloat, val)
224
225def test_float_valueerrror():
226    for val in valueErrorTrigger("FLOAT"):
227        AssertError(ValueError, com_obj.mFloat, val)
228
229def test_float_overflowerror():
230    for val in overflowErrorTrigger("FLOAT"):
231        AssertError(OverflowError, com_obj.mFloat, val)
232
233#------------------------------------------------------------------------------
234def test_double():
235    for test_list in pythonToCOM("DOUBLE"):
236        testhelper(com_obj.mDouble, test_list, equality_func=AlmostEqual)
237
238    #Min/Max double values
239    Assert(str(com_obj.mDouble(-1.797693134864e+309)), "-1.#INF") 
240    Assert(str(com_obj.mDouble(1.797693134862313e309)), "1.#INF")
241
242def test_double_typeerrror():
243    for val in typeErrorTrigger("DOUBLE"):
244        AssertError(TypeError, com_obj.mDouble, val)
245
246def test_double_valueerrror():
247    for val in valueErrorTrigger("DOUBLE"):
248        AssertError(ValueError, com_obj.mDouble, val)
249
250def test_double_overflowerror():
251    for val in overflowErrorTrigger("DOUBLE"):
252        AssertError(OverflowError, com_obj.mDouble, val)
253
254#------------------------------------------------------------------------------
255def test_ushort():
256    for test_list in pythonToCOM("USHORT"):
257        testhelper(com_obj.mUShort, test_list)
258
259def test_ushort_typeerrror():
260    for val in typeErrorTrigger("USHORT"):
261        AssertError(TypeError, com_obj.mUShort, val)
262
263def test_ushort_valueerrror():
264    for val in valueErrorTrigger("USHORT"):
265        AssertError(ValueError, com_obj.mUShort, val)
266
267def test_ushort_overflowerror():
268    for val in overflowErrorTrigger("USHORT"):
269        AssertError(OverflowError, com_obj.mUShort, val)
270
271#------------------------------------------------------------------------------
272def test_ulong():
273    for test_list in pythonToCOM("ULONG"):
274        testhelper(com_obj.mUlong, test_list)
275
276def test_ulong_typeerrror():
277    for val in typeErrorTrigger("ULONG"):
278        AssertError(TypeError, com_obj.mUlong, val)
279
280def test_ulong_valueerrror():
281    for val in valueErrorTrigger("ULONG"):
282        AssertError(ValueError, com_obj.mUlong, val)
283
284def test_ulong_overflowerror():
285    for val in overflowErrorTrigger("ULONG"):
286        AssertError(OverflowError, com_obj.mUlong, val)
287
288#------------------------------------------------------------------------------
289#@disabled("Dev10 409933")
290#Hack: b/c PY converts long numeric literals to bigints which has different coercion rules than .net does
291#we'll just be using the ulong literals instead
292def test_ulonglong():
293    for test_list in pythonToCOM("ULONG"):  
294        testhelper(com_obj.mULongLong, test_list)
295
296def test_ulonglong_typeerrror():
297    for val in typeErrorTrigger("ULONGLONG"):
298        AssertError(TypeError, com_obj.mULongLong, val)
299
300def test_ulonglong_valueerrror():
301    for val in valueErrorTrigger("ULONGLONG"):
302        AssertError(ValueError, com_obj.mULongLong, val)
303
304def test_ulonglong_overflowerror():
305    for val in overflowErrorTrigger("ULONGLONG"):
306        AssertError(OverflowError, com_obj.mULongLong, val)
307
308#------------------------------------------------------------------------------
309def test_short():
310    for test_list in pythonToCOM("SHORT"):
311        testhelper(com_obj.mShort, test_list)
312
313def test_short_typeerrror():
314    for val in typeErrorTrigger("SHORT"):
315        AssertError(TypeError, com_obj.mShort, val)
316
317def test_short_valueerrror():
318    for val in valueErrorTrigger("SHORT"):
319        AssertError(ValueError, com_obj.mShort, val)
320
321def test_short_overflowerror():
322    for val in overflowErrorTrigger("SHORT"):
323        AssertError(OverflowError, com_obj.mShort, val)
324
325#------------------------------------------------------------------------------
326def test_long():
327    for test_list in pythonToCOM("LONG"):
328        testhelper(com_obj.mLong, test_list)
329
330def test_long_typeerrror():
331    for val in typeErrorTrigger("LONG"):
332        AssertError(TypeError, com_obj.mLong, val)
333
334def test_long_valueerrror():
335    for val in valueErrorTrigger("LONG"):
336        AssertError(ValueError, com_obj.mLong, val)
337
338def test_long_overflowerror():
339    for val in overflowErrorTrigger("LONG"):
340        AssertError(OverflowError, com_obj.mLong, val)
341
342#------------------------------------------------------------------------------
343#@disabled("Dev10 409933")
344#Hack: b/c PY converts long numeric literals to bigints which has different coercion rules than .net does
345#we'll just be using the long literals instead
346def test_longlong():
347    for test_list in pythonToCOM("LONG"):
348        testhelper(com_obj.mLongLong, test_list)
349
350def test_longlong_typeerrror():
351    for val in typeErrorTrigger("LONGLONG"):
352        AssertError(TypeError, com_obj.mLongLong, val)
353
354def test_longlong_valueerrror():
355    for val in valueErrorTrigger("LONGLONG"):
356        AssertError(ValueError, com_obj.mLongLong, val)
357
358def test_longlong_overflowerror():
359    for val in overflowErrorTrigger("LONGLONG"):
360        AssertError(OverflowError, com_obj.mLongLong, val)
361
362
363###############################################################################
364##INTERFACE TYPES##############################################################
365###############################################################################
366'''
367    [id(8), helpstring("method mIDispatch")] HRESULT mIDispatch([in] IDispatch* a, [out,retval] IDispatch** b);
368    [id(9), helpstring("method mIFontDisp")] HRESULT mIFontDisp([in] IFontDisp* a, [out,retval] IDispatch** b);
369    [id(10), helpstring("method mIPictureDisp")] HRESULT mIPictureDisp([in] IPictureDisp* a, [out,retval] IDispatch** b);
370    [id(11), helpstring("method mIUnknown")] HRESULT mIUnknown([in] IUnknown* a, [out,retval] IUnknown** b);
371'''
372    
373def test_interface_types():   
374    '''
375    TODO:
376    - mIFontDisp 
377    - mIPictureDisp
378    '''
379    AreEqual(com_obj.mIDispatch(com_obj), com_obj)
380    AreEqual(com_obj.mIUnknown(com_obj), com_obj)
381    
382    AssertError(ValueError, com_obj.mIUnknown, None) # DISP_E_TYPEMISMATCH when using VT_EMPTY
383    
384    AssertError(ValueError, com_obj.mIUnknown, 3j)   # Dev10 409936   
385    AssertError(ValueError, com_obj.mIUnknown, "")
386    AssertError(ValueError, com_obj.mIUnknown, 123)
387	
388def test_interface_types_typerror():
389    '''
390    TODO:
391    - mIFontDisp 
392    - mIPictureDisp
393    '''
394    if is_net40:
395        print "http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=26000"
396        return
397    test_cases = shallow_copy(NON_NUMBER_VALUES)
398    test_cases = [x for x in test_cases if type(x)!=object and type(x)!=KOld and type(x)!=KNew]
399    
400    for val in test_cases:
401        AssertError(TypeError, com_obj.mIDispatch, val)
402        com_obj.mIUnknown(val)
403        #AssertError(TypeError, com_obj.mIUnknown, val)
404
405def test_interface_types_valueerror():
406    test_cases = [None]
407    for val in test_cases:
408        AssertError(ValueError, com_obj.mIDispatch, val)
409
410###############################################################################
411##COMPLEX TYPES################################################################
412###############################################################################
413'''
414    [id(31), helpstring("method mVariant")] HRESULT mVariant([in] VARIANT a, [out,retval] VARIANT* b);
415    [id(4), helpstring("method mCy")] HRESULT mCy([in] CY a, CY* b);
416    [id(5), helpstring("method mDate")] HRESULT mDate([in] DATE a, [out,retval] DATE* b);
417    [id(26), helpstring("method mScode")] HRESULT mScode([in] SCODE a, [out,retval] SCODE* b);
418'''    
419
420
421###############################################################################
422##OLE TYPES####################################################################
423###############################################################################
424'''
425    [id(14), helpstring("method mOleColor")] HRESULT mOleColor([in] OLE_COLOR a, [out,retval] OLE_COLOR* b);
426    [id(15), helpstring("method mOleXposHimetric")] HRESULT mOleXposHimetric([in] OLE_XPOS_HIMETRIC a, [out,retval] OLE_XPOS_HIMETRIC* b);
427    [id(16), helpstring("method mOleYposHimetric")] HRESULT mOleYposHimetric([in] OLE_YPOS_HIMETRIC a, [out,retval] OLE_YPOS_HIMETRIC* b);
428    [id(17), helpstring("method mOleXsizeHimetric")] HRESULT mOleXsizeHimetric([in] OLE_XSIZE_HIMETRIC a, [out,retval] OLE_XSIZE_HIMETRIC* b);
429    [id(18), helpstring("method mOleYsizeHimetric")] HRESULT mOleYsizeHimetric([in] OLE_YSIZE_HIMETRIC a, [out,retval] OLE_YSIZE_HIMETRIC* b);
430    [id(19), helpstring("method mOleXposPixels")] HRESULT mOleXposPixels([in] OLE_XPOS_PIXELS a, [out,retval] OLE_XPOS_PIXELS* b);
431    [id(20), helpstring("method mOleYposPixels")] HRESULT mOleYposPixels([in] OLE_YPOS_PIXELS a, [out,retval] OLE_YPOS_PIXELS* b);
432    [id(21), helpstring("method mOleXsizePixels")] HRESULT mOleXsizePixels([in] OLE_XSIZE_PIXELS a, [out,retval] OLE_XSIZE_PIXELS* b);
433    [id(22), helpstring("method mOleYsizePixels")] HRESULT mOleYsizePixels([in] OLE_YSIZE_PIXELS a, [out,retval] OLE_YSIZE_PIXELS* b);
434    [id(23), helpstring("method mOleHandle")] HRESULT mOleHandle([in] OLE_HANDLE a, [out,retval] OLE_HANDLE* b);
435    [id(24), helpstring("method mOleOptExclusive")] HRESULT mOleOptExclusive([in] OLE_OPTEXCLUSIVE a, [out,retval] OLE_OPTEXCLUSIVE* b);
436    [id(25), helpstring("method mOleTristate")] HRESULT mOleTristate([in] enum OLE_TRISTATE a, [out,retval] enum OLE_TRISTATE* b);
437'''    
438
439
440###############################################################################
441##MISC#########################################################################
442###############################################################################
443def test_misc():
444    AreEqual(com_obj.mCy(100), 100)
445    now = DateTime.Now
446    AreEqual(com_obj.mDate(now).ToOADate(), now.ToOADate())
447    
448    
449    
450
451
452###############################################################################
453##MAIN#########################################################################
454###############################################################################
455run_com_test(__name__, __file__)