ExpressionTest_Convert.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. p = Expression.Parameter (typeof (ITzap), null);
  73. conv = Expression.Convert (p, typeof (IFoo));
  74. Assert.AreEqual (typeof (IFoo), conv.Type);
  75. }
  76. [Test]
  77. public void ConvertCheckedInt32ToInt64 ()
  78. {
  79. var c = Expression.ConvertChecked (
  80. Expression.Constant (2, typeof (int)), typeof (long));
  81. Assert.AreEqual (ExpressionType.ConvertChecked, c.NodeType);
  82. Assert.AreEqual ("ConvertChecked(2)", c.ToString ());
  83. }
  84. [Test]
  85. public void ConvertCheckedFallbackToConvertForNonPrimitives ()
  86. {
  87. var p = Expression.ConvertChecked (
  88. Expression.Constant (null, typeof (object)), typeof (IFoo));
  89. Assert.AreEqual (ExpressionType.Convert, p.NodeType);
  90. }
  91. [Test]
  92. [ExpectedException (typeof (InvalidOperationException))]
  93. public void ConvertBazToFoo ()
  94. {
  95. Expression.Convert (Expression.Parameter (typeof (Baz), ""), typeof (Foo));
  96. }
  97. struct EineStrukt { }
  98. [Test]
  99. [ExpectedException (typeof (InvalidOperationException))]
  100. public void ConvertStructToFoo ()
  101. {
  102. Expression.Convert (Expression.Parameter (typeof (EineStrukt), ""), typeof (Foo));
  103. }
  104. [Test]
  105. [ExpectedException (typeof (InvalidOperationException))]
  106. public void ConvertInt32ToBool ()
  107. {
  108. Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (bool));
  109. }
  110. [Test]
  111. public void ConvertIFooToFoo ()
  112. {
  113. var c = Expression.Convert (Expression.Parameter (typeof (IFoo), ""), typeof (Foo));
  114. Assert.AreEqual (typeof (Foo), c.Type);
  115. Assert.IsFalse (c.IsLifted);
  116. Assert.IsFalse (c.IsLiftedToNull);
  117. Assert.IsNull (c.Method);
  118. }
  119. [Test]
  120. public void BoxInt32 ()
  121. {
  122. var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (object));
  123. Assert.AreEqual (typeof (object), c.Type);
  124. Assert.IsFalse (c.IsLifted);
  125. Assert.IsFalse (c.IsLiftedToNull);
  126. Assert.IsNull (c.Method);
  127. }
  128. [Test]
  129. public void UnBoxInt32 ()
  130. {
  131. var c = Expression.Convert (Expression.Parameter (typeof (object), ""), typeof (int));
  132. Assert.AreEqual (typeof (int), c.Type);
  133. Assert.IsFalse (c.IsLifted);
  134. Assert.IsFalse (c.IsLiftedToNull);
  135. Assert.IsNull (c.Method);
  136. }
  137. [Test]
  138. public void ConvertInt32ToInt64 ()
  139. {
  140. var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (long));
  141. Assert.AreEqual (typeof (long), c.Type);
  142. Assert.IsFalse (c.IsLifted);
  143. Assert.IsFalse (c.IsLiftedToNull);
  144. Assert.IsNull (c.Method);
  145. }
  146. [Test]
  147. public void ConvertInt64ToInt32 ()
  148. {
  149. var c = Expression.Convert (Expression.Parameter (typeof (long), ""), typeof (int));
  150. Assert.AreEqual (typeof (int), c.Type);
  151. Assert.IsFalse (c.IsLifted);
  152. Assert.IsFalse (c.IsLiftedToNull);
  153. Assert.IsNull (c.Method);
  154. }
  155. enum EineEnum {
  156. EineValue,
  157. }
  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. [Category ("NotWorkingInterpreter")]
  304. public void CompileNullableToNotNullable ()
  305. {
  306. var p = Expression.Parameter (typeof (int?), "i");
  307. var c = Expression.Lambda<Func<int?, int>> (
  308. Expression.Convert (p, typeof (int)), p).Compile ();
  309. Assert.AreEqual (0, c ((int?) 0));
  310. Assert.AreEqual (42, c ((int?) 42));
  311. Action a = () => c (null);
  312. a.AssertThrows (typeof (InvalidOperationException));
  313. }
  314. [Test]
  315. public void CompiledConvertToSameType ()
  316. {
  317. var k = new Klang (42);
  318. var p = Expression.Parameter (typeof (Klang), "klang");
  319. var c = Expression.Lambda<Func<Klang, Klang>> (
  320. Expression.Convert (
  321. p, typeof (Klang)),
  322. p).Compile ();
  323. Assert.AreEqual (k, c (k));
  324. }
  325. [Test]
  326. public void CompiledConvertNullableToNullable ()
  327. {
  328. var p = Expression.Parameter (typeof (int?), "i");
  329. var c = Expression.Lambda<Func<int?, short?>> (
  330. Expression.Convert (p, typeof (short?)), p).Compile ();
  331. Assert.AreEqual ((short?) null, c (null));
  332. Assert.AreEqual ((short?) 12, c (12));
  333. }
  334. [Test]
  335. public void CompiledNullableBoxing ()
  336. {
  337. var p = Expression.Parameter (typeof (int?), "i");
  338. var c = Expression.Lambda<Func<int?, object>> (
  339. Expression.Convert (p, typeof (object)), p).Compile ();
  340. Assert.AreEqual (null, c (null));
  341. Assert.AreEqual ((object) (int?) 42, c (42));
  342. }
  343. [Test]
  344. public void CompiledNullableUnboxing ()
  345. {
  346. var p = Expression.Parameter (typeof (object), "o");
  347. var c = Expression.Lambda<Func<object, int?>> (
  348. Expression.Convert (p, typeof (int?)), p).Compile ();
  349. Assert.AreEqual ((int?) null, c (null));
  350. Assert.AreEqual ((int?) 42, c ((int?) 42));
  351. }
  352. [Test]
  353. public void ChainedNullableConvert ()
  354. {
  355. var p = Expression.Parameter (typeof (sbyte?), "a");
  356. var test = Expression.Lambda<Func<sbyte?, long?>> (
  357. Expression.Convert (
  358. Expression.Convert (
  359. p,
  360. typeof (int?)),
  361. typeof (long?)), p).Compile ();
  362. Assert.AreEqual ((long?) 3, test ((sbyte?) 3));
  363. Assert.AreEqual (null, test (null));
  364. }
  365. struct ImplicitToShort {
  366. short value;
  367. public ImplicitToShort (short v)
  368. {
  369. value = v;
  370. }
  371. public static implicit operator short (ImplicitToShort i)
  372. {
  373. return i.value;
  374. }
  375. }
  376. [Test]
  377. [Category ("NotWorkingInterpreter")]
  378. public void ConvertImplicitToShortToNullableInt ()
  379. {
  380. var a = Expression.Parameter (typeof (ImplicitToShort?), "a");
  381. var method = typeof (ImplicitToShort).GetMethod ("op_Implicit");
  382. var node = Expression.Convert (a, typeof (short), method);
  383. Assert.IsTrue (node.IsLifted);
  384. Assert.IsFalse (node.IsLiftedToNull);
  385. Assert.AreEqual (typeof (short), node.Type);
  386. Assert.AreEqual (method, node.Method);
  387. var conv = Expression.Lambda<Func<ImplicitToShort?, int?>> (
  388. Expression.Convert (
  389. node,
  390. typeof (int?)), a).Compile ();
  391. Assert.AreEqual ((int?) 42, conv (new ImplicitToShort (42)));
  392. Action convnull = () => Assert.AreEqual (null, conv (null));
  393. convnull.AssertThrows (typeof (InvalidOperationException));
  394. }
  395. [Test]
  396. [Category ("NotWorkingInterpreter")]
  397. public void NullableImplicitToShort ()
  398. {
  399. var i = Expression.Parameter (typeof (ImplicitToShort?), "i");
  400. var method = typeof (ImplicitToShort).GetMethod ("op_Implicit");
  401. var node = Expression.Convert (i, typeof (short?), method);
  402. Assert.IsTrue (node.IsLifted);
  403. Assert.IsTrue (node.IsLiftedToNull);
  404. Assert.AreEqual (typeof (short?), node.Type);
  405. Assert.AreEqual (method, node.Method);
  406. var convert = Expression.Lambda<Func<ImplicitToShort?, short?>> (node, i).Compile ();
  407. Assert.AreEqual ((short?) 42, convert (new ImplicitToShort (42)));
  408. }
  409. [Test]
  410. public void ConvertLongToDecimal ()
  411. {
  412. var p = Expression.Parameter (typeof (long), "l");
  413. var node = Expression.Convert (p, typeof (decimal));
  414. Assert.IsFalse (node.IsLifted);
  415. Assert.IsFalse (node.IsLiftedToNull);
  416. Assert.AreEqual (typeof (decimal), node.Type);
  417. Assert.IsNotNull (node.Method);
  418. var convert = Expression.Lambda<Func<long, decimal>> (node, p).Compile ();
  419. Assert.AreEqual (42, convert (42));
  420. }
  421. [Test]
  422. [Category ("NotWorkingInterpreter")]
  423. public void ConvertNullableULongToNullableDecimal ()
  424. {
  425. var p = Expression.Parameter (typeof (ulong?), "l");
  426. var node = Expression.Convert (p, typeof (decimal?));
  427. Assert.IsTrue (node.IsLifted);
  428. Assert.IsTrue (node.IsLiftedToNull);
  429. Assert.AreEqual (typeof (decimal?), node.Type);
  430. Assert.IsNotNull (node.Method);
  431. var convert = Expression.Lambda<Func<ulong?, decimal?>> (node, p).Compile ();
  432. Assert.AreEqual (42, convert (42));
  433. Assert.AreEqual (null, convert (null));
  434. }
  435. [Test]
  436. public void ConvertCheckedNullableIntToInt ()
  437. {
  438. var p = Expression.Parameter (typeof (int?), "i");
  439. var node = Expression.ConvertChecked (p, typeof (int));
  440. Assert.AreEqual (ExpressionType.ConvertChecked, node.NodeType);
  441. Assert.IsTrue (node.IsLifted);
  442. Assert.IsFalse (node.IsLiftedToNull);
  443. Assert.AreEqual (typeof (int), node.Type);
  444. Assert.IsNull (node.Method);
  445. }
  446. struct ImplicitToInt {
  447. int Value;
  448. public ImplicitToInt (int v)
  449. {
  450. Value = v;
  451. }
  452. public static implicit operator int (ImplicitToInt i)
  453. {
  454. return i.Value;
  455. }
  456. }
  457. [Test]
  458. [Category ("NotWorkingInterpreter")]
  459. public void ConvertNullableImplictToIntToNullableLong ()
  460. {
  461. var i = Expression.Parameter (typeof (ImplicitToInt?), "i");
  462. var method = typeof (ImplicitToInt).GetMethod ("op_Implicit");
  463. var node = Expression.Convert (i, typeof (int), method);
  464. node = Expression.Convert (node, typeof (long?));
  465. var conv = Expression.Lambda<Func<ImplicitToInt?, long?>> (node, i).Compile ();
  466. Assert.AreEqual ((long?) 42, conv (new ImplicitToInt (42)));
  467. Action convnull = () => Assert.AreEqual (null, conv (null));
  468. convnull.AssertThrows (typeof (InvalidOperationException));
  469. }
  470. [Test]
  471. [ExpectedException (typeof (InvalidOperationException))]
  472. [Category ("NotWorking")]
  473. public void ConvertNullableIntToStringWithConvertMethod ()
  474. {
  475. Expression.Convert (
  476. Expression.Constant ((int?) 0),
  477. typeof (string),
  478. typeof (Convert).GetMethod ("ToString", new [] { typeof (object) }));
  479. }
  480. [Test] // #678897
  481. public void ConvertEnumValueToEnum ()
  482. {
  483. var node = Expression.Convert (
  484. Expression.Constant (EineEnum.EineValue, typeof (EineEnum)),
  485. typeof (Enum));
  486. Assert.IsNotNull (node);
  487. Assert.AreEqual (typeof (Enum), node.Type);
  488. }
  489. }
  490. }