PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/DLR_Main/Languages/IronPython/Tests/interop/net/event/test_event.py

https://bitbucket.org/mdavid/dlr
Python | 281 lines | 167 code | 65 blank | 49 comment | 5 complexity | 326fad3f1819d296de132f22acfae395 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. Operations on event type.
  17. '''
  18. #------------------------------------------------------------------------------
  19. from iptest.assert_util import *
  20. skiptest("silverlight")
  21. add_clr_assemblies("eventdefinitions", "typesamples")
  22. from Merlin.Testing import *
  23. from Merlin.Testing.Event import *
  24. def test_basic():
  25. # t1 is where the event is declared
  26. for t1 in [ ClassImplicitlyImplementInterface,
  27. #StructImplicitlyImplementInterface, # bug: 361955
  28. ClassWithSimpleEvent,
  29. #StructWithSimpleEvent, # bug: 361955
  30. ]:
  31. # t2 is where the handler is defined
  32. for t2 in [ TargetClass, TargetStruct ]:
  33. o = t2()
  34. # try both static and instance method
  35. for (double, square, negate, bad) in [
  36. (t2.s_Double, t2.s_Square, t2.s_Negate, t2.s_Throw),
  37. (o.i_Double, o.i_Square, o.i_Negate, o.i_Throw),
  38. ]:
  39. # no duplicate handlers, add/remove
  40. x = t1()
  41. Flag.Set(0)
  42. AreEqual(x.CallInside(1), -1)
  43. Flag.Check(0)
  44. Flag.Set(0)
  45. x.OnAction += double
  46. AreEqual(x.CallInside(2), 4)
  47. Flag.Check(1)
  48. Flag.Set(0)
  49. x.OnAction += square
  50. AreEqual(x.CallInside(3), 9)
  51. Flag.Check(101)
  52. Flag.Set(0)
  53. x.OnAction += negate
  54. AreEqual(x.CallInside(4), -4)
  55. Flag.Check(111)
  56. Flag.Set(0)
  57. x.OnAction -= square
  58. AreEqual(x.CallInside(5), -5)
  59. Flag.Check(11)
  60. Flag.Set(0)
  61. x.OnAction -= double
  62. AreEqual(x.CallInside(6), -6)
  63. Flag.Check(10)
  64. Flag.Set(0)
  65. x.OnAction -= negate
  66. AreEqual(x.CallInside(7), -1)
  67. Flag.Check(0)
  68. # duplicate: which one get removed
  69. x = t1()
  70. x.OnAction += double
  71. x.OnAction += square
  72. x.OnAction += double
  73. x.OnAction += double
  74. Flag.Set(0)
  75. AreEqual(x.CallInside(8), 16)
  76. Flag.Check(103)
  77. x.OnAction -= double
  78. AreEqual(x.CallInside(9), 18)
  79. x.OnAction -= double # verify the last one is removed
  80. Flag.Set(0)
  81. AreEqual(x.CallInside(10), 100) # bug 361971
  82. Flag.Check(101)
  83. x.OnAction -= double
  84. AreEqual(x.CallInside(11), 121)
  85. x.OnAction -= square
  86. AreEqual(x.CallInside(12), -1)
  87. # remove from empty invocation list
  88. x.OnAction -= double
  89. Flag.Set(0)
  90. AreEqual(x.CallInside(13), -1)
  91. Flag.Check(0)
  92. # troubling event handler in the middle
  93. x = t1()
  94. x.OnAction += double
  95. x.OnAction += bad
  96. x.OnAction += negate
  97. Flag.Set(0)
  98. AssertError(StandardError, lambda: x.CallInside(14))
  99. Flag.Check(1) # this also verified double was added/thus called first
  100. # different handler handling path:
  101. # - explicitly created delegate objects (d_xxx)
  102. # - mixed
  103. x = t1()
  104. d_double = Int32Int32Delegate(double)
  105. d_negate = Int32Int32Delegate(negate)
  106. d_square = Int32Int32Delegate(square)
  107. x.OnAction += d_double
  108. x.OnAction += d_square
  109. x.OnAction += double
  110. Flag.Set(0)
  111. AreEqual(x.CallInside(15), 30)
  112. Flag.Check(102)
  113. x.OnAction += d_negate
  114. AreEqual(x.CallInside(16), -16)
  115. x.OnAction -= d_square
  116. Flag.Set(0)
  117. AreEqual(x.CallInside(17), -17)
  118. Flag.Check(12)
  119. x.OnAction -= negate # remove the "native"
  120. AreEqual(x.CallInside(18), -18)
  121. x.OnAction -= d_negate # remove the 'stub'ed
  122. AreEqual(x.CallInside(19), 38)
  123. x.OnAction -= negate # list is not empty, try to remove the not-in-list
  124. x.OnAction -= d_negate # same
  125. AreEqual(x.CallInside(20), 40)
  126. x.OnAction -= double
  127. x.OnAction -= d_double
  128. AreEqual(x.CallInside(21), -1)
  129. def test_things_from_bound_event():
  130. #print ClassWithEvents.StaticEvent
  131. #print ClassWithEvents.InstanceEvent
  132. pass
  133. def test_explicitly_implemented_event():
  134. t1 = StructExplicitlyImplementInterface()
  135. #t1.OnAction += TargetClass.s_Double
  136. #print IInterface.OnAction # bound event
  137. #print dir(IInterface.OnAction)
  138. #print IInterface.OnAction.Event # reflected event
  139. #IInterface.OnAction.Add(t1, TargetClass.s_Double)
  140. @skip("multiple_execute")
  141. def test_static_event():
  142. for t1 in [
  143. ClassWithStaticEvent,
  144. #StructWithStaticEvent
  145. ]:
  146. x = t1()
  147. tc, ts = TargetClass(), TargetStruct()
  148. t1.OnAction += TargetClass.s_Double
  149. t1.OnAction += TargetStruct.s_Negate
  150. t1.OnAction += tc.i_Square
  151. Flag.Set(0)
  152. AreEqual(x.CallInside(30), 900)
  153. Flag.Set(111)
  154. t1.OnAction += ts.i_Negate
  155. Flag.Set(0)
  156. AreEqual(x.CallInside(31), -31)
  157. Flag.Check(121)
  158. t1.OnAction -= TargetStruct.s_Negate
  159. t1.OnAction -= ts.i_Double # not added before
  160. t1.OnAction -= tc.i_Square
  161. Flag.Set(0)
  162. AreEqual(x.CallInside(32), -32)
  163. Flag.Set(11)
  164. Flag.Set(0)
  165. t1.OnAction -= ts.i_Negate
  166. AreEqual(x.CallInside(33), 66)
  167. Flag.Set(1)
  168. def test_access_static_event_from_derived_type():
  169. def f1(): DerivedClassWithStaticEvent.OnAction += TargetClass.s_Double
  170. def f2(): DerivedClassWithStaticEvent.OnAction -= TargetStruct().s_Double
  171. for f in [f1, f2]:
  172. AssertErrorWithMessage(AttributeError,
  173. "attribute 'OnAction' of 'DerivedClassWithStaticEvent' object is read-only",
  174. f)
  175. #x = DerivedClassWithStaticEvent()
  176. #def f1(): x.OnAction += TargetClass.s_Double
  177. #def f2(): x.OnAction -= TargetStruct().s_Double
  178. #f1()
  179. def test_assignment():
  180. x = ClassWithSimpleEvent()
  181. # 362440
  182. def f(): x.OnAction = TargetClass.s_Double
  183. AssertErrorWithMessage(AttributeError,
  184. "attribute 'OnAction' of 'ClassWithSimpleEvent' object is read-only",
  185. f)
  186. def test_add_sub():
  187. x = ClassWithSimpleEvent()
  188. def f(): x.OnAction = x.OnAction + TargetClass.s_Negate
  189. AssertErrorWithMessage(TypeError,
  190. "unsupported operand type(s) for +: 'BoundEvent' and 'builtin_function_or_method'",
  191. f)
  192. def f(): x.OnAction - TargetClass.s_Negate
  193. AssertErrorWithMessage(TypeError,
  194. "unsupported operand type(s) for -: 'BoundEvent' and 'builtin_function_or_method'",
  195. f)
  196. def test_iadd_isub():
  197. x = ClassWithSimpleEvent()
  198. # 362447
  199. #x.OnAction.__iadd__(TargetClass.s_Negate)
  200. #AreEqual(x.CallInside(101), -101)
  201. #x.OnAction.__isub__(TargetClass.s_Negate)
  202. #AreEqual(x.CallInside(102), -1)
  203. def test_add_method_descriptor():
  204. x = ClassWithSimpleEvent()
  205. x.OnAction += TargetClass.s_Negate
  206. x.OnAction += TargetClass.i_Double # method
  207. Flag.Set(0)
  208. AssertErrorWithMessage(TypeError,
  209. "i_Double() takes exactly 2 arguments (1 given)",
  210. lambda: x.CallInside(4))
  211. Flag.Check(10)
  212. def test_call_outside():
  213. x = ClassWithSimpleEvent()
  214. x.OnAction += TargetClass.s_Negate
  215. #x.OnAction(3) # 362449
  216. run_test(__name__)