ExpressionTest_Convert.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. [Category("SRE")]
  36. public class ExpressionTest_Convert {
  37. [Test]
  38. [ExpectedException (typeof (ArgumentNullException))]
  39. public void NullExpression ()
  40. {
  41. Expression.Convert (null, typeof (int));
  42. }
  43. [Test]
  44. [ExpectedException (typeof (ArgumentNullException))]
  45. public void NullType ()
  46. {
  47. Expression.Convert (1.ToConstant (), null);
  48. }
  49. [Test]
  50. [ExpectedException (typeof (InvalidOperationException))]
  51. public void ConvertIntToString ()
  52. {
  53. Expression.Convert (1.ToConstant (), typeof (string));
  54. }
  55. interface IFoo { }
  56. class Foo : IFoo { }
  57. class Bar : Foo { }
  58. class Baz { }
  59. interface ITzap { }
  60. [Test]
  61. public void ConvertBackwardAssignability ()
  62. {
  63. var c = Expression.Convert (
  64. Expression.Constant (null, typeof (Bar)), typeof (Foo));
  65. Assert.AreEqual ("Convert(null, Foo)", c.ToString ());
  66. }
  67. [Test]
  68. public void ConvertInterfaces ()
  69. {
  70. var p = Expression.Parameter (typeof (IFoo), null);
  71. var conv = Expression.Convert (p, typeof (ITzap));
  72. Assert.AreEqual (typeof (ITzap), conv.Type);
  73. p = Expression.Parameter (typeof (ITzap), null);
  74. conv = Expression.Convert (p, typeof (IFoo));
  75. Assert.AreEqual (typeof (IFoo), conv.Type);
  76. }
  77. [Test]
  78. public void ConvertCheckedInt32ToInt64 ()
  79. {
  80. var c = Expression.ConvertChecked (
  81. Expression.Constant (2, typeof (int)), typeof (long));
  82. Assert.AreEqual (ExpressionType.ConvertChecked, c.NodeType);
  83. Assert.AreEqual ("ConvertChecked(2, Int64)", c.ToString ());
  84. }
  85. [Test]
  86. public void ConvertCheckedFallbackToConvertForNonPrimitives ()
  87. {
  88. var p = Expression.ConvertChecked (
  89. Expression.Constant (null, typeof (object)), typeof (IFoo));
  90. Assert.AreEqual (ExpressionType.Convert, p.NodeType);
  91. }
  92. [Test]
  93. [ExpectedException (typeof (InvalidOperationException))]
  94. public void ConvertBazToFoo ()
  95. {
  96. Expression.Convert (Expression.Parameter (typeof (Baz), ""), typeof (Foo));
  97. }
  98. struct EineStrukt { }
  99. [Test]
  100. [ExpectedException (typeof (InvalidOperationException))]
  101. public void ConvertStructToFoo ()
  102. {
  103. Expression.Convert (Expression.Parameter (typeof (EineStrukt), ""), typeof (Foo));
  104. }
  105. [Test]
  106. [ExpectedException (typeof (InvalidOperationException))]
  107. public void ConvertInt32ToBool ()
  108. {
  109. Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (bool));
  110. }
  111. [Test]
  112. public void ConvertIFooToFoo ()
  113. {
  114. var c = Expression.Convert (Expression.Parameter (typeof (IFoo), ""), typeof (Foo));
  115. Assert.AreEqual (typeof (Foo), c.Type);
  116. Assert.IsFalse (c.IsLifted);
  117. Assert.IsFalse (c.IsLiftedToNull);
  118. Assert.IsNull (c.Method);
  119. }
  120. [Test]
  121. public void BoxInt32 ()
  122. {
  123. var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (object));
  124. Assert.AreEqual (typeof (object), c.Type);
  125. Assert.IsFalse (c.IsLifted);
  126. Assert.IsFalse (c.IsLiftedToNull);
  127. Assert.IsNull (c.Method);
  128. }
  129. [Test]
  130. public void UnBoxInt32 ()
  131. {
  132. var c = Expression.Convert (Expression.Parameter (typeof (object), ""), typeof (int));
  133. Assert.AreEqual (typeof (int), c.Type);
  134. Assert.IsFalse (c.IsLifted);
  135. Assert.IsFalse (c.IsLiftedToNull);
  136. Assert.IsNull (c.Method);
  137. }
  138. [Test]
  139. public void ConvertInt32ToInt64 ()
  140. {
  141. var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (long));
  142. Assert.AreEqual (typeof (long), c.Type);
  143. Assert.IsFalse (c.IsLifted);
  144. Assert.IsFalse (c.IsLiftedToNull);
  145. Assert.IsNull (c.Method);
  146. }
  147. [Test]
  148. public void ConvertInt64ToInt32 ()
  149. {
  150. var c = Expression.Convert (Expression.Parameter (typeof (long), ""), typeof (int));
  151. Assert.AreEqual (typeof (int), c.Type);
  152. Assert.IsFalse (c.IsLifted);
  153. Assert.IsFalse (c.IsLiftedToNull);
  154. Assert.IsNull (c.Method);
  155. }
  156. enum EineEnum {
  157. EineValue,
  158. }
  159. [Test]
  160. public void ConvertEnumToInt32 ()
  161. {
  162. var c = Expression.Convert (Expression.Parameter (typeof (EineEnum), ""), typeof (int));
  163. Assert.AreEqual (typeof (int), c.Type);
  164. Assert.IsFalse (c.IsLifted);
  165. Assert.IsFalse (c.IsLiftedToNull);
  166. Assert.IsNull (c.Method);
  167. }
  168. [Test]
  169. public void ConvertNullableInt32ToInt32 ()
  170. {
  171. var c = Expression.Convert (Expression.Parameter (typeof (int?), ""), typeof (int));
  172. Assert.AreEqual (typeof (int), c.Type);
  173. Assert.IsTrue (c.IsLifted);
  174. Assert.IsFalse (c.IsLiftedToNull);
  175. Assert.IsNull (c.Method);
  176. }
  177. [Test]
  178. public void ConvertInt32ToNullableInt32 ()
  179. {
  180. var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (int?));
  181. Assert.AreEqual (typeof (int?), c.Type);
  182. Assert.IsTrue (c.IsLifted);
  183. Assert.IsTrue (c.IsLiftedToNull);
  184. Assert.IsNull (c.Method);
  185. }
  186. class Klang {
  187. int i;
  188. public Klang (int i)
  189. {
  190. this.i = i;
  191. }
  192. public static explicit operator int (Klang k)
  193. {
  194. return k.i;
  195. }
  196. }
  197. [Test]
  198. public void ConvertClassWithExplicitOp ()
  199. {
  200. var c = Expression.Convert (Expression.Parameter (typeof (Klang), ""), typeof (int));
  201. Assert.AreEqual (typeof (int), c.Type);
  202. Assert.IsFalse (c.IsLifted);
  203. Assert.IsFalse (c.IsLiftedToNull);
  204. Assert.IsNotNull (c.Method);
  205. }
  206. [Test]
  207. public void CompileConvertClassWithExplicitOp ()
  208. {
  209. var p = Expression.Parameter (typeof (Klang), "klang");
  210. var c = Expression.Lambda<Func<Klang, int>> (
  211. Expression.Convert (p, typeof (int)), p).Compile ();
  212. Assert.AreEqual (42, c (new Klang (42)));
  213. }
  214. [Test]
  215. public void ConvertClassWithExplicitOpToNullableInt ()
  216. {
  217. var c = Expression.Convert (Expression.Parameter (typeof (Klang), ""), typeof (int?));
  218. Assert.AreEqual (typeof (int?), c.Type);
  219. Assert.IsTrue (c.IsLifted);
  220. Assert.IsTrue (c.IsLiftedToNull);
  221. Assert.IsNotNull (c.Method);
  222. }
  223. struct Kling {
  224. int i;
  225. public Kling (int i)
  226. {
  227. this.i = i;
  228. }
  229. public static implicit operator int (Kling k)
  230. {
  231. return k.i;
  232. }
  233. }
  234. [Test]
  235. public void ConvertStructWithImplicitOp ()
  236. {
  237. var c = Expression.Convert (Expression.Parameter (typeof (Kling), ""), typeof (int));
  238. Assert.AreEqual (typeof (int), c.Type);
  239. Assert.IsFalse (c.IsLifted);
  240. Assert.IsFalse (c.IsLiftedToNull);
  241. Assert.IsNotNull (c.Method);
  242. }
  243. [Test]
  244. public void CompileConvertStructWithImplicitOp ()
  245. {
  246. var p = Expression.Parameter (typeof (Kling), "kling");
  247. var c = Expression.Lambda<Func<Kling, int>> (
  248. Expression.Convert (p, typeof (int)), p).Compile ();
  249. Assert.AreEqual (42, c (new Kling (42)));
  250. }
  251. [Test]
  252. public void ConvertStructWithImplicitOpToNullableInt ()
  253. {
  254. var c = Expression.Convert (Expression.Parameter (typeof (Kling), ""), typeof (int?));
  255. Assert.AreEqual (typeof (int?), c.Type);
  256. Assert.IsTrue (c.IsLifted);
  257. Assert.IsTrue (c.IsLiftedToNull);
  258. Assert.IsNotNull (c.Method);
  259. }
  260. [Test]
  261. public void ConvertNullableStructWithImplicitOpToNullableInt ()
  262. {
  263. var c = Expression.Convert (Expression.Parameter (typeof (Kling?), ""), typeof (int?));
  264. Assert.AreEqual (typeof (int?), c.Type);
  265. Assert.IsTrue (c.IsLifted);
  266. Assert.IsTrue (c.IsLiftedToNull);
  267. Assert.IsNotNull (c.Method);
  268. }
  269. [Test]
  270. public void CompiledBoxing ()
  271. {
  272. var b = Expression.Lambda<Func<object>> (
  273. Expression.Convert (42.ToConstant (), typeof (object))).Compile ();
  274. Assert.AreEqual ((object) 42, b ());
  275. }
  276. [Test]
  277. public void CompiledUnBoxing ()
  278. {
  279. var p = Expression.Parameter (typeof (object), "o");
  280. var u = Expression.Lambda<Func<object, int>> (
  281. Expression.Convert (p, typeof (int)), p).Compile ();
  282. Assert.AreEqual (42, u ((object) 42));
  283. }
  284. [Test]
  285. public void CompiledCast ()
  286. {
  287. var p = Expression.Parameter (typeof (IFoo), "foo");
  288. var c = Expression.Lambda<Func<IFoo, Bar>> (
  289. Expression.Convert (p, typeof (Bar)), p).Compile ();
  290. IFoo foo = new Bar ();
  291. Bar b = c (foo);
  292. Assert.AreEqual (b, foo);
  293. }
  294. [Test]
  295. public void CompileNotNullableToNullable ()
  296. {
  297. var p = Expression.Parameter (typeof (int), "i");
  298. var c = Expression.Lambda<Func<int, int?>> (
  299. Expression.Convert (p, typeof (int?)), p).Compile ();
  300. Assert.AreEqual ((int?) 0, c (0));
  301. Assert.AreEqual ((int?) 42, c (42));
  302. }
  303. [Test]
  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. public void ConvertImplicitToShortToNullableInt ()
  378. {
  379. var a = Expression.Parameter (typeof (ImplicitToShort?), "a");
  380. var method = typeof (ImplicitToShort).GetMethod ("op_Implicit");
  381. var node = Expression.Convert (a, typeof (short), method);
  382. Assert.IsTrue (node.IsLifted);
  383. Assert.IsFalse (node.IsLiftedToNull);
  384. Assert.AreEqual (typeof (short), node.Type);
  385. Assert.AreEqual (method, node.Method);
  386. var conv = Expression.Lambda<Func<ImplicitToShort?, int?>> (
  387. Expression.Convert (
  388. node,
  389. typeof (int?)), a).Compile ();
  390. Assert.AreEqual ((int?) 42, conv (new ImplicitToShort (42)));
  391. Action convnull = () => Assert.AreEqual (null, conv (null));
  392. convnull.AssertThrows (typeof (InvalidOperationException));
  393. }
  394. [Test]
  395. public void NullableImplicitToShort ()
  396. {
  397. var i = Expression.Parameter (typeof (ImplicitToShort?), "i");
  398. var method = typeof (ImplicitToShort).GetMethod ("op_Implicit");
  399. var node = Expression.Convert (i, typeof (short?), method);
  400. Assert.IsTrue (node.IsLifted);
  401. Assert.IsTrue (node.IsLiftedToNull);
  402. Assert.AreEqual (typeof (short?), node.Type);
  403. Assert.AreEqual (method, node.Method);
  404. var convert = Expression.Lambda<Func<ImplicitToShort?, short?>> (node, i).Compile ();
  405. Assert.AreEqual ((short?) 42, convert (new ImplicitToShort (42)));
  406. }
  407. [Test]
  408. public void ConvertLongToDecimal ()
  409. {
  410. var p = Expression.Parameter (typeof (long), "l");
  411. var node = Expression.Convert (p, typeof (decimal));
  412. Assert.IsFalse (node.IsLifted);
  413. Assert.IsFalse (node.IsLiftedToNull);
  414. Assert.AreEqual (typeof (decimal), node.Type);
  415. Assert.IsNotNull (node.Method);
  416. var convert = Expression.Lambda<Func<long, decimal>> (node, p).Compile ();
  417. Assert.AreEqual (42, convert (42));
  418. }
  419. [Test]
  420. public void ConvertNullableULongToNullableDecimal ()
  421. {
  422. var p = Expression.Parameter (typeof (ulong?), "l");
  423. var node = Expression.Convert (p, typeof (decimal?));
  424. Assert.IsTrue (node.IsLifted);
  425. Assert.IsTrue (node.IsLiftedToNull);
  426. Assert.AreEqual (typeof (decimal?), node.Type);
  427. Assert.IsNotNull (node.Method);
  428. var convert = Expression.Lambda<Func<ulong?, decimal?>> (node, p).Compile ();
  429. Assert.AreEqual (42, convert (42));
  430. Assert.AreEqual (null, convert (null));
  431. }
  432. [Test]
  433. public void ConvertCheckedNullableIntToInt ()
  434. {
  435. var p = Expression.Parameter (typeof (int?), "i");
  436. var node = Expression.ConvertChecked (p, typeof (int));
  437. Assert.AreEqual (ExpressionType.ConvertChecked, node.NodeType);
  438. Assert.IsTrue (node.IsLifted);
  439. Assert.IsFalse (node.IsLiftedToNull);
  440. Assert.AreEqual (typeof (int), node.Type);
  441. Assert.IsNull (node.Method);
  442. }
  443. struct ImplicitToInt {
  444. int Value;
  445. public ImplicitToInt (int v)
  446. {
  447. Value = v;
  448. }
  449. public static implicit operator int (ImplicitToInt i)
  450. {
  451. return i.Value;
  452. }
  453. }
  454. [Test]
  455. public void ConvertNullableImplictToIntToNullableLong ()
  456. {
  457. var i = Expression.Parameter (typeof (ImplicitToInt?), "i");
  458. var method = typeof (ImplicitToInt).GetMethod ("op_Implicit");
  459. var node = Expression.Convert (i, typeof (int), method);
  460. node = Expression.Convert (node, typeof (long?));
  461. var conv = Expression.Lambda<Func<ImplicitToInt?, long?>> (node, i).Compile ();
  462. Assert.AreEqual ((long?) 42, conv (new ImplicitToInt (42)));
  463. Action convnull = () => Assert.AreEqual (null, conv (null));
  464. convnull.AssertThrows (typeof (InvalidOperationException));
  465. }
  466. [Test]
  467. [ExpectedException (typeof (InvalidOperationException))]
  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. [Test] // #678897
  476. public void ConvertEnumValueToEnum ()
  477. {
  478. var node = Expression.Convert (
  479. Expression.Constant (EineEnum.EineValue, typeof (EineEnum)),
  480. typeof (Enum));
  481. Assert.IsNotNull (node);
  482. Assert.AreEqual (typeof (Enum), node.Type);
  483. }
  484. }
  485. }