PageRenderTime 55ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/Languages/IronPython/Tests/test_methoddispatch.py

http://github.com/IronLanguages/main
Python | 1437 lines | 1104 code | 185 blank | 148 comment | 31 complexity | 00a5f591412c48c8d81a5f8539e32e58 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception

Large files files are truncated, but you can click here to view the full 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. from iptest.assert_util import *
  16. skiptest("win32")
  17. import clr
  18. import System
  19. load_iron_python_test()
  20. from IronPythonTest import *
  21. #A few minimal checks for error messages - using bad exact match rules
  22. #???These tests should be rewritten to capture essence of errors without details
  23. def test_sanity():
  24. md = MixedDispatch("hi")
  25. AssertErrorWithMessage(TypeError, "Combine() takes exactly 2 arguments (1 given)", MixedDispatch.Combine, md)
  26. AssertErrorWithMessage(TypeError, "Combine() takes at least 1 argument (0 given)", md.Combine)
  27. md.Combine(md)
  28. x = BindingTestClass.Bind("Hello")
  29. Assert(x == "Hello")
  30. x = BindingTestClass.Bind(10)
  31. Assert(x == 10)
  32. x = BindingTestClass.Bind(False)
  33. Assert(x == False)
  34. b = InheritedBindingSub()
  35. Assert(b.Bind(True) == "Subclass bool")
  36. Assert(b.Bind("Hi") == "Subclass string")
  37. Assert(b.Bind(10) == "Subclass int")
  38. x = "this is a string"
  39. y = x.Split(' ')
  40. Assert(y[0] == "this")
  41. Assert(y[1] == "is")
  42. Assert(y[2] == "a")
  43. Assert(y[3] == "string")
  44. def verify_complex(x, xx):
  45. Assert(x.Real == xx.real)
  46. Assert(x.Imag == xx.imag)
  47. i = Cmplx(3, 4)
  48. ii = (3 + 4j)
  49. j = Cmplx(2, 1)
  50. jj = (2 + 1j)
  51. verify_complex(i, ii)
  52. verify_complex(j, jj)
  53. verify_complex(i + j, ii + jj)
  54. verify_complex(i - j, ii - jj)
  55. verify_complex(i * j, ii * jj)
  56. verify_complex(i / j, ii / jj)
  57. verify_complex(i + 2.5, ii + 2.5)
  58. verify_complex(i - 2.5, ii - 2.5)
  59. verify_complex(i * 2.5, ii * 2.5)
  60. verify_complex(i / 2.5, ii / 2.5)
  61. verify_complex(2.5 + j, 2.5 + jj)
  62. verify_complex(2.5 - j, 2.5 - jj)
  63. verify_complex(2.5 * j, 2.5 * jj)
  64. verify_complex(2.5 / j, 2.5 / jj)
  65. verify_complex(i + 2, ii + 2)
  66. verify_complex(i - 2, ii - 2)
  67. verify_complex(i * 2, ii * 2)
  68. verify_complex(i / 2, ii / 2)
  69. verify_complex(2 + j, 2 + jj)
  70. verify_complex(2 - j, 2 - jj)
  71. verify_complex(2 * j, 2 * jj)
  72. verify_complex(2 / j, 2 / jj)
  73. verify_complex(-i, -ii)
  74. verify_complex(-j, -jj)
  75. i *= j
  76. ii *= jj
  77. verify_complex(i, ii)
  78. i /= j
  79. ii /= jj
  80. verify_complex(i, ii)
  81. i += j
  82. ii += jj
  83. verify_complex(i, ii)
  84. i -= j
  85. ii -= jj
  86. verify_complex(i, ii)
  87. i -= 2
  88. ii -= 2
  89. verify_complex(i, ii)
  90. i += 2
  91. ii += 2
  92. verify_complex(i, ii)
  93. i *= 2
  94. ii *= 2
  95. verify_complex(i, ii)
  96. i /= 2
  97. ii /= 2
  98. verify_complex(i, ii)
  99. class D(Infinite):
  100. pass
  101. class E(D):
  102. def __cmp__(self, other):
  103. return super(E, self).__cmp__(other)
  104. e = E()
  105. result = E.__cmp__(e, 20)
  106. retuls = e.__cmp__(20)
  107. class F(Infinite):
  108. def __cmp__(self, other):
  109. return super(F, self).__cmp__(other)
  110. f = F()
  111. result = F.__cmp__(f, 20)
  112. result = f.__cmp__(20)
  113. @skip("silverlight")
  114. def test_system_drawing():
  115. if is_netstandard:
  116. clr.AddReference("System.Drawing.Primitives")
  117. else:
  118. clr.AddReference("System.Drawing")
  119. from System.Drawing import Rectangle
  120. r = Rectangle(0, 0, 3, 7)
  121. s = Rectangle(3, 0, 8, 14)
  122. # calling the static method
  123. i = Rectangle.Intersect(r, s)
  124. AreEqual(i, Rectangle(3, 0, 0, 7))
  125. AreEqual(r, Rectangle(0, 0, 3, 7))
  126. AreEqual(s, Rectangle(3, 0, 8, 14))
  127. # calling the instance
  128. i = r.Intersect(s)
  129. AreEqual(i, None)
  130. AreEqual(r, Rectangle(3, 0, 0, 7))
  131. AreEqual(s, Rectangle(3, 0, 8, 14))
  132. # calling instance w/ StrongBox
  133. r = Rectangle(0, 0, 3, 7)
  134. box = clr.StrongBox[Rectangle](r)
  135. i = box.Intersect(s)
  136. AreEqual(i, None)
  137. AreEqual(box.Value, Rectangle(3, 0, 0, 7))
  138. AreEqual(s, Rectangle(3, 0, 8, 14))
  139. # should be able to access properties through the box
  140. AreEqual(box.X, 3)
  141. # multiple sites should produce the same function
  142. i = box.Intersect
  143. j = box.Intersect
  144. def test_io_memorystream():
  145. s = System.IO.MemoryStream()
  146. a = System.Array.CreateInstance(System.Byte, 10)
  147. b = System.Array.CreateInstance(System.Byte, a.Length)
  148. for i in range(a.Length):
  149. a[i] = a.Length - i
  150. s.Write(a, 0, a.Length)
  151. result = s.Seek(0, System.IO.SeekOrigin.Begin)
  152. r = s.Read(b, 0, b.Length)
  153. Assert(r == b.Length)
  154. for i in range(a.Length):
  155. AreEqual(a[i], b[i])
  156. def test_types():
  157. global type
  158. BoolType = (System.Boolean, BindTest.BoolValue, BindResult.Bool)
  159. ByteType = (System.Byte, BindTest.ByteValue, BindResult.Byte)
  160. CharType = (System.Char, BindTest.CharValue, BindResult.Char)
  161. DecimalType = (System.Decimal, BindTest.DecimalValue, BindResult.Decimal)
  162. DoubleType = (System.Double, BindTest.DoubleValue, BindResult.Double)
  163. FloatType = (System.Single, BindTest.FloatValue, BindResult.Float)
  164. IntType = (System.Int32, BindTest.IntValue, BindResult.Int)
  165. LongType = (System.Int64, BindTest.LongValue, BindResult.Long)
  166. ObjectType = (System.Object, BindTest.ObjectValue, BindResult.Object)
  167. SByteType = (System.SByte, BindTest.SByteValue, BindResult.SByte)
  168. ShortType = (System.Int16, BindTest.ShortValue, BindResult.Short)
  169. StringType = (System.String, BindTest.StringValue, BindResult.String)
  170. UIntType = (System.UInt32, BindTest.UIntValue, BindResult.UInt)
  171. ULongType = (System.UInt64, BindTest.ULongValue, BindResult.ULong)
  172. UShortType = (System.UInt16, BindTest.UShortValue, BindResult.UShort)
  173. saveType = type
  174. for binding in [BoolType, ByteType, CharType, DecimalType, DoubleType,
  175. FloatType, IntType, LongType, ObjectType, SByteType,
  176. ShortType, StringType, UIntType, ULongType, UShortType]:
  177. type = binding[0]
  178. value = binding[1]
  179. expect = binding[2]
  180. # Select using System.Type object
  181. select = BindTest.Bind.Overloads[type]
  182. result = select(value)
  183. AreEqual(expect, result)
  184. # Select using ReflectedType object
  185. select = BindTest.Bind.Overloads[type]
  186. result = select(value)
  187. AreEqual(expect, result)
  188. # Make simple call
  189. result = BindTest.Bind(value)
  190. if not binding is CharType:
  191. AreEqual(expect, result)
  192. result, output = BindTest.BindRef(value)
  193. if not binding is CharType:
  194. AreEqual(expect | BindResult.Ref, result)
  195. # MakeArrayType and MakeByRefType are SecurityCritical and thus cannot be called via reflection in silverlight,
  196. # so disable these in interpreted mode.
  197. if not (is_silverlight):
  198. # Select using Array type
  199. arrtype = System.Type.MakeArrayType(type)
  200. select = BindTest.Bind.Overloads[arrtype]
  201. array = System.Array.CreateInstance(type, 1)
  202. array[0] = value
  203. result = select(array)
  204. AreEqual(expect | BindResult.Array, result)
  205. # Select using ByRef type
  206. reftype = System.Type.MakeByRefType(type)
  207. select = BindTest.Bind.Overloads[reftype]
  208. result, output = select()
  209. AreEqual(expect | BindResult.Out, result)
  210. select = BindTest.BindRef.Overloads[reftype]
  211. result, output = select(value)
  212. AreEqual(expect | BindResult.Ref, result)
  213. type = saveType
  214. select = BindTest.Bind.Overloads[()]
  215. result = select()
  216. AreEqual(BindResult.None, result)
  217. def test_enum():
  218. class MyEnumTest(EnumTest):
  219. def TestDaysInt(self):
  220. return DaysInt.Weekdays
  221. def TestDaysShort(self):
  222. return DaysShort.Weekdays
  223. def TestDaysLong(self):
  224. return DaysLong.Weekdays
  225. def TestDaysSByte(self):
  226. return DaysSByte.Weekdays
  227. def TestDaysByte(self):
  228. return DaysByte.Weekdays
  229. def TestDaysUShort(self):
  230. return DaysUShort.Weekdays
  231. def TestDaysUInt(self):
  232. return DaysUInt.Weekdays
  233. def TestDaysULong(self):
  234. return DaysULong.Weekdays
  235. et = MyEnumTest()
  236. AreEqual(et.TestDaysInt(), DaysInt.Weekdays)
  237. AreEqual(et.TestDaysShort(), DaysShort.Weekdays)
  238. AreEqual(et.TestDaysLong(), DaysLong.Weekdays)
  239. AreEqual(et.TestDaysSByte(), DaysSByte.Weekdays)
  240. AreEqual(et.TestDaysByte(), DaysByte.Weekdays)
  241. AreEqual(et.TestDaysUShort(), DaysUShort.Weekdays)
  242. AreEqual(et.TestDaysUInt(), DaysUInt.Weekdays)
  243. AreEqual(et.TestDaysULong(), DaysULong.Weekdays)
  244. for l in range(10):
  245. a = System.Array.CreateInstance(str, l)
  246. r = []
  247. for i in range(l):
  248. a[i] = "ip" * i
  249. r.append("IP" * i)
  250. m = map(str.upper, a)
  251. AreEqual(m, r)
  252. methods = [
  253. MyEnumTest.TestEnumInt,
  254. MyEnumTest.TestEnumShort,
  255. MyEnumTest.TestEnumLong,
  256. MyEnumTest.TestEnumSByte,
  257. MyEnumTest.TestEnumUInt,
  258. MyEnumTest.TestEnumUShort,
  259. MyEnumTest.TestEnumULong,
  260. MyEnumTest.TestEnumByte,
  261. #MyEnumTest.TestEnumBoolean,
  262. ]
  263. parameters = [
  264. DaysInt.Weekdays,
  265. DaysShort.Weekdays,
  266. DaysLong.Weekdays,
  267. DaysSByte.Weekdays,
  268. DaysByte.Weekdays,
  269. DaysUShort.Weekdays,
  270. DaysUInt.Weekdays,
  271. DaysULong.Weekdays,
  272. ]
  273. for p in parameters:
  274. #No implicit conversions from enum to numeric types are allowed
  275. for m in methods:
  276. AssertError(TypeError, m, p)
  277. x = int(p)
  278. x = bool(p)
  279. ######################################################################################
  280. def Check(flagValue, func, *args):
  281. Dispatch.Flag = 0
  282. func(*args)
  283. Assert(Dispatch.Flag == flagValue)
  284. d = Dispatch()
  285. #======================================================================
  286. # public void M1(int arg) { Flag = 101; }
  287. # public void M1(DispatchHelpers.Color arg) { Flag = 201; }
  288. #======================================================================
  289. Check(101, d.M1, 1)
  290. Check(201, d.M1, DispatchHelpers.Color.Red)
  291. AssertError(TypeError, d.M1, None)
  292. #======================================================================
  293. # public void M2(int arg) { Flag = 102; }
  294. # public void M2(int arg, params int[] arg2) { Flag = 202; }
  295. #======================================================================
  296. Check(102, d.M2, 1)
  297. Check(202, d.M2, 1, 1)
  298. Check(202, d.M2, 1, 1, 1)
  299. Check(202, d.M2, 1, None)
  300. AssertError(TypeError, d.M2, 1, 1, "string", 1)
  301. AssertError(TypeError, d.M2, None, None)
  302. AssertError(TypeError, d.M2, None)
  303. #======================================================================
  304. # public void M3(int arg) { Flag = 103; }
  305. # public void M3(int arg, int arg2) { Flag = 203; }
  306. #======================================================================
  307. AssertError(TypeError, d.M3, DispatchHelpers.Color.Red)
  308. Check(103, d.M3, 1)
  309. Check(203, d.M3, 1, 1)
  310. AssertError(TypeError, d.M3, None, None)
  311. AssertError(TypeError, d.M4, None)
  312. #======================================================================
  313. # public void M4(int arg) { Flag = 104; }
  314. # public void M4(int arg, __arglist) { Flag = 204; }
  315. #======================================================================
  316. AssertError(TypeError, DispatchHelpers.Color.Red)
  317. Check(104, d.M4, 1)
  318. #VarArgs methods can not be called from IronPython by design
  319. AssertError(TypeError, d.M4, 1, 1)
  320. AssertError(TypeError, d.M4, None, None)
  321. AssertError(TypeError, d.M4, None)
  322. #======================================================================
  323. # public void M5(float arg) { Flag = 105; }
  324. # public void M5(double arg) { Flag = 205; }
  325. #======================================================================
  326. #!!! easy way to get M5(float) invoked
  327. Check(105, d.M5, System.Single.Parse("3.14"))
  328. Check(205, d.M5, 3.14)
  329. AssertError(TypeError, d.M5, None)
  330. #======================================================================
  331. # public void M6(char arg) { Flag = 106; }
  332. # public void M6(string arg) { Flag = 206; }
  333. #======================================================================
  334. #!!! no way to invoke M6(char)
  335. Check(206, d.M6, 'a')
  336. Check(206, d.M6, 'hello')
  337. Check(206, d.M6, 'hello'[0])
  338. Check(206, d.M6, None)
  339. #======================================================================
  340. # public void M7(int arg) { Flag = 107; }
  341. # public void M7(params int[] args) { Flag = 207; }
  342. #======================================================================
  343. Check(207, d.M7)
  344. Check(107, d.M7, 1)
  345. Check(207, d.M7, 1, 1)
  346. Check(207, d.M7, None)
  347. #======================================================================
  348. # public void M8(int arg) { Flag = 108; }
  349. # public void M8(ref int arg) { Flag = 208; arg = 999; }
  350. # public void M10(ref int arg) { Flag = 210; arg = 999; }
  351. #======================================================================
  352. Check(108, d.M8, 1);
  353. Assert(d.M10(1) == 999)
  354. Check(210, d.M10, 1);
  355. AssertError(TypeError, d.M10, None)
  356. #======================================================================
  357. # public void M11(int arg, int arg2) { Flag = 111; }
  358. # public void M11(DispatchHelpers.Color arg, int arg2) { Flag = 211; }
  359. #======================================================================
  360. Check(111, d.M11, 1, 1)
  361. AssertError(TypeError, d.M11, 1, DispatchHelpers.Color.Red)
  362. Check(211, d.M11, DispatchHelpers.Color.Red, 1)
  363. AssertError(TypeError, d.M11, DispatchHelpers.Color.Red, DispatchHelpers.Color.Red)
  364. #======================================================================
  365. # public void M12(int arg, DispatchHelpers.Color arg2) { Flag = 112; }
  366. # public void M12(DispatchHelpers.Color arg, int arg2) { Flag = 212; }
  367. #======================================================================
  368. AssertError(TypeError, d.M12, 1, 1)
  369. Check(112, d.M12, 1, DispatchHelpers.Color.Red)
  370. Check(212, d.M12, DispatchHelpers.Color.Red, 1)
  371. AssertError(TypeError, d.M12, DispatchHelpers.Color.Red, DispatchHelpers.Color.Red)
  372. #======================================================================
  373. # public void M20(DispatchHelpers.B arg) { Flag = 120; }
  374. #======================================================================
  375. Check(120, d.M20, None)
  376. #======================================================================
  377. # public void M22(DispatchHelpers.B arg) { Flag = 122; }
  378. # public void M22(DispatchHelpers.D arg) { Flag = 222; }
  379. #======================================================================
  380. Check(222, d.M22, None)
  381. Check(122, d.M22, DispatchHelpers.B())
  382. Check(222, d.M22, DispatchHelpers.D())
  383. #======================================================================
  384. # public void M23(DispatchHelpers.I arg) { Flag = 123; }
  385. # public void M23(DispatchHelpers.C2 arg) { Flag = 223; }
  386. #======================================================================
  387. Check(123, d.M23, DispatchHelpers.C1())
  388. Check(223, d.M23, DispatchHelpers.C2())
  389. #======================================================================
  390. # Bug 20 - public void M50(params DispatchHelpers.B[] args) { Flag = 150; }
  391. #======================================================================
  392. Check(150, d.M50, DispatchHelpers.B())
  393. Check(150, d.M50, DispatchHelpers.D())
  394. Check(150, d.M50, DispatchHelpers.B(), DispatchHelpers.B())
  395. Check(150, d.M50, DispatchHelpers.B(), DispatchHelpers.D())
  396. Check(150, d.M50, DispatchHelpers.D(), DispatchHelpers.D())
  397. #======================================================================
  398. # public void M51(params DispatchHelpers.B[] args) { Flag = 151; }
  399. # public void M51(params DispatchHelpers.D[] args) { Flag = 251; }
  400. #======================================================================
  401. Check(151, d.M51, DispatchHelpers.B())
  402. Check(251, d.M51, DispatchHelpers.D())
  403. Check(151, d.M51, DispatchHelpers.B(), DispatchHelpers.B())
  404. Check(151, d.M51, DispatchHelpers.B(), DispatchHelpers.D())
  405. Check(251, d.M51, DispatchHelpers.D(), DispatchHelpers.D())
  406. #======================================================================
  407. # public void M60(int? arg) { Flag = 160; }
  408. #======================================================================
  409. Check(160, d.M60, 1)
  410. Check(160, d.M60, None)
  411. #======================================================================
  412. # public void M70(Dispatch arg) { Flag = 170; }
  413. #======================================================================
  414. Check(170, d.M70, d)
  415. AssertError(TypeError, Dispatch.M70, d)
  416. AssertError(TypeError, d.M70, d, d)
  417. Check(170, Dispatch.M70, d, d)
  418. #======================================================================
  419. # public static void M71(Dispatch arg) { Flag = 171; }
  420. #======================================================================
  421. Check(171, d.M71, d)
  422. Check(171, Dispatch.M71, d)
  423. AssertError(TypeError, d.M71, d, d)
  424. AssertError(TypeError, Dispatch.M71, d, d)
  425. #======================================================================
  426. # public static void M81(Dispatch arg, int arg2) { Flag = 181; }
  427. # public void M81(int arg) { Flag = 281; }
  428. #======================================================================
  429. AssertError(TypeError, d.M81, d, 1)
  430. Check(181, Dispatch.M81, d, 1)
  431. Check(281, d.M81, 1)
  432. AssertError(TypeError, Dispatch.M81, 1)
  433. #======================================================================
  434. # public static void M82(bool arg) { Flag = 182; }
  435. # public static void M82(string arg) { Flag = 282; }
  436. #======================================================================
  437. Check(182, d.M82, True)
  438. Check(282, d.M82, "True")
  439. Check(182, Dispatch.M82, True)
  440. Check(282, Dispatch.M82, "True")
  441. #======================================================================
  442. # public void M83(bool arg) { Flag = 183; }
  443. # public void M83(string arg) { Flag = 283; }
  444. #======================================================================
  445. Check(183, d.M83, True)
  446. Check(283, d.M83, "True")
  447. AssertError(TypeError, Dispatch.M83, True)
  448. AssertError(TypeError, Dispatch.M83, "True")
  449. AssertError(TypeError, d.M83, d, True)
  450. AssertError(TypeError, d.M83, d, "True")
  451. Check(183, Dispatch.M83, d, True)
  452. Check(283, Dispatch.M83, d, "True")
  453. #======================================================================
  454. # public void M90<T>(int arg) { Flag = 190; }
  455. #======================================================================
  456. AssertError(TypeError, d.M90, 1)
  457. Check(191, d.M91, 1)
  458. #======================================================================
  459. #======================================================================
  460. d = DispatchDerived()
  461. Check(201, d.M1, 1)
  462. Check(102, d.M2, 1)
  463. Check(202, d.M2, DispatchHelpers.Color.Red)
  464. Check(103, d.M3, 1)
  465. Check(203, d.M3, "hello")
  466. Check(104, d.M4, 100)
  467. Check(204, d.M4, "python")
  468. Check(205, d.M5, 1)
  469. Check(106, d.M6, 1)
  470. #======================================================================
  471. # ConversionDispatch - Test binding List / Tuple to array/enum/IList/ArrayList/etc...
  472. #======================================================================
  473. cd = ConversionDispatch()
  474. ###########################################
  475. # checker functions - verify the result of the test
  476. def Check(res, orig):
  477. if hasattr(res, "__len__"):
  478. AreEqual(len(res), len(orig))
  479. i = 0
  480. for a in res:
  481. AreEqual(a, orig[i])
  482. i = i+1
  483. AreEqual(i, len(orig))
  484. def len_helper(o):
  485. if hasattr(o, 'Count'): return o.Count
  486. return len(o)
  487. def clear_helper(o):
  488. if hasattr(o, 'Clear'):
  489. o.Clear()
  490. else:
  491. del o[:]
  492. def CheckModify(res, orig):
  493. Check(res, orig)
  494. index = len_helper(res)
  495. res.Add(orig[0])
  496. Check(res, orig)
  497. res.RemoveAt(index)
  498. Check(res, orig)
  499. x = res[0]
  500. res.Remove(orig[0])
  501. Check(res, orig)
  502. res.Insert(0, x)
  503. Check(res, orig)
  504. if(hasattr(res, "sort")):
  505. res.sort()
  506. Check(res, orig)
  507. clear_helper(res)
  508. Check(res, orig)
  509. def keys_helper(o):
  510. if hasattr(o, 'keys'): return o.keys()
  511. return o.Keys
  512. def CheckDict(res, orig):
  513. if hasattr(res, "__len__"):
  514. AreEqual(len(res), len(orig))
  515. i = 0
  516. for a in keys_helper(res):
  517. AreEqual(res[a], orig[a])
  518. i = i+1
  519. AreEqual(i, len(orig))
  520. ###################################
  521. # test data sets used for all the checks
  522. # list/tuple data
  523. inttuple = (2,3,4,5)
  524. strtuple = ('a', 'b', 'c', 'd')
  525. othertuple = (['a', 2], ['c', 'd', 3], 5)
  526. intlist = [2,3,4,5]
  527. strlist = ['a', 'b', 'c', 'd']
  528. otherlist = [('a', 2), ('c', 'd', 3), 5]
  529. intdict = {2:5, 7:8, 9:10}
  530. strdict = {'abc': 'def', 'xyz':'abc', 'mno':'prq'}
  531. objdict = { (2,3) : (4,5), (1,2):(3,4), (8,9):(1,4)}
  532. mixeddict = {'abc': 2, 'def': 9, 'qrs': 8}
  533. objFunctions = [cd.Array,cd.ObjIList, cd.Enumerable]
  534. objData = [inttuple, strtuple, othertuple]
  535. intFunctions = [cd.IntEnumerable, cd.IntIList]
  536. intData = [inttuple, intlist]
  537. intTupleFunctions = [cd.IntArray]
  538. intTupleData = [inttuple]
  539. strFunctions = [cd.StringEnumerable, cd.StringIList]
  540. strData = [strtuple, strlist]
  541. strTupleFunctions = [cd.StringArray]
  542. strTupleData = [strtuple]
  543. # dictionary data
  544. objDictFunctions = [cd.DictTest]
  545. objDictData = [intdict, strdict, objdict, mixeddict]
  546. intDictFunctions = [cd.IntDictTest]
  547. intDictData = [intdict]
  548. strDictFunctions = [cd.StringDictTest]
  549. strDictData = [strdict]
  550. mixedDictFunctions = [cd.MixedDictTest]
  551. mixedDictData = [mixeddict]
  552. modCases = [ (cd.ObjIList, (intlist, strlist, otherlist)),
  553. ( cd.IntIList, (intlist,) ),
  554. ( cd.StringIList, (strlist,) ),
  555. ]
  556. testCases = [ [objFunctions, objData],
  557. [intFunctions, intData],
  558. [strFunctions, strData],
  559. [intTupleFunctions, intTupleData],
  560. [strTupleFunctions, strTupleData] ]
  561. dictTestCases = ( (objDictFunctions, objDictData ),
  562. (intDictFunctions, intDictData ),
  563. (strDictFunctions, strDictData),
  564. (mixedDictFunctions, mixedDictData) )
  565. ############################################3
  566. # run the test cases:
  567. # verify all conversions succeed properly
  568. for cases in testCases:
  569. for func in cases[0]:
  570. for data in cases[1]:
  571. Check(func(data), data)
  572. # verify that modifications show up as appropriate.
  573. for case in modCases:
  574. for data in case[1]:
  575. newData = list(data)
  576. CheckModify(case[0](newData), newData)
  577. # verify dictionary test cases
  578. for case in dictTestCases:
  579. for data in case[1]:
  580. for func in case[0]:
  581. newData = dict(data)
  582. CheckDict(func(newData), newData)
  583. x = FieldTest()
  584. y = System.Collections.Generic.List[System.Type]()
  585. x.Field = y
  586. # verify we can bind w/ add & radd
  587. AreEqual(x.Field, y)
  588. a = Cmplx(2, 3)
  589. b = Cmplx2(3, 4)
  590. x = a + b
  591. y = b + a
  592. #############################################################
  593. # Verify combinaions of instance / no instance
  594. a = MixedDispatch("one")
  595. b = MixedDispatch("two")
  596. c = MixedDispatch("three")
  597. d = MixedDispatch("four")
  598. x= a.Combine(b)
  599. y = MixedDispatch.Combine(a,b)
  600. AreEqual(x.called, "instance")
  601. AreEqual(y.called, "static")
  602. x= a.Combine2(b)
  603. y = MixedDispatch.Combine2(a,b)
  604. z = MixedDispatch.Combine2(a,b,c,d)
  605. v = a.Combine2(b,c,d)
  606. AreEqual(x.called, "instance")
  607. AreEqual(y.called, "static")
  608. AreEqual(z.called, "instance_three")
  609. AreEqual(v.called, "instance_three")
  610. ###########################################################
  611. # verify non-instance built-in's don't get bound
  612. class C:
  613. mycmp = cmp
  614. a = C()
  615. AreEqual(a.mycmp(0,0), 0)
  616. ####################################################################################
  617. # Default parameter value tests
  618. def test_default_value():
  619. tst = DefaultValueTest()
  620. AreEqual(tst.Test_Enum(), BindResult.Bool)
  621. AreEqual(tst.Test_BigEnum(), BigEnum.BigValue)
  622. AreEqual(tst.Test_String(), 'Hello World')
  623. AreEqual(tst.Test_Int(), 5)
  624. AreEqual(tst.Test_UInt(), 4294967295)
  625. AreEqual(tst.Test_Bool(), True)
  626. AreEqual(str(tst.Test_Char()), 'A')
  627. AreEqual(tst.Test_Byte(), 2)
  628. AreEqual(tst.Test_SByte(), 2)
  629. AreEqual(tst.Test_Short(), 2)
  630. AreEqual(tst.Test_UShort(), 2)
  631. AreEqual(tst.Test_Long(), 9223372036854775807)
  632. AreEqual(tst.Test_ULong(), 18446744073709551615)
  633. r = clr.Reference[object]("Hi")
  634. s = clr.Reference[object]("Hello")
  635. t = clr.Reference[object]("Ciao")
  636. AreEqual(tst.Test_ByRef_Object(), "System.Reflection.Missing; System.Reflection.Missing; System.Reflection.Missing")
  637. AreEqual(tst.Test_ByRef_Object(r), "Hi; System.Reflection.Missing; System.Reflection.Missing")
  638. AreEqual(tst.Test_ByRef_Object(r, s), "Hi; Hello; System.Reflection.Missing")
  639. AreEqual(tst.Test_ByRef_Object(r, s, t), "Hi; Hello; Ciao")
  640. AreEqual(tst.Test_ByRef_Object("Hi", "Hello", "Ciao"), ("Hi; Hello; Ciao"))
  641. AssertError(TypeError, tst.Test_ByRef_Object, "Hi")
  642. AreEqual(tst.Test_Default_Cast(), "1")
  643. AreEqual(tst.Test_Default_Cast("Hello"), ("Hello", "Hello"))
  644. AreEqual(tst.Test_Default_Cast(None), ("(null)", None))
  645. AreEqual(tst.Test_Default_ValueType(), "1")
  646. AreEqual(tst.Test_Default_ValueType("Hello"), "Hello")
  647. AreEqual(tst.Test_Default_ValueType(None), "(null)")
  648. ####################################################################################
  649. # test missing parameter
  650. def test_missing_value():
  651. tst = MissingValueTest()
  652. AreEqual(tst.Test_1(), "(bool)False")
  653. AreEqual(tst.Test_2(), "(bool)False")
  654. AreEqual(tst.Test_3(), "(sbyte)0")
  655. AreEqual(tst.Test_4(), "(sbyte)0")
  656. AreEqual(tst.Test_5(), "(byte)0")
  657. AreEqual(tst.Test_6(), "(byte)0")
  658. AreEqual(tst.Test_7(), "(short)0")
  659. AreEqual(tst.Test_8(), "(short)0")
  660. AreEqual(tst.Test_9(), "(ushort)0")
  661. AreEqual(tst.Test_10(), "(ushort)0")
  662. AreEqual(tst.Test_11(), "(int)0")
  663. AreEqual(tst.Test_12(), "(int)0")
  664. AreEqual(tst.Test_13(), "(uint)0")
  665. AreEqual(tst.Test_14(), "(uint)0")
  666. AreEqual(tst.Test_15(), "(long)0")
  667. AreEqual(tst.Test_16(), "(long)0")
  668. AreEqual(tst.Test_17(), "(ulong)0")
  669. AreEqual(tst.Test_18(), "(ulong)0")
  670. AreEqual(tst.Test_19(), "(decimal)0")
  671. AreEqual(tst.Test_20(), "(decimal)0")
  672. AreEqual(tst.Test_21(), "(float)0")
  673. AreEqual(tst.Test_22(), "(float)0")
  674. AreEqual(tst.Test_23(), "(double)0")
  675. AreEqual(tst.Test_24(), "(double)0")
  676. AreEqual(tst.Test_25(), "(DaysByte)None")
  677. AreEqual(tst.Test_26(), "(DaysByte)None")
  678. AreEqual(tst.Test_27(), "(DaysSByte)None")
  679. AreEqual(tst.Test_28(), "(DaysSByte)None")
  680. AreEqual(tst.Test_29(), "(DaysShort)None")
  681. AreEqual(tst.Test_30(), "(DaysShort)None")
  682. AreEqual(tst.Test_31(), "(DaysUShort)None")
  683. AreEqual(tst.Test_32(), "(DaysUShort)None")
  684. AreEqual(tst.Test_33(), "(DaysInt)None")
  685. AreEqual(tst.Test_34(), "(DaysInt)None")
  686. AreEqual(tst.Test_35(), "(DaysUInt)None")
  687. AreEqual(tst.Test_36(), "(DaysUInt)None")
  688. AreEqual(tst.Test_37(), "(DaysLong)None")
  689. AreEqual(tst.Test_38(), "(DaysLong)None")
  690. AreEqual(tst.Test_39(), "(DaysULong)None")
  691. AreEqual(tst.Test_40(), "(DaysULong)None")
  692. AreEqual(tst.Test_41(), "(char)\x00")
  693. AreEqual(tst.Test_42(), "(char)\x00")
  694. AreEqual(tst.Test_43(), "(Structure)IronPythonTest.Structure")
  695. AreEqual(tst.Test_44(), "(Structure)IronPythonTest.Structure")
  696. AreEqual(tst.Test_45(), "(EnumSByte)Zero")
  697. AreEqual(tst.Test_46(), "(EnumSByte)Zero")
  698. # TODO: determine if this is a mono bug or not
  699. # both Zero and MinByte have the value of 0, Mono
  700. # https://github.com/IronLanguages/main/issues/1596
  701. if not is_posix:
  702. AreEqual(tst.Test_47(), "(EnumByte)Zero")
  703. AreEqual(tst.Test_48(), "(EnumByte)Zero")
  704. AreEqual(tst.Test_49(), "(EnumShort)Zero")
  705. AreEqual(tst.Test_50(), "(EnumShort)Zero")
  706. AreEqual(tst.Test_51(), "(EnumUShort)Zero")
  707. AreEqual(tst.Test_52(), "(EnumUShort)Zero")
  708. AreEqual(tst.Test_53(), "(EnumInt)MinUShort")
  709. AreEqual(tst.Test_54(), "(EnumInt)MinUShort")
  710. if not is_posix:
  711. AreEqual(tst.Test_55(), "(EnumUInt)MinUInt")
  712. AreEqual(tst.Test_56(), "(EnumUInt)MinUInt")
  713. AreEqual(tst.Test_57(), "(EnumLong)MinUInt")
  714. AreEqual(tst.Test_58(), "(EnumLong)MinUInt")
  715. AreEqual(tst.Test_59(), "(EnumULong)MinUInt")
  716. AreEqual(tst.Test_60(), "(EnumULong)MinUInt")
  717. AreEqual(tst.Test_61(), "(string)(null)")
  718. AreEqual(tst.Test_62(), "(string)(null)")
  719. AreEqual(tst.Test_63(), "(object)System.Reflection.Missing")
  720. AreEqual(tst.Test_64(), "(object)System.Reflection.Missing")
  721. AreEqual(tst.Test_65(), "(MissingValueTest)(null)")
  722. AreEqual(tst.Test_66(), "(MissingValueTest)(null)")
  723. ####################################################################################
  724. # coverage
  725. def test_function():
  726. def testfunctionhelper(c, o):
  727. ############ OptimizedFunctionX ############
  728. line = ""
  729. for i in range(6):
  730. args = ",".join(['1'] * i)
  731. line += 'AreEqual(o.IM%d(%s), "IM%d")\n' % (i, args, i)
  732. line += 'AreEqual(c.IM%d(o,%s), "IM%d")\n' % (i, args, i)
  733. if i > 0:
  734. line += 'try: o.IM%d(%s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (i, ",".join(['1'] * (i-1)))
  735. line += 'try: c.IM%d(o, %s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (i, ",".join(['1'] * (i-1)))
  736. line += 'try: o.IM%d(%s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (i, ",".join(['1'] * (i+1)))
  737. line += 'try: c.IM%d(o, %s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (i, ",".join(['1'] * (i+1)))
  738. line += 'AreEqual(o.SM%d(%s), "SM%d")\n' % (i, args, i)
  739. line += 'AreEqual(c.SM%d(%s), "SM%d")\n' % (i, args, i)
  740. if i > 0:
  741. line += 'try: o.SM%d(%s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (i, ",".join(['1'] * (i-1)))
  742. line += 'try: c.SM%d(%s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (i, ",".join(['1'] * (i-1)))
  743. line += 'try: o.SM%d(%s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (i, ",".join(['1'] * (i+1)))
  744. line += 'try: c.SM%d(%s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (i, ",".join(['1'] * (i+1)))
  745. #print line
  746. exec line in globals(), locals()
  747. ############ OptimizedFunctionAny ############
  748. ## 1
  749. line = ""
  750. for i in range(7):
  751. args = ",".join(['1'] * i)
  752. if i in [0, 3, 4]:
  753. line += 'AreEqual(o.IDM0(%s), "IDM0-%d")\n' % (args, i)
  754. line += 'AreEqual(c.IDM0(o,%s), "IDM0-%d")\n' % (args, i)
  755. else:
  756. line += 'try: o.IDM0(%s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (args)
  757. line += 'try: c.IDM0(o, %s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (args)
  758. #print line
  759. exec line in globals(), locals()
  760. line = ""
  761. for i in range(7):
  762. args = ",".join(['1'] * i)
  763. if i in [0, 3]:
  764. line += 'AreEqual(o.SDM0(%s), "SDM0-%d")\n' % (args, i)
  765. line += 'AreEqual(c.SDM0(%s), "SDM0-%d")\n' % (args, i)
  766. else:
  767. line += 'try: o.SDM0(%s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (args)
  768. line += 'try: c.SDM0(%s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (args)
  769. #print line
  770. exec line in globals(), locals()
  771. ## 2
  772. line = ""
  773. for i in range(7):
  774. args = ",".join(['1'] * i)
  775. if i in [1]:
  776. line += 'AreEqual(o.IDM1(%s), "IDM1-%d")\n' % (args, i)
  777. line += 'AreEqual(c.IDM1(o,%s), "IDM1-%d")\n' % (args, i)
  778. line += 'AreEqual(o.SDM1(%s), "SDM1-%d")\n' % (args, i)
  779. line += 'AreEqual(c.SDM1(%s), "SDM1-%d")\n' % (args, i)
  780. else:
  781. line += 'AreEqual(o.IDM1(%s), "IDM1-x")\n' % (args)
  782. line += 'AreEqual(c.IDM1(o,%s), "IDM1-x")\n' % (args)
  783. line += 'AreEqual(o.SDM1(%s), "SDM1-x")\n' % (args)
  784. line += 'AreEqual(c.SDM1(%s), "SDM1-x")\n' % (args)
  785. #print line
  786. exec line in globals(), locals()
  787. line = ""
  788. for i in range(7):
  789. args = ",".join(['1'] * i)
  790. if i in [2]:
  791. line += 'AreEqual(o.IDM4(%s), "IDM4-%d")\n' % (args, i)
  792. line += 'AreEqual(c.IDM4(o,%s), "IDM4-%d")\n' % (args, i)
  793. line += 'AreEqual(o.SDM4(%s), "SDM4-%d")\n' % (args, i)
  794. line += 'AreEqual(c.SDM4(%s), "SDM4-%d")\n' % (args, i)
  795. else:
  796. line += 'AreEqual(o.IDM4(%s), "IDM4-x")\n' % (args)
  797. line += 'AreEqual(c.IDM4(o,%s), "IDM4-x")\n' % (args)
  798. line += 'AreEqual(o.SDM4(%s), "SDM4-x")\n' % (args)
  799. line += 'AreEqual(c.SDM4(%s), "SDM4-x")\n' % (args)
  800. #print line
  801. exec line in globals(), locals()
  802. ## 3
  803. line = ""
  804. for i in range(7):
  805. args = ",".join(['1'] * i)
  806. if i in range(5):
  807. line += 'AreEqual(o.IDM2(%s), "IDM2-%d")\n' % (args, i)
  808. line += 'AreEqual(c.IDM2(o,%s), "IDM2-%d")\n' % (args, i)
  809. else:
  810. line += 'try: o.IDM2(%s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (args)
  811. line += 'try: c.IDM2(o, %s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (args)
  812. #print line
  813. exec line in globals(), locals()
  814. line = ""
  815. for i in range(7):
  816. args = ",".join(['1'] * i)
  817. if i in range(6):
  818. line += 'AreEqual(o.SDM2(%s), "SDM2-%d")\n' % (args, i)
  819. line += 'AreEqual(c.SDM2(%s), "SDM2-%d")\n' % (args, i)
  820. else:
  821. line += 'try: o.SDM2(%s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (args)
  822. line += 'try: c.SDM2(%s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (args)
  823. #print line
  824. exec line in globals(), locals()
  825. ## 4
  826. line = ""
  827. for i in range(7):
  828. args = ",".join(['1'] * i)
  829. if i in [0, 5]:
  830. line += 'AreEqual(o.IDM5(%s), "IDM5-%d")\n' % (args, i)
  831. line += 'AreEqual(c.IDM5(o,%s), "IDM5-%d")\n' % (args, i)
  832. else:
  833. line += 'try: o.IDM5(%s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (args)
  834. line += 'try: c.IDM5(o, %s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (args)
  835. #print line
  836. exec line in globals(), locals()
  837. line = ""
  838. for i in range(7):
  839. args = ",".join(['1'] * i)
  840. if i in [0, 6]:
  841. line += 'AreEqual(o.SDM5(%s), "SDM5-%d")\n' % (args, i)
  842. line += 'AreEqual(c.SDM5(%s), "SDM5-%d")\n' % (args, i)
  843. else:
  844. line += 'try: o.SDM5(%s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (args)
  845. line += 'try: c.SDM5(%s) \nexcept TypeError: pass \nelse: raise AssertionError\n' % (args)
  846. #print line
  847. exec line in globals(), locals()
  848. ## 5
  849. line = ""
  850. for i in range(7):
  851. args = ",".join(['1'] * i)
  852. if i in range(5):
  853. line += 'AreEqual(o.IDM3(%s), "IDM3-%d")\n' % (args, i)
  854. line += 'AreEqual(c.IDM3(o,%s), "IDM3-%d")\n' % (args, i)
  855. else:
  856. line += 'AreEqual(o.IDM3(%s), "IDM3-x")\n' % (args)
  857. line += 'AreEqual(c.IDM3(o,%s), "IDM3-x")\n' % (args)
  858. #print line
  859. exec line in globals(), locals()
  860. line = ""
  861. for i in range(7):
  862. args = ",".join(['1'] * i)
  863. if i in range(6):
  864. line += 'AreEqual(o.SDM3(%s), "SDM3-%d")\n' % (args, i)
  865. line += 'AreEqual(c.SDM3(%s), "SDM3-%d")\n' % (args, i)
  866. else:
  867. line += 'AreEqual(o.SDM3(%s), "SDM3-x")\n' % (args)
  868. line += 'AreEqual(c.SDM3(%s), "SDM3-x")\n' % (args)
  869. #print line
  870. exec line in globals(), locals()
  871. ############ OptimizedFunctionN ############
  872. line = ""
  873. for i in range(6):
  874. args = ",".join(['1'] * i)
  875. line += 'AreEqual(o.IPM0(%s), "IPM0-%d")\n' % (args, i)
  876. line += 'AreEqual(o.SPM0(%s), "SPM0-%d")\n' % (args, i)
  877. line += 'AreEqual(c.IPM0(o,%s), "IPM0-%d")\n' % (args, i)
  878. line += 'AreEqual(c.SPM0(%s), "SPM0-%d")\n' % (args, i)
  879. line += 'AreEqual(o.SPM1(0,%s), "SPM1-%d")\n' % (args, i)
  880. line += 'AreEqual(o.IPM1(0,%s), "IPM1-%d")\n' % (args, i)
  881. line += 'AreEqual(c.IPM1(o, 0,%s), "IPM1-%d")\n' % (args, i)
  882. line += 'AreEqual(c.SPM1(0,%s), "SPM1-%d")\n' % (args, i)
  883. #print line
  884. exec line in globals(), locals()
  885. class DispatchAgain2(DispatchAgain): pass
  886. testfunctionhelper(DispatchAgain, DispatchAgain())
  887. testfunctionhelper(DispatchAgain2, DispatchAgain2())
  888. AreEqual(type(BindTest.ReturnTest('char')), System.Char)
  889. AreEqual(type(BindTest.ReturnTest('null')), type(None))
  890. AreEqual(type(BindTest.ReturnTest('object')), object)
  891. if not is_silverlight and not is_net40: #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=25897
  892. Assert(repr(BindTest.ReturnTest("com")).startswith('<System.__ComObject'))
  893. #####################################################################
  894. ## testing multicall generator
  895. def test_multicall_generator():
  896. c = MultiCall()
  897. def AllEqual(exp, meth, passins):
  898. for arg in passins:
  899. #print meth, arg
  900. AreEqual(meth(*arg), exp)
  901. def AllAssert(type, meth, passins):
  902. for arg in passins:
  903. #print meth, arg
  904. AssertError(type, meth, arg)
  905. import sys
  906. maxint = sys.maxint
  907. import System
  908. maxlong1 = System.Int64.MaxValue
  909. maxlong2 = long(str(maxlong1))
  910. class MyInt(int):
  911. def __repr__(self):
  912. return "MyInt(%s)" % super(MyInt, self).__repr__()
  913. myint = MyInt(10)
  914. #############################################################################################
  915. # public int M0(int arg) { return 1; }
  916. # public int M0(long arg) { return 2; }
  917. func = c.M0
  918. AllEqual(1, func, [(0,), (1,), (maxint,), (myint,)])
  919. AllEqual(2, func, [(maxint + 1,), (-maxint-10,), (10L,)])
  920. AllAssert(TypeError, func, [
  921. (-10.2,),
  922. (1+2j,),
  923. ("10",),
  924. (System.Byte.Parse("2"),),
  925. ])
  926. #############################################################################################
  927. # public int M1(int arg) { return 1; }
  928. # public int M1(long arg) { return 2; }
  929. # public int M1(object arg) { return 3; }
  930. func = c.M1
  931. AllEqual(1, func, [
  932. (0,),
  933. (1,),
  934. (maxint,),
  935. (System.Byte.Parse("2"),),
  936. (myint, ),
  937. #(10L,),
  938. #(-1234.0,),
  939. ])
  940. AllEqual(2, func, [
  941. #(maxint + 1,),
  942. #(-maxint-10,),
  943. ])
  944. AllEqual(3, func, [(-10.2,), (1+2j,), ("10",),])
  945. #############################################################################################
  946. # public int M2(int arg1, int arg2) { return 1; }
  947. # public int M2(long arg1, int arg2) { return 2; }
  948. # public int M2(int arg1, long arg2) { return 3; }
  949. # public int M2(long arg1, long arg2) { return 4; }
  950. # public int M2(object arg1, object arg2) { return 5; }
  951. func = c.M2
  952. AllEqual(1, func, [
  953. (0, 0), (1, maxint), (maxint, 1), (maxint, maxint),
  954. #(10L, 0),
  955. ])
  956. AllEqual(2, func, [
  957. #(maxint+1, 0),
  958. #(maxint+10, 10),
  959. #(maxint+10, 10L),
  960. #(maxlong1, 0),
  961. #(maxlong2, 0),
  962. ])
  963. AllEqual(3, func, [
  964. #(0, maxint+1),
  965. #(10, maxint+10),
  966. #(10L, maxint+10),
  967. ])
  968. AllEqual(4, func, [
  969. #(maxint+10, maxint+1),
  970. #(-maxint-10, maxint+10),
  971. #(-maxint-10L, maxint+100),
  972. #(maxlong1, maxlong1),
  973. #(maxlong2, maxlong1),
  974. ])
  975. AllEqual(5, func, [
  976. (maxlong1 + 1, 1),
  977. (maxlong2 + 1, 1),
  978. (maxint, maxlong1 + 10),
  979. (maxint, maxlong2 + 10),
  980. (1, "100L"),
  981. (10.2, 1),
  982. ])
  983. #############################################################################################
  984. # public int M4(int arg1, int arg2, int arg3, int arg4) { return 1; }
  985. # public int M4(object arg1, object arg2, object arg3, object arg4) { return 2; }
  986. if not is_silverlight:
  987. one = [t.Parse("5") for t in [System.Byte, System.SByte, System.UInt16, System.Int16, System.UInt32, System.Int32,
  988. System.UInt32, System.Int32, System.UInt64, System.Int64,
  989. System.Char, System.Decimal, System.Single, System.Double] ]
  990. else: one = []
  991. one.extend([True, False, 5L, DispatchHelpers.Color.Red ])
  992. if not is_silverlight:
  993. two = [t.Parse("5.5") for t in [ System.Decimal, System.Single, System.Double] ]
  994. else: two = []
  995. two.extend([None, "5", "5.5", maxint * 2, ])
  996. together = []
  997. together.extend(one)
  998. together.extend(two)
  999. ignore = '''
  1000. for a1 in together:
  1001. for a2 in together:
  1002. for a3 in together:
  1003. for a4 in together:
  1004. # print a1, a2, a3, a4, type(a1), type(a1), type(a2), type(a3), type(a4)
  1005. if a1 in two or a2 is two or a3 in two or a4 in two:
  1006. AreEqual(c.M4(a1, a2, a3, a4), 2)
  1007. else :
  1008. AreEqual(c.M4(a1, a2, a3, a4), 1)
  1009. '''
  1010. #############################################################################################
  1011. # public int M5(DispatchHelpers.B arg1, DispatchHelpers.B args) { return 1; }
  1012. # public int M5(DispatchHelpers.D arg1, DispatchHelpers.B args) { return 2; }
  1013. # public int M5(object arg1, object args) { return 3; }
  1014. b = DispatchHelpers.B()
  1015. d = DispatchHelpers.D()
  1016. func = c.M5
  1017. AllEqual(1, func, [(b, b), (b, d)])
  1018. AllEqual(2, func, [(d, b), (d, d)])
  1019. AllEqual(3, func, [(1, 2)])
  1020. #############################################################################################
  1021. # public int M6(DispatchHelpers.B arg1, DispatchHelpers.B args) { return 1; }
  1022. # public int M6(DispatchHelpers.B arg1, DispatchHelpers.D args) { return 2; }
  1023. # public int M6(object arg1, DispatchHelpers.D args) { return 3; }
  1024. func = c.M6
  1025. AllEqual(1, func, [(b, b), (d, b)])
  1026. AllEqual(2, func, [(b, d), (d, d)])
  1027. AllEqual(3, func, [(1, d), (6L, d)])
  1028. AllAssert(TypeError, func, [(1,1), (None, None), (None, d), (3, b)])
  1029. #############################################################################################
  1030. # public int M7(DispatchHelpers.B arg1, DispatchHelpers.B args) { return 1; }
  1031. # public int M7(DispatchHelpers.B arg1, DispatchHelpers.D args) { return 2; }
  1032. # public int M7(DispatchHelpers.D arg1, DispatchHelpers.B args) { return 3; }
  1033. # public int M7(DispatchHelpers.D arg1, DispatchHelpers.D args) { return 4; }
  1034. func = c.M7
  1035. AllEqual(1, func, [(b, b)])
  1036. AllEqual(2, func, [(b, d)])
  1037. AllEqual(3, func, [(d, b)])
  1038. AllEqual(4, func, [(d, d)])
  1039. AllAssert(TypeError, func, [(1,1), (None, None), (None, d)])
  1040. #############################################################################################
  1041. # public int M8(int arg1, int arg2) { return 1;}
  1042. # public int M8(DispatchHelpers.B arg1, DispatchHelpers.B args) { return 2; }
  1043. # public int M8(object arg1, object arg2) { return 3; }
  1044. func = c.M8
  1045. AllEqual(1, func, [(1, 2), ]) #(maxint, 2L)])
  1046. AllEqual(2, func, [(b, b), (b, d), (d, b), (d, d)])
  1047. AllEqual(3, func, [(5.1, b), (1, d), (d, 1), (d, maxlong2), (maxlong1, d), (None, 3), (3, None)])
  1048. #############################################################################################
  1049. # public static int M92(out int i, out int j, out int k, bool boolIn)
  1050. AreEqual(Dispatch.M92(True), (4, 1,2,3))
  1051. AreEqual(Dispatch.Flag, 192)
  1052. #############################################################################################
  1053. # public int M93(out int i, out int j, out int k, bool boolIn)
  1054. AreEqual(Dispatch().M93(True), (4, 1,2,3))
  1055. AreEqual(Dispatch.Flag, 193)
  1056. #############################################################################################
  1057. # public int M94(out int i, out int j, bool boolIn, out int k)
  1058. AreEqual(Dispatch().M94(True), (4, 1,2,3))
  1059. AreEqual(Dispatch.Flag, 194)
  1060. #############################################################################################
  1061. # public static int M95(out int i, out int j, bool boolIn, out int k)
  1062. AreEqual(Dispatch.M95(True), (4, 1,2,3))
  1063. AreEqual(Dispatch.Flag, 195)
  1064. #############################################################################################
  1065. # public static int M96(out int x, out int j, params int[] extras)
  1066. AreEqual(Dispatch.M96(), (0, 1,2))
  1067. AreEqual(Dispatch.Flag, 196)
  1068. AreEqual(Dispatch.M96(1,2), (3, 1,2))
  1069. AreEqual(Dispatch.Flag, 196)
  1070. AreEqual(Dispatch.M96(1,2,3), (6, 1,2))
  1071. AreEqual(Dispatch.Flag, 196)
  1072. #############################################################################################
  1073. # public int M97(out int x, out int j, params int[] extras)
  1074. AreEqual(Dispatch().M97(), (0, 1,2))
  1075. AreEqual(Dispatch.Flag, 197)
  1076. AreEqual(Dispatch().M97(1,2), (3, 1,2))
  1077. AreEqual(Dispatch.Flag, 197)
  1078. AreEqual(Dispatch().M97(1,2,3), (6, 1,2))
  1079. AreEqual(Dispatch.Flag, 197)
  1080. #############################################################################################
  1081. # public void M98(string a, string b, string c, string d, out int x, ref Dispatch di)
  1082. a = Dispatch()
  1083. x = a.M98('1', '2', '3', '4', a)
  1084. AreEqual(x[0], 10)
  1085. AreEqual(x[1], a)
  1086. # doc for this method should have the out & ref params as return values
  1087. AreEqual(a.M98.__doc__, 'M98(self: Dispatch, a: str, b: str, c: str, d: str, di: Dispatch) -> (int, Dispatch)%s' % os.linesep)
  1088. #DDB 76340
  1089. if not is_silverlight:
  1090. # call type.InvokeMember on String.ToString - all methods have more arguments than max args.
  1091. res = clr.GetClrType(str).InvokeMember('ToString',
  1092. System.Reflection.BindingFlags.Instance|System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.InvokeMethod,
  1093. None,
  1094. 'abc',
  1095. ())
  1096. AreEqual(res, 'abc')
  1097. # verify calling a generic method w/o args throws a reasonable exception
  1098. def test_missing_generic_args():
  1099. import System
  1100. #TODO specify clearly which exception is appropriate here
  1101. AssertError(Exception, System.Collections.Generic.List)
  1102. # verify calls to explicit interface implementations
  1103. def test_explicit():
  1104. x = ExplicitTest()
  1105. Assert(not hasattr(x, "A"))
  1106. try:
  1107. x.A()
  1108. except AttributeError:
  1109. pass
  1110. else:
  1111. Fail("Expected AttributeError, got none")
  1112. AreEqual(x.B(), "ExplicitTest.B")
  1113. Assert(not hasattr(x, "C"))
  1114. try:
  1115. x.C()
  1116. except AttributeError:
  1117. pass
  1118. else:
  1119. Fail("Expected AttributeError, got none")
  1120. AreEqual(x.D(), "ExplicitTest.D")
  1121. AreEqual(IExplicitTest1.A(x), "ExplicitTest.IExplicitTest1.A")
  1122. AreEqual(IExplicitTest1.B(x), "ExplicitTest.IExplicitTest1.B")
  1123. AreEqual(IExplicitTest1.C(x), "ExplicitTest.IExplicitTest1.C")
  1124. AreEqual(IExplicitTest1.D(x), "ExplicitTest.D")
  1125. AreEqual(IExplicitTest2.A(x), "ExplicitTest.IExplicitTest2.A")
  1126. AreEqual(IExplicitTest2.B(x), "ExplicitTest.B")
  1127. x = ExplicitTestArg()
  1128. try:
  1129. x.M()
  1130. except AttributeError:
  1131. pass
  1132. else:
  1133. Fail("Expected AttributeError, got none")
  1134. AreEqual(IExplicitTest3.M(x), 3)
  1135. AreEqual(IExplicitTest4.M(x, 7), 4)
  1136. @skip("silverlight")
  1137. def test_security_crypto():
  1138. if is_netstandard:
  1139. clr.AddReference("System.Security.Cryptography.Algorithms")
  1140. Assert(issubclass(type(System.Security.Cryptography.MD5.Create()),
  1141. System.Security.Crypt

Large files files are truncated, but you can click here to view the full file