ExpressionTest_Convert.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. Assert.AreEqual ("Convert(<param>)", conv.ToString ());
  73. p = Expression.Parameter (typeof (ITzap), null);
  74. conv = Expression.Convert (p, typeof (IFoo));
  75. Assert.AreEqual (typeof (IFoo), conv.Type);
  76. Assert.AreEqual ("Convert(<param>)", conv.ToString ());
  77. }
  78. [Test]
  79. public void ConvertCheckedInt32ToInt64 ()
  80. {
  81. var c = Expression.ConvertChecked (
  82. Expression.Constant (2, typeof (int)), typeof (long));
  83. Assert.AreEqual (ExpressionType.ConvertChecked, c.NodeType);
  84. Assert.AreEqual ("ConvertChecked(2)", c.ToString ());
  85. }
  86. [Test]
  87. public void ConvertCheckedFallbackToConvertForNonPrimitives ()
  88. {
  89. var p = Expression.ConvertChecked (
  90. Expression.Constant (null, typeof (object)), typeof (IFoo));
  91. Assert.AreEqual (ExpressionType.Convert, p.NodeType);
  92. }
  93. [Test]
  94. [ExpectedException (typeof (InvalidOperationException))]
  95. public void ConvertBazToFoo ()
  96. {
  97. Expression.Convert (Expression.Parameter (typeof (Baz), ""), typeof (Foo));
  98. }
  99. struct EineStrukt { }
  100. [Test]
  101. [ExpectedException (typeof (InvalidOperationException))]
  102. public void ConvertStructToFoo ()
  103. {
  104. Expression.Convert (Expression.Parameter (typeof (EineStrukt), ""), typeof (Foo));
  105. }
  106. [Test]
  107. [ExpectedException (typeof (InvalidOperationException))]
  108. public void ConvertInt32ToBool ()
  109. {
  110. Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (bool));
  111. }
  112. [Test]
  113. public void ConvertIFooToFoo ()
  114. {
  115. var c = Expression.Convert (Expression.Parameter (typeof (IFoo), ""), typeof (Foo));
  116. Assert.AreEqual (typeof (Foo), c.Type);
  117. Assert.IsFalse (c.IsLifted);
  118. Assert.IsFalse (c.IsLiftedToNull);
  119. Assert.IsNull (c.Method);
  120. }
  121. [Test]
  122. public void BoxInt32 ()
  123. {
  124. var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (object));
  125. Assert.AreEqual (typeof (object), c.Type);
  126. Assert.IsFalse (c.IsLifted);
  127. Assert.IsFalse (c.IsLiftedToNull);
  128. Assert.IsNull (c.Method);
  129. }
  130. [Test]
  131. public void UnBoxInt32 ()
  132. {
  133. var c = Expression.Convert (Expression.Parameter (typeof (object), ""), typeof (int));
  134. Assert.AreEqual (typeof (int), c.Type);
  135. Assert.IsFalse (c.IsLifted);
  136. Assert.IsFalse (c.IsLiftedToNull);
  137. Assert.IsNull (c.Method);
  138. }
  139. [Test]
  140. public void ConvertInt32ToInt64 ()
  141. {
  142. var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (long));
  143. Assert.AreEqual (typeof (long), c.Type);
  144. Assert.IsFalse (c.IsLifted);
  145. Assert.IsFalse (c.IsLiftedToNull);
  146. Assert.IsNull (c.Method);
  147. }
  148. [Test]
  149. public void ConvertInt64ToInt32 ()
  150. {
  151. var c = Expression.Convert (Expression.Parameter (typeof (long), ""), typeof (int));
  152. Assert.AreEqual (typeof (int), c.Type);
  153. Assert.IsFalse (c.IsLifted);
  154. Assert.IsFalse (c.IsLiftedToNull);
  155. Assert.IsNull (c.Method);
  156. }
  157. enum EineEnum { }
  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. [Category ("NotWorking")]
  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. [Category ("NotWorking")]
  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. [Category ("NotWorking")]
  263. public void ConvertNullableStructWithImplicitOpToNullableInt ()
  264. {
  265. var c = Expression.Convert (Expression.Parameter (typeof (Kling?), ""), typeof (int?));
  266. Assert.AreEqual (typeof (int?), c.Type);
  267. Assert.IsTrue (c.IsLifted);
  268. Assert.IsTrue (c.IsLiftedToNull);
  269. Assert.IsNotNull (c.Method);
  270. }
  271. [Test]
  272. public void CompiledBoxing ()
  273. {
  274. var b = Expression.Lambda<Func<object>> (
  275. Expression.Convert (42.ToConstant (), typeof (object))).Compile ();
  276. Assert.AreEqual ((object) 42, b ());
  277. }
  278. [Test]
  279. public void CompiledUnBoxing ()
  280. {
  281. var p = Expression.Parameter (typeof (object), "o");
  282. var u = Expression.Lambda<Func<object, int>> (
  283. Expression.Convert (p, typeof (int)), p).Compile ();
  284. Assert.AreEqual (42, u ((object) 42));
  285. }
  286. [Test]
  287. public void CompiledCast ()
  288. {
  289. var p = Expression.Parameter (typeof (IFoo), "foo");
  290. var c = Expression.Lambda<Func<IFoo, Bar>> (
  291. Expression.Convert (p, typeof (Bar)), p).Compile ();
  292. IFoo foo = new Bar ();
  293. Bar b = c (foo);
  294. Assert.AreEqual (b, foo);
  295. }
  296. [Test]
  297. [Category ("NotWorking")]
  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. [Category ("NotWorking")]
  308. public void CompileNullableToNotNullable ()
  309. {
  310. var p = Expression.Parameter (typeof (int?), "i");
  311. var c = Expression.Lambda<Func<int?, int>> (
  312. Expression.Convert (p, typeof (int)), p).Compile ();
  313. Assert.AreEqual (0, c ((int?) 0));
  314. Assert.AreEqual (42, c ((int?) 42));
  315. Action a = () => c (null);
  316. a.AssertThrows (typeof (InvalidOperationException));
  317. }
  318. [Test]
  319. public void CompiledConvertToSameType ()
  320. {
  321. var k = new Klang (42);
  322. var p = Expression.Parameter (typeof (Klang), "klang");
  323. var c = Expression.Lambda<Func<Klang, Klang>> (
  324. Expression.Convert (
  325. p, typeof (Klang)),
  326. p).Compile ();
  327. Assert.AreEqual (k, c (k));
  328. }
  329. }
  330. }