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 [ExpectedException (typeof (ArgumentNullException))]
91 public void ArgInstanceNullForNonStaticMethod ()
93 Expression.Call (null, typeof (object).GetMethod ("ToString"));
97 [ExpectedException (typeof (ArgumentException))]
98 public void InstanceTypeDoesntMatchMethodDeclaringType ()
100 Expression.Call (Expression.Constant (1), typeof (string).GetMethod ("Intern"));
104 [ExpectedException (typeof (ArgumentException))]
105 public void MethodArgumentCountDoesnMatchParameterLength ()
107 Expression.Call (Expression.Constant (new object ()), typeof (object).GetMethod ("ToString"), Expression.Constant (new object ()));
111 public void Bar (string s)
117 [ExpectedException (typeof (ArgumentNullException))]
118 public void MethodHasNullArgument ()
120 Expression.Call (Expression.New (typeof (Foo)), typeof (Foo).GetMethod ("Bar"), null as Expression);
124 [ExpectedException (typeof (ArgumentException))]
125 public void MethodArgumentDoesntMatchParameterType ()
127 Expression.Call (Expression.New (typeof (Foo)), typeof (Foo).GetMethod ("Bar"), Expression.Constant (42));
131 public void CallToString ()
133 var call = Expression.Call (Expression.Constant (new object ()), typeof (object).GetMethod ("ToString"));
134 Assert.AreEqual ("value(System.Object).ToString()", call.ToString ());
138 public void CallStringIsNullOrEmpty ()
140 var call = Expression.Call (null, typeof (string).GetMethod ("IsNullOrEmpty"), Expression.Constant (""));
141 Assert.AreEqual ("IsNullOrEmpty(\"\")", call.ToString ());
144 public static object Identity (object o)
150 public void CompileSimpleStaticCall ()
152 var p = Expression.Parameter (typeof (object), "o");
153 var lambda = Expression.Lambda<Func<object, object>> (Expression.Call (GetType ().GetMethod ("Identity"), p), p);
155 var i = lambda.Compile ();
157 Assert.AreEqual (2, i (2));
158 Assert.AreEqual ("Foo", i ("Foo"));
162 public void CompileSimpleInstanceCall ()
164 var p = Expression.Parameter (typeof (string), "p");
165 var lambda = Expression.Lambda<Func<string, string>> (
167 p, typeof (string).GetMethod ("ToString", Type.EmptyTypes)),
170 var ts = lambda.Compile ();
172 Assert.AreEqual ("foo", ts ("foo"));
173 Assert.AreEqual ("bar", ts ("bar"));
177 [ExpectedException (typeof (InvalidOperationException))]
178 public void CheckTypeArgsIsNotUsedForParameterLookup ()
180 Expression.Call (GetType (), "EineMethod", new [] { typeof (string), typeof (int) }, "foo".ToConstant (), 2.ToConstant ());
183 public static void EineGenericMethod<X, Y> (string foo, int bar)
188 public void CheckTypeArgsIsUsedForGenericArguments ()
190 var m = Expression.Call (GetType (), "EineGenericMethod", new [] { typeof (string), typeof (int) }, "foo".ToConstant (), 2.ToConstant ());
191 Assert.IsNotNull (m.Method);
192 Assert.AreEqual ("Void EineGenericMethod[String,Int32](System.String, Int32)", m.Method.ToString ());
195 public struct EineStrukt {
199 public EineStrukt (string foo)
204 public string GimmeFoo ()
211 public void CallMethodOnStruct ()
213 var param = Expression.Parameter (typeof (EineStrukt), "s");
214 var foo = Expression.Lambda<Func<EineStrukt, string>> (
215 Expression.Call (param, typeof (EineStrukt).GetMethod ("GimmeFoo")), param).Compile ();
217 var s = new EineStrukt ("foo");
218 Assert.AreEqual ("foo", foo (s));
221 public static int OneStaticMethod ()
227 public void CallStaticMethodOnNonSenseInstanceExpression ()
229 var call = Expression.Call (
230 Expression.Constant ("la la la"),
231 this.GetType ().GetMethod ("OneStaticMethod"));
233 Assert.IsNotNull (call.Object);
235 var callMethod = Expression.Lambda<Func<int>> (call).Compile ();
237 Assert.AreEqual (42, callMethod ());
240 public static int DoSomethingWith (ref int a)
245 public static string DoAnotherThing (ref int a, string s)
251 public void CallStaticMethodWithRefParameter ()
253 var p = Expression.Parameter (typeof (int), "i");
255 var c = Expression.Lambda<Func<int, int>> (
256 Expression.Call (GetType ().GetMethod ("DoSomethingWith"), p), p).Compile ();
258 Assert.AreEqual (42, c (38));
262 public void CallStaticMethodWithRefParameterAndOtherParameter ()
264 var i = Expression.Parameter (typeof (int), "i");
265 var s = Expression.Parameter (typeof (string), "s");
267 var lamda = Expression.Lambda<Func<int, string, string>> (
268 Expression.Call (GetType ().GetMethod ("DoAnotherThing"), i, s), i, s).Compile ();
270 Assert.AreEqual ("foo42", lamda (42, "foo"));
273 public static int Bang (Expression i)
275 return (int) (i as ConstantExpression).Value;
279 public void CallMethodWithExpressionParameter ()
281 var call = Expression.Call (GetType ().GetMethod ("Bang"), Expression.Constant (42));
282 Assert.AreEqual (ExpressionType.Quote, call.Arguments [0].NodeType);
284 var l = Expression.Lambda<Func<int>> (call).Compile ();
286 Assert.AreEqual (42, l ());
289 static bool fout_called = false;
291 public static int FooOut (out int x)
298 public void Connect282729 ()
300 // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=282729
302 var p = Expression.Parameter (typeof (int), "p");
303 var lambda = Expression.Lambda<Func<int, int>> (
305 GetType ().GetMethod ("FooOut"),
306 Expression.ArrayIndex(
307 Expression.NewArrayBounds (
313 Assert.AreEqual (0, lambda (0));
314 Assert.IsTrue (fout_called);
317 public static int FooOut2 (out int x)
324 [Category ("NotWorking")]
325 public void Connect290278 ()
327 // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=290278
329 var p = Expression.Parameter (typeof (int [,]), "p");
330 var lambda = Expression.Lambda<Func<int [,], int>> (
332 GetType ().GetMethod ("FooOut2"),
333 Expression.ArrayIndex (p, 0.ToConstant (), 0.ToConstant ())),
336 int [,] data = { { 1 } };
338 Assert.AreEqual (3, lambda (data));
339 Assert.AreEqual (2, data [0, 0]);
342 public static void FooRef (ref string s)
347 public void Connect297597 ()
349 // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=297597
351 var strings = new string [1];
353 var lambda = Expression.Lambda<Action> (
355 GetType ().GetMethod ("FooRef"),
356 Expression.ArrayIndex (
357 Expression.Constant (strings), 0.ToConstant ()))).Compile ();
363 [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=319190
364 public void Connect319190 ()
366 var lambda = Expression.Lambda<Func<bool>> (
368 Expression.New (typeof (TypedReference)),
369 typeof (object))).Compile ();
371 Assert.IsTrue (lambda ());
374 public static int Truc ()
380 public void Connect282702 ()
382 var lambda = Expression.Lambda<Func<Func<int>>> (
385 typeof (Delegate).GetMethod ("CreateDelegate", new [] { typeof (Type), typeof (object), typeof (MethodInfo) }),
386 Expression.Constant (typeof (Func<int>), typeof (Type)),
387 Expression.Constant (null, typeof (object)),
388 Expression.Constant (GetType ().GetMethod ("Truc"))),
389 typeof (Func<int>))).Compile ();
391 Assert.AreEqual (42, lambda ().Invoke ());