2 // ExpressionTest_Call.cs
5 // Federico Di Gregorio <fog@initd.org>
6 // Jb Evain (jbevain@novell.com)
8 // (C) 2008 Novell, Inc. (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Reflection;
32 using System.Collections.Generic;
34 using System.Linq.Expressions;
35 using NUnit.Framework;
37 namespace MonoTests.System.Linq.Expressions {
40 public class ExpressionTest_Call {
43 [ExpectedException (typeof (ArgumentNullException))]
44 public void Arg1Null ()
46 Expression.Call ((Type)null, "TestMethod", null, Expression.Constant (1));
50 [ExpectedException (typeof (ArgumentNullException))]
51 public void Arg2Null ()
53 Expression.Call (typeof (MemberClass), null, null, Expression.Constant (1));
57 [ExpectedException (typeof (InvalidOperationException))]
58 public void Arg4WrongType ()
60 Expression.Call (typeof (MemberClass), "StaticMethod", null, Expression.Constant (true));
64 [ExpectedException (typeof (InvalidOperationException))]
65 public void InstanceMethod ()
67 Expression.Call (typeof (MemberClass), "TestMethod", null, Expression.Constant (1));
71 public void StaticMethod ()
73 Expression.Call (typeof (MemberClass), "StaticMethod", null, Expression.Constant (1));
77 public void StaticGenericMethod ()
79 Expression.Call (typeof (MemberClass), "StaticGenericMethod", new [] { typeof (int) }, Expression.Constant (1));
83 [ExpectedException (typeof (ArgumentNullException))]
84 public void ArgMethodNull ()
86 Expression.Call (Expression.Constant (new object ()), null);
91 [ExpectedException (typeof (ArgumentException))]
93 [ExpectedException (typeof (ArgumentNullException))]
95 public void ArgInstanceNullForNonStaticMethod ()
97 Expression.Call (null, typeof (object).GetMethod ("ToString"));
101 [ExpectedException (typeof (ArgumentException))]
102 public void InstanceTypeDoesntMatchMethodDeclaringType ()
104 Expression.Call (Expression.Constant (1), typeof (string).GetMethod ("Intern"));
108 [ExpectedException (typeof (ArgumentException))]
109 public void MethodArgumentCountDoesnMatchParameterLength ()
111 Expression.Call (Expression.Constant (new object ()), typeof (object).GetMethod ("ToString"), Expression.Constant (new object ()));
115 public void Bar (string s)
121 [ExpectedException (typeof (ArgumentNullException))]
122 public void MethodHasNullArgument ()
124 Expression.Call (Expression.New (typeof (Foo)), typeof (Foo).GetMethod ("Bar"), null as Expression);
128 [ExpectedException (typeof (ArgumentException))]
129 public void MethodArgumentDoesntMatchParameterType ()
131 Expression.Call (Expression.New (typeof (Foo)), typeof (Foo).GetMethod ("Bar"), Expression.Constant (42));
135 public void CallToString ()
137 var call = Expression.Call (Expression.Constant (new object ()), typeof (object).GetMethod ("ToString"));
138 Assert.AreEqual ("value(System.Object).ToString()", call.ToString ());
142 public void CallStringIsNullOrEmpty ()
144 var call = Expression.Call (null, typeof (string).GetMethod ("IsNullOrEmpty"), Expression.Constant (""));
145 Assert.AreEqual ("IsNullOrEmpty(\"\")", call.ToString ());
149 [Category ("NotDotNet")] // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=339351
150 [ExpectedException (typeof (ArgumentException))]
151 public void CallStaticMethodWithInstanceArgument ()
154 Expression.Parameter (GetType (), "t"),
155 GetType ().GetMethod ("Identity"),
156 Expression.Constant (null));
159 public static object Identity (object o)
165 public void CompileSimpleStaticCall ()
167 var p = Expression.Parameter (typeof (object), "o");
168 var lambda = Expression.Lambda<Func<object, object>> (Expression.Call (GetType ().GetMethod ("Identity"), p), p);
170 var i = lambda.Compile ();
172 Assert.AreEqual (2, i (2));
173 Assert.AreEqual ("Foo", i ("Foo"));
177 public void CompileSimpleInstanceCall ()
179 var p = Expression.Parameter (typeof (string), "p");
180 var lambda = Expression.Lambda<Func<string, string>> (
182 p, typeof (string).GetMethod ("ToString", Type.EmptyTypes)),
185 var ts = lambda.Compile ();
187 Assert.AreEqual ("foo", ts ("foo"));
188 Assert.AreEqual ("bar", ts ("bar"));
192 [ExpectedException (typeof (InvalidOperationException))]
193 public void CheckTypeArgsIsNotUsedForParameterLookup ()
195 Expression.Call (GetType (), "EineMethod", new [] { typeof (string), typeof (int) }, "foo".ToConstant (), 2.ToConstant ());
198 public static void EineGenericMethod<X, Y> (string foo, int bar)
203 public void CheckTypeArgsIsUsedForGenericArguments ()
205 var m = Expression.Call (GetType (), "EineGenericMethod", new [] { typeof (string), typeof (int) }, "foo".ToConstant (), 2.ToConstant ());
206 Assert.IsNotNull (m.Method);
207 Assert.AreEqual ("Void EineGenericMethod[String,Int32](System.String, Int32)", m.Method.ToString ());
210 public struct EineStrukt {
214 public EineStrukt (string foo)
219 public string GimmeFoo ()
226 public void CallMethodOnStruct ()
228 var param = Expression.Parameter (typeof (EineStrukt), "s");
229 var foo = Expression.Lambda<Func<EineStrukt, string>> (
230 Expression.Call (param, typeof (EineStrukt).GetMethod ("GimmeFoo")), param).Compile ();
232 var s = new EineStrukt ("foo");
233 Assert.AreEqual ("foo", foo (s));
236 public static int OneStaticMethod ()
242 [Category ("NotDotNet")] // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=339351
243 [ExpectedException (typeof (ArgumentException))]
244 public void CallStaticMethodOnNonSenseInstanceExpression ()
247 Expression.Constant ("la la la"),
248 this.GetType ().GetMethod ("OneStaticMethod"));
251 public static int DoSomethingWith (ref int a)
256 public static string DoAnotherThing (ref int a, string s)
262 public void CallStaticMethodWithRefParameter ()
264 var p = Expression.Parameter (typeof (int), "i");
266 var c = Expression.Lambda<Func<int, int>> (
267 Expression.Call (GetType ().GetMethod ("DoSomethingWith"), p), p).Compile ();
269 Assert.AreEqual (42, c (38));
273 public void CallStaticMethodWithRefParameterAndOtherParameter ()
275 var i = Expression.Parameter (typeof (int), "i");
276 var s = Expression.Parameter (typeof (string), "s");
278 var lamda = Expression.Lambda<Func<int, string, string>> (
279 Expression.Call (GetType ().GetMethod ("DoAnotherThing"), i, s), i, s).Compile ();
281 Assert.AreEqual ("foo42", lamda (42, "foo"));
284 public static int Bang (Expression i)
286 return (int) (i as ConstantExpression).Value;
288 #if !NET_4_0 // dlr bug 5875
290 public void CallMethodWithExpressionParameter ()
292 var call = Expression.Call (GetType ().GetMethod ("Bang"), Expression.Constant (42));
293 Assert.AreEqual (ExpressionType.Quote, call.Arguments [0].NodeType);
295 var l = Expression.Lambda<Func<int>> (call).Compile ();
297 Assert.AreEqual (42, l ());
300 static bool fout_called = false;
302 public static int FooOut (out int x)
309 public void Connect282729 ()
311 // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=282729
313 var p = Expression.Parameter (typeof (int), "p");
314 var lambda = Expression.Lambda<Func<int, int>> (
316 GetType ().GetMethod ("FooOut"),
317 Expression.ArrayIndex(
318 Expression.NewArrayBounds (
324 Assert.AreEqual (0, lambda (0));
325 Assert.IsTrue (fout_called);
328 public static int FooOut2 (out int x)
335 [Category ("NotWorking")]
336 public void Connect290278 ()
338 // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=290278
340 var p = Expression.Parameter (typeof (int [,]), "p");
341 var lambda = Expression.Lambda<Func<int [,], int>> (
343 GetType ().GetMethod ("FooOut2"),
344 Expression.ArrayIndex (p, 0.ToConstant (), 0.ToConstant ())),
347 int [,] data = { { 1 } };
349 Assert.AreEqual (3, lambda (data));
350 Assert.AreEqual (2, data [0, 0]);
353 public static void FooRef (ref string s)
358 public void Connect297597 ()
360 // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=297597
362 var strings = new string [1];
364 var lambda = Expression.Lambda<Action> (
366 GetType ().GetMethod ("FooRef"),
367 Expression.ArrayIndex (
368 Expression.Constant (strings), 0.ToConstant ()))).Compile ();
374 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=319190
375 public void Connect319190 ()
377 var lambda = Expression.Lambda<Func<bool>> (
379 Expression.New (typeof (TypedReference)),
380 typeof (object))).Compile ();
382 Assert.IsTrue (lambda ());
385 public static int Truc ()
391 public void Connect282702 ()
393 var lambda = Expression.Lambda<Func<Func<int>>> (
396 typeof (Delegate).GetMethod ("CreateDelegate", new [] { typeof (Type), typeof (object), typeof (MethodInfo) }),
397 Expression.Constant (typeof (Func<int>), typeof (Type)),
398 Expression.Constant (null, typeof (object)),
399 Expression.Constant (GetType ().GetMethod ("Truc"))),
400 typeof (Func<int>))).Compile ();
402 Assert.AreEqual (42, lambda ().Invoke ());
406 public void CallQueryableWhere ()
408 var queryable = new [] { 1, 2, 3 }.AsQueryable ();
410 var parameter = Expression.Parameter (typeof (int), "i");
411 var lambda = Expression.Lambda<Func<int, bool>> (
412 Expression.LessThan (parameter, Expression.Constant (2)),
415 var selector = Expression.Quote (lambda);
417 var call = Expression.Call (
420 new [] { typeof (int) },
421 queryable.Expression,
424 Assert.IsNotNull (call);
425 Assert.IsNotNull (call.Method);
429 public void CallAsQueryable () // #537768
431 var constant = Expression.Constant (
433 typeof (IEnumerable<string>));
435 var call = Expression.Call (
438 new [] { typeof (string) },
441 Assert.IsNotNull (call);
442 Assert.AreEqual (1, call.Arguments.Count);
443 Assert.AreEqual (constant, call.Arguments [0]);
445 var method = call.Method;
447 Assert.AreEqual ("AsQueryable", method.Name);
448 Assert.IsTrue (method.IsGenericMethod);
449 Assert.AreEqual (typeof (string), method.GetGenericArguments () [0]);
454 public void CallQueryableSelect () // #536637
456 var parameter = Expression.Parameter (typeof (string), "s");
457 var string_length = Expression.Property (parameter, typeof (string).GetProperty ("Length"));
458 var lambda = Expression.Lambda (string_length, parameter);
460 var strings = new [] { "1", "22", "333" };
462 var call = Expression.Call (
465 new [] { typeof (string), typeof (int) },
466 Expression.Constant (strings.AsQueryable ()),
469 Assert.IsNotNull (call);
471 var method = call.Method;
473 Assert.AreEqual ("Select", method.Name);
474 Assert.IsTrue (method.IsGenericMethod);
475 Assert.AreEqual (typeof (string), method.GetGenericArguments () [0]);
476 Assert.AreEqual (typeof (int), method.GetGenericArguments () [1]);
480 public void CallNullableGetValueOrDefault () // #568989
482 var value = Expression.Parameter (typeof (int?), "value");
483 var default_parameter = Expression.Parameter (typeof (int), "default");
485 var getter = Expression.Lambda<Func<int?, int, int>> (
492 default_parameter).Compile ();
494 Assert.AreEqual (2, getter (null, 2));
495 Assert.AreEqual (4, getter (4, 2));