PageRenderTime 53ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Core/Test/System.Linq.Expressions/ExpressionTest_Convert.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 585 lines | 449 code | 109 blank | 27 comment | 0 complexity | 9d426a308a1f381d0191c3bf9955af4b MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  1. //
  2. // ExpressionTest_Convert.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@novell.com)
  6. //
  7. // (C) 2008 Novell, Inc. (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Reflection;
  30. using System.Linq;
  31. using System.Linq.Expressions;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Linq.Expressions {
  34. [TestFixture]
  35. public class ExpressionTest_Convert {
  36. [Test]
  37. [ExpectedException (typeof (ArgumentNullException))]
  38. public void NullExpression ()
  39. {
  40. Expression.Convert (null, typeof (int));
  41. }
  42. [Test]
  43. [ExpectedException (typeof (ArgumentNullException))]
  44. public void NullType ()
  45. {
  46. Expression.Convert (1.ToConstant (), null);
  47. }
  48. [Test]
  49. [ExpectedException (typeof (InvalidOperationException))]
  50. public void ConvertIntToString ()
  51. {
  52. Expression.Convert (1.ToConstant (), typeof (string));
  53. }
  54. interface IFoo { }
  55. class Foo : IFoo { }
  56. class Bar : Foo { }
  57. class Baz { }
  58. interface ITzap { }
  59. [Test]
  60. public void ConvertBackwardAssignability ()
  61. {
  62. var c = Expression.Convert (
  63. Expression.Constant (null, typeof (Bar)), typeof (Foo));
  64. Assert.AreEqual ("Convert(null)", c.ToString ());
  65. }
  66. [Test]
  67. public void ConvertInterfaces ()
  68. {
  69. var p = Expression.Parameter (typeof (IFoo), null);
  70. var conv = Expression.Convert (p, typeof (ITzap));
  71. Assert.AreEqual (typeof (ITzap), conv.Type);
  72. Assert.AreEqual ("Convert(<param>)", conv.ToString ());
  73. p = Expression.Parameter (typeof (ITzap), null);
  74. conv = Expression.Convert (p, typeof (IFoo));
  75. Assert.AreEqual (typeof (IFoo), conv.Type);
  76. Assert.AreEqual ("Convert(<param>)", conv.ToString ());
  77. }
  78. [Test]
  79. public void ConvertCheckedInt32ToInt64 ()
  80. {
  81. var c = Expression.ConvertChecked (
  82. Expression.Constant (2, typeof (int)), typeof (long));
  83. Assert.AreEqual (ExpressionType.ConvertChecked, c.NodeType);
  84. Assert.AreEqual ("ConvertChecked(2)", c.ToString ());
  85. }
  86. [Test]
  87. public void ConvertCheckedFallbackToConvertForNonPrimitives ()
  88. {
  89. var p = Expression.ConvertChecked (
  90. Expression.Constant (null, typeof (object)), typeof (IFoo));
  91. Assert.AreEqual (ExpressionType.Convert, p.NodeType);
  92. }
  93. [Test]
  94. [ExpectedException (typeof (InvalidOperationException))]
  95. public void ConvertBazToFoo ()
  96. {
  97. Expression.Convert (Expression.Parameter (typeof (Baz), ""), typeof (Foo));
  98. }
  99. struct EineStrukt { }
  100. [Test]
  101. [ExpectedException (typeof (InvalidOperationException))]
  102. public void ConvertStructToFoo ()
  103. {
  104. Expression.Convert (Expression.Parameter (typeof (EineStrukt), ""), typeof (Foo));
  105. }
  106. [Test]
  107. [ExpectedException (typeof (InvalidOperationException))]
  108. public void ConvertInt32ToBool ()
  109. {
  110. Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (bool));
  111. }
  112. [Test]
  113. public void ConvertIFooToFoo ()
  114. {
  115. var c = Expression.Convert (Expression.Parameter (typeof (IFoo), ""), typeof (Foo));
  116. Assert.AreEqual (typeof (Foo), c.Type);
  117. Assert.IsFalse (c.IsLifted);
  118. Assert.IsFalse (c.IsLiftedToNull);
  119. Assert.IsNull (c.Method);
  120. }
  121. [Test]
  122. public void BoxInt32 ()
  123. {
  124. var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (object));
  125. Assert.AreEqual (typeof (object), c.Type);
  126. Assert.IsFalse (c.IsLifted);
  127. Assert.IsFalse (c.IsLiftedToNull);
  128. Assert.IsNull (c.Method);
  129. }
  130. [Test]
  131. public void UnBoxInt32 ()
  132. {
  133. var c = Expression.Convert (Expression.Parameter (typeof (object), ""), typeof (int));
  134. Assert.AreEqual (typeof (int), c.Type);
  135. Assert.IsFalse (c.IsLifted);
  136. Assert.IsFalse (c.IsLiftedToNull);
  137. Assert.IsNull (c.Method);
  138. }
  139. [Test]
  140. public void ConvertInt32ToInt64 ()
  141. {
  142. var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (long));
  143. Assert.AreEqual (typeof (long), c.Type);
  144. Assert.IsFalse (c.IsLifted);
  145. Assert.IsFalse (c.IsLiftedToNull);
  146. Assert.IsNull (c.Method);
  147. }
  148. [Test]
  149. public void ConvertInt64ToInt32 ()
  150. {
  151. var c = Expression.Convert (Expression.Parameter (typeof (long), ""), typeof (int));
  152. Assert.AreEqual (typeof (int), c.Type);
  153. Assert.IsFalse (c.IsLifted);
  154. Assert.IsFalse (c.IsLiftedToNull);
  155. Assert.IsNull (c.Method);
  156. }
  157. enum EineEnum { }
  158. [Test]
  159. public void ConvertEnumToInt32 ()
  160. {
  161. var c = Expression.Convert (Expression.Parameter (typeof (EineEnum), ""), typeof (int));
  162. Assert.AreEqual (typeof (int), c.Type);
  163. Assert.IsFalse (c.IsLifted);
  164. Assert.IsFalse (c.IsLiftedToNull);
  165. Assert.IsNull (c.Method);
  166. }
  167. [Test]
  168. public void ConvertNullableInt32ToInt32 ()
  169. {
  170. var c = Expression.Convert (Expression.Parameter (typeof (int?), ""), typeof (int));
  171. Assert.AreEqual (typeof (int), c.Type);
  172. Assert.IsTrue (c.IsLifted);
  173. Assert.IsFalse (c.IsLiftedToNull);
  174. Assert.IsNull (c.Method);
  175. }
  176. [Test]
  177. public void ConvertInt32ToNullableInt32 ()
  178. {
  179. var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (int?));
  180. Assert.AreEqual (typeof (int?), c.Type);
  181. Assert.IsTrue (c.IsLifted);
  182. Assert.IsTrue (c.IsLiftedToNull);
  183. Assert.IsNull (c.Method);
  184. }
  185. class Klang {
  186. int i;
  187. public Klang (int i)
  188. {
  189. this.i = i;
  190. }
  191. public static explicit operator int (Klang k)
  192. {
  193. return k.i;
  194. }
  195. }
  196. [Test]
  197. public void ConvertClassWithExplicitOp ()
  198. {
  199. var c = Expression.Convert (Expression.Parameter (typeof (Klang), ""), typeof (int));
  200. Assert.AreEqual (typeof (int), c.Type);
  201. Assert.IsFalse (c.IsLifted);
  202. Assert.IsFalse (c.IsLiftedToNull);
  203. Assert.IsNotNull (c.Method);
  204. }
  205. [Test]
  206. public void CompileConvertClassWithExplicitOp ()
  207. {
  208. var p = Expression.Parameter (typeof (Klang), "klang");
  209. var c = Expression.Lambda<Func<Klang, int>> (
  210. Expression.Convert (p, typeof (int)), p).Compile ();
  211. Assert.AreEqual (42, c (new Klang (42)));
  212. }
  213. [Test]
  214. public void ConvertClassWithExplicitOpToNullableInt ()
  215. {
  216. var c = Expression.Convert (Expression.Parameter (typeof (Klang), ""), typeof (int?));
  217. Assert.AreEqual (typeof (int?), c.Type);
  218. Assert.IsTrue (c.IsLifted);
  219. Assert.IsTrue (c.IsLiftedToNull);
  220. Assert.IsNotNull (c.Method);
  221. }
  222. struct Kling {
  223. int i;
  224. public Kling (int i)
  225. {
  226. this.i = i;
  227. }
  228. public static implicit operator int (Kling k)
  229. {
  230. return k.i;
  231. }
  232. }
  233. [Test]
  234. public void ConvertStructWithImplicitOp ()
  235. {
  236. var c = Expression.Convert (Expression.Parameter (typeof (Kling), ""), typeof (int));
  237. Assert.AreEqual (typeof (int), c.Type);
  238. Assert.IsFalse (c.IsLifted);
  239. Assert.IsFalse (c.IsLiftedToNull);
  240. Assert.IsNotNull (c.Method);
  241. }
  242. [Test]
  243. public void CompileConvertStructWithImplicitOp ()
  244. {
  245. var p = Expression.Parameter (typeof (Kling), "kling");
  246. var c = Expression.Lambda<Func<Kling, int>> (
  247. Expression.Convert (p, typeof (int)), p).Compile ();
  248. Assert.AreEqual (42, c (new Kling (42)));
  249. }
  250. [Test]
  251. public void ConvertStructWithImplicitOpToNullableInt ()
  252. {
  253. var c = Expression.Convert (Expression.Parameter (typeof (Kling), ""), typeof (int?));
  254. Assert.AreEqual (typeof (int?), c.Type);
  255. Assert.IsTrue (c.IsLifted);
  256. Assert.IsTrue (c.IsLiftedToNull);
  257. Assert.IsNotNull (c.Method);
  258. }
  259. [Test]
  260. public void ConvertNullableStructWithImplicitOpToNullableInt ()
  261. {
  262. var c = Expression.Convert (Expression.Parameter (typeof (Kling?), ""), typeof (int?));
  263. Assert.AreEqual (typeof (int?), c.Type);
  264. Assert.IsTrue (c.IsLifted);
  265. Assert.IsTrue (c.IsLiftedToNull);
  266. Assert.IsNotNull (c.Method);
  267. }
  268. [Test]
  269. public void CompiledBoxing ()
  270. {
  271. var b = Expression.Lambda<Func<object>> (
  272. Expression.Convert (42.ToConstant (), typeof (object))).Compile ();
  273. Assert.AreEqual ((object) 42, b ());
  274. }
  275. [Test]
  276. public void CompiledUnBoxing ()
  277. {
  278. var p = Expression.Parameter (typeof (object), "o");
  279. var u = Expression.Lambda<Func<object, int>> (
  280. Expression.Convert (p, typeof (int)), p).Compile ();
  281. Assert.AreEqual (42, u ((object) 42));
  282. }
  283. [Test]
  284. public void CompiledCast ()
  285. {
  286. var p = Expression.Parameter (typeof (IFoo), "foo");
  287. var c = Expression.Lambda<Func<IFoo, Bar>> (
  288. Expression.Convert (p, typeof (Bar)), p).Compile ();
  289. IFoo foo = new Bar ();
  290. Bar b = c (foo);
  291. Assert.AreEqual (b, foo);
  292. }
  293. [Test]
  294. public void CompileNotNullableToNullable ()
  295. {
  296. var p = Expression.Parameter (typeof (int), "i");
  297. var c = Expression.Lambda<Func<int, int?>> (
  298. Expression.Convert (p, typeof (int?)), p).Compile ();
  299. Assert.AreEqual ((int?) 0, c (0));
  300. Assert.AreEqual ((int?) 42, c (42));
  301. }
  302. [Test]
  303. public void CompileNullableToNotNullable ()
  304. {
  305. var p = Expression.Parameter (typeof (int?), "i");
  306. var c = Expression.Lambda<Func<int?, int>> (
  307. Expression.Convert (p, typeof (int)), p).Compile ();
  308. Assert.AreEqual (0, c ((int?) 0));
  309. Assert.AreEqual (42, c ((int?) 42));
  310. Action a = () => c (null);
  311. a.AssertThrows (typeof (InvalidOperationException));
  312. }
  313. [Test]
  314. public void CompiledConvertToSameType ()
  315. {
  316. var k = new Klang (42);
  317. var p = Expression.Parameter (typeof (Klang), "klang");
  318. var c = Expression.Lambda<Func<Klang, Klang>> (
  319. Expression.Convert (
  320. p, typeof (Klang)),
  321. p).Compile ();
  322. Assert.AreEqual (k, c (k));
  323. }
  324. [Test]
  325. public void CompiledConvertNullableToNullable ()
  326. {
  327. var p = Expression.Parameter (typeof (int?), "i");
  328. var c = Expression.Lambda<Func<int?, short?>> (
  329. Expression.Convert (p, typeof (short?)), p).Compile ();
  330. Assert.AreEqual ((short?) null, c (null));
  331. Assert.AreEqual ((short?) 12, c (12));
  332. }
  333. [Test]
  334. public void CompiledNullableBoxing ()
  335. {
  336. var p = Expression.Parameter (typeof (int?), "i");
  337. var c = Expression.Lambda<Func<int?, object>> (
  338. Expression.Convert (p, typeof (object)), p).Compile ();
  339. Assert.AreEqual (null, c (null));
  340. Assert.AreEqual ((object) (int?) 42, c (42));
  341. }
  342. [Test]
  343. public void CompiledNullableUnboxing ()
  344. {
  345. var p = Expression.Parameter (typeof (object), "o");
  346. var c = Expression.Lambda<Func<object, int?>> (
  347. Expression.Convert (p, typeof (int?)), p).Compile ();
  348. Assert.AreEqual ((int?) null, c (null));
  349. Assert.AreEqual ((int?) 42, c ((int?) 42));
  350. }
  351. [Test]
  352. public void ChainedNullableConvert ()
  353. {
  354. var p = Expression.Parameter (typeof (sbyte?), "a");
  355. var test = Expression.Lambda<Func<sbyte?, long?>> (
  356. Expression.Convert (
  357. Expression.Convert (
  358. p,
  359. typeof (int?)),
  360. typeof (long?)), p).Compile ();
  361. Assert.AreEqual ((long?) 3, test ((sbyte?) 3));
  362. Assert.AreEqual (null, test (null));
  363. }
  364. struct ImplicitToShort {
  365. short value;
  366. public ImplicitToShort (short v)
  367. {
  368. value = v;
  369. }
  370. public static implicit operator short (ImplicitToShort i)
  371. {
  372. return i.value;
  373. }
  374. }
  375. [Test]
  376. public void ConvertImplicitToShortToNullableInt ()
  377. {
  378. var a = Expression.Parameter (typeof (ImplicitToShort?), "a");
  379. var method = typeof (ImplicitToShort).GetMethod ("op_Implicit");
  380. var node = Expression.Convert (a, typeof (short), method);
  381. Assert.IsTrue (node.IsLifted);
  382. Assert.IsFalse (node.IsLiftedToNull);
  383. Assert.AreEqual (typeof (short), node.Type);
  384. Assert.AreEqual (method, node.Method);
  385. var conv = Expression.Lambda<Func<ImplicitToShort?, int?>> (
  386. Expression.Convert (
  387. node,
  388. typeof (int?)), a).Compile ();
  389. Assert.AreEqual ((int?) 42, conv (new ImplicitToShort (42)));
  390. Action convnull = () => Assert.AreEqual (null, conv (null));
  391. convnull.AssertThrows (typeof (InvalidOperationException));
  392. }
  393. [Test]
  394. public void NullableImplicitToShort ()
  395. {
  396. var i = Expression.Parameter (typeof (ImplicitToShort?), "i");
  397. var method = typeof (ImplicitToShort).GetMethod ("op_Implicit");
  398. var node = Expression.Convert (i, typeof (short?), method);
  399. Assert.IsTrue (node.IsLifted);
  400. Assert.IsTrue (node.IsLiftedToNull);
  401. Assert.AreEqual (typeof (short?), node.Type);
  402. Assert.AreEqual (method, node.Method);
  403. var convert = Expression.Lambda<Func<ImplicitToShort?, short?>> (node, i).Compile ();
  404. Assert.AreEqual ((short?) 42, convert (new ImplicitToShort (42)));
  405. }
  406. [Test]
  407. public void ConvertLongToDecimal ()
  408. {
  409. var p = Expression.Parameter (typeof (long), "l");
  410. var node = Expression.Convert (p, typeof (decimal));
  411. Assert.IsFalse (node.IsLifted);
  412. Assert.IsFalse (node.IsLiftedToNull);
  413. Assert.AreEqual (typeof (decimal), node.Type);
  414. Assert.IsNotNull (node.Method);
  415. var convert = Expression.Lambda<Func<long, decimal>> (node, p).Compile ();
  416. Assert.AreEqual (42, convert (42));
  417. }
  418. [Test]
  419. public void ConvertNullableULongToNullableDecimal ()
  420. {
  421. var p = Expression.Parameter (typeof (ulong?), "l");
  422. var node = Expression.Convert (p, typeof (decimal?));
  423. Assert.IsTrue (node.IsLifted);
  424. Assert.IsTrue (node.IsLiftedToNull);
  425. Assert.AreEqual (typeof (decimal?), node.Type);
  426. Assert.IsNotNull (node.Method);
  427. var convert = Expression.Lambda<Func<ulong?, decimal?>> (node, p).Compile ();
  428. Assert.AreEqual (42, convert (42));
  429. Assert.AreEqual (null, convert (null));
  430. }
  431. [Test]
  432. public void ConvertCheckedNullableIntToInt ()
  433. {
  434. var p = Expression.Parameter (typeof (int?), "i");
  435. var node = Expression.ConvertChecked (p, typeof (int));
  436. Assert.AreEqual (ExpressionType.ConvertChecked, node.NodeType);
  437. Assert.IsTrue (node.IsLifted);
  438. Assert.IsFalse (node.IsLiftedToNull);
  439. Assert.AreEqual (typeof (int), node.Type);
  440. Assert.IsNull (node.Method);
  441. }
  442. struct ImplicitToInt {
  443. int Value;
  444. public ImplicitToInt (int v)
  445. {
  446. Value = v;
  447. }
  448. public static implicit operator int (ImplicitToInt i)
  449. {
  450. return i.Value;
  451. }
  452. }
  453. [Test]
  454. public void ConvertNullableImplictToIntToNullableLong ()
  455. {
  456. var i = Expression.Parameter (typeof (ImplicitToInt?), "i");
  457. var method = typeof (ImplicitToInt).GetMethod ("op_Implicit");
  458. var node = Expression.Convert (i, typeof (int), method);
  459. node = Expression.Convert (node, typeof (long?));
  460. var conv = Expression.Lambda<Func<ImplicitToInt?, long?>> (node, i).Compile ();
  461. Assert.AreEqual ((long?) 42, conv (new ImplicitToInt (42)));
  462. Action convnull = () => Assert.AreEqual (null, conv (null));
  463. convnull.AssertThrows (typeof (InvalidOperationException));
  464. }
  465. [Test]
  466. [ExpectedException (typeof (InvalidOperationException))]
  467. [Category ("NotWorking")]
  468. public void ConvertNullableIntToStringWithConvertMethod ()
  469. {
  470. Expression.Convert (
  471. Expression.Constant ((int?) 0),
  472. typeof (string),
  473. typeof (Convert).GetMethod ("ToString", new [] { typeof (object) }));
  474. }
  475. }
  476. }