Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_Convert.cs
1 //
2 // ExpressionTest_Convert.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@novell.com)
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
29 using System;
30 using System.Reflection;
31 using System.Linq;
32 using System.Linq.Expressions;
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Linq.Expressions {
36
37         [TestFixture]
38         public class ExpressionTest_Convert {
39
40                 [Test]
41                 [ExpectedException (typeof (ArgumentNullException))]
42                 public void NullExpression ()
43                 {
44                         Expression.Convert (null, typeof (int));
45                 }
46
47                 [Test]
48                 [ExpectedException (typeof (ArgumentNullException))]
49                 public void NullType ()
50                 {
51                         Expression.Convert (1.ToConstant (), null);
52                 }
53
54                 [Test]
55                 [ExpectedException (typeof (InvalidOperationException))]
56                 public void ConvertIntToString ()
57                 {
58                         Expression.Convert (1.ToConstant (), typeof (string));
59                 }
60
61                 interface IFoo { }
62                 class Foo : IFoo { }
63                 class Bar : Foo { }
64                 class Baz { }
65
66                 interface ITzap { }
67
68                 [Test]
69                 public void ConvertBackwardAssignability ()
70                 {
71                         var c = Expression.Convert (
72                                 Expression.Constant (null, typeof (Bar)), typeof (Foo));
73
74                         Assert.AreEqual ("Convert(null)", c.ToString ());
75                 }
76
77                 [Test]
78                 public void ConvertInterfaces ()
79                 {
80                         var p = Expression.Parameter (typeof (IFoo), null);
81
82                         var conv = Expression.Convert (p, typeof (ITzap));
83                         Assert.AreEqual (typeof (ITzap), conv.Type);
84 #if !NET_4_0
85                         Assert.AreEqual ("Convert(<param>)", conv.ToString ());
86 #endif
87                         p = Expression.Parameter (typeof (ITzap), null);
88                         conv = Expression.Convert (p, typeof (IFoo));
89
90                         Assert.AreEqual (typeof (IFoo), conv.Type);
91 #if !NET_4_0
92                         Assert.AreEqual ("Convert(<param>)", conv.ToString ());
93 #endif
94                 }
95
96                 [Test]
97                 public void ConvertCheckedInt32ToInt64 ()
98                 {
99                         var c = Expression.ConvertChecked (
100                                 Expression.Constant (2, typeof (int)), typeof (long));
101
102                         Assert.AreEqual (ExpressionType.ConvertChecked, c.NodeType);
103                         Assert.AreEqual ("ConvertChecked(2)", c.ToString ());
104                 }
105
106                 [Test]
107                 public void ConvertCheckedFallbackToConvertForNonPrimitives ()
108                 {
109                         var p = Expression.ConvertChecked (
110                                 Expression.Constant (null, typeof (object)), typeof (IFoo));
111
112                         Assert.AreEqual (ExpressionType.Convert, p.NodeType);
113                 }
114
115                 [Test]
116                 [ExpectedException (typeof (InvalidOperationException))]
117                 public void ConvertBazToFoo ()
118                 {
119                         Expression.Convert (Expression.Parameter (typeof (Baz), ""), typeof (Foo));
120                 }
121
122                 struct EineStrukt { }
123
124                 [Test]
125                 [ExpectedException (typeof (InvalidOperationException))]
126                 public void ConvertStructToFoo ()
127                 {
128                         Expression.Convert (Expression.Parameter (typeof (EineStrukt), ""), typeof (Foo));
129                 }
130
131                 [Test]
132                 [ExpectedException (typeof (InvalidOperationException))]
133                 public void ConvertInt32ToBool ()
134                 {
135                         Expression.Convert (Expression.Parameter (typeof (int), ""), typeof (bool));
136                 }
137
138                 [Test]
139                 public void ConvertIFooToFoo ()
140                 {
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);
146                 }
147
148                 [Test]
149                 public void BoxInt32 ()
150                 {
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);
156                 }
157
158                 [Test]
159                 public void UnBoxInt32 ()
160                 {
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);
166                 }
167
168                 [Test]
169                 public void ConvertInt32ToInt64 ()
170                 {
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);
176                 }
177
178                 [Test]
179                 public void ConvertInt64ToInt32 ()
180                 {
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);
186                 }
187
188                 enum EineEnum {
189                         EineValue,
190                 }
191
192                 [Test]
193                 public void ConvertEnumToInt32 ()
194                 {
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);
200                 }
201
202                 [Test]
203                 public void ConvertNullableInt32ToInt32 ()
204                 {
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);
210                 }
211
212                 [Test]
213                 public void ConvertInt32ToNullableInt32 ()
214                 {
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);
220                 }
221
222
223                 class Klang {
224                         int i;
225
226                         public Klang (int i)
227                         {
228                                 this.i = i;
229                         }
230
231                         public static explicit operator int (Klang k)
232                         {
233                                 return k.i;
234                         }
235                 }
236
237                 [Test]
238                 public void ConvertClassWithExplicitOp ()
239                 {
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);
245                 }
246
247                 [Test]
248                 public void CompileConvertClassWithExplicitOp ()
249                 {
250                         var p = Expression.Parameter (typeof (Klang), "klang");
251                         var c = Expression.Lambda<Func<Klang, int>> (
252                                 Expression.Convert (p, typeof (int)), p).Compile ();
253
254                         Assert.AreEqual (42, c (new Klang (42)));
255                 }
256
257                 [Test]
258                 public void ConvertClassWithExplicitOpToNullableInt ()
259                 {
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);
265                 }
266
267                 struct Kling {
268                         int i;
269
270                         public Kling (int i)
271                         {
272                                 this.i = i;
273                         }
274
275                         public static implicit operator int (Kling k)
276                         {
277                                 return k.i;
278                         }
279                 }
280
281                 [Test]
282                 public void ConvertStructWithImplicitOp ()
283                 {
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);
289                 }
290
291                 [Test]
292                 public void CompileConvertStructWithImplicitOp ()
293                 {
294                         var p = Expression.Parameter (typeof (Kling), "kling");
295                         var c = Expression.Lambda<Func<Kling, int>> (
296                                 Expression.Convert (p, typeof (int)), p).Compile ();
297
298                         Assert.AreEqual (42, c (new Kling (42)));
299                 }
300
301                 [Test]
302                 public void ConvertStructWithImplicitOpToNullableInt ()
303                 {
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);
309                 }
310
311                 [Test]
312                 public void ConvertNullableStructWithImplicitOpToNullableInt ()
313                 {
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);
319                 }
320
321                 [Test]
322                 public void CompiledBoxing ()
323                 {
324                         var b = Expression.Lambda<Func<object>> (
325                                 Expression.Convert (42.ToConstant (), typeof (object))).Compile ();
326
327                         Assert.AreEqual ((object) 42, b ());
328                 }
329
330                 [Test]
331                 public void CompiledUnBoxing ()
332                 {
333                         var p = Expression.Parameter (typeof (object), "o");
334
335                         var u = Expression.Lambda<Func<object, int>> (
336                                 Expression.Convert (p, typeof (int)), p).Compile ();
337
338                         Assert.AreEqual (42, u ((object) 42));
339                 }
340
341                 [Test]
342                 public void CompiledCast ()
343                 {
344                         var p = Expression.Parameter (typeof (IFoo), "foo");
345
346                         var c = Expression.Lambda<Func<IFoo, Bar>> (
347                                 Expression.Convert (p, typeof (Bar)), p).Compile ();
348
349                         IFoo foo = new Bar ();
350
351                         Bar b = c (foo);
352
353                         Assert.AreEqual (b, foo);
354                 }
355
356                 [Test]
357                 public void CompileNotNullableToNullable ()
358                 {
359                         var p = Expression.Parameter (typeof (int), "i");
360                         var c = Expression.Lambda<Func<int, int?>> (
361                                 Expression.Convert (p, typeof (int?)), p).Compile ();
362
363                         Assert.AreEqual ((int?) 0, c (0));
364                         Assert.AreEqual ((int?) 42, c (42));
365                 }
366
367                 [Test]
368                 [Category ("NotWorkingInterpreter")]
369                 public void CompileNullableToNotNullable ()
370                 {
371                         var p = Expression.Parameter (typeof (int?), "i");
372                         var c = Expression.Lambda<Func<int?, int>> (
373                                 Expression.Convert (p, typeof (int)), p).Compile ();
374
375                         Assert.AreEqual (0, c ((int?) 0));
376                         Assert.AreEqual (42, c ((int?) 42));
377
378                         Action a = () => c (null);
379
380                         a.AssertThrows (typeof (InvalidOperationException));
381                 }
382
383                 [Test]
384                 public void CompiledConvertToSameType ()
385                 {
386                         var k = new Klang (42);
387
388                         var p = Expression.Parameter (typeof (Klang), "klang");
389                         var c = Expression.Lambda<Func<Klang, Klang>> (
390                                 Expression.Convert (
391                                         p, typeof (Klang)),
392                                 p).Compile ();
393
394                         Assert.AreEqual (k, c (k));
395                 }
396
397                 [Test]
398                 public void CompiledConvertNullableToNullable ()
399                 {
400                         var p = Expression.Parameter (typeof (int?), "i");
401                         var c = Expression.Lambda<Func<int?, short?>> (
402                                 Expression.Convert (p, typeof (short?)), p).Compile ();
403
404                         Assert.AreEqual ((short?) null, c (null));
405                         Assert.AreEqual ((short?) 12, c (12));
406                 }
407
408                 [Test]
409                 public void CompiledNullableBoxing ()
410                 {
411                         var p = Expression.Parameter (typeof (int?), "i");
412                         var c = Expression.Lambda<Func<int?, object>> (
413                                 Expression.Convert (p, typeof (object)), p).Compile ();
414
415                         Assert.AreEqual (null, c (null));
416                         Assert.AreEqual ((object) (int?) 42, c (42));
417                 }
418
419                 [Test]
420                 public void CompiledNullableUnboxing ()
421                 {
422                         var p = Expression.Parameter (typeof (object), "o");
423                         var c = Expression.Lambda<Func<object, int?>> (
424                                 Expression.Convert (p, typeof (int?)), p).Compile ();
425
426                         Assert.AreEqual ((int?) null, c (null));
427                         Assert.AreEqual ((int?) 42, c ((int?) 42));
428                 }
429
430                 [Test]
431                 public void ChainedNullableConvert ()
432                 {
433                         var p = Expression.Parameter (typeof (sbyte?), "a");
434
435                         var test = Expression.Lambda<Func<sbyte?, long?>> (
436                                 Expression.Convert (
437                                         Expression.Convert (
438                                                 p,
439                                                 typeof (int?)),
440                                         typeof (long?)), p).Compile ();
441
442                         Assert.AreEqual ((long?) 3, test ((sbyte?) 3));
443                         Assert.AreEqual (null, test (null));
444                 }
445
446                 struct ImplicitToShort {
447                         short value;
448
449                         public ImplicitToShort (short v)
450                         {
451                                 value = v;
452                         }
453
454                         public static implicit operator short (ImplicitToShort i)
455                         {
456                                 return i.value;
457                         }
458                 }
459
460                 [Test]
461                 [Category ("NotWorkingInterpreter")]
462                 public void ConvertImplicitToShortToNullableInt ()
463                 {
464                         var a = Expression.Parameter (typeof (ImplicitToShort?), "a");
465
466                         var method = typeof (ImplicitToShort).GetMethod ("op_Implicit");
467
468                         var node = Expression.Convert (a, typeof (short), method);
469                         Assert.IsTrue (node.IsLifted);
470                         Assert.IsFalse (node.IsLiftedToNull);
471                         Assert.AreEqual (typeof (short), node.Type);
472                         Assert.AreEqual (method, node.Method);
473
474                         var conv = Expression.Lambda<Func<ImplicitToShort?, int?>> (
475                                 Expression.Convert (
476                                         node,
477                                         typeof (int?)), a).Compile ();
478
479                         Assert.AreEqual ((int?) 42, conv (new ImplicitToShort (42)));
480
481                         Action convnull = () => Assert.AreEqual (null, conv (null));
482
483                         convnull.AssertThrows (typeof (InvalidOperationException));
484                 }
485
486                 [Test]
487                 [Category ("NotWorkingInterpreter")]
488                 public void NullableImplicitToShort ()
489                 {
490                         var i = Expression.Parameter (typeof (ImplicitToShort?), "i");
491
492                         var method = typeof (ImplicitToShort).GetMethod ("op_Implicit");
493
494                         var node = Expression.Convert (i, typeof (short?), method);
495
496                         Assert.IsTrue (node.IsLifted);
497                         Assert.IsTrue (node.IsLiftedToNull);
498                         Assert.AreEqual (typeof (short?), node.Type);
499                         Assert.AreEqual (method, node.Method);
500
501                         var convert = Expression.Lambda<Func<ImplicitToShort?, short?>> (node, i).Compile ();
502
503                         Assert.AreEqual ((short?) 42, convert (new ImplicitToShort (42)));
504                 }
505
506                 [Test]
507                 public void ConvertLongToDecimal ()
508                 {
509                         var p = Expression.Parameter (typeof (long), "l");
510
511                         var node = Expression.Convert (p, typeof (decimal));
512                         Assert.IsFalse (node.IsLifted);
513                         Assert.IsFalse (node.IsLiftedToNull);
514                         Assert.AreEqual (typeof (decimal), node.Type);
515                         Assert.IsNotNull (node.Method);
516
517                         var convert = Expression.Lambda<Func<long, decimal>> (node, p).Compile ();
518
519                         Assert.AreEqual (42, convert (42));
520                 }
521
522                 [Test]
523                 [Category ("NotWorkingInterpreter")]
524                 public void ConvertNullableULongToNullableDecimal ()
525                 {
526                         var p = Expression.Parameter (typeof (ulong?), "l");
527
528                         var node = Expression.Convert (p, typeof (decimal?));
529                         Assert.IsTrue (node.IsLifted);
530                         Assert.IsTrue (node.IsLiftedToNull);
531                         Assert.AreEqual (typeof (decimal?), node.Type);
532                         Assert.IsNotNull (node.Method);
533
534                         var convert = Expression.Lambda<Func<ulong?, decimal?>> (node, p).Compile ();
535
536                         Assert.AreEqual (42, convert (42));
537                         Assert.AreEqual (null, convert (null));
538                 }
539
540                 [Test]
541                 public void ConvertCheckedNullableIntToInt ()
542                 {
543                         var p = Expression.Parameter (typeof (int?), "i");
544
545                         var node = Expression.ConvertChecked (p, typeof (int));
546                         Assert.AreEqual (ExpressionType.ConvertChecked, node.NodeType);
547                         Assert.IsTrue (node.IsLifted);
548                         Assert.IsFalse (node.IsLiftedToNull);
549                         Assert.AreEqual (typeof (int), node.Type);
550                         Assert.IsNull (node.Method);
551                 }
552
553                 struct ImplicitToInt {
554                         int Value;
555
556                         public ImplicitToInt (int v)
557                         {
558                                 Value = v;
559                         }
560
561                         public static implicit operator int (ImplicitToInt i)
562                         {
563                                 return i.Value;
564                         }
565                 }
566
567                 [Test]
568                 [Category ("NotWorkingInterpreter")]
569                 public void ConvertNullableImplictToIntToNullableLong ()
570                 {
571                         var i = Expression.Parameter (typeof (ImplicitToInt?), "i");
572
573                         var method = typeof (ImplicitToInt).GetMethod ("op_Implicit");
574
575                         var node = Expression.Convert (i, typeof (int), method);
576                         node = Expression.Convert (node, typeof (long?));
577                         var conv = Expression.Lambda<Func<ImplicitToInt?, long?>> (node, i).Compile ();
578
579                         Assert.AreEqual ((long?) 42, conv (new ImplicitToInt (42)));
580                         Action convnull = () => Assert.AreEqual (null, conv (null));
581                         convnull.AssertThrows (typeof (InvalidOperationException));
582                 }
583
584                 [Test]
585                 [ExpectedException (typeof (InvalidOperationException))]
586                 [Category ("NotWorking")]
587                 public void ConvertNullableIntToStringWithConvertMethod ()
588                 {
589                         Expression.Convert (
590                                 Expression.Constant ((int?) 0),
591                                 typeof (string),
592                                 typeof (Convert).GetMethod ("ToString", new [] { typeof (object) }));
593                 }
594
595                 [Test] // #678897
596                 public void ConvertEnumValueToEnum ()
597                 {
598                         var node = Expression.Convert (
599                                 Expression.Constant (EineEnum.EineValue, typeof (EineEnum)),
600                                 typeof (Enum));
601
602                         Assert.IsNotNull (node);
603                         Assert.AreEqual (typeof (Enum), node.Type);
604                 }
605         }
606 }