ExpressionTest_Convert.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. //
  2. // ExpressionTest_Convert.cs
  3. //
  4. // Author:
  5. // Jb Evain ([email protected])
  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. #if !NET_4_0
  73. Assert.AreEqual ("Convert(<param>)", conv.ToString ());
  74. #endif
  75. p = Expression.Parameter (typeof (ITzap), null);
  76. conv = Expression.Convert (p, typeof (IFoo));
  77. Assert.AreEqual (typeof (IFoo), conv.Type);
  78. #if !NET_4_0
  79. Assert.AreEqual ("Convert(<param>)", conv.ToString ());
  80. #endif
  81. }
  82. [Test]
  83. public void ConvertCheckedInt32ToInt64 ()
  84. {
  85. var c = Expression.ConvertChecked (
  86. Expression.Constant (2, typeof (int)), typeof (long));
  87. Assert.AreEqual (ExpressionType.ConvertChecked, c.NodeType);
  88. Assert.AreEqual ("ConvertChecked(2)", c.ToString ());
  89. }
  90. [Test]
  91. public void ConvertCheckedFallbackToConvertForNonPrimitives ()
  92. {
  93. var p = Expression.ConvertChecked (
  94. Expression.Constant (null, typeof (object)), typeof (IFoo));
  95. Assert.AreEqual (ExpressionType.Convert, p.NodeType);
  96. }
  97. [Test]
  98. [ExpectedException (typeof (InvalidOperationException))]
  99. public void ConvertBazToFoo ()
  100. {
  101. Expression.Convert (Expression.Parameter (typeof (Baz), ""), typeof (Foo));
  102. }
  103. struct EineStrukt { }
  104. [Test]
  105. [ExpectedException (typeof (InvalidOperationException))]
  106. public void ConvertStructToFoo ()
  107. {
  108. Expression.Convert (Expression.Parameter (typeof (EineStrukt), ""), typeof (Foo));
  109. }
  110. [Test]
  111. [ExpectedException (typeof (InvalidOperationException))]
  112. public void ConvertInt32ToBool ()
  113. {
  114. Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (bool));
  115. }
  116. [Test]
  117. public void ConvertIFooToFoo ()
  118. {
  119. var c = Expression.Convert (Expression.Parameter (typeof (IFoo), ""), typeof (Foo));
  120. Assert.AreEqual (typeof (Foo), c.Type);
  121. Assert.IsFalse (c.IsLifted);
  122. Assert.IsFalse (c.IsLiftedToNull);
  123. Assert.IsNull (c.Method);
  124. }
  125. [Test]
  126. public void BoxInt32 ()
  127. {
  128. var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (object));
  129. Assert.AreEqual (typeof (object), c.Type);
  130. Assert.IsFalse (c.IsLifted);
  131. Assert.IsFalse (c.IsLiftedToNull);
  132. Assert.IsNull (c.Method);
  133. }
  134. [Test]
  135. public void UnBoxInt32 ()
  136. {
  137. var c = Expression.Convert (Expression.Parameter (typeof (object), ""), typeof (int));
  138. Assert.AreEqual (typeof (int), c.Type);
  139. Assert.IsFalse (c.IsLifted);
  140. Assert.IsFalse (c.IsLiftedToNull);
  141. Assert.IsNull (c.Method);
  142. }
  143. [Test]
  144. public void ConvertInt32ToInt64 ()
  145. {
  146. var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (long));
  147. Assert.AreEqual (typeof (long), c.Type);
  148. Assert.IsFalse (c.IsLifted);
  149. Assert.IsFalse (c.IsLiftedToNull);
  150. Assert.IsNull (c.Method);
  151. }
  152. [Test]
  153. public void ConvertInt64ToInt32 ()
  154. {
  155. var c = Expression.Convert (Expression.Parameter (typeof (long), ""), typeof (int));
  156. Assert.AreEqual (typeof (int), c.Type);
  157. Assert.IsFalse (c.IsLifted);
  158. Assert.IsFalse (c.IsLiftedToNull);
  159. Assert.IsNull (c.Method);
  160. }
  161. enum EineEnum {
  162. EineValue,
  163. }
  164. [Test]
  165. public void ConvertEnumToInt32 ()
  166. {
  167. var c = Expression.Convert (Expression.Parameter (typeof (EineEnum), ""), typeof (int));
  168. Assert.AreEqual (typeof (int), c.Type);
  169. Assert.IsFalse (c.IsLifted);
  170. Assert.IsFalse (c.IsLiftedToNull);
  171. Assert.IsNull (c.Method);
  172. }
  173. [Test]
  174. public void ConvertNullableInt32ToInt32 ()
  175. {
  176. var c = Expression.Convert (Expression.Parameter (typeof (int?), ""), typeof (int));
  177. Assert.AreEqual (typeof (int), c.Type);
  178. Assert.IsTrue (c.IsLifted);
  179. Assert.IsFalse (c.IsLiftedToNull);
  180. Assert.IsNull (c.Method);
  181. }
  182. [Test]
  183. public void ConvertInt32ToNullableInt32 ()
  184. {
  185. var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (int?));
  186. Assert.AreEqual (typeof (int?), c.Type);
  187. Assert.IsTrue (c.IsLifted);
  188. Assert.IsTrue (c.IsLiftedToNull);
  189. Assert.IsNull (c.Method);
  190. }
  191. class Klang {
  192. int i;
  193. public Klang (int i)
  194. {
  195. this.i = i;
  196. }
  197. public static explicit operator int (Klang k)
  198. {
  199. return k.i;
  200. }
  201. }
  202. [Test]
  203. public void ConvertClassWithExplicitOp ()
  204. {
  205. var c = Expression.Convert (Expression.Parameter (typeof (Klang), ""), typeof (int));
  206. Assert.AreEqual (typeof (int), c.Type);
  207. Assert.IsFalse (c.IsLifted);
  208. Assert.IsFalse (c.IsLiftedToNull);
  209. Assert.IsNotNull (c.Method);
  210. }
  211. [Test]
  212. public void CompileConvertClassWithExplicitOp ()
  213. {
  214. var p = Expression.Parameter (typeof (Klang), "klang");
  215. var c = Expression.Lambda<Func<Klang, int>> (
  216. Expression.Convert (p, typeof (int)), p).Compile ();
  217. Assert.AreEqual (42, c (new Klang (42)));
  218. }
  219. [Test]
  220. public void ConvertClassWithExplicitOpToNullableInt ()
  221. {
  222. var c = Expression.Convert (Expression.Parameter (typeof (Klang), ""), typeof (int?));
  223. Assert.AreEqual (typeof (int?), c.Type);
  224. Assert.IsTrue (c.IsLifted);
  225. Assert.IsTrue (c.IsLiftedToNull);
  226. Assert.IsNotNull (c.Method);
  227. }
  228. struct Kling {
  229. int i;
  230. public Kling (int i)
  231. {
  232. this.i = i;
  233. }
  234. public static implicit operator int (Kling k)
  235. {
  236. return k.i;
  237. }
  238. }
  239. [Test]
  240. public void ConvertStructWithImplicitOp ()
  241. {
  242. var c = Expression.Convert (Expression.Parameter (typeof (Kling), ""), typeof (int));
  243. Assert.AreEqual (typeof (int), c.Type);
  244. Assert.IsFalse (c.IsLifted);
  245. Assert.IsFalse (c.IsLiftedToNull);
  246. Assert.IsNotNull (c.Method);
  247. }
  248. [Test]
  249. public void CompileConvertStructWithImplicitOp ()
  250. {
  251. var p = Expression.Parameter (typeof (Kling), "kling");
  252. var c = Expression.Lambda<Func<Kling, int>> (
  253. Expression.Convert (p, typeof (int)), p).Compile ();
  254. Assert.AreEqual (42, c (new Kling (42)));
  255. }
  256. [Test]
  257. public void ConvertStructWithImplicitOpToNullableInt ()
  258. {
  259. var c = Expression.Convert (Expression.Parameter (typeof (Kling), ""), typeof (int?));
  260. Assert.AreEqual (typeof (int?), c.Type);
  261. Assert.IsTrue (c.IsLifted);
  262. Assert.IsTrue (c.IsLiftedToNull);
  263. Assert.IsNotNull (c.Method);
  264. }
  265. [Test]
  266. public void ConvertNullableStructWithImplicitOpToNullableInt ()
  267. {
  268. var c = Expression.Convert (Expression.Parameter (typeof (Kling?), ""), typeof (int?));
  269. Assert.AreEqual (typeof (int?), c.Type);
  270. Assert.IsTrue (c.IsLifted);
  271. Assert.IsTrue (c.IsLiftedToNull);
  272. Assert.IsNotNull (c.Method);
  273. }
  274. [Test]
  275. public void CompiledBoxing ()
  276. {
  277. var b = Expression.Lambda<Func<object>> (
  278. Expression.Convert (42.ToConstant (), typeof (object))).Compile ();
  279. Assert.AreEqual ((object) 42, b ());
  280. }
  281. [Test]
  282. public void CompiledUnBoxing ()
  283. {
  284. var p = Expression.Parameter (typeof (object), "o");
  285. var u = Expression.Lambda<Func<object, int>> (
  286. Expression.Convert (p, typeof (int)), p).Compile ();
  287. Assert.AreEqual (42, u ((object) 42));
  288. }
  289. [Test]
  290. public void CompiledCast ()
  291. {
  292. var p = Expression.Parameter (typeof (IFoo), "foo");
  293. var c = Expression.Lambda<Func<IFoo, Bar>> (
  294. Expression.Convert (p, typeof (Bar)), p).Compile ();
  295. IFoo foo = new Bar ();
  296. Bar b = c (foo);
  297. Assert.AreEqual (b, foo);
  298. }
  299. [Test]
  300. public void CompileNotNullableToNullable ()
  301. {
  302. var p = Expression.Parameter (typeof (int), "i");
  303. var c = Expression.Lambda<Func<int, int?>> (
  304. Expression.Convert (p, typeof (int?)), p).Compile ();
  305. Assert.AreEqual ((int?) 0, c (0));
  306. Assert.AreEqual ((int?) 42, c (42));
  307. }
  308. [Test]
  309. [Category ("NotWorkingInterpreter")]
  310. public void CompileNullableToNotNullable ()
  311. {
  312. var p = Expression.Parameter (typeof (int?), "i");
  313. var c = Expression.Lambda<Func<int?, int>> (
  314. Expression.Convert (p, typeof (int)), p).Compile ();
  315. Assert.AreEqual (0, c ((int?) 0));
  316. Assert.AreEqual (42, c ((int?) 42));
  317. Action a = () => c (null);
  318. a.AssertThrows (typeof (InvalidOperationException));
  319. }
  320. [Test]
  321. public void CompiledConvertToSameType ()
  322. {
  323. var k = new Klang (42);
  324. var p = Expression.Parameter (typeof (Klang), "klang");
  325. var c = Expression.Lambda<Func<Klang, Klang>> (
  326. Expression.Convert (
  327. p, typeof (Klang)),
  328. p).Compile ();
  329. Assert.AreEqual (k, c (k));
  330. }
  331. [Test]
  332. public void CompiledConvertNullableToNullable ()
  333. {
  334. var p = Expression.Parameter (typeof (int?), "i");
  335. var c = Expression.Lambda<Func<int?, short?>> (
  336. Expression.Convert (p, typeof (short?)), p).Compile ();
  337. Assert.AreEqual ((short?) null, c (null));
  338. Assert.AreEqual ((short?) 12, c (12));
  339. }
  340. [Test]
  341. public void CompiledNullableBoxing ()
  342. {
  343. var p = Expression.Parameter (typeof (int?), "i");
  344. var c = Expression.Lambda<Func<int?, object>> (
  345. Expression.Convert (p, typeof (object)), p).Compile ();
  346. Assert.AreEqual (null, c (null));
  347. Assert.AreEqual ((object) (int?) 42, c (42));
  348. }
  349. [Test]
  350. public void CompiledNullableUnboxing ()
  351. {
  352. var p = Expression.Parameter (typeof (object), "o");
  353. var c = Expression.Lambda<Func<object, int?>> (
  354. Expression.Convert (p, typeof (int?)), p).Compile ();
  355. Assert.AreEqual ((int?) null, c (null));
  356. Assert.AreEqual ((int?) 42, c ((int?) 42));
  357. }
  358. [Test]
  359. public void ChainedNullableConvert ()
  360. {
  361. var p = Expression.Parameter (typeof (sbyte?), "a");
  362. var test = Expression.Lambda<Func<sbyte?, long?>> (
  363. Expression.Convert (
  364. Expression.Convert (
  365. p,
  366. typeof (int?)),
  367. typeof (long?)), p).Compile ();
  368. Assert.AreEqual ((long?) 3, test ((sbyte?) 3));
  369. Assert.AreEqual (null, test (null));
  370. }
  371. struct ImplicitToShort {
  372. short value;
  373. public ImplicitToShort (short v)
  374. {
  375. value = v;
  376. }
  377. public static implicit operator short (ImplicitToShort i)
  378. {
  379. return i.value;
  380. }
  381. }
  382. [Test]
  383. [Category ("NotWorkingInterpreter")]
  384. public void ConvertImplicitToShortToNullableInt ()
  385. {
  386. var a = Expression.Parameter (typeof (ImplicitToShort?), "a");
  387. var method = typeof (ImplicitToShort).GetMethod ("op_Implicit");
  388. var node = Expression.Convert (a, typeof (short), method);
  389. Assert.IsTrue (node.IsLifted);
  390. Assert.IsFalse (node.IsLiftedToNull);
  391. Assert.AreEqual (typeof (short), node.Type);
  392. Assert.AreEqual (method, node.Method);
  393. var conv = Expression.Lambda<Func<ImplicitToShort?, int?>> (
  394. Expression.Convert (
  395. node,
  396. typeof (int?)), a).Compile ();
  397. Assert.AreEqual ((int?) 42, conv (new ImplicitToShort (42)));
  398. Action convnull = () => Assert.AreEqual (null, conv (null));
  399. convnull.AssertThrows (typeof (InvalidOperationException));
  400. }
  401. [Test]
  402. [Category ("NotWorkingInterpreter")]
  403. public void NullableImplicitToShort ()
  404. {
  405. var i = Expression.Parameter (typeof (ImplicitToShort?), "i");
  406. var method = typeof (ImplicitToShort).GetMethod ("op_Implicit");
  407. var node = Expression.Convert (i, typeof (short?), method);
  408. Assert.IsTrue (node.IsLifted);
  409. Assert.IsTrue (node.IsLiftedToNull);
  410. Assert.AreEqual (typeof (short?), node.Type);
  411. Assert.AreEqual (method, node.Method);
  412. var convert = Expression.Lambda<Func<ImplicitToShort?, short?>> (node, i).Compile ();
  413. Assert.AreEqual ((short?) 42, convert (new ImplicitToShort (42)));
  414. }
  415. [Test]
  416. public void ConvertLongToDecimal ()
  417. {
  418. var p = Expression.Parameter (typeof (long), "l");
  419. var node = Expression.Convert (p, typeof (decimal));
  420. Assert.IsFalse (node.IsLifted);
  421. Assert.IsFalse (node.IsLiftedToNull);
  422. Assert.AreEqual (typeof (decimal), node.Type);
  423. Assert.IsNotNull (node.Method);
  424. var convert = Expression.Lambda<Func<long, decimal>> (node, p).Compile ();
  425. Assert.AreEqual (42, convert (42));
  426. }
  427. [Test]
  428. [Category ("NotWorkingInterpreter")]
  429. public void ConvertNullableULongToNullableDecimal ()
  430. {
  431. var p = Expression.Parameter (typeof (ulong?), "l");
  432. var node = Expression.Convert (p, typeof (decimal?));
  433. Assert.IsTrue (node.IsLifted);
  434. Assert.IsTrue (node.IsLiftedToNull);
  435. Assert.AreEqual (typeof (decimal?), node.Type);
  436. Assert.IsNotNull (node.Method);
  437. var convert = Expression.Lambda<Func<ulong?, decimal?>> (node, p).Compile ();
  438. Assert.AreEqual (42, convert (42));
  439. Assert.AreEqual (null, convert (null));
  440. }
  441. [Test]
  442. public void ConvertCheckedNullableIntToInt ()
  443. {
  444. var p = Expression.Parameter (typeof (int?), "i");
  445. var node = Expression.ConvertChecked (p, typeof (int));
  446. Assert.AreEqual (ExpressionType.ConvertChecked, node.NodeType);
  447. Assert.IsTrue (node.IsLifted);
  448. Assert.IsFalse (node.IsLiftedToNull);
  449. Assert.AreEqual (typeof (int), node.Type);
  450. Assert.IsNull (node.Method);
  451. }
  452. struct ImplicitToInt {
  453. int Value;
  454. public ImplicitToInt (int v)
  455. {
  456. Value = v;
  457. }
  458. public static implicit operator int (ImplicitToInt i)
  459. {
  460. return i.Value;
  461. }
  462. }
  463. [Test]
  464. [Category ("NotWorkingInterpreter")]
  465. public void ConvertNullableImplictToIntToNullableLong ()
  466. {
  467. var i = Expression.Parameter (typeof (ImplicitToInt?), "i");
  468. var method = typeof (ImplicitToInt).GetMethod ("op_Implicit");
  469. var node = Expression.Convert (i, typeof (int), method);
  470. node = Expression.Convert (node, typeof (long?));
  471. var conv = Expression.Lambda<Func<ImplicitToInt?, long?>> (node, i).Compile ();
  472. Assert.AreEqual ((long?) 42, conv (new ImplicitToInt (42)));
  473. Action convnull = () => Assert.AreEqual (null, conv (null));
  474. convnull.AssertThrows (typeof (InvalidOperationException));
  475. }
  476. [Test]
  477. [ExpectedException (typeof (InvalidOperationException))]
  478. [Category ("NotWorking")]
  479. public void ConvertNullableIntToStringWithConvertMethod ()
  480. {
  481. Expression.Convert (
  482. Expression.Constant ((int?) 0),
  483. typeof (string),
  484. typeof (Convert).GetMethod ("ToString", new [] { typeof (object) }));
  485. }
  486. [Test] // #678897
  487. public void ConvertEnumValueToEnum ()
  488. {
  489. var node = Expression.Convert (
  490. Expression.Constant (EineEnum.EineValue, typeof (EineEnum)),
  491. typeof (Enum));
  492. Assert.IsNotNull (node);
  493. Assert.AreEqual (typeof (Enum), node.Type);
  494. }
  495. }
  496. }