new tests
[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
22 using System;
23 using System.Reflection;
24 using System.Linq;
25 using System.Linq.Expressions;
26 using NUnit.Framework;
27
28 namespace MonoTests.System.Linq.Expressions
29 {
30         [TestFixture]
31         public class ExpressionTest_Lambda
32         {
33                 [Test]
34                 [ExpectedException (typeof (ArgumentException))]
35                 public void NonDelegateTypeInCtor ()
36                 {
37                         // The first parameter must be a delegate type
38                         Expression.Lambda (typeof(string), Expression.Constant (1), new ParameterExpression [0]);
39                 }
40
41                 delegate object delegate_object_emtpy ();
42                 delegate object delegate_object_int (int a);
43                 delegate object delegate_object_string (string s);
44                 delegate object delegate_object_object (object s);
45
46                 [Test]
47                 [ExpectedException (typeof (ArgumentException))]
48                 public void InvalidConversion ()
49                 {
50                         // float to object, invalid
51                         Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant (1.0), new ParameterExpression [0]);
52                 }
53
54                 [Test]
55                 [ExpectedException (typeof (ArgumentException))]
56                 public void InvalidConversion2 ()
57                 {
58                         // float to object, invalid
59                         Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant (1), new ParameterExpression [0]);
60                 }
61
62                 [Test]
63                 [ExpectedException (typeof (ArgumentException))]
64                 public void InvalidArgCount ()
65                 {
66                         // missing a parameter
67                         Expression.Lambda (typeof (delegate_object_int), Expression.Constant ("foo"), new ParameterExpression [0]);
68                 }
69
70                 [Test]
71                 [ExpectedException (typeof (ArgumentException))]
72                 public void InvalidArgCount2 ()
73                 {
74                         // extra parameter
75                         ParameterExpression p = Expression.Parameter (typeof (int), "AAA");
76                         Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant ("foo"), new ParameterExpression [1] {p});
77                 }
78
79                 [Test]
80                 [ExpectedException (typeof (ArgumentException))]
81                 public void InvalidArgType ()
82                 {
83                         // invalid argument type
84                         ParameterExpression p = Expression.Parameter (typeof (string), "AAA");
85                         Expression.Lambda (typeof (delegate_object_int), Expression.Constant ("foo"), new ParameterExpression [1] {p});
86                 }
87
88                 [Test]
89                 [ExpectedException (typeof (ArgumentException))]
90                 public void InvalidArgType2 ()
91                 {
92                         // invalid argument type
93
94                         ParameterExpression p = Expression.Parameter (typeof (string), "AAA");
95                         Expression.Lambda (typeof (delegate_object_object), Expression.Constant ("foo"), new ParameterExpression [1] {p});
96                 }
97
98                 [Test]
99                 public void Assignability ()
100                 {
101                         // allowed: string to object
102                         Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant ("string"), new ParameterExpression [0]);
103
104                         // allowed delegate has string, delegate has base class (object)
105                         ParameterExpression p = Expression.Parameter (typeof (object), "ParObject");
106                         var l = Expression.Lambda (typeof (delegate_object_string), Expression.Constant (""), new ParameterExpression [1] {p});
107
108                         Assert.AreEqual ("ParObject => \"\"", l.ToString ());
109                 }
110
111                 [Test]
112                 [ExpectedException(typeof(InvalidOperationException))]
113                 public void ParameterOutOfScope ()
114                 {
115                         ParameterExpression a = Expression.Parameter(typeof (int), "a");
116                         ParameterExpression second_a = Expression.Parameter(typeof(int), "a");
117
118                         // Here we have the same name for the parameter expression, but
119                         // we pass a different object to the Lambda expression, so they are
120                         // different, this should throw
121                         Expression<Func<int,int>> l = Expression.Lambda<Func<int,int>>(a, new ParameterExpression [] { second_a });
122                         l.Compile ();
123                 }
124
125                 [Test]
126                 public void ParameterRefTest ()
127                 {
128                         ParameterExpression a = Expression.Parameter(typeof(int), "a");
129                         ParameterExpression b = Expression.Parameter(typeof(int), "b");
130
131                         Expression<Func<int,int,int>> l = Expression.Lambda<Func<int,int,int>>(
132                                 Expression.Add (a, b), new ParameterExpression [] { a, b });
133
134                         Assert.AreEqual (typeof (Func<int, int, int>), l.Type);
135                         Assert.AreEqual ("(a, b) => (a + b)", l.ToString ());
136
137                         Func<int,int,int> xx = l.Compile ();
138                         int res = xx (10, 20);
139                         Assert.AreEqual (res, 30);
140                 }
141
142                 [Test]
143                 public void Compile ()
144                 {
145                         Expression<Func<int>> l = Expression.Lambda<Func<int>> (Expression.Constant (1), new ParameterExpression [0]);
146                         Assert.AreEqual (typeof (Func<int>), l.Type);
147                         Assert.AreEqual ("() => 1", l.ToString ());
148
149                         Func<int> fi = l.Compile ();
150                         fi ();
151                 }
152
153                 [Test]
154                 [ExpectedException(typeof(ArgumentException))]
155                 public void ReturnValueCheck ()
156                 {
157                         ParameterExpression p1 = Expression.Parameter(typeof(int?), "va");
158                         ParameterExpression p2 = Expression.Parameter(typeof(int?), "vb");
159                         Expression add = Expression.Add(p1, p2);
160
161
162                         // This should throw, since the add.Type is "int?" and the return
163                         // type we have here is int.
164                         Expression.Lambda<Func<int?,int?,int>> (add, p1, p2);
165                 }
166
167                 public static void Foo ()
168                 {
169                 }
170
171                 [Test]
172                 public void LambdaType ()
173                 {
174                         var l = Expression.Lambda (Expression.Constant (1), new [] { Expression.Parameter (typeof (int), "foo") });
175
176                         Assert.AreEqual (typeof (Func<int, int>), l.Type);
177
178                         l = Expression.Lambda (Expression.Call (null, GetType ().GetMethod ("Foo")), new [] { Expression.Parameter (typeof (string), "foofoo") });
179
180                         Assert.AreEqual (typeof (Action<string>), l.Type);
181                 }
182
183                 [Test]
184                 public void UnTypedLambdaReturnsExpressionOfDelegateType ()
185                 {
186                         var l = Expression.Lambda ("foo".ToConstant ());
187
188                         Assert.AreEqual (typeof (Expression<Func<string>>), l.GetType ());
189                 }
190
191                 public static int CallDelegate (Func<int, int> e)
192                 {
193                         return e (42);
194                 }
195
196                 [Test]
197                 public void LambdaPassedAsDelegate ()
198                 {
199                         var pi = Expression.Parameter (typeof (int), "i");
200                         var identity = Expression.Lambda<Func<int, int>> (pi, pi);
201
202                         var l = Expression.Lambda<Func<int>> (
203                                 Expression.Call (
204                                         GetType ().GetMethod ("CallDelegate"),
205                                         identity)).Compile ();
206
207                         Assert.AreEqual (42, l ());
208                 }
209
210                 [Test]
211                 [Category ("NotWorking")]
212                 public void LambdaReturningExpression ()
213                 {
214                         var l = Expression.Lambda<Func<Expression>> (Expression.Constant (42));
215                         Assert.AreEqual (ExpressionType.Quote, l.Body.NodeType);
216
217                         var quoter = l.Compile ();
218
219                         var q = quoter ();
220
221                         Assert.AreEqual (ExpressionType.Constant, q.NodeType);
222                 }
223         }
224 }