2 // ExpressionTest_Convert.cs
5 // Jb Evain (jbevain@novell.com)
7 // (C) 2008 Novell, Inc. (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Reflection;
32 using System.Linq.Expressions;
33 using NUnit.Framework;
35 namespace MonoTests.System.Linq.Expressions {
38 public class ExpressionTest_Convert {
41 [ExpectedException (typeof (ArgumentNullException))]
42 public void NullExpression ()
44 Expression.Convert (null, typeof (int));
48 [ExpectedException (typeof (ArgumentNullException))]
49 public void NullType ()
51 Expression.Convert (1.ToConstant (), null);
55 [ExpectedException (typeof (InvalidOperationException))]
56 public void ConvertIntToString ()
58 Expression.Convert (1.ToConstant (), typeof (string));
69 public void ConvertBackwardAssignability ()
71 var c = Expression.Convert (
72 Expression.Constant (null, typeof (Bar)), typeof (Foo));
74 Assert.AreEqual ("Convert(null)", c.ToString ());
78 public void ConvertInterfaces ()
80 var p = Expression.Parameter (typeof (IFoo), null);
82 var conv = Expression.Convert (p, typeof (ITzap));
83 Assert.AreEqual (typeof (ITzap), conv.Type);
84 Assert.AreEqual ("Convert(<param>)", conv.ToString ());
86 p = Expression.Parameter (typeof (ITzap), null);
87 conv = Expression.Convert (p, typeof (IFoo));
89 Assert.AreEqual (typeof (IFoo), conv.Type);
90 Assert.AreEqual ("Convert(<param>)", conv.ToString ());
94 public void ConvertCheckedInt32ToInt64 ()
96 var c = Expression.ConvertChecked (
97 Expression.Constant (2, typeof (int)), typeof (long));
99 Assert.AreEqual (ExpressionType.ConvertChecked, c.NodeType);
100 Assert.AreEqual ("ConvertChecked(2)", c.ToString ());
104 public void ConvertCheckedFallbackToConvertForNonPrimitives ()
106 var p = Expression.ConvertChecked (
107 Expression.Constant (null, typeof (object)), typeof (IFoo));
109 Assert.AreEqual (ExpressionType.Convert, p.NodeType);
113 [ExpectedException (typeof (InvalidOperationException))]
114 public void ConvertBazToFoo ()
116 Expression.Convert (Expression.Parameter (typeof (Baz), ""), typeof (Foo));
119 struct EineStrukt { }
122 [ExpectedException (typeof (InvalidOperationException))]
123 public void ConvertStructToFoo ()
125 Expression.Convert (Expression.Parameter (typeof (EineStrukt), ""), typeof (Foo));
129 [ExpectedException (typeof (InvalidOperationException))]
130 public void ConvertInt32ToBool ()
132 Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (bool));
136 public void ConvertIFooToFoo ()
138 var c = Expression.Convert (Expression.Parameter (typeof (IFoo), ""), typeof (Foo));
139 Assert.AreEqual (typeof (Foo), c.Type);
140 Assert.IsFalse (c.IsLifted);
141 Assert.IsFalse (c.IsLiftedToNull);
142 Assert.IsNull (c.Method);
146 public void BoxInt32 ()
148 var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (object));
149 Assert.AreEqual (typeof (object), c.Type);
150 Assert.IsFalse (c.IsLifted);
151 Assert.IsFalse (c.IsLiftedToNull);
152 Assert.IsNull (c.Method);
156 public void UnBoxInt32 ()
158 var c = Expression.Convert (Expression.Parameter (typeof (object), ""), typeof (int));
159 Assert.AreEqual (typeof (int), c.Type);
160 Assert.IsFalse (c.IsLifted);
161 Assert.IsFalse (c.IsLiftedToNull);
162 Assert.IsNull (c.Method);
166 public void ConvertInt32ToInt64 ()
168 var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (long));
169 Assert.AreEqual (typeof (long), c.Type);
170 Assert.IsFalse (c.IsLifted);
171 Assert.IsFalse (c.IsLiftedToNull);
172 Assert.IsNull (c.Method);
176 public void ConvertInt64ToInt32 ()
178 var c = Expression.Convert (Expression.Parameter (typeof (long), ""), typeof (int));
179 Assert.AreEqual (typeof (int), c.Type);
180 Assert.IsFalse (c.IsLifted);
181 Assert.IsFalse (c.IsLiftedToNull);
182 Assert.IsNull (c.Method);
188 public void ConvertEnumToInt32 ()
190 var c = Expression.Convert (Expression.Parameter (typeof (EineEnum), ""), typeof (int));
191 Assert.AreEqual (typeof (int), c.Type);
192 Assert.IsFalse (c.IsLifted);
193 Assert.IsFalse (c.IsLiftedToNull);
194 Assert.IsNull (c.Method);
198 public void ConvertNullableInt32ToInt32 ()
200 var c = Expression.Convert (Expression.Parameter (typeof (int?), ""), typeof (int));
201 Assert.AreEqual (typeof (int), c.Type);
202 Assert.IsTrue (c.IsLifted);
203 Assert.IsFalse (c.IsLiftedToNull);
204 Assert.IsNull (c.Method);
208 public void ConvertInt32ToNullableInt32 ()
210 var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (int?));
211 Assert.AreEqual (typeof (int?), c.Type);
212 Assert.IsTrue (c.IsLifted);
213 Assert.IsTrue (c.IsLiftedToNull);
214 Assert.IsNull (c.Method);
226 public static explicit operator int (Klang k)
233 public void ConvertClassWithExplicitOp ()
235 var c = Expression.Convert (Expression.Parameter (typeof (Klang), ""), typeof (int));
236 Assert.AreEqual (typeof (int), c.Type);
237 Assert.IsFalse (c.IsLifted);
238 Assert.IsFalse (c.IsLiftedToNull);
239 Assert.IsNotNull (c.Method);
243 public void CompileConvertClassWithExplicitOp ()
245 var p = Expression.Parameter (typeof (Klang), "klang");
246 var c = Expression.Lambda<Func<Klang, int>> (
247 Expression.Convert (p, typeof (int)), p).Compile ();
249 Assert.AreEqual (42, c (new Klang (42)));
253 public void ConvertClassWithExplicitOpToNullableInt ()
255 var c = Expression.Convert (Expression.Parameter (typeof (Klang), ""), typeof (int?));
256 Assert.AreEqual (typeof (int?), c.Type);
257 Assert.IsTrue (c.IsLifted);
258 Assert.IsTrue (c.IsLiftedToNull);
259 Assert.IsNotNull (c.Method);
270 public static implicit operator int (Kling k)
277 public void ConvertStructWithImplicitOp ()
279 var c = Expression.Convert (Expression.Parameter (typeof (Kling), ""), typeof (int));
280 Assert.AreEqual (typeof (int), c.Type);
281 Assert.IsFalse (c.IsLifted);
282 Assert.IsFalse (c.IsLiftedToNull);
283 Assert.IsNotNull (c.Method);
287 public void CompileConvertStructWithImplicitOp ()
289 var p = Expression.Parameter (typeof (Kling), "kling");
290 var c = Expression.Lambda<Func<Kling, int>> (
291 Expression.Convert (p, typeof (int)), p).Compile ();
293 Assert.AreEqual (42, c (new Kling (42)));
297 public void ConvertStructWithImplicitOpToNullableInt ()
299 var c = Expression.Convert (Expression.Parameter (typeof (Kling), ""), typeof (int?));
300 Assert.AreEqual (typeof (int?), c.Type);
301 Assert.IsTrue (c.IsLifted);
302 Assert.IsTrue (c.IsLiftedToNull);
303 Assert.IsNotNull (c.Method);
307 public void ConvertNullableStructWithImplicitOpToNullableInt ()
309 var c = Expression.Convert (Expression.Parameter (typeof (Kling?), ""), typeof (int?));
310 Assert.AreEqual (typeof (int?), c.Type);
311 Assert.IsTrue (c.IsLifted);
312 Assert.IsTrue (c.IsLiftedToNull);
313 Assert.IsNotNull (c.Method);
317 public void CompiledBoxing ()
319 var b = Expression.Lambda<Func<object>> (
320 Expression.Convert (42.ToConstant (), typeof (object))).Compile ();
322 Assert.AreEqual ((object) 42, b ());
326 public void CompiledUnBoxing ()
328 var p = Expression.Parameter (typeof (object), "o");
330 var u = Expression.Lambda<Func<object, int>> (
331 Expression.Convert (p, typeof (int)), p).Compile ();
333 Assert.AreEqual (42, u ((object) 42));
337 public void CompiledCast ()
339 var p = Expression.Parameter (typeof (IFoo), "foo");
341 var c = Expression.Lambda<Func<IFoo, Bar>> (
342 Expression.Convert (p, typeof (Bar)), p).Compile ();
344 IFoo foo = new Bar ();
348 Assert.AreEqual (b, foo);
352 public void CompileNotNullableToNullable ()
354 var p = Expression.Parameter (typeof (int), "i");
355 var c = Expression.Lambda<Func<int, int?>> (
356 Expression.Convert (p, typeof (int?)), p).Compile ();
358 Assert.AreEqual ((int?) 0, c (0));
359 Assert.AreEqual ((int?) 42, c (42));
363 public void CompileNullableToNotNullable ()
365 var p = Expression.Parameter (typeof (int?), "i");
366 var c = Expression.Lambda<Func<int?, int>> (
367 Expression.Convert (p, typeof (int)), p).Compile ();
369 Assert.AreEqual (0, c ((int?) 0));
370 Assert.AreEqual (42, c ((int?) 42));
372 Action a = () => c (null);
374 a.AssertThrows (typeof (InvalidOperationException));
378 public void CompiledConvertToSameType ()
380 var k = new Klang (42);
382 var p = Expression.Parameter (typeof (Klang), "klang");
383 var c = Expression.Lambda<Func<Klang, Klang>> (
388 Assert.AreEqual (k, c (k));
392 public void CompiledConvertNullableToNullable ()
394 var p = Expression.Parameter (typeof (int?), "i");
395 var c = Expression.Lambda<Func<int?, short?>> (
396 Expression.Convert (p, typeof (short?)), p).Compile ();
398 Assert.AreEqual ((short?) null, c (null));
399 Assert.AreEqual ((short?) 12, c (12));
403 public void CompiledNullableBoxing ()
405 var p = Expression.Parameter (typeof (int?), "i");
406 var c = Expression.Lambda<Func<int?, object>> (
407 Expression.Convert (p, typeof (object)), p).Compile ();
409 Assert.AreEqual (null, c (null));
410 Assert.AreEqual ((object) (int?) 42, c (42));
414 public void CompiledNullableUnboxing ()
416 var p = Expression.Parameter (typeof (object), "o");
417 var c = Expression.Lambda<Func<object, int?>> (
418 Expression.Convert (p, typeof (int?)), p).Compile ();
420 Assert.AreEqual ((int?) null, c (null));
421 Assert.AreEqual ((int?) 42, c ((int?) 42));
425 public void ChainedNullableConvert ()
427 var p = Expression.Parameter (typeof (sbyte?), "a");
429 var test = Expression.Lambda<Func<sbyte?, long?>> (
434 typeof (long?)), p).Compile ();
436 Assert.AreEqual ((long?) 3, test ((sbyte?) 3));
437 Assert.AreEqual (null, test (null));
440 struct ImplicitToShort {
443 public ImplicitToShort (short v)
448 public static implicit operator short (ImplicitToShort i)
455 public void ConvertImplicitToShortToNullableInt ()
457 var a = Expression.Parameter (typeof (ImplicitToShort?), "a");
459 var method = typeof (ImplicitToShort).GetMethod ("op_Implicit");
461 var node = Expression.Convert (a, typeof (short), method);
462 Assert.IsTrue (node.IsLifted);
463 Assert.IsFalse (node.IsLiftedToNull);
464 Assert.AreEqual (typeof (short), node.Type);
465 Assert.AreEqual (method, node.Method);
467 var conv = Expression.Lambda<Func<ImplicitToShort?, int?>> (
470 typeof (int?)), a).Compile ();
472 Assert.AreEqual ((int?) 42, conv (new ImplicitToShort (42)));
474 Action convnull = () => Assert.AreEqual (null, conv (null));
476 convnull.AssertThrows (typeof (InvalidOperationException));
480 public void NullableImplicitToShort ()
482 var i = Expression.Parameter (typeof (ImplicitToShort?), "i");
484 var method = typeof (ImplicitToShort).GetMethod ("op_Implicit");
486 var node = Expression.Convert (i, typeof (short?), method);
488 Assert.IsTrue (node.IsLifted);
489 Assert.IsTrue (node.IsLiftedToNull);
490 Assert.AreEqual (typeof (short?), node.Type);
491 Assert.AreEqual (method, node.Method);
493 var convert = Expression.Lambda<Func<ImplicitToShort?, short?>> (node, i).Compile ();
495 Assert.AreEqual ((short?) 42, convert (new ImplicitToShort (42)));
499 public void ConvertLongToDecimal ()
501 var p = Expression.Parameter (typeof (long), "l");
503 var node = Expression.Convert (p, typeof (decimal));
504 Assert.IsFalse (node.IsLifted);
505 Assert.IsFalse (node.IsLiftedToNull);
506 Assert.AreEqual (typeof (decimal), node.Type);
507 Assert.IsNotNull (node.Method);
509 var convert = Expression.Lambda<Func<long, decimal>> (node, p).Compile ();
511 Assert.AreEqual (42, convert (42));
515 public void ConvertNullableULongToNullableDecimal ()
517 var p = Expression.Parameter (typeof (ulong?), "l");
519 var node = Expression.Convert (p, typeof (decimal?));
520 Assert.IsTrue (node.IsLifted);
521 Assert.IsTrue (node.IsLiftedToNull);
522 Assert.AreEqual (typeof (decimal?), node.Type);
523 Assert.IsNotNull (node.Method);
525 var convert = Expression.Lambda<Func<ulong?, decimal?>> (node, p).Compile ();
527 Assert.AreEqual (42, convert (42));
528 Assert.AreEqual (null, convert (null));
532 public void ConvertCheckedNullableIntToInt ()
534 var p = Expression.Parameter (typeof (int?), "i");
536 var node = Expression.ConvertChecked (p, typeof (int));
537 Assert.AreEqual (ExpressionType.ConvertChecked, node.NodeType);
538 Assert.IsTrue (node.IsLifted);
539 Assert.IsFalse (node.IsLiftedToNull);
540 Assert.AreEqual (typeof (int), node.Type);
541 Assert.IsNull (node.Method);
544 struct ImplicitToInt {
547 public ImplicitToInt (int v)
552 public static implicit operator int (ImplicitToInt i)
559 public void ConvertNullableImplictToIntToNullableLong ()
561 var i = Expression.Parameter (typeof (ImplicitToInt?), "i");
563 var method = typeof (ImplicitToInt).GetMethod ("op_Implicit");
565 var node = Expression.Convert (i, typeof (int), method);
566 node = Expression.Convert (node, typeof (long?));
567 var conv = Expression.Lambda<Func<ImplicitToInt?, long?>> (node, i).Compile ();
569 Assert.AreEqual ((long?) 42, conv (new ImplicitToInt (42)));
570 Action convnull = () => Assert.AreEqual (null, conv (null));
571 convnull.AssertThrows (typeof (InvalidOperationException));
575 [ExpectedException (typeof (InvalidOperationException))]
576 [Category ("NotWorking")]
577 public void ConvertNullableIntToStringWithConvertMethod ()
580 Expression.Constant ((int?) 0),
582 typeof (Convert).GetMethod ("ToString", new [] { typeof (object) }));