Bug 15572. Lookup KnownTypeCollection element types in MSSimpleNamespace
[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 #if MONOTOUCH
122                 [Category ("NotWorking")]
123 #endif
124                 [ExpectedException(typeof(InvalidOperationException))]
125                 public void ParameterOutOfScope ()
126                 {
127                         ParameterExpression a = Expression.Parameter(typeof (int), "a");
128                         ParameterExpression second_a = Expression.Parameter(typeof(int), "a");
129
130                         // Here we have the same name for the parameter expression, but
131                         // we pass a different object to the Lambda expression, so they are
132                         // different, this should throw
133                         Expression<Func<int,int>> l = Expression.Lambda<Func<int,int>>(a, new ParameterExpression [] { second_a });
134                         l.Compile ();
135                 }
136
137                 [Test]
138                 public void ParameterRefTest ()
139                 {
140                         ParameterExpression a = Expression.Parameter(typeof(int), "a");
141                         ParameterExpression b = Expression.Parameter(typeof(int), "b");
142
143                         Expression<Func<int,int,int>> l = Expression.Lambda<Func<int,int,int>>(
144                                 Expression.Add (a, b), new ParameterExpression [] { a, b });
145
146                         Assert.AreEqual (typeof (Func<int, int, int>), l.Type);
147                         Assert.AreEqual ("(a, b) => (a + b)", l.ToString ());
148
149                         Func<int,int,int> xx = l.Compile ();
150                         int res = xx (10, 20);
151                         Assert.AreEqual (res, 30);
152                 }
153
154                 [Test]
155                 public void Compile ()
156                 {
157                         Expression<Func<int>> l = Expression.Lambda<Func<int>> (Expression.Constant (1), new ParameterExpression [0]);
158                         Assert.AreEqual (typeof (Func<int>), l.Type);
159                         Assert.AreEqual ("() => 1", l.ToString ());
160
161                         Func<int> fi = l.Compile ();
162                         fi ();
163                 }
164
165                 [Test]
166                 [ExpectedException(typeof(ArgumentException))]
167                 public void ReturnValueCheck ()
168                 {
169                         ParameterExpression p1 = Expression.Parameter(typeof(int?), "va");
170                         ParameterExpression p2 = Expression.Parameter(typeof(int?), "vb");
171                         Expression add = Expression.Add(p1, p2);
172
173
174                         // This should throw, since the add.Type is "int?" and the return
175                         // type we have here is int.
176                         Expression.Lambda<Func<int?,int?,int>> (add, p1, p2);
177                 }
178
179                 public static void Foo ()
180                 {
181                 }
182
183                 [Test]
184                 public void LambdaType ()
185                 {
186                         var l = Expression.Lambda (Expression.Constant (1), new [] { Expression.Parameter (typeof (int), "foo") });
187
188                         Assert.AreEqual (typeof (Func<int, int>), l.Type);
189
190                         l = Expression.Lambda (Expression.Call (null, GetType ().GetMethod ("Foo")), new [] { Expression.Parameter (typeof (string), "foofoo") });
191
192                         Assert.AreEqual (typeof (Action<string>), l.Type);
193                 }
194
195                 [Test]
196                 public void UnTypedLambdaReturnsExpressionOfDelegateType ()
197                 {
198                         var l = Expression.Lambda ("foo".ToConstant ());
199
200                         Assert.AreEqual (typeof (Expression<Func<string>>), l.GetType ());
201                 }
202
203                 public static int CallDelegate (Func<int, int> e)
204                 {
205                         return e (42);
206                 }
207
208                 [Test]
209                 public void LambdaPassedAsDelegate ()
210                 {
211                         var pi = Expression.Parameter (typeof (int), "i");
212                         var identity = Expression.Lambda<Func<int, int>> (pi, pi);
213
214                         var l = Expression.Lambda<Func<int>> (
215                                 Expression.Call (
216                                         GetType ().GetMethod ("CallDelegate"),
217                                         identity)).Compile ();
218
219                         Assert.AreEqual (42, l ());
220                 }
221
222                 [Test]
223                 [Category ("NotWorking")]
224                 public void LambdaPassedAsDelegateUsingParentParameter ()
225                 {
226                         var a = Expression.Parameter (typeof (int), "a");
227                         var b = Expression.Parameter (typeof (int), "b");
228
229                         var l = Expression.Lambda<Func<int, int>> (
230                                 Expression.Call (
231                                         this.GetType ().GetMethod ("CallDelegate"),
232                                         Expression.Lambda<Func<int, int>> (
233                                                         Expression.Multiply (a, b), b)),
234                                 a).Compile ();
235
236                         Assert.AreEqual (84, l (2));
237                 }
238
239                 public static int CallFunc (Func<int, int> e, int i)
240                 {
241                         return e (i);
242                 }
243
244                 [Test]
245                 public void NestedParentParameterUse ()
246                 {
247                         var a = Expression.Parameter (typeof (int), null);
248                         var b = Expression.Parameter (typeof (int), null);
249                         var c = Expression.Parameter (typeof (int), null);
250                         var d = Expression.Parameter (typeof (int), null);
251
252                         var l = 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.Call (
260                                                                         this.GetType ().GetMethod ("CallFunc"),
261                                                                         Expression.Lambda<Func<int, int>> (
262                                                                                 Expression.Add (c, d),
263                                                                                 d),
264                                                                         Expression.Add (b, c)),
265                                                                 c),
266                                                         Expression.Add (a, b)),
267                                                 b),
268                                         a),
269                                 a).Compile ();
270
271                         Assert.AreEqual (5, l (1));
272                 }
273 #if !NET_4_0 // dlr bug 5875
274                 [Test]
275                 public void LambdaReturningExpression ()
276                 {
277                         var l = Expression.Lambda<Func<Expression>> (Expression.Constant (42));
278                         Assert.AreEqual (ExpressionType.Quote, l.Body.NodeType);
279
280                         var quoter = l.Compile ();
281
282                         var q = quoter ();
283
284                         Assert.AreEqual (ExpressionType.Constant, q.NodeType);
285                 }
286 #endif
287         }
288 }