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);
50 static Type [] GetTestTypeArray (int length)
52 return Enumerable.Range (0, length - 1)
53 .Select (i => typeof (int))
58 [ExpectedException (typeof (ArgumentException))]
59 public void GetFuncTypeArgEmpty ()
61 Expression.GetFuncType (Type.EmptyTypes);
65 [ExpectedException (typeof (ArgumentException))]
66 public void GetFuncTypeArgTooBig ()
68 Expression.GetFuncType (GetTestTypeArray (64));
72 public void GetFuncTypeTest ()
74 var func = Expression.GetFuncType (new [] {typeof (int)});
75 Assert.AreEqual (typeof (Func<int>), func);
77 func = Expression.GetFuncType (new [] {typeof (int), typeof (int)});
78 Assert.AreEqual (typeof (Func<int, int>), func);
80 func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int)});
81 Assert.AreEqual (typeof (Func<int, int, int>), func);
83 func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int), typeof (int)});
84 Assert.AreEqual (typeof (Func<int, int, int, int>), func);
86 func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int), typeof (int), typeof (int)});
87 Assert.AreEqual (typeof (Func<int, int, int, int, int>), func);
91 [ExpectedException (typeof (ArgumentNullException))]
92 public void GetActionTypeArgNull ()
94 Expression.GetActionType (null);
98 [ExpectedException (typeof (ArgumentException))]
99 public void GetActionTypeArgTooBig ()
101 Expression.GetActionType (GetTestTypeArray (45));
105 public void GetActionTypeTest ()
107 var action = Expression.GetActionType (new Type [0]);
108 Assert.AreEqual (typeof (Action), action);
110 action = Expression.GetActionType (new [] {typeof (int)});
111 Assert.AreEqual (typeof (Action<int>), action);
113 action = Expression.GetActionType (new [] {typeof (int), typeof (int)});
114 Assert.AreEqual (typeof (Action<int, int>), action);
116 action = Expression.GetActionType (new [] {typeof (int), typeof (int), typeof (int)});
117 Assert.AreEqual (typeof (Action<int, int, int>), action);
119 action = Expression.GetActionType (new [] {typeof (int), typeof (int), typeof (int), typeof (int)});
120 Assert.AreEqual (typeof (Action<int, int, int, int>), action);
124 [ExpectedException (typeof (ArgumentNullException))]
125 public void ParameterNullType ()
127 Expression.Parameter (null, "foo");
131 public void ParameterNullName ()
133 var p = Expression.Parameter (typeof (string), null);
134 Assert.AreEqual (null, p.Name);
135 Assert.AreEqual (typeof (string), p.Type);
139 public void ParameterEmptyName ()
141 var p = Expression.Parameter (typeof (string), "");
142 Assert.AreEqual ("", p.Name);
143 Assert.AreEqual (typeof (string), p.Type);
147 public void Parameter ()
149 var p = Expression.Parameter (typeof (string), "foo");
150 Assert.AreEqual ("foo", p.Name);
151 Assert.AreEqual (typeof (string), p.Type);
152 Assert.AreEqual ("foo", p.ToString ());
156 [Category ("NotDotNet")]
157 [ExpectedException (typeof (ArgumentException))]
158 public void VoidParameter ()
160 Expression.Parameter (typeof (void), "hello");
165 public static int Identity (int i)
172 public void CompileActionDiscardingRetValue ()
174 var p = Expression.Parameter (typeof (int), "i");
175 var identity = GetType ().GetMethod ("Identity", BindingFlags.Static | BindingFlags.Public );
176 Assert.IsNotNull (identity);
178 var lambda = Expression.Lambda<Action<int>> (Expression.Call (identity, p), p);
180 var method = lambda.Compile ();
185 Assert.AreEqual (42, buffer);
189 public void ExpressionDelegateTarget ()
191 var p = Expression.Parameter (typeof (string), "str");
192 var identity = Expression.Lambda<Func<string, string>> (p, p).Compile ();
194 Assert.AreEqual (typeof (Func<string, string>), identity.GetType ());
195 Assert.IsNotNull (identity.Target);
199 public string gazonk;
205 public override string ToString ()
207 return baz.ToString ();
213 public void SimpleHoistedParameter ()
215 var p = Expression.Parameter (typeof (string), "s");
217 var f = Expression.Lambda<Func<string, Func<string>>> (
218 Expression.Lambda<Func<string>> (
220 new ParameterExpression [0]),
225 Assert.AreEqual ("x", f2 ());
229 public void TwoHoistingLevels ()
231 var p1 = Expression.Parameter (typeof (string), "x");
232 var p2 = Expression.Parameter (typeof (string), "y");
234 Expression<Func<string, Func<string, Func<string>>>> e =
235 Expression.Lambda<Func<string, Func<string, Func<string>>>> (
236 Expression.Lambda<Func<string, Func<string>>> (
237 Expression.Lambda<Func<string>> (
239 typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }),
241 new ParameterExpression [0]),
245 var f = e.Compile ();
246 var f2 = f ("Hello ");
247 var f3 = f2 ("World !");
249 Assert.AreEqual ("Hello World !", f3 ());
253 public void HoistedParameter ()
255 var i = Expression.Parameter (typeof (int), "i");
257 var l = Expression.Lambda<Func<int, string>> (
259 Expression.Lambda<Func<string>> (
260 Expression.Call (i, typeof (int).GetMethod ("ToString", Type.EmptyTypes)))), i).Compile ();
262 Assert.AreEqual ("42", l (42));