ExpressionTest_Convert.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. [Test]
  163. public void ConvertEnumToInt32 ()
  164. {
  165. var c = Expression.Convert (Expression.Parameter (typeof (EineEnum), ""), typeof (int));
  166. Assert.AreEqual (typeof (int), c.Type);
  167. Assert.IsFalse (c.IsLifted);
  168. Assert.IsFalse (c.IsLiftedToNull);
  169. Assert.IsNull (c.Method);
  170. }
  171. [Test]
  172. public void ConvertNullableInt32ToInt32 ()
  173. {
  174. var c = Expression.Convert (Expression.Parameter (typeof (int?), ""), typeof (int));
  175. Assert.AreEqual (typeof (int), c.Type);
  176. Assert.IsTrue (c.IsLifted);
  177. Assert.IsFalse (c.IsLiftedToNull);
  178. Assert.IsNull (c.Method);
  179. }
  180. [Test]
  181. public void ConvertInt32ToNullableInt32 ()
  182. {
  183. var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (int?));
  184. Assert.AreEqual (typeof (int?), c.Type);
  185. Assert.IsTrue (c.IsLifted);
  186. Assert.IsTrue (c.IsLiftedToNull);
  187. Assert.IsNull (c.Method);
  188. }
  189. class Klang {
  190. int i;
  191. public Klang (int i)
  192. {
  193. this.i = i;
  194. }
  195. public static explicit operator int (Klang k)
  196. {
  197. return k.i;
  198. }
  199. }
  200. [Test]
  201. public void ConvertClassWithExplicitOp ()
  202. {
  203. var c = Expression.Convert (Expression.Parameter (typeof (Klang), ""), typeof (int));
  204. Assert.AreEqual (typeof (int), c.Type);
  205. Assert.IsFalse (c.IsLifted);
  206. Assert.IsFalse (c.IsLiftedToNull);
  207. Assert.IsNotNull (c.Method);
  208. }
  209. [Test]
  210. public void CompileConvertClassWithExplicitOp ()
  211. {
  212. var p = Expression.Parameter (typeof (Klang), "klang");
  213. var c = Expression.Lambda<Func<Klang, int>> (
  214. Expression.Convert (p, typeof (int)), p).Compile ();
  215. Assert.AreEqual (42, c (new Klang (42)));
  216. }
  217. [Test]
  218. public void ConvertClassWithExplicitOpToNullableInt ()
  219. {
  220. var c = Expression.Convert (Expression.Parameter (typeof (Klang), ""), typeof (int?));
  221. Assert.AreEqual (typeof (int?), c.Type);
  222. Assert.IsTrue (c.IsLifted);
  223. Assert.IsTrue (c.IsLiftedToNull);
  224. Assert.IsNotNull (c.Method);
  225. }
  226. struct Kling {
  227. int i;
  228. public Kling (int i)
  229. {
  230. this.i = i;
  231. }
  232. public static implicit operator int (Kling k)
  233. {
  234. return k.i;
  235. }
  236. }
  237. [Test]
  238. public void ConvertStructWithImplicitOp ()
  239. {
  240. var c = Expression.Convert (Expression.Parameter (typeof (Kling), ""), typeof (int));
  241. Assert.AreEqual (typeof (int), c.Type);
  242. Assert.IsFalse (c.IsLifted);
  243. Assert.IsFalse (c.IsLiftedToNull);
  244. Assert.IsNotNull (c.Method);
  245. }
  246. [Test]
  247. public void CompileConvertStructWithImplicitOp ()
  248. {
  249. var p = Expression.Parameter (typeof (Kling), "kling");
  250. var c = Expression.Lambda<Func<Kling, int>> (
  251. Expression.Convert (p, typeof (int)), p).Compile ();
  252. Assert.AreEqual (42, c (new Kling (42)));
  253. }
  254. [Test]
  255. public void ConvertStructWithImplicitOpToNullableInt ()
  256. {
  257. var c = Expression.Convert (Expression.Parameter (typeof (Kling), ""), typeof (int?));
  258. Assert.AreEqual (typeof (int?), c.Type);
  259. Assert.IsTrue (c.IsLifted);
  260. Assert.IsTrue (c.IsLiftedToNull);
  261. Assert.IsNotNull (c.Method);
  262. }
  263. [Test]
  264. public void ConvertNullableStructWithImplicitOpToNullableInt ()
  265. {
  266. var c = Expression.Convert (Expression.Parameter (typeof (Kling?), ""), typeof (int?));
  267. Assert.AreEqual (typeof (int?), c.Type);
  268. Assert.IsTrue (c.IsLifted);
  269. Assert.IsTrue (c.IsLiftedToNull);
  270. Assert.IsNotNull (c.Method);
  271. }
  272. [Test]
  273. public void CompiledBoxing ()
  274. {
  275. var b = Expression.Lambda<Func<object>> (
  276. Expression.Convert (42.ToConstant (), typeof (object))).Compile ();
  277. Assert.AreEqual ((object) 42, b ());
  278. }
  279. [Test]
  280. public void CompiledUnBoxing ()
  281. {
  282. var p = Expression.Parameter (typeof (object), "o");
  283. var u = Expression.Lambda<Func<object, int>> (
  284. Expression.Convert (p, typeof (int)), p).Compile ();
  285. Assert.AreEqual (42, u ((object) 42));
  286. }
  287. [Test]
  288. public void CompiledCast ()
  289. {
  290. var p = Expression.Parameter (typeof (IFoo), "foo");
  291. var c = Expression.Lambda<Func<IFoo, Bar>> (
  292. Expression.Convert (p, typeof (Bar)), p).Compile ();
  293. IFoo foo = new Bar ();
  294. Bar b = c (foo);
  295. Assert.AreEqual (b, foo);
  296. }
  297. [Test]
  298. public void CompileNotNullableToNullable ()
  299. {
  300. var p = Expression.Parameter (typeof (int), "i");
  301. var c = Expression.Lambda<Func<int, int?>> (
  302. Expression.Convert (p, typeof (int?)), p).Compile ();
  303. Assert.AreEqual ((int?) 0, c (0));
  304. Assert.AreEqual ((int?) 42, c (42));
  305. }
  306. [Test]
  307. public void CompileNullableToNotNullable ()
  308. {
  309. var p = Expression.Parameter (typeof (int?), "i");
  310. var c = Expression.Lambda<Func<int?, int>> (
  311. Expression.Convert (p, typeof (int)), p).Compile ();
  312. Assert.AreEqual (0, c ((int?) 0));
  313. Assert.AreEqual (42, c ((int?) 42));
  314. Action a = () => c (null);
  315. a.AssertThrows (typeof (InvalidOperationException));
  316. }
  317. [Test]
  318. public void CompiledConvertToSameType ()
  319. {
  320. var k = new Klang (42);
  321. var p = Expression.Parameter (typeof (Klang), "klang");
  322. var c = Expression.Lambda<Func<Klang, Klang>> (
  323. Expression.Convert (
  324. p, typeof (Klang)),
  325. p).Compile ();
  326. Assert.AreEqual (k, c (k));
  327. }
  328. [Test]
  329. public void CompiledConvertNullableToNullable ()
  330. {
  331. var p = Expression.Parameter (typeof (int?), "i");
  332. var c = Expression.Lambda<Func<int?, short?>> (
  333. Expression.Convert (p, typeof (short?)), p).Compile ();
  334. Assert.AreEqual ((short?) null, c (null));
  335. Assert.AreEqual ((short?) 12, c (12));
  336. }
  337. [Test]
  338. public void CompiledNullableBoxing ()
  339. {
  340. var p = Expression.Parameter (typeof (int?), "i");
  341. var c = Expression.Lambda<Func<int?, object>> (
  342. Expression.Convert (p, typeof (object)), p).Compile ();
  343. Assert.AreEqual (null, c (null));
  344. Assert.AreEqual ((object) (int?) 42, c (42));
  345. }
  346. [Test]
  347. public void CompiledNullableUnboxing ()
  348. {
  349. var p = Expression.Parameter (typeof (object), "o");
  350. var c = Expression.Lambda<Func<object, int?>> (
  351. Expression.Convert (p, typeof (int?)), p).Compile ();
  352. Assert.AreEqual ((int?) null, c (null));
  353. Assert.AreEqual ((int?) 42, c ((int?) 42));
  354. }
  355. [Test]
  356. public void ChainedNullableConvert ()
  357. {
  358. var p = Expression.Parameter (typeof (sbyte?), "a");
  359. var test = Expression.Lambda<Func<sbyte?, long?>> (
  360. Expression.Convert (
  361. Expression.Convert (
  362. p,
  363. typeof (int?)),
  364. typeof (long?)), p).Compile ();
  365. Assert.AreEqual ((long?) 3, test ((sbyte?) 3));
  366. Assert.AreEqual (null, test (null));
  367. }
  368. struct ImplicitToShort {
  369. short value;
  370. public ImplicitToShort (short v)
  371. {
  372. value = v;
  373. }
  374. public static implicit operator short (ImplicitToShort i)
  375. {
  376. return i.value;
  377. }
  378. }
  379. [Test]
  380. public void ConvertImplicitToShortToNullableInt ()
  381. {
  382. var a = Expression.Parameter (typeof (ImplicitToShort?), "a");
  383. var method = typeof (ImplicitToShort).GetMethod ("op_Implicit");
  384. var node = Expression.Convert (a, typeof (short), method);
  385. Assert.IsTrue (node.IsLifted);
  386. Assert.IsFalse (node.IsLiftedToNull);
  387. Assert.AreEqual (typeof (short), node.Type);
  388. Assert.AreEqual (method, node.Method);
  389. var conv = Expression.Lambda<Func<ImplicitToShort?, int?>> (
  390. Expression.Convert (
  391. node,
  392. typeof (int?)), a).Compile ();
  393. Assert.AreEqual ((int?) 42, conv (new ImplicitToShort (42)));
  394. Action convnull = () => Assert.AreEqual (null, conv (null));
  395. convnull.AssertThrows (typeof (InvalidOperationException));
  396. }
  397. [Test]
  398. public void NullableImplicitToShort ()
  399. {
  400. var i = Expression.Parameter (typeof (ImplicitToShort?), "i");
  401. var method = typeof (ImplicitToShort).GetMethod ("op_Implicit");
  402. var node = Expression.Convert (i, typeof (short?), method);
  403. Assert.IsTrue (node.IsLifted);
  404. Assert.IsTrue (node.IsLiftedToNull);
  405. Assert.AreEqual (typeof (short?), node.Type);
  406. Assert.AreEqual (method, node.Method);
  407. var convert = Expression.Lambda<Func<ImplicitToShort?, short?>> (node, i).Compile ();
  408. Assert.AreEqual ((short?) 42, convert (new ImplicitToShort (42)));
  409. }
  410. [Test]
  411. public void ConvertLongToDecimal ()
  412. {
  413. var p = Expression.Parameter (typeof (long), "l");
  414. var node = Expression.Convert (p, typeof (decimal));
  415. Assert.IsFalse (node.IsLifted);
  416. Assert.IsFalse (node.IsLiftedToNull);
  417. Assert.AreEqual (typeof (decimal), node.Type);
  418. Assert.IsNotNull (node.Method);
  419. var convert = Expression.Lambda<Func<long, decimal>> (node, p).Compile ();
  420. Assert.AreEqual (42, convert (42));
  421. }
  422. [Test]
  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. public void ConvertNullableImplictToIntToNullableLong ()
  459. {
  460. var i = Expression.Parameter (typeof (ImplicitToInt?), "i");
  461. var method = typeof (ImplicitToInt).GetMethod ("op_Implicit");
  462. var node = Expression.Convert (i, typeof (int), method);
  463. node = Expression.Convert (node, typeof (long?));
  464. var conv = Expression.Lambda<Func<ImplicitToInt?, long?>> (node, i).Compile ();
  465. Assert.AreEqual ((long?) 42, conv (new ImplicitToInt (42)));
  466. Action convnull = () => Assert.AreEqual (null, conv (null));
  467. convnull.AssertThrows (typeof (InvalidOperationException));
  468. }
  469. [Test]
  470. [ExpectedException (typeof (InvalidOperationException))]
  471. [Category ("NotWorking")]
  472. public void ConvertNullableIntToStringWithConvertMethod ()
  473. {
  474. Expression.Convert (
  475. Expression.Constant ((int?) 0),
  476. typeof (string),
  477. typeof (Convert).GetMethod ("ToString", new [] { typeof (object) }));
  478. }
  479. }
  480. }