Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / System.Core / Test / System.Linq.Expressions / ExpressionTest_Lambda.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 //
19 // Authors:
20 //   Miguel de Icaza (miguel@novell.com)
21 //   Jb Evain (jbevain@novell.com)
22 //
23
24 using System;
25 using System.Reflection;
26 using System.Linq;
27 using System.Linq.Expressions;
28 using NUnit.Framework;
29
30 namespace MonoTests.System.Linq.Expressions
31 {
32         [TestFixture]
33         public class ExpressionTest_Lambda
34         {
35                 [Test]
36                 [ExpectedException (typeof (ArgumentException))]
37                 public void NonDelegateTypeInCtor ()
38                 {
39                         // The first parameter must be a delegate type
40                         Expression.Lambda (typeof(string), Expression.Constant (1), new ParameterExpression [0]);
41                 }
42
43                 delegate object delegate_object_emtpy ();
44                 delegate object delegate_object_int (int a);
45                 delegate object delegate_object_string (string s);
46                 delegate object delegate_object_object (object s);
47
48                 [Test]
49                 [ExpectedException (typeof (ArgumentException))]
50                 public void InvalidConversion ()
51                 {
52                         // float to object, invalid
53                         Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant (1.0), new ParameterExpression [0]);
54                 }
55
56                 [Test]
57                 [ExpectedException (typeof (ArgumentException))]
58                 public void InvalidConversion2 ()
59                 {
60                         // float to object, invalid
61                         Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant (1), new ParameterExpression [0]);
62                 }
63
64                 [Test]
65                 [ExpectedException (typeof (ArgumentException))]
66                 public void InvalidArgCount ()
67                 {
68                         // missing a parameter
69                         Expression.Lambda (typeof (delegate_object_int), Expression.Constant ("foo"), new ParameterExpression [0]);
70                 }
71
72                 [Test]
73                 [ExpectedException (typeof (ArgumentException))]
74                 public void InvalidArgCount2 ()
75                 {
76                         // extra parameter
77                         ParameterExpression p = Expression.Parameter (typeof (int), "AAA");
78                         Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant ("foo"), new ParameterExpression [1] {p});
79                 }
80
81                 [Test]
82                 [ExpectedException (typeof (ArgumentException))]
83                 public void InvalidArgType ()
84                 {
85                         // invalid argument type
86                         ParameterExpression p = Expression.Parameter (typeof (string), "AAA");
87                         Expression.Lambda (typeof (delegate_object_int), Expression.Constant ("foo"), new ParameterExpression [1] {p});
88                 }
89
90                 [Test]
91                 [ExpectedException (typeof (ArgumentException))]
92                 public void InvalidArgType2 ()
93                 {
94                         // invalid argument type
95
96                         ParameterExpression p = Expression.Parameter (typeof (string), "AAA");
97                         Expression.Lambda (typeof (delegate_object_object), Expression.Constant ("foo"), new ParameterExpression [1] {p});
98                 }
99
100                 [Test]
101                 [ExpectedException (typeof (ArgumentNullException))]
102                 public void NullParameter ()
103                 {
104                         Expression.Lambda<Func<int, int>> (Expression.Constant (1), new ParameterExpression [] { null });
105                 }
106
107                 [Test]
108                 public void Assignability ()
109                 {
110                         // allowed: string to object
111                         Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant ("string"), new ParameterExpression [0]);
112
113                         // allowed delegate has string, delegate has base class (object)
114                         ParameterExpression p = Expression.Parameter (typeof (object), "ParObject");
115                         var l = Expression.Lambda (typeof (delegate_object_string), Expression.Constant (""), new ParameterExpression [1] {p});
116
117                         Assert.AreEqual ("ParObject => \"\"", l.ToString ());
118                 }
119
120                 [Test]
121                 [ExpectedException(typeof(InvalidOperationException))]
122                 public void ParameterOutOfScope ()
123                 {
124                         ParameterExpression a = Expression.Parameter(typeof (int), "a");
125                         ParameterExpression second_a = Expression.Parameter(typeof(int), "a");
126
127                         // Here we have the same name for the parameter expression, but
128                         // we pass a different object to the Lambda expression, so they are
129                         // different, this should throw
130                         Expression<Func<int,int>> l = Expression.Lambda<Func<int,int>>(a, new ParameterExpression [] { second_a });
131                         l.Compile ();
132                 }
133
134                 [Test]
135                 public void ParameterRefTest ()
136                 {
137                         ParameterExpression a = Expression.Parameter(typeof(int), "a");
138                         ParameterExpression b = Expression.Parameter(typeof(int), "b");
139
140                         Expression<Func<int,int,int>> l = Expression.Lambda<Func<int,int,int>>(
141                                 Expression.Add (a, b), new ParameterExpression [] { a, b });
142
143                         Assert.AreEqual (typeof (Func<int, int, int>), l.Type);
144                         Assert.AreEqual ("(a, b) => (a + b)", l.ToString ());
145
146                         Func<int,int,int> xx = l.Compile ();
147                         int res = xx (10, 20);
148                         Assert.AreEqual (res, 30);
149                 }
150
151                 [Test]
152                 public void Compile ()
153                 {
154                         Expression<Func<int>> l = Expression.Lambda<Func<int>> (Expression.Constant (1), new ParameterExpression [0]);
155                         Assert.AreEqual (typeof (Func<int>), l.Type);
156                         Assert.AreEqual ("() => 1", l.ToString ());
157
158                         Func<int> fi = l.Compile ();
159                         fi ();
160                 }
161
162                 [Test]
163                 [ExpectedException(typeof(ArgumentException))]
164                 public void ReturnValueCheck ()
165                 {
166                         ParameterExpression p1 = Expression.Parameter(typeof(int?), "va");
167                         ParameterExpression p2 = Expression.Parameter(typeof(int?), "vb");
168                         Expression add = Expression.Add(p1, p2);
169
170
171                         // This should throw, since the add.Type is "int?" and the return
172                         // type we have here is int.
173                         Expression.Lambda<Func<int?,int?,int>> (add, p1, p2);
174                 }
175
176                 public static void Foo ()
177                 {
178                 }
179
180                 [Test]
181                 public void LambdaType ()
182                 {
183                         var l = Expression.Lambda (Expression.Constant (1), new [] { Expression.Parameter (typeof (int), "foo") });
184
185                         Assert.AreEqual (typeof (Func<int, int>), l.Type);
186
187                         l = Expression.Lambda (Expression.Call (null, GetType ().GetMethod ("Foo")), new [] { Expression.Parameter (typeof (string), "foofoo") });
188
189                         Assert.AreEqual (typeof (Action<string>), l.Type);
190                 }
191
192                 [Test]
193                 public void UnTypedLambdaReturnsExpressionOfDelegateType ()
194                 {
195                         var l = Expression.Lambda ("foo".ToConstant ());
196
197                         Assert.AreEqual (typeof (Expression<Func<string>>), l.GetType ());
198                 }
199
200                 public static int CallDelegate (Func<int, int> e)
201                 {
202                         return e (42);
203                 }
204
205                 [Test]
206                 public void LambdaPassedAsDelegate ()
207                 {
208                         var pi = Expression.Parameter (typeof (int), "i");
209                         var identity = Expression.Lambda<Func<int, int>> (pi, pi);
210
211                         var l = Expression.Lambda<Func<int>> (
212                                 Expression.Call (
213                                         GetType ().GetMethod ("CallDelegate"),
214                                         identity)).Compile ();
215
216                         Assert.AreEqual (42, l ());
217                 }
218
219                 [Test]
220                 [Category ("NotWorking")]
221                 public void LambdaPassedAsDelegateUsingParentParameter ()
222                 {
223                         var a = Expression.Parameter (typeof (int), "a");
224                         var b = Expression.Parameter (typeof (int), "b");
225
226                         var l = Expression.Lambda<Func<int, int>> (
227                                 Expression.Call (
228                                         this.GetType ().GetMethod ("CallDelegate"),
229                                         Expression.Lambda<Func<int, int>> (
230                                                         Expression.Multiply (a, b), b)),
231                                 a).Compile ();
232
233                         Assert.AreEqual (84, l (2));
234                 }
235
236                 public static int CallFunc (Func<int, int> e, int i)
237                 {
238                         return e (i);
239                 }
240
241                 [Test]
242                 public void NestedParentParameterUse ()
243                 {
244                         var a = Expression.Parameter (typeof (int), null);
245                         var b = Expression.Parameter (typeof (int), null);
246                         var c = Expression.Parameter (typeof (int), null);
247                         var d = Expression.Parameter (typeof (int), null);
248
249                         var l = Expression.Lambda<Func<int, int>> (
250                                 Expression.Call (
251                                         this.GetType ().GetMethod ("CallFunc"),
252                                         Expression.Lambda<Func<int, int>> (
253                                                 Expression.Call (
254                                                         this.GetType ().GetMethod ("CallFunc"),
255                                                         Expression.Lambda<Func<int, int>> (
256                                                                 Expression.Call (
257                                                                         this.GetType ().GetMethod ("CallFunc"),
258                                                                         Expression.Lambda<Func<int, int>> (
259                                                                                 Expression.Add (c, d),
260                                                                                 d),
261                                                                         Expression.Add (b, c)),
262                                                                 c),
263                                                         Expression.Add (a, b)),
264                                                 b),
265                                         a),
266                                 a).Compile ();
267
268                         Assert.AreEqual (5, l (1));
269                 }
270 #if !NET_4_0 // dlr bug 5875
271                 [Test]
272                 public void LambdaReturningExpression ()
273                 {
274                         var l = Expression.Lambda<Func<Expression>> (Expression.Constant (42));
275                         Assert.AreEqual (ExpressionType.Quote, l.Body.NodeType);
276
277                         var quoter = l.Compile ();
278
279                         var q = quoter ();
280
281                         Assert.AreEqual (ExpressionType.Constant, q.NodeType);
282                 }
283 #endif
284         }
285 }