5 // Jb Evain (jbevain@novell.com)
7 // (C) 2008 Novell, Inc. (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections.Generic;
31 using System.Reflection;
32 using System.Runtime.CompilerServices;
34 using System.Linq.Expressions;
36 using NUnit.Framework;
38 namespace MonoTests.System.Linq.Expressions {
41 public class ExpressionTest {
44 [ExpectedException (typeof (ArgumentNullException))]
45 public void GetFuncTypeArgNull ()
47 Expression.GetFuncType (null);
51 [ExpectedException (typeof (ArgumentException))]
52 public void GetFuncTypeArgEmpty ()
54 Expression.GetFuncType (new Type [0]);
58 [ExpectedException (typeof (ArgumentException))]
59 public void GetFuncTypeArgTooBig ()
61 Expression.GetFuncType (new Type [6]);
65 public void GetFuncTypeTest ()
67 var func = Expression.GetFuncType (new [] {typeof (int)});
68 Assert.AreEqual (typeof (Func<int>), func);
70 func = Expression.GetFuncType (new [] {typeof (int), typeof (int)});
71 Assert.AreEqual (typeof (Func<int, int>), func);
73 func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int)});
74 Assert.AreEqual (typeof (Func<int, int, int>), func);
76 func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int), typeof (int)});
77 Assert.AreEqual (typeof (Func<int, int, int, int>), func);
79 func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int), typeof (int), typeof (int)});
80 Assert.AreEqual (typeof (Func<int, int, int, int, int>), func);
84 [ExpectedException (typeof (ArgumentNullException))]
85 public void GetActionTypeArgNull ()
87 Expression.GetActionType (null);
91 [ExpectedException (typeof (ArgumentException))]
92 public void GetActionTypeArgTooBig ()
94 Expression.GetActionType (new Type [5]);
98 public void GetActionTypeTest ()
100 var action = Expression.GetActionType (new Type [0]);
101 Assert.AreEqual (typeof (Action), action);
103 action = Expression.GetActionType (new [] {typeof (int)});
104 Assert.AreEqual (typeof (Action<int>), action);
106 action = Expression.GetActionType (new [] {typeof (int), typeof (int)});
107 Assert.AreEqual (typeof (Action<int, int>), action);
109 action = Expression.GetActionType (new [] {typeof (int), typeof (int), typeof (int)});
110 Assert.AreEqual (typeof (Action<int, int, int>), action);
112 action = Expression.GetActionType (new [] {typeof (int), typeof (int), typeof (int), typeof (int)});
113 Assert.AreEqual (typeof (Action<int, int, int, int>), action);
117 [ExpectedException (typeof (ArgumentNullException))]
118 public void ParameterNullType ()
120 Expression.Parameter (null, "foo");
124 public void ParameterNullName ()
126 var p = Expression.Parameter (typeof (string), null);
127 Assert.AreEqual (null, p.Name);
128 Assert.AreEqual (typeof (string), p.Type);
129 Assert.AreEqual ("<param>", p.ToString ());
133 public void ParameterEmptyName ()
135 var p = Expression.Parameter (typeof (string), "");
136 Assert.AreEqual ("", p.Name);
137 Assert.AreEqual (typeof (string), p.Type);
138 Assert.AreEqual ("", p.ToString ());
142 public void Parameter ()
144 var p = Expression.Parameter (typeof (string), "foo");
145 Assert.AreEqual ("foo", p.Name);
146 Assert.AreEqual (typeof (string), p.Type);
147 Assert.AreEqual ("foo", p.ToString ());
151 [Category ("NotDotNet")]
152 [ExpectedException (typeof (ArgumentException))]
153 public void VoidParameter ()
155 Expression.Parameter (typeof (void), "hello");
160 public static int Identity (int i)
167 public void CompileActionDiscardingRetValue ()
169 var p = Expression.Parameter (typeof (int), "i");
170 var identity = GetType ().GetMethod ("Identity", BindingFlags.Static | BindingFlags.Public );
171 Assert.IsNotNull (identity);
173 var lambda = Expression.Lambda<Action<int>> (Expression.Call (identity, p), p);
175 var method = lambda.Compile ();
180 Assert.AreEqual (42, buffer);
184 [Category("TargetJvmNotSupported")]
185 public void ExpressionDelegateTarget ()
187 var p = Expression.Parameter (typeof (string), "str");
188 var identity = Expression.Lambda<Func<string, string>> (p, p).Compile ();
190 Assert.AreEqual (typeof (Func<string, string>), identity.GetType ());
191 Assert.IsNotNull (identity.Target);
193 Assert.AreEqual (typeof (ExecutionScope), identity.Target.GetType ());
198 public string gazonk;
204 public override string ToString ()
206 return baz.ToString ();
212 [Category ("TargetJvmNotSupported")]
213 public void GlobalsInScope ()
215 var foo = new Foo { gazonk = "gazonk" };
216 var bar = new Bar { baz = 42 };
218 var l = Expression.Lambda<Func<string>> (
220 typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }),
221 Expression.Field (Expression.Constant (foo), typeof (Foo).GetField ("gazonk")),
222 Expression.Call (Expression.Constant (bar), typeof (Bar).GetMethod ("ToString"))));
224 var del = l.Compile ();
226 var scope = del.Target as ExecutionScope;
228 Assert.IsNotNull (scope);
230 var globals = scope.Globals;
232 Assert.IsNotNull (globals);
234 Assert.AreEqual (2, globals.Length);
235 Assert.AreEqual (typeof (StrongBox<Foo>), globals [0].GetType ());
236 Assert.AreEqual (typeof (StrongBox<Bar>), globals [1].GetType ());
238 Assert.AreEqual (foo, ((StrongBox<Foo>) globals [0]).Value);
239 Assert.AreEqual (bar, ((StrongBox<Bar>) globals [1]).Value);
241 Assert.AreEqual ("gazonk42", del ());
246 public void SimpleHoistedParameter ()
248 var p = Expression.Parameter (typeof (string), "s");
250 var f = Expression.Lambda<Func<string, Func<string>>> (
251 Expression.Lambda<Func<string>> (
253 new ParameterExpression [0]),
258 Assert.AreEqual ("x", f2 ());
262 public void TwoHoistingLevels ()
264 var p1 = Expression.Parameter (typeof (string), "x");
265 var p2 = Expression.Parameter (typeof (string), "y");
267 Expression<Func<string, Func<string, Func<string>>>> e =
268 Expression.Lambda<Func<string, Func<string, Func<string>>>> (
269 Expression.Lambda<Func<string, Func<string>>> (
270 Expression.Lambda<Func<string>> (
272 typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }),
274 new ParameterExpression [0]),
278 var f = e.Compile ();
279 var f2 = f ("Hello ");
280 var f3 = f2 ("World !");
282 Assert.AreEqual ("Hello World !", f3 ());
286 public void HoistedParameter ()
288 var i = Expression.Parameter (typeof (int), "i");
290 var l = Expression.Lambda<Func<int, string>> (
292 Expression.Lambda<Func<string>> (
293 Expression.Call (i, typeof (int).GetMethod ("ToString", Type.EmptyTypes)))), i).Compile ();
295 Assert.AreEqual ("42", l (42));