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"));
102 [Category ("NotWorking")] // String:Intern () is linked away
104 [ExpectedException (typeof (ArgumentException))]
105 public void InstanceTypeDoesntMatchMethodDeclaringType ()
107 Expression.Call (Expression.Constant (1), typeof (string).GetMethod ("Intern"));
111 [ExpectedException (typeof (ArgumentException))]
112 public void MethodArgumentCountDoesnMatchParameterLength ()
114 Expression.Call (Expression.Constant (new object ()), typeof (object).GetMethod ("ToString"), Expression.Constant (new object ()));
118 public void Bar (string s)
124 [ExpectedException (typeof (ArgumentNullException))]
125 public void MethodHasNullArgument ()
127 Expression.Call (Expression.New (typeof (Foo)), typeof (Foo).GetMethod ("Bar"), null as Expression);
131 [ExpectedException (typeof (ArgumentException))]
132 public void MethodArgumentDoesntMatchParameterType ()
134 Expression.Call (Expression.New (typeof (Foo)), typeof (Foo).GetMethod ("Bar"), Expression.Constant (42));
138 public void CallToString ()
140 var call = Expression.Call (Expression.Constant (new object ()), typeof (object).GetMethod ("ToString"));
141 Assert.AreEqual ("value(System.Object).ToString()", call.ToString ());
145 public void CallStringIsNullOrEmpty ()
147 var call = Expression.Call (null, typeof (string).GetMethod ("IsNullOrEmpty"), Expression.Constant (""));
148 Assert.AreEqual ("IsNullOrEmpty(\"\")", call.ToString ());
152 [Category ("NotDotNet")] // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=339351
153 [ExpectedException (typeof (ArgumentException))]
154 public void CallStaticMethodWithInstanceArgument ()
157 Expression.Parameter (GetType (), "t"),
158 GetType ().GetMethod ("Identity"),
159 Expression.Constant (null));
162 public static object Identity (object o)
168 public void CompileSimpleStaticCall ()
170 var p = Expression.Parameter (typeof (object), "o");
171 var lambda = Expression.Lambda<Func<object, object>> (Expression.Call (GetType ().GetMethod ("Identity"), p), p);
173 var i = lambda.Compile ();
175 Assert.AreEqual (2, i (2));
176 Assert.AreEqual ("Foo", i ("Foo"));
180 public void CompileSimpleInstanceCall ()
182 var p = Expression.Parameter (typeof (string), "p");
183 var lambda = Expression.Lambda<Func<string, string>> (
185 p, typeof (string).GetMethod ("ToString", Type.EmptyTypes)),
188 var ts = lambda.Compile ();
190 Assert.AreEqual ("foo", ts ("foo"));
191 Assert.AreEqual ("bar", ts ("bar"));
195 [ExpectedException (typeof (InvalidOperationException))]
196 public void CheckTypeArgsIsNotUsedForParameterLookup ()
198 Expression.Call (GetType (), "EineMethod", new [] { typeof (string), typeof (int) }, "foo".ToConstant (), 2.ToConstant ());
201 public static void EineGenericMethod<X, Y> (string foo, int bar)
206 public void CheckTypeArgsIsUsedForGenericArguments ()
208 var m = Expression.Call (GetType (), "EineGenericMethod", new [] { typeof (string), typeof (int) }, "foo".ToConstant (), 2.ToConstant ());
209 Assert.IsNotNull (m.Method);
210 Assert.AreEqual ("Void EineGenericMethod[String,Int32](System.String, Int32)", m.Method.ToString ());
213 public struct EineStrukt {
217 public EineStrukt (string foo)
222 public string GimmeFoo ()
229 public void CallMethodOnStruct ()
231 var param = Expression.Parameter (typeof (EineStrukt), "s");
232 var foo = Expression.Lambda<Func<EineStrukt, string>> (
233 Expression.Call (param, typeof (EineStrukt).GetMethod ("GimmeFoo")), param).Compile ();
235 var s = new EineStrukt ("foo");
236 Assert.AreEqual ("foo", foo (s));
239 public static int OneStaticMethod ()
245 [Category ("NotDotNet")] // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=339351
246 [ExpectedException (typeof (ArgumentException))]
247 public void CallStaticMethodOnNonSenseInstanceExpression ()
250 Expression.Constant ("la la la"),
251 this.GetType ().GetMethod ("OneStaticMethod"));
254 public static int DoSomethingWith (ref int a)
259 public static string DoAnotherThing (ref int a, string s)
265 public void CallStaticMethodWithRefParameter ()
267 var p = Expression.Parameter (typeof (int), "i");
269 var c = Expression.Lambda<Func<int, int>> (
270 Expression.Call (GetType ().GetMethod ("DoSomethingWith"), p), p).Compile ();
272 Assert.AreEqual (42, c (38));
276 public void CallStaticMethodWithRefParameterAndOtherParameter ()
278 var i = Expression.Parameter (typeof (int), "i");
279 var s = Expression.Parameter (typeof (string), "s");
281 var lamda = Expression.Lambda<Func<int, string, string>> (
282 Expression.Call (GetType ().GetMethod ("DoAnotherThing"), i, s), i, s).Compile ();
284 Assert.AreEqual ("foo42", lamda (42, "foo"));
287 public static int Bang (Expression i)
289 return (int) (i as ConstantExpression).Value;
291 #if !NET_4_0 // dlr bug 5875
293 public void CallMethodWithExpressionParameter ()
295 var call = Expression.Call (GetType ().GetMethod ("Bang"), Expression.Constant (42));
296 Assert.AreEqual (ExpressionType.Quote, call.Arguments [0].NodeType);
298 var l = Expression.Lambda<Func<int>> (call).Compile ();
300 Assert.AreEqual (42, l ());
303 static bool fout_called = false;
305 public static int FooOut (out int x)
312 public void Connect282729 ()
314 // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=282729
316 var p = Expression.Parameter (typeof (int), "p");
317 var lambda = Expression.Lambda<Func<int, int>> (
319 GetType ().GetMethod ("FooOut"),
320 Expression.ArrayIndex(
321 Expression.NewArrayBounds (
327 Assert.AreEqual (0, lambda (0));
328 Assert.IsTrue (fout_called);
331 public static int FooOut2 (out int x)
338 [Category ("NotWorking")]
339 public void Connect290278 ()
341 // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=290278
343 var p = Expression.Parameter (typeof (int [,]), "p");
344 var lambda = Expression.Lambda<Func<int [,], int>> (
346 GetType ().GetMethod ("FooOut2"),
347 Expression.ArrayIndex (p, 0.ToConstant (), 0.ToConstant ())),
350 int [,] data = { { 1 } };
352 Assert.AreEqual (3, lambda (data));
353 Assert.AreEqual (2, data [0, 0]);
356 public static void FooRef (ref string s)
361 public void Connect297597 ()
363 // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=297597
365 var strings = new string [1];
367 var lambda = Expression.Lambda<Action> (
369 GetType ().GetMethod ("FooRef"),
370 Expression.ArrayIndex (
371 Expression.Constant (strings), 0.ToConstant ()))).Compile ();
378 [Category ("NotWorking")]
380 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=319190
381 public void Connect319190 ()
383 var lambda = Expression.Lambda<Func<bool>> (
385 Expression.New (typeof (TypedReference)),
386 typeof (object))).Compile ();
388 Assert.IsTrue (lambda ());
391 public static int Truc ()
397 public void Connect282702 ()
399 var lambda = Expression.Lambda<Func<Func<int>>> (
402 typeof (Delegate).GetMethod ("CreateDelegate", new [] { typeof (Type), typeof (object), typeof (MethodInfo) }),
403 Expression.Constant (typeof (Func<int>), typeof (Type)),
404 Expression.Constant (null, typeof (object)),
405 Expression.Constant (GetType ().GetMethod ("Truc"))),
406 typeof (Func<int>))).Compile ();
408 Assert.AreEqual (42, lambda ().Invoke ());
412 public void CallQueryableWhere ()
414 var queryable = new [] { 1, 2, 3 }.AsQueryable ();
416 var parameter = Expression.Parameter (typeof (int), "i");
417 var lambda = Expression.Lambda<Func<int, bool>> (
418 Expression.LessThan (parameter, Expression.Constant (2)),
421 var selector = Expression.Quote (lambda);
423 var call = Expression.Call (
426 new [] { typeof (int) },
427 queryable.Expression,
430 Assert.IsNotNull (call);
431 Assert.IsNotNull (call.Method);
435 public void CallAsQueryable () // #537768
437 var constant = Expression.Constant (
439 typeof (IEnumerable<string>));
441 var call = Expression.Call (
444 new [] { typeof (string) },
447 Assert.IsNotNull (call);
448 Assert.AreEqual (1, call.Arguments.Count);
449 Assert.AreEqual (constant, call.Arguments [0]);
451 var method = call.Method;
453 Assert.AreEqual ("AsQueryable", method.Name);
454 Assert.IsTrue (method.IsGenericMethod);
455 Assert.AreEqual (typeof (string), method.GetGenericArguments () [0]);
460 public void CallQueryableSelect () // #536637
462 var parameter = Expression.Parameter (typeof (string), "s");
463 var string_length = Expression.Property (parameter, typeof (string).GetProperty ("Length"));
464 var lambda = Expression.Lambda (string_length, parameter);
466 var strings = new [] { "1", "22", "333" };
468 var call = Expression.Call (
471 new [] { typeof (string), typeof (int) },
472 Expression.Constant (strings.AsQueryable ()),
475 Assert.IsNotNull (call);
477 var method = call.Method;
479 Assert.AreEqual ("Select", method.Name);
480 Assert.IsTrue (method.IsGenericMethod);
481 Assert.AreEqual (typeof (string), method.GetGenericArguments () [0]);
482 Assert.AreEqual (typeof (int), method.GetGenericArguments () [1]);
487 [Category ("NotWorking")]
489 public void CallNullableGetValueOrDefault () // #568989
491 var value = Expression.Parameter (typeof (int?), "value");
492 var default_parameter = Expression.Parameter (typeof (int), "default");
494 var getter = Expression.Lambda<Func<int?, int, int>> (
501 default_parameter).Compile ();
503 Assert.AreEqual (2, getter (null, 2));
504 Assert.AreEqual (4, getter (4, 2));
508 public void CallToStringOnEnum () // #625367
510 var lambda = Expression.Lambda<Func<string>> (
512 Expression.Constant (TypeCode.Boolean, typeof (TypeCode)),
513 typeof (object).GetMethod ("ToString"))).Compile ();
515 Assert.AreEqual ("Boolean", lambda ());
518 public static void AcceptsIEnumerable(IEnumerable<object> o)
523 public void CallIQueryableMethodWithNewArrayBoundExpression () // #2304
526 GetType ().GetMethod ("AcceptsIEnumerable", BindingFlags.Public | BindingFlags.Static),
527 Expression.NewArrayBounds (typeof (object), Expression.Constant (0)));