ExpressionTest_Convert.cs 15 KB

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