PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

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