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);
85 Assert.AreEqual ("Convert(<param>)", conv.ToString ());
87 p = Expression.Parameter (typeof (ITzap), null);
88 conv = Expression.Convert (p, typeof (IFoo));
90 Assert.AreEqual (typeof (IFoo), conv.Type);
92 Assert.AreEqual ("Convert(<param>)", conv.ToString ());
97 public void ConvertCheckedInt32ToInt64 ()
99 var c = Expression.ConvertChecked (
100 Expression.Constant (2, typeof (int)), typeof (long));
102 Assert.AreEqual (ExpressionType.ConvertChecked, c.NodeType);
103 Assert.AreEqual ("ConvertChecked(2)", c.ToString ());
107 public void ConvertCheckedFallbackToConvertForNonPrimitives ()
109 var p = Expression.ConvertChecked (
110 Expression.Constant (null, typeof (object)), typeof (IFoo));
112 Assert.AreEqual (ExpressionType.Convert, p.NodeType);
116 [ExpectedException (typeof (InvalidOperationException))]
117 public void ConvertBazToFoo ()
119 Expression.Convert (Expression.Parameter (typeof (Baz), ""), typeof (Foo));
122 struct EineStrukt { }
125 [ExpectedException (typeof (InvalidOperationException))]
126 public void ConvertStructToFoo ()
128 Expression.Convert (Expression.Parameter (typeof (EineStrukt), ""), typeof (Foo));
132 [ExpectedException (typeof (InvalidOperationException))]
133 public void ConvertInt32ToBool ()
135 Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (bool));
139 public void ConvertIFooToFoo ()
141 var c = Expression.Convert (Expression.Parameter (typeof (IFoo), ""), typeof (Foo));
142 Assert.AreEqual (typeof (Foo), c.Type);
143 Assert.IsFalse (c.IsLifted);
144 Assert.IsFalse (c.IsLiftedToNull);
145 Assert.IsNull (c.Method);
149 public void BoxInt32 ()
151 var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (object));
152 Assert.AreEqual (typeof (object), c.Type);
153 Assert.IsFalse (c.IsLifted);
154 Assert.IsFalse (c.IsLiftedToNull);
155 Assert.IsNull (c.Method);
159 public void UnBoxInt32 ()
161 var c = Expression.Convert (Expression.Parameter (typeof (object), ""), typeof (int));
162 Assert.AreEqual (typeof (int), c.Type);
163 Assert.IsFalse (c.IsLifted);
164 Assert.IsFalse (c.IsLiftedToNull);
165 Assert.IsNull (c.Method);
169 public void ConvertInt32ToInt64 ()
171 var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (long));
172 Assert.AreEqual (typeof (long), c.Type);
173 Assert.IsFalse (c.IsLifted);
174 Assert.IsFalse (c.IsLiftedToNull);
175 Assert.IsNull (c.Method);
179 public void ConvertInt64ToInt32 ()
181 var c = Expression.Convert (Expression.Parameter (typeof (long), ""), typeof (int));
182 Assert.AreEqual (typeof (int), c.Type);
183 Assert.IsFalse (c.IsLifted);
184 Assert.IsFalse (c.IsLiftedToNull);
185 Assert.IsNull (c.Method);
193 public void ConvertEnumToInt32 ()
195 var c = Expression.Convert (Expression.Parameter (typeof (EineEnum), ""), typeof (int));
196 Assert.AreEqual (typeof (int), c.Type);
197 Assert.IsFalse (c.IsLifted);
198 Assert.IsFalse (c.IsLiftedToNull);
199 Assert.IsNull (c.Method);
203 public void ConvertNullableInt32ToInt32 ()
205 var c = Expression.Convert (Expression.Parameter (typeof (int?), ""), typeof (int));
206 Assert.AreEqual (typeof (int), c.Type);
207 Assert.IsTrue (c.IsLifted);
208 Assert.IsFalse (c.IsLiftedToNull);
209 Assert.IsNull (c.Method);
213 public void ConvertInt32ToNullableInt32 ()
215 var c = Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (int?));
216 Assert.AreEqual (typeof (int?), c.Type);
217 Assert.IsTrue (c.IsLifted);
218 Assert.IsTrue (c.IsLiftedToNull);
219 Assert.IsNull (c.Method);
231 public static explicit operator int (Klang k)
238 public void ConvertClassWithExplicitOp ()
240 var c = Expression.Convert (Expression.Parameter (typeof (Klang), ""), typeof (int));
241 Assert.AreEqual (typeof (int), c.Type);
242 Assert.IsFalse (c.IsLifted);
243 Assert.IsFalse (c.IsLiftedToNull);
244 Assert.IsNotNull (c.Method);
248 public void CompileConvertClassWithExplicitOp ()
250 var p = Expression.Parameter (typeof (Klang), "klang");
251 var c = Expression.Lambda<Func<Klang, int>> (
252 Expression.Convert (p, typeof (int)), p).Compile ();
254 Assert.AreEqual (42, c (new Klang (42)));
258 public void ConvertClassWithExplicitOpToNullableInt ()
260 var c = Expression.Convert (Expression.Parameter (typeof (Klang), ""), typeof (int?));
261 Assert.AreEqual (typeof (int?), c.Type);
262 Assert.IsTrue (c.IsLifted);
263 Assert.IsTrue (c.IsLiftedToNull);
264 Assert.IsNotNull (c.Method);
275 public static implicit operator int (Kling k)
282 public void ConvertStructWithImplicitOp ()
284 var c = Expression.Convert (Expression.Parameter (typeof (Kling), ""), typeof (int));
285 Assert.AreEqual (typeof (int), c.Type);
286 Assert.IsFalse (c.IsLifted);
287 Assert.IsFalse (c.IsLiftedToNull);
288 Assert.IsNotNull (c.Method);
292 public void CompileConvertStructWithImplicitOp ()
294 var p = Expression.Parameter (typeof (Kling), "kling");
295 var c = Expression.Lambda<Func<Kling, int>> (
296 Expression.Convert (p, typeof (int)), p).Compile ();
298 Assert.AreEqual (42, c (new Kling (42)));
302 public void ConvertStructWithImplicitOpToNullableInt ()
304 var c = Expression.Convert (Expression.Parameter (typeof (Kling), ""), typeof (int?));
305 Assert.AreEqual (typeof (int?), c.Type);
306 Assert.IsTrue (c.IsLifted);
307 Assert.IsTrue (c.IsLiftedToNull);
308 Assert.IsNotNull (c.Method);
312 public void ConvertNullableStructWithImplicitOpToNullableInt ()
314 var c = Expression.Convert (Expression.Parameter (typeof (Kling?), ""), typeof (int?));
315 Assert.AreEqual (typeof (int?), c.Type);
316 Assert.IsTrue (c.IsLifted);
317 Assert.IsTrue (c.IsLiftedToNull);
318 Assert.IsNotNull (c.Method);
322 public void CompiledBoxing ()
324 var b = Expression.Lambda<Func<object>> (
325 Expression.Convert (42.ToConstant (), typeof (object))).Compile ();
327 Assert.AreEqual ((object) 42, b ());
331 public void CompiledUnBoxing ()
333 var p = Expression.Parameter (typeof (object), "o");
335 var u = Expression.Lambda<Func<object, int>> (
336 Expression.Convert (p, typeof (int)), p).Compile ();
338 Assert.AreEqual (42, u ((object) 42));
342 public void CompiledCast ()
344 var p = Expression.Parameter (typeof (IFoo), "foo");
346 var c = Expression.Lambda<Func<IFoo, Bar>> (
347 Expression.Convert (p, typeof (Bar)), p).Compile ();
349 IFoo foo = new Bar ();
353 Assert.AreEqual (b, foo);
357 public void CompileNotNullableToNullable ()
359 var p = Expression.Parameter (typeof (int), "i");
360 var c = Expression.Lambda<Func<int, int?>> (
361 Expression.Convert (p, typeof (int?)), p).Compile ();
363 Assert.AreEqual ((int?) 0, c (0));
364 Assert.AreEqual ((int?) 42, c (42));
368 public void CompileNullableToNotNullable ()
370 var p = Expression.Parameter (typeof (int?), "i");
371 var c = Expression.Lambda<Func<int?, int>> (
372 Expression.Convert (p, typeof (int)), p).Compile ();
374 Assert.AreEqual (0, c ((int?) 0));
375 Assert.AreEqual (42, c ((int?) 42));
377 Action a = () => c (null);
379 a.AssertThrows (typeof (InvalidOperationException));
383 public void CompiledConvertToSameType ()
385 var k = new Klang (42);
387 var p = Expression.Parameter (typeof (Klang), "klang");
388 var c = Expression.Lambda<Func<Klang, Klang>> (
393 Assert.AreEqual (k, c (k));
397 public void CompiledConvertNullableToNullable ()
399 var p = Expression.Parameter (typeof (int?), "i");
400 var c = Expression.Lambda<Func<int?, short?>> (
401 Expression.Convert (p, typeof (short?)), p).Compile ();
403 Assert.AreEqual ((short?) null, c (null));
404 Assert.AreEqual ((short?) 12, c (12));
408 public void CompiledNullableBoxing ()
410 var p = Expression.Parameter (typeof (int?), "i");
411 var c = Expression.Lambda<Func<int?, object>> (
412 Expression.Convert (p, typeof (object)), p).Compile ();
414 Assert.AreEqual (null, c (null));
415 Assert.AreEqual ((object) (int?) 42, c (42));
419 public void CompiledNullableUnboxing ()
421 var p = Expression.Parameter (typeof (object), "o");
422 var c = Expression.Lambda<Func<object, int?>> (
423 Expression.Convert (p, typeof (int?)), p).Compile ();
425 Assert.AreEqual ((int?) null, c (null));
426 Assert.AreEqual ((int?) 42, c ((int?) 42));
430 public void ChainedNullableConvert ()
432 var p = Expression.Parameter (typeof (sbyte?), "a");
434 var test = Expression.Lambda<Func<sbyte?, long?>> (
439 typeof (long?)), p).Compile ();
441 Assert.AreEqual ((long?) 3, test ((sbyte?) 3));
442 Assert.AreEqual (null, test (null));
445 struct ImplicitToShort {
448 public ImplicitToShort (short v)
453 public static implicit operator short (ImplicitToShort i)
460 public void ConvertImplicitToShortToNullableInt ()
462 var a = Expression.Parameter (typeof (ImplicitToShort?), "a");
464 var method = typeof (ImplicitToShort).GetMethod ("op_Implicit");
466 var node = Expression.Convert (a, typeof (short), method);
467 Assert.IsTrue (node.IsLifted);
468 Assert.IsFalse (node.IsLiftedToNull);
469 Assert.AreEqual (typeof (short), node.Type);
470 Assert.AreEqual (method, node.Method);
472 var conv = Expression.Lambda<Func<ImplicitToShort?, int?>> (
475 typeof (int?)), a).Compile ();
477 Assert.AreEqual ((int?) 42, conv (new ImplicitToShort (42)));
479 Action convnull = () => Assert.AreEqual (null, conv (null));
481 convnull.AssertThrows (typeof (InvalidOperationException));
485 public void NullableImplicitToShort ()
487 var i = Expression.Parameter (typeof (ImplicitToShort?), "i");
489 var method = typeof (ImplicitToShort).GetMethod ("op_Implicit");
491 var node = Expression.Convert (i, typeof (short?), method);
493 Assert.IsTrue (node.IsLifted);
494 Assert.IsTrue (node.IsLiftedToNull);
495 Assert.AreEqual (typeof (short?), node.Type);
496 Assert.AreEqual (method, node.Method);
498 var convert = Expression.Lambda<Func<ImplicitToShort?, short?>> (node, i).Compile ();
500 Assert.AreEqual ((short?) 42, convert (new ImplicitToShort (42)));
504 public void ConvertLongToDecimal ()
506 var p = Expression.Parameter (typeof (long), "l");
508 var node = Expression.Convert (p, typeof (decimal));
509 Assert.IsFalse (node.IsLifted);
510 Assert.IsFalse (node.IsLiftedToNull);
511 Assert.AreEqual (typeof (decimal), node.Type);
512 Assert.IsNotNull (node.Method);
514 var convert = Expression.Lambda<Func<long, decimal>> (node, p).Compile ();
516 Assert.AreEqual (42, convert (42));
520 public void ConvertNullableULongToNullableDecimal ()
522 var p = Expression.Parameter (typeof (ulong?), "l");
524 var node = Expression.Convert (p, typeof (decimal?));
525 Assert.IsTrue (node.IsLifted);
526 Assert.IsTrue (node.IsLiftedToNull);
527 Assert.AreEqual (typeof (decimal?), node.Type);
528 Assert.IsNotNull (node.Method);
530 var convert = Expression.Lambda<Func<ulong?, decimal?>> (node, p).Compile ();
532 Assert.AreEqual (42, convert (42));
533 Assert.AreEqual (null, convert (null));
537 public void ConvertCheckedNullableIntToInt ()
539 var p = Expression.Parameter (typeof (int?), "i");
541 var node = Expression.ConvertChecked (p, typeof (int));
542 Assert.AreEqual (ExpressionType.ConvertChecked, node.NodeType);
543 Assert.IsTrue (node.IsLifted);
544 Assert.IsFalse (node.IsLiftedToNull);
545 Assert.AreEqual (typeof (int), node.Type);
546 Assert.IsNull (node.Method);
549 struct ImplicitToInt {
552 public ImplicitToInt (int v)
557 public static implicit operator int (ImplicitToInt i)
564 public void ConvertNullableImplictToIntToNullableLong ()
566 var i = Expression.Parameter (typeof (ImplicitToInt?), "i");
568 var method = typeof (ImplicitToInt).GetMethod ("op_Implicit");
570 var node = Expression.Convert (i, typeof (int), method);
571 node = Expression.Convert (node, typeof (long?));
572 var conv = Expression.Lambda<Func<ImplicitToInt?, long?>> (node, i).Compile ();
574 Assert.AreEqual ((long?) 42, conv (new ImplicitToInt (42)));
575 Action convnull = () => Assert.AreEqual (null, conv (null));
576 convnull.AssertThrows (typeof (InvalidOperationException));
580 [ExpectedException (typeof (InvalidOperationException))]
581 [Category ("NotWorking")]
582 public void ConvertNullableIntToStringWithConvertMethod ()
585 Expression.Constant ((int?) 0),
587 typeof (Convert).GetMethod ("ToString", new [] { typeof (object) }));
591 public void ConvertEnumValueToEnum ()
593 var node = Expression.Convert (
594 Expression.Constant (EineEnum.EineValue, typeof (EineEnum)),
597 Assert.IsNotNull (node);
598 Assert.AreEqual (typeof (Enum), node.Type);