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, Foo)", 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 p = Expression.Parameter (typeof (ITzap), null);
85 conv = Expression.Convert (p, typeof (IFoo));
87 Assert.AreEqual (typeof (IFoo), conv.Type);
91 public void ConvertCheckedInt32ToInt64 ()
93 var c = Expression.ConvertChecked (
94 Expression.Constant (2, typeof (int)), typeof (long));
96 Assert.AreEqual (ExpressionType.ConvertChecked, c.NodeType);
97 Assert.AreEqual ("ConvertChecked(2, Int64)", c.ToString ());
101 public void ConvertCheckedFallbackToConvertForNonPrimitives ()
103 var p = Expression.ConvertChecked (
104 Expression.Constant (null, typeof (object)), typeof (IFoo));
106 Assert.AreEqual (ExpressionType.Convert, p.NodeType);
110 [ExpectedException (typeof (InvalidOperationException))]
111 public void ConvertBazToFoo ()
113 Expression.Convert (Expression.Parameter (typeof (Baz), ""), typeof (Foo));
116 struct EineStrukt { }
119 [ExpectedException (typeof (InvalidOperationException))]
120 public void ConvertStructToFoo ()
122 Expression.Convert (Expression.Parameter (typeof (EineStrukt), ""), typeof (Foo));
126 [ExpectedException (typeof (InvalidOperationException))]
127 public void ConvertInt32ToBool ()
129 Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (bool));
133 public void ConvertIFooToFoo ()
135 var c = Expression.Convert (Expression.Parameter (typeof (IFoo), ""), typeof (Foo));
136 Assert.AreEqual (typeof (Foo), c.Type);
137 Assert.IsFalse (c.IsLifted);
138 Assert.IsFalse (c.IsLiftedToNull);
139 Assert.IsNull (c.Method);
143 public void BoxInt32 ()
145 var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (object));
146 Assert.AreEqual (typeof (object), c.Type);
147 Assert.IsFalse (c.IsLifted);
148 Assert.IsFalse (c.IsLiftedToNull);
149 Assert.IsNull (c.Method);
153 public void UnBoxInt32 ()
155 var c = Expression.Convert (Expression.Parameter (typeof (object), ""), typeof (int));
156 Assert.AreEqual (typeof (int), c.Type);
157 Assert.IsFalse (c.IsLifted);
158 Assert.IsFalse (c.IsLiftedToNull);
159 Assert.IsNull (c.Method);
163 public void ConvertInt32ToInt64 ()
165 var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (long));
166 Assert.AreEqual (typeof (long), c.Type);
167 Assert.IsFalse (c.IsLifted);
168 Assert.IsFalse (c.IsLiftedToNull);
169 Assert.IsNull (c.Method);
173 public void ConvertInt64ToInt32 ()
175 var c = Expression.Convert (Expression.Parameter (typeof (long), ""), typeof (int));
176 Assert.AreEqual (typeof (int), c.Type);
177 Assert.IsFalse (c.IsLifted);
178 Assert.IsFalse (c.IsLiftedToNull);
179 Assert.IsNull (c.Method);
187 public void ConvertEnumToInt32 ()
189 var c = Expression.Convert (Expression.Parameter (typeof (EineEnum), ""), typeof (int));
190 Assert.AreEqual (typeof (int), c.Type);
191 Assert.IsFalse (c.IsLifted);
192 Assert.IsFalse (c.IsLiftedToNull);
193 Assert.IsNull (c.Method);
197 public void ConvertNullableInt32ToInt32 ()
199 var c = Expression.Convert (Expression.Parameter (typeof (int?), ""), typeof (int));
200 Assert.AreEqual (typeof (int), c.Type);
201 Assert.IsTrue (c.IsLifted);
202 Assert.IsFalse (c.IsLiftedToNull);
203 Assert.IsNull (c.Method);
207 public void ConvertInt32ToNullableInt32 ()
209 var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (int?));
210 Assert.AreEqual (typeof (int?), c.Type);
211 Assert.IsTrue (c.IsLifted);
212 Assert.IsTrue (c.IsLiftedToNull);
213 Assert.IsNull (c.Method);
225 public static explicit operator int (Klang k)
232 public void ConvertClassWithExplicitOp ()
234 var c = Expression.Convert (Expression.Parameter (typeof (Klang), ""), typeof (int));
235 Assert.AreEqual (typeof (int), c.Type);
236 Assert.IsFalse (c.IsLifted);
237 Assert.IsFalse (c.IsLiftedToNull);
238 Assert.IsNotNull (c.Method);
242 public void CompileConvertClassWithExplicitOp ()
244 var p = Expression.Parameter (typeof (Klang), "klang");
245 var c = Expression.Lambda<Func<Klang, int>> (
246 Expression.Convert (p, typeof (int)), p).Compile ();
248 Assert.AreEqual (42, c (new Klang (42)));
252 public void ConvertClassWithExplicitOpToNullableInt ()
254 var c = Expression.Convert (Expression.Parameter (typeof (Klang), ""), typeof (int?));
255 Assert.AreEqual (typeof (int?), c.Type);
256 Assert.IsTrue (c.IsLifted);
257 Assert.IsTrue (c.IsLiftedToNull);
258 Assert.IsNotNull (c.Method);
269 public static implicit operator int (Kling k)
276 public void ConvertStructWithImplicitOp ()
278 var c = Expression.Convert (Expression.Parameter (typeof (Kling), ""), typeof (int));
279 Assert.AreEqual (typeof (int), c.Type);
280 Assert.IsFalse (c.IsLifted);
281 Assert.IsFalse (c.IsLiftedToNull);
282 Assert.IsNotNull (c.Method);
286 public void CompileConvertStructWithImplicitOp ()
288 var p = Expression.Parameter (typeof (Kling), "kling");
289 var c = Expression.Lambda<Func<Kling, int>> (
290 Expression.Convert (p, typeof (int)), p).Compile ();
292 Assert.AreEqual (42, c (new Kling (42)));
296 public void ConvertStructWithImplicitOpToNullableInt ()
298 var c = Expression.Convert (Expression.Parameter (typeof (Kling), ""), typeof (int?));
299 Assert.AreEqual (typeof (int?), c.Type);
300 Assert.IsTrue (c.IsLifted);
301 Assert.IsTrue (c.IsLiftedToNull);
302 Assert.IsNotNull (c.Method);
306 public void ConvertNullableStructWithImplicitOpToNullableInt ()
308 var c = Expression.Convert (Expression.Parameter (typeof (Kling?), ""), typeof (int?));
309 Assert.AreEqual (typeof (int?), c.Type);
310 Assert.IsTrue (c.IsLifted);
311 Assert.IsTrue (c.IsLiftedToNull);
312 Assert.IsNotNull (c.Method);
316 public void CompiledBoxing ()
318 var b = Expression.Lambda<Func<object>> (
319 Expression.Convert (42.ToConstant (), typeof (object))).Compile ();
321 Assert.AreEqual ((object) 42, b ());
325 public void CompiledUnBoxing ()
327 var p = Expression.Parameter (typeof (object), "o");
329 var u = Expression.Lambda<Func<object, int>> (
330 Expression.Convert (p, typeof (int)), p).Compile ();
332 Assert.AreEqual (42, u ((object) 42));
336 public void CompiledCast ()
338 var p = Expression.Parameter (typeof (IFoo), "foo");
340 var c = Expression.Lambda<Func<IFoo, Bar>> (
341 Expression.Convert (p, typeof (Bar)), p).Compile ();
343 IFoo foo = new Bar ();
347 Assert.AreEqual (b, foo);
351 public void CompileNotNullableToNullable ()
353 var p = Expression.Parameter (typeof (int), "i");
354 var c = Expression.Lambda<Func<int, int?>> (
355 Expression.Convert (p, typeof (int?)), p).Compile ();
357 Assert.AreEqual ((int?) 0, c (0));
358 Assert.AreEqual ((int?) 42, c (42));
362 public void CompileNullableToNotNullable ()
364 var p = Expression.Parameter (typeof (int?), "i");
365 var c = Expression.Lambda<Func<int?, int>> (
366 Expression.Convert (p, typeof (int)), p).Compile ();
368 Assert.AreEqual (0, c ((int?) 0));
369 Assert.AreEqual (42, c ((int?) 42));
371 Action a = () => c (null);
373 a.AssertThrows (typeof (InvalidOperationException));
377 public void CompiledConvertToSameType ()
379 var k = new Klang (42);
381 var p = Expression.Parameter (typeof (Klang), "klang");
382 var c = Expression.Lambda<Func<Klang, Klang>> (
387 Assert.AreEqual (k, c (k));
391 public void CompiledConvertNullableToNullable ()
393 var p = Expression.Parameter (typeof (int?), "i");
394 var c = Expression.Lambda<Func<int?, short?>> (
395 Expression.Convert (p, typeof (short?)), p).Compile ();
397 Assert.AreEqual ((short?) null, c (null));
398 Assert.AreEqual ((short?) 12, c (12));
402 public void CompiledNullableBoxing ()
404 var p = Expression.Parameter (typeof (int?), "i");
405 var c = Expression.Lambda<Func<int?, object>> (
406 Expression.Convert (p, typeof (object)), p).Compile ();
408 Assert.AreEqual (null, c (null));
409 Assert.AreEqual ((object) (int?) 42, c (42));
413 public void CompiledNullableUnboxing ()
415 var p = Expression.Parameter (typeof (object), "o");
416 var c = Expression.Lambda<Func<object, int?>> (
417 Expression.Convert (p, typeof (int?)), p).Compile ();
419 Assert.AreEqual ((int?) null, c (null));
420 Assert.AreEqual ((int?) 42, c ((int?) 42));
424 public void ChainedNullableConvert ()
426 var p = Expression.Parameter (typeof (sbyte?), "a");
428 var test = Expression.Lambda<Func<sbyte?, long?>> (
433 typeof (long?)), p).Compile ();
435 Assert.AreEqual ((long?) 3, test ((sbyte?) 3));
436 Assert.AreEqual (null, test (null));
439 struct ImplicitToShort {
442 public ImplicitToShort (short v)
447 public static implicit operator short (ImplicitToShort i)
454 public void ConvertImplicitToShortToNullableInt ()
456 var a = Expression.Parameter (typeof (ImplicitToShort?), "a");
458 var method = typeof (ImplicitToShort).GetMethod ("op_Implicit");
460 var node = Expression.Convert (a, typeof (short), method);
461 Assert.IsTrue (node.IsLifted);
462 Assert.IsFalse (node.IsLiftedToNull);
463 Assert.AreEqual (typeof (short), node.Type);
464 Assert.AreEqual (method, node.Method);
466 var conv = Expression.Lambda<Func<ImplicitToShort?, int?>> (
469 typeof (int?)), a).Compile ();
471 Assert.AreEqual ((int?) 42, conv (new ImplicitToShort (42)));
473 Action convnull = () => Assert.AreEqual (null, conv (null));
475 convnull.AssertThrows (typeof (InvalidOperationException));
479 public void NullableImplicitToShort ()
481 var i = Expression.Parameter (typeof (ImplicitToShort?), "i");
483 var method = typeof (ImplicitToShort).GetMethod ("op_Implicit");
485 var node = Expression.Convert (i, typeof (short?), method);
487 Assert.IsTrue (node.IsLifted);
488 Assert.IsTrue (node.IsLiftedToNull);
489 Assert.AreEqual (typeof (short?), node.Type);
490 Assert.AreEqual (method, node.Method);
492 var convert = Expression.Lambda<Func<ImplicitToShort?, short?>> (node, i).Compile ();
494 Assert.AreEqual ((short?) 42, convert (new ImplicitToShort (42)));
498 public void ConvertLongToDecimal ()
500 var p = Expression.Parameter (typeof (long), "l");
502 var node = Expression.Convert (p, typeof (decimal));
503 Assert.IsFalse (node.IsLifted);
504 Assert.IsFalse (node.IsLiftedToNull);
505 Assert.AreEqual (typeof (decimal), node.Type);
506 Assert.IsNotNull (node.Method);
508 var convert = Expression.Lambda<Func<long, decimal>> (node, p).Compile ();
510 Assert.AreEqual (42, convert (42));
514 public void ConvertNullableULongToNullableDecimal ()
516 var p = Expression.Parameter (typeof (ulong?), "l");
518 var node = Expression.Convert (p, typeof (decimal?));
519 Assert.IsTrue (node.IsLifted);
520 Assert.IsTrue (node.IsLiftedToNull);
521 Assert.AreEqual (typeof (decimal?), node.Type);
522 Assert.IsNotNull (node.Method);
524 var convert = Expression.Lambda<Func<ulong?, decimal?>> (node, p).Compile ();
526 Assert.AreEqual (42, convert (42));
527 Assert.AreEqual (null, convert (null));
531 public void ConvertCheckedNullableIntToInt ()
533 var p = Expression.Parameter (typeof (int?), "i");
535 var node = Expression.ConvertChecked (p, typeof (int));
536 Assert.AreEqual (ExpressionType.ConvertChecked, node.NodeType);
537 Assert.IsTrue (node.IsLifted);
538 Assert.IsFalse (node.IsLiftedToNull);
539 Assert.AreEqual (typeof (int), node.Type);
540 Assert.IsNull (node.Method);
543 struct ImplicitToInt {
546 public ImplicitToInt (int v)
551 public static implicit operator int (ImplicitToInt i)
558 public void ConvertNullableImplictToIntToNullableLong ()
560 var i = Expression.Parameter (typeof (ImplicitToInt?), "i");
562 var method = typeof (ImplicitToInt).GetMethod ("op_Implicit");
564 var node = Expression.Convert (i, typeof (int), method);
565 node = Expression.Convert (node, typeof (long?));
566 var conv = Expression.Lambda<Func<ImplicitToInt?, long?>> (node, i).Compile ();
568 Assert.AreEqual ((long?) 42, conv (new ImplicitToInt (42)));
569 Action convnull = () => Assert.AreEqual (null, conv (null));
570 convnull.AssertThrows (typeof (InvalidOperationException));
574 [ExpectedException (typeof (InvalidOperationException))]
575 public void ConvertNullableIntToStringWithConvertMethod ()
578 Expression.Constant ((int?) 0),
580 typeof (Convert).GetMethod ("ToString", new [] { typeof (object) }));
584 public void ConvertEnumValueToEnum ()
586 var node = Expression.Convert (
587 Expression.Constant (EineEnum.EineValue, typeof (EineEnum)),
590 Assert.IsNotNull (node);
591 Assert.AreEqual (typeof (Enum), node.Type);