/src/libraries/System.Dynamic.Runtime/tests/Dynamic.Context/Conformance.dynamic.context.operator.compound.event.negative.cs

https://github.com/dotnet/runtime · C# · 420 lines · 312 code · 54 blank · 54 comment · 24 complexity · 76535e855df05c9d26d81c2bbb45d288 MD5 · raw file

  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. using Xunit;
  4. namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.operate.compound.evnt.negative.neg001.neg001
  5. {
  6. // <Area> Dynamic -- compound operator</Area>
  7. // <Title> compound operator +=/-= on event </Title>
  8. // <Description>
  9. // Negative: The operator is *=, /=, %=, &=, |=, ^=, <<=, >>=
  10. // </Description>
  11. // <RelatedBugs></RelatedBugs>
  12. // <Expects Status=success></Expects>
  13. // <Code>
  14. public delegate int Dele(int i);
  15. public class C
  16. {
  17. public event Dele E = x => x;
  18. public static int Foo(int i)
  19. {
  20. return i;
  21. }
  22. public static void DynamicCSharpRunTest()
  23. {
  24. Assert.Equal(0, MainMethod());
  25. }
  26. public static int MainMethod()
  27. {
  28. dynamic c = new C();
  29. int result = 0;
  30. try
  31. {
  32. c.E *= new Dele(C.Foo);
  33. }
  34. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  35. {
  36. if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, e.Message, "*=", "Dele", "Dele"))
  37. result += 1;
  38. }
  39. try
  40. {
  41. c.E /= new Dele(C.Foo);
  42. }
  43. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  44. {
  45. if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, e.Message, "/=", "Dele", "Dele"))
  46. result += 2;
  47. }
  48. try
  49. {
  50. c.E %= new Dele(C.Foo);
  51. }
  52. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  53. {
  54. if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, e.Message, "%=", "Dele", "Dele"))
  55. result += 4;
  56. }
  57. try
  58. {
  59. c.E &= new Dele(C.Foo);
  60. }
  61. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  62. {
  63. if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, e.Message, "&=", "Dele", "Dele"))
  64. result += 8;
  65. }
  66. try
  67. {
  68. c.E |= new Dele(C.Foo);
  69. }
  70. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  71. {
  72. if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, e.Message, "|=", "Dele", "Dele"))
  73. result += 16;
  74. }
  75. try
  76. {
  77. c.E ^= new Dele(C.Foo);
  78. }
  79. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  80. {
  81. if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, e.Message, "^=", "Dele", "Dele"))
  82. result += 32;
  83. }
  84. try
  85. {
  86. c.E <<= new Dele(C.Foo);
  87. }
  88. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  89. {
  90. if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, e.Message, "<<=", "Dele", "Dele"))
  91. result += 64;
  92. }
  93. try
  94. {
  95. c.E >>= new Dele(C.Foo);
  96. }
  97. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  98. {
  99. if (ErrorVerifier.Verify(ErrorMessageId.BadBinaryOps, e.Message, ">>=", "Dele", "Dele"))
  100. result += 128;
  101. }
  102. if (result != 255)
  103. {
  104. System.Console.WriteLine("result = {0}", result);
  105. return 1;
  106. }
  107. return 0;
  108. }
  109. public int DoEvent(int arg)
  110. {
  111. return this.E(arg);
  112. }
  113. }
  114. // </Code>
  115. }
  116. namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.operate.compound.evnt.negative.neg002.neg002
  117. {
  118. // <Area> Dynamic -- compound operator</Area>
  119. // <Title> compound operator +=/-= on event </Title>
  120. // <Description>
  121. // Negative: rhs is dynamic expression with runtime non-delegate type
  122. // </Description>
  123. // <RelatedBugs></RelatedBugs>
  124. // <Expects Status=success></Expects>
  125. // <Code>
  126. public delegate int Dele(int i);
  127. public class C
  128. {
  129. public event Dele E;
  130. public static int Foo(int i)
  131. {
  132. return i;
  133. }
  134. [Fact]
  135. public static void DynamicCSharpRunTest()
  136. {
  137. Assert.Equal(0, MainMethod());
  138. }
  139. public static int MainMethod()
  140. {
  141. dynamic c = new C();
  142. try
  143. {
  144. c.E += c;
  145. }
  146. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  147. {
  148. // incorrect error message
  149. // resolution is 'By design' so this error message should be use again
  150. // new error message
  151. if (ErrorVerifier.Verify(ErrorMessageId.NoImplicitConv, e.Message, "C", "Dele"))
  152. return 0;
  153. }
  154. return 1;
  155. }
  156. public int DoEvent(int arg)
  157. {
  158. return this.E(arg);
  159. }
  160. }
  161. // </Code>
  162. }
  163. namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.operate.compound.evnt.negative.neg003.neg003
  164. {
  165. // <Area> Dynamic -- compound operator</Area>
  166. // <Title> compound operator +=/-= on event </Title>
  167. // <Description>
  168. // Negative: rhs is non-matched delegate
  169. // </Description>
  170. // <RelatedBugs></RelatedBugs>
  171. // <Expects Status=success></Expects>
  172. // <Code>
  173. using System;
  174. public delegate int Dele(int i);
  175. public class C
  176. {
  177. public event Dele E;
  178. public static int Foo(int i)
  179. {
  180. return i;
  181. }
  182. [Fact]
  183. public static void DynamicCSharpRunTest()
  184. {
  185. Assert.Equal(0, MainMethod());
  186. }
  187. public static int MainMethod()
  188. {
  189. dynamic c = new C();
  190. int result = 0;
  191. try
  192. {
  193. c.E += (Func<int, int>)(x => x);
  194. }
  195. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  196. {
  197. if (ErrorVerifier.Verify(ErrorMessageId.NoImplicitConv, e.Message, "System.Func<int,int>", "Dele"))
  198. result += 1;
  199. }
  200. try
  201. {
  202. c.E -= (Func<int, int>)(x => x);
  203. }
  204. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  205. {
  206. if (ErrorVerifier.Verify(ErrorMessageId.NoImplicitConv, e.Message, "System.Func<int,int>", "Dele"))
  207. result += 2;
  208. }
  209. if (result != 3)
  210. {
  211. System.Console.WriteLine("Result = {0}", result);
  212. return 1;
  213. }
  214. return 0;
  215. }
  216. public int DoEvent(int arg)
  217. {
  218. return this.E(arg);
  219. }
  220. }
  221. // </Code>
  222. }
  223. namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.operate.compound.evnt.negative.neg004.neg004
  224. {
  225. // <Area> Dynamic -- compound operator</Area>
  226. // <Title> compound operator +=/-= on event </Title>
  227. // <Description>
  228. // Negative: rhs is compile time known type and it's non valid type
  229. // </Description>
  230. // <RelatedBugs></RelatedBugs>
  231. // <Expects Status=success></Expects>
  232. // <Code>
  233. public delegate int Dele(int i);
  234. public class C
  235. {
  236. public event Dele E;
  237. public static int Foo(int i)
  238. {
  239. return i;
  240. }
  241. [Fact]
  242. public static void DynamicCSharpRunTest()
  243. {
  244. Assert.Equal(0, MainMethod());
  245. }
  246. public static int MainMethod()
  247. {
  248. dynamic c = new C();
  249. int result = 0;
  250. try
  251. {
  252. c.E += new C();
  253. }
  254. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  255. {
  256. if (!ErrorVerifier.Verify(ErrorMessageId.NoImplicitConv, e.Message, "C", "Dele"))
  257. result++;
  258. }
  259. try
  260. {
  261. c.E += 1;
  262. } // : not type info for c.E at runtime as it's null
  263. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  264. {
  265. if (!ErrorVerifier.Verify(ErrorMessageId.NoImplicitConv, e.Message, "int", "Dele"))
  266. result++;
  267. ;
  268. }
  269. try
  270. {
  271. c.E -= new C();
  272. }
  273. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  274. {
  275. if (!ErrorVerifier.Verify(ErrorMessageId.NoImplicitConv, e.Message, "C", "Dele"))
  276. result++;
  277. ;
  278. }
  279. try
  280. {
  281. c.E -= 1; // : not type info for c.E at runtime as it's null
  282. }
  283. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  284. {
  285. if (!ErrorVerifier.Verify(ErrorMessageId.NoImplicitConv, e.Message, "int", "Dele"))
  286. result++;
  287. ;
  288. }
  289. return result;
  290. }
  291. public int DoEvent(int arg)
  292. {
  293. return this.E(arg);
  294. }
  295. }
  296. // </Code>
  297. }
  298. namespace ManagedTests.DynamicCSharp.Conformance.dynamic.context.operate.compound.evnt.negative.neg005.neg005
  299. {
  300. // <Area> Dynamic -- compound operator</Area>
  301. // <Title> compound operator +=/-= on event </Title>
  302. // <Description>
  303. // Negative: rhs is non-matched delegate and lhs is null
  304. // </Description>
  305. // <RelatedBugs></RelatedBugs>
  306. // <Expects Status=success></Expects>
  307. // <Code>
  308. //<Expects Status=warning>\(17,23\).*CS0067</Expects>
  309. using System;
  310. public delegate int Dele(int i);
  311. public class C
  312. {
  313. public event Dele E;
  314. [Fact]
  315. public static void DynamicCSharpRunTest()
  316. {
  317. Assert.Equal(0, MainMethod());
  318. }
  319. public static int MainMethod()
  320. {
  321. dynamic c = new C();
  322. int result = 0;
  323. try
  324. {
  325. c.E += (Func<int, int>)(x => x);
  326. }
  327. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  328. {
  329. if (ErrorVerifier.Verify(ErrorMessageId.NoImplicitConv, e.Message, "System.Func<int,int>", "Dele"))
  330. result += 1;
  331. }
  332. try
  333. {
  334. c.E += (Func<int, int, int>)((x, y) => x);
  335. }
  336. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  337. {
  338. if (ErrorVerifier.Verify(ErrorMessageId.NoImplicitConv, e.Message, "System.Func<int,int,int>", "Dele"))
  339. result += 2;
  340. }
  341. try
  342. {
  343. // - no error for -= (by design :()
  344. // it seems that it is fixed now.
  345. c.E -= (Func<int, int>)(x => x);
  346. }
  347. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  348. {
  349. if (ErrorVerifier.Verify(ErrorMessageId.NoImplicitConv, e.Message, "System.Func<int,int>", "Dele"))
  350. result += 4;
  351. }
  352. try
  353. {
  354. // - no error for -= (by design :()
  355. c.E -= (Func<int, int, int>)((x, y) => x);
  356. }
  357. catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
  358. {
  359. if (ErrorVerifier.Verify(ErrorMessageId.NoImplicitConv, e.Message, "System.Func<int,int,int>", "Dele"))
  360. result += 8;
  361. }
  362. return (result == 15) ? 0 : 1;
  363. }
  364. }
  365. // </Code>
  366. }