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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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
20 // Miguel de Icaza (miguel@novell.com)
23 using System.Reflection;
25 using System.Linq.Expressions;
26 using NUnit.Framework;
28 namespace MonoTests.System.Linq.Expressions
31 public class ExpressionTest_Lambda
34 [ExpectedException (typeof (ArgumentException))]
35 public void NonDelegateTypeInCtor ()
37 // The first parameter must be a delegate type
38 Expression.Lambda (typeof(string), Expression.Constant (1), new ParameterExpression [0]);
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);
47 [ExpectedException (typeof (ArgumentException))]
48 public void InvalidConversion ()
50 // float to object, invalid
51 Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant (1.0), new ParameterExpression [0]);
55 [ExpectedException (typeof (ArgumentException))]
56 public void InvalidConversion2 ()
58 // float to object, invalid
59 Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant (1), new ParameterExpression [0]);
63 [ExpectedException (typeof (ArgumentException))]
64 public void InvalidArgCount ()
66 // missing a parameter
67 Expression.Lambda (typeof (delegate_object_int), Expression.Constant ("foo"), new ParameterExpression [0]);
71 [ExpectedException (typeof (ArgumentException))]
72 public void InvalidArgCount2 ()
75 ParameterExpression p = Expression.Parameter (typeof (int), "AAA");
76 Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant ("foo"), new ParameterExpression [1] {p});
80 [ExpectedException (typeof (ArgumentException))]
81 public void InvalidArgType ()
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});
89 [ExpectedException (typeof (ArgumentException))]
90 public void InvalidArgType2 ()
92 // invalid argument type
94 ParameterExpression p = Expression.Parameter (typeof (string), "AAA");
95 Expression.Lambda (typeof (delegate_object_object), Expression.Constant ("foo"), new ParameterExpression [1] {p});
99 public void Assignability ()
101 // allowed: string to object
102 Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant ("string"), new ParameterExpression [0]);
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});
108 Assert.AreEqual ("ParObject => \"\"", l.ToString ());
112 [ExpectedException(typeof(InvalidOperationException))]
113 public void ParameterOutOfScope ()
115 ParameterExpression a = Expression.Parameter(typeof (int), "a");
116 ParameterExpression second_a = Expression.Parameter(typeof(int), "a");
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 });
126 public void ParameterRefTest ()
128 ParameterExpression a = Expression.Parameter(typeof(int), "a");
129 ParameterExpression b = Expression.Parameter(typeof(int), "b");
131 Expression<Func<int,int,int>> l = Expression.Lambda<Func<int,int,int>>(
132 Expression.Add (a, b), new ParameterExpression [] { a, b });
134 Assert.AreEqual (typeof (Func<int, int, int>), l.Type);
135 Assert.AreEqual ("(a, b) => (a + b)", l.ToString ());
137 Func<int,int,int> xx = l.Compile ();
138 int res = xx (10, 20);
139 Assert.AreEqual (res, 30);
143 public void Compile ()
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 ());
149 Func<int> fi = l.Compile ();
154 [ExpectedException(typeof(ArgumentException))]
155 public void ReturnValueCheck ()
157 ParameterExpression p1 = Expression.Parameter(typeof(int?), "va");
158 ParameterExpression p2 = Expression.Parameter(typeof(int?), "vb");
159 Expression add = Expression.Add(p1, p2);
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);
167 public static void Foo ()
172 public void LambdaType ()
174 var l = Expression.Lambda (Expression.Constant (1), new [] { Expression.Parameter (typeof (int), "foo") });
176 Assert.AreEqual (typeof (Func<int, int>), l.Type);
178 l = Expression.Lambda (Expression.Call (null, GetType ().GetMethod ("Foo")), new [] { Expression.Parameter (typeof (string), "foofoo") });
180 Assert.AreEqual (typeof (Action<string>), l.Type);
184 public void UnTypedLambdaReturnsExpressionOfDelegateType ()
186 var l = Expression.Lambda ("foo".ToConstant ());
188 Assert.AreEqual (typeof (Expression<Func<string>>), l.GetType ());
191 public static int CallDelegate (Func<int, int> e)
197 public void LambdaPassedAsDelegate ()
199 var pi = Expression.Parameter (typeof (int), "i");
200 var identity = Expression.Lambda<Func<int, int>> (pi, pi);
202 var l = Expression.Lambda<Func<int>> (
204 GetType ().GetMethod ("CallDelegate"),
205 identity)).Compile ();
207 Assert.AreEqual (42, l ());
211 [Category ("NotWorking")]
212 public void LambdaReturningExpression ()
214 var l = Expression.Lambda<Func<Expression>> (Expression.Constant (42));
215 Assert.AreEqual (ExpressionType.Quote, l.Body.NodeType);
217 var quoter = l.Compile ();
221 Assert.AreEqual (ExpressionType.Constant, q.NodeType);