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);
90 #if NET_4_0 && !MONOTOUCH
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 ()
105 // ensure that String.Intern won't be removed by the linker
106 string s = String.Intern (String.Empty);
108 Expression.Call (Expression.Constant (1), typeof (string).GetMethod ("Intern"));
112 [ExpectedException (typeof (ArgumentException))]
113 public void MethodArgumentCountDoesnMatchParameterLength ()
115 Expression.Call (Expression.Constant (new object ()), typeof (object).GetMethod ("ToString"), Expression.Constant (new object ()));
119 public void Bar (string s)
125 [ExpectedException (typeof (ArgumentNullException))]
126 public void MethodHasNullArgument ()
128 Expression.Call (Expression.New (typeof (Foo)), typeof (Foo).GetMethod ("Bar"), null as Expression);
132 [ExpectedException (typeof (ArgumentException))]
133 public void MethodArgumentDoesntMatchParameterType ()
135 Expression.Call (Expression.New (typeof (Foo)), typeof (Foo).GetMethod ("Bar"), Expression.Constant (42));
139 public void CallToString ()
141 var call = Expression.Call (Expression.Constant (new object ()), typeof (object).GetMethod ("ToString"));
142 Assert.AreEqual ("value(System.Object).ToString()", call.ToString ());
146 public void CallStringIsNullOrEmpty ()
148 var call = Expression.Call (null, typeof (string).GetMethod ("IsNullOrEmpty"), Expression.Constant (""));
149 Assert.AreEqual ("IsNullOrEmpty(\"\")", call.ToString ());
153 [Category ("NotDotNet")] // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=339351
154 [ExpectedException (typeof (ArgumentException))]
155 public void CallStaticMethodWithInstanceArgument ()
158 Expression.Parameter (GetType (), "t"),
159 GetType ().GetMethod ("Identity"),
160 Expression.Constant (null));
163 public static object Identity (object o)
169 public void CompileSimpleStaticCall ()
171 var p = Expression.Parameter (typeof (object), "o");
172 var lambda = Expression.Lambda<Func<object, object>> (Expression.Call (GetType ().GetMethod ("Identity"), p), p);
174 var i = lambda.Compile ();
176 Assert.AreEqual (2, i (2));
177 Assert.AreEqual ("Foo", i ("Foo"));
181 public void CompileSimpleInstanceCall ()
183 var p = Expression.Parameter (typeof (string), "p");
184 var lambda = Expression.Lambda<Func<string, string>> (
186 p, typeof (string).GetMethod ("ToString", Type.EmptyTypes)),
189 var ts = lambda.Compile ();
191 Assert.AreEqual ("foo", ts ("foo"));
192 Assert.AreEqual ("bar", ts ("bar"));
196 [ExpectedException (typeof (InvalidOperationException))]
197 public void CheckTypeArgsIsNotUsedForParameterLookup ()
199 Expression.Call (GetType (), "EineMethod", new [] { typeof (string), typeof (int) }, "foo".ToConstant (), 2.ToConstant ());
202 public static void EineGenericMethod<X, Y> (string foo, int bar)
207 public void CheckTypeArgsIsUsedForGenericArguments ()
209 var m = Expression.Call (GetType (), "EineGenericMethod", new [] { typeof (string), typeof (int) }, "foo".ToConstant (), 2.ToConstant ());
210 Assert.IsNotNull (m.Method);
211 Assert.AreEqual ("Void EineGenericMethod[String,Int32](System.String, Int32)", m.Method.ToString ());
214 public struct EineStrukt {
218 public EineStrukt (string foo)
223 public string GimmeFoo ()
230 public void CallMethodOnStruct ()
232 var param = Expression.Parameter (typeof (EineStrukt), "s");
233 var foo = Expression.Lambda<Func<EineStrukt, string>> (
234 Expression.Call (param, typeof (EineStrukt).GetMethod ("GimmeFoo")), param).Compile ();
236 var s = new EineStrukt ("foo");
237 Assert.AreEqual ("foo", foo (s));
240 public static int OneStaticMethod ()
246 [Category ("NotDotNet")] // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=339351
247 [ExpectedException (typeof (ArgumentException))]
248 public void CallStaticMethodOnNonSenseInstanceExpression ()
251 Expression.Constant ("la la la"),
252 this.GetType ().GetMethod ("OneStaticMethod"));
255 public static int DoSomethingWith (ref int a)
260 public static string DoAnotherThing (ref int a, string s)
266 public void CallStaticMethodWithRefParameter ()
268 var p = Expression.Parameter (typeof (int), "i");
270 var c = Expression.Lambda<Func<int, int>> (
271 Expression.Call (GetType ().GetMethod ("DoSomethingWith"), p), p).Compile ();
273 Assert.AreEqual (42, c (38));
277 public void CallStaticMethodWithRefParameterAndOtherParameter ()
279 var i = Expression.Parameter (typeof (int), "i");
280 var s = Expression.Parameter (typeof (string), "s");
282 var lamda = Expression.Lambda<Func<int, string, string>> (
283 Expression.Call (GetType ().GetMethod ("DoAnotherThing"), i, s), i, s).Compile ();
285 Assert.AreEqual ("foo42", lamda (42, "foo"));
288 public static int Bang (Expression i)
290 return (int) (i as ConstantExpression).Value;
292 #if !NET_4_0 // dlr bug 5875
294 public void CallMethodWithExpressionParameter ()
296 var call = Expression.Call (GetType ().GetMethod ("Bang"), Expression.Constant (42));
297 Assert.AreEqual (ExpressionType.Quote, call.Arguments [0].NodeType);
299 var l = Expression.Lambda<Func<int>> (call).Compile ();
301 Assert.AreEqual (42, l ());
304 static bool fout_called = false;
306 public static int FooOut (out int x)
313 public void Connect282729 ()
315 // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=282729
317 var p = Expression.Parameter (typeof (int), "p");
318 var lambda = Expression.Lambda<Func<int, int>> (
320 GetType ().GetMethod ("FooOut"),
321 Expression.ArrayIndex(
322 Expression.NewArrayBounds (
328 Assert.AreEqual (0, lambda (0));
329 Assert.IsTrue (fout_called);
332 public static int FooOut2 (out int x)
339 [Category ("NotWorking")]
340 public void Connect290278 ()
342 // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=290278
344 var p = Expression.Parameter (typeof (int [,]), "p");
345 var lambda = Expression.Lambda<Func<int [,], int>> (
347 GetType ().GetMethod ("FooOut2"),
348 Expression.ArrayIndex (p, 0.ToConstant (), 0.ToConstant ())),
351 int [,] data = { { 1 } };
353 Assert.AreEqual (3, lambda (data));
354 Assert.AreEqual (2, data [0, 0]);
357 public static void FooRef (ref string s)
362 public void Connect297597 ()
364 // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=297597
366 var strings = new string [1];
368 var lambda = Expression.Lambda<Action> (
370 GetType ().GetMethod ("FooRef"),
371 Expression.ArrayIndex (
372 Expression.Constant (strings), 0.ToConstant ()))).Compile ();
379 [Category ("NotWorking")]
381 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=319190
382 public void Connect319190 ()
384 var lambda = Expression.Lambda<Func<bool>> (
386 Expression.New (typeof (TypedReference)),
387 typeof (object))).Compile ();
389 Assert.IsTrue (lambda ());
392 public static int Truc ()
398 public void Connect282702 ()
400 var lambda = Expression.Lambda<Func<Func<int>>> (
403 typeof (Delegate).GetMethod ("CreateDelegate", new [] { typeof (Type), typeof (object), typeof (MethodInfo) }),
404 Expression.Constant (typeof (Func<int>), typeof (Type)),
405 Expression.Constant (null, typeof (object)),
406 Expression.Constant (GetType ().GetMethod ("Truc"))),
407 typeof (Func<int>))).Compile ();
409 Assert.AreEqual (42, lambda ().Invoke ());
413 public void CallQueryableWhere ()
415 var queryable = new [] { 1, 2, 3 }.AsQueryable ();
417 var parameter = Expression.Parameter (typeof (int), "i");
418 var lambda = Expression.Lambda<Func<int, bool>> (
419 Expression.LessThan (parameter, Expression.Constant (2)),
422 var selector = Expression.Quote (lambda);
424 var call = Expression.Call (
427 new [] { typeof (int) },
428 queryable.Expression,
431 Assert.IsNotNull (call);
432 Assert.IsNotNull (call.Method);
436 public void CallAsQueryable () // #537768
438 var constant = Expression.Constant (
440 typeof (IEnumerable<string>));
442 var call = Expression.Call (
445 new [] { typeof (string) },
448 Assert.IsNotNull (call);
449 Assert.AreEqual (1, call.Arguments.Count);
450 Assert.AreEqual (constant, call.Arguments [0]);
452 var method = call.Method;
454 Assert.AreEqual ("AsQueryable", method.Name);
455 Assert.IsTrue (method.IsGenericMethod);
456 Assert.AreEqual (typeof (string), method.GetGenericArguments () [0]);
461 public void CallQueryableSelect () // #536637
463 var parameter = Expression.Parameter (typeof (string), "s");
464 var string_length = Expression.Property (parameter, typeof (string).GetProperty ("Length"));
465 var lambda = Expression.Lambda (string_length, parameter);
467 var strings = new [] { "1", "22", "333" };
469 var call = Expression.Call (
472 new [] { typeof (string), typeof (int) },
473 Expression.Constant (strings.AsQueryable ()),
476 Assert.IsNotNull (call);
478 var method = call.Method;
480 Assert.AreEqual ("Select", method.Name);
481 Assert.IsTrue (method.IsGenericMethod);
482 Assert.AreEqual (typeof (string), method.GetGenericArguments () [0]);
483 Assert.AreEqual (typeof (int), method.GetGenericArguments () [1]);
488 [Category ("NotWorking")]
490 public void CallNullableGetValueOrDefault () // #568989
492 var value = Expression.Parameter (typeof (int?), "value");
493 var default_parameter = Expression.Parameter (typeof (int), "default");
495 var getter = Expression.Lambda<Func<int?, int, int>> (
502 default_parameter).Compile ();
504 Assert.AreEqual (2, getter (null, 2));
505 Assert.AreEqual (4, getter (4, 2));
509 public void CallToStringOnEnum () // #625367
511 var lambda = Expression.Lambda<Func<string>> (
513 Expression.Constant (TypeCode.Boolean, typeof (TypeCode)),
514 typeof (object).GetMethod ("ToString"))).Compile ();
516 Assert.AreEqual ("Boolean", lambda ());
519 public static void AcceptsIEnumerable(IEnumerable<object> o)
524 public void CallIQueryableMethodWithNewArrayBoundExpression () // #2304
527 GetType ().GetMethod ("AcceptsIEnumerable", BindingFlags.Public | BindingFlags.Static),
528 Expression.NewArrayBounds (typeof (object), Expression.Constant (0)));