ExpressionTest_Call.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. //
  2. // ExpressionTest_Call.cs
  3. //
  4. // Author:
  5. // Federico Di Gregorio <[email protected]>
  6. // Jb Evain ([email protected])
  7. //
  8. // (C) 2008 Novell, Inc. (http://www.novell.com)
  9. //
  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:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  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.
  28. //
  29. using System;
  30. using System.Reflection;
  31. using System.Reflection.Emit;
  32. using System.Collections.Generic;
  33. using System.Linq;
  34. using System.Linq.Expressions;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Linq.Expressions {
  37. [TestFixture]
  38. public class ExpressionTest_Call {
  39. [Test]
  40. [ExpectedException (typeof (ArgumentNullException))]
  41. public void Arg1Null ()
  42. {
  43. Expression.Call ((Type)null, "TestMethod", null, Expression.Constant (1));
  44. }
  45. [Test]
  46. [ExpectedException (typeof (ArgumentNullException))]
  47. public void Arg2Null ()
  48. {
  49. Expression.Call (typeof (MemberClass), null, null, Expression.Constant (1));
  50. }
  51. [Test]
  52. [ExpectedException (typeof (InvalidOperationException))]
  53. public void Arg4WrongType ()
  54. {
  55. Expression.Call (typeof (MemberClass), "StaticMethod", null, Expression.Constant (true));
  56. }
  57. [Test]
  58. [ExpectedException (typeof (InvalidOperationException))]
  59. public void InstanceMethod ()
  60. {
  61. Expression.Call (typeof (MemberClass), "TestMethod", null, Expression.Constant (1));
  62. }
  63. [Test]
  64. public void StaticMethod ()
  65. {
  66. Expression.Call (typeof (MemberClass), "StaticMethod", null, Expression.Constant (1));
  67. }
  68. [Test]
  69. public void StaticGenericMethod ()
  70. {
  71. Expression.Call (typeof (MemberClass), "StaticGenericMethod", new [] { typeof (int) }, Expression.Constant (1));
  72. }
  73. [Test]
  74. [ExpectedException (typeof (ArgumentNullException))]
  75. public void ArgMethodNull ()
  76. {
  77. Expression.Call (Expression.Constant (new object ()), null);
  78. }
  79. [Test]
  80. [ExpectedException (typeof (ArgumentException))]
  81. public void ArgInstanceNullForNonStaticMethod ()
  82. {
  83. Expression.Call (null, typeof (object).GetMethod ("ToString"));
  84. }
  85. [Test]
  86. [ExpectedException (typeof (ArgumentException))]
  87. public void InstanceTypeDoesntMatchMethodDeclaringType ()
  88. {
  89. #if MOBILE
  90. // ensure that String.Intern won't be removed by the linker
  91. string s = String.Intern (String.Empty);
  92. #endif
  93. Expression.Call (Expression.Constant (1), typeof (string).GetMethod ("Intern"));
  94. }
  95. [Test]
  96. [ExpectedException (typeof (ArgumentException))]
  97. public void MethodArgumentCountDoesnMatchParameterLength ()
  98. {
  99. Expression.Call (Expression.Constant (new object ()), typeof (object).GetMethod ("ToString"), Expression.Constant (new object ()));
  100. }
  101. public class Foo {
  102. public void Bar (string s)
  103. {
  104. }
  105. }
  106. [Test]
  107. [ExpectedException (typeof (ArgumentNullException))]
  108. public void MethodHasNullArgument ()
  109. {
  110. Expression.Call (Expression.New (typeof (Foo)), typeof (Foo).GetMethod ("Bar"), null as Expression);
  111. }
  112. [Test]
  113. [ExpectedException (typeof (ArgumentException))]
  114. public void MethodArgumentDoesntMatchParameterType ()
  115. {
  116. Expression.Call (Expression.New (typeof (Foo)), typeof (Foo).GetMethod ("Bar"), Expression.Constant (42));
  117. }
  118. [Test]
  119. public void CallToString ()
  120. {
  121. var call = Expression.Call (Expression.Constant (new object ()), typeof (object).GetMethod ("ToString"));
  122. Assert.AreEqual ("value(System.Object).ToString()", call.ToString ());
  123. }
  124. [Test]
  125. public void CallStringIsNullOrEmpty ()
  126. {
  127. var call = Expression.Call (null, typeof (string).GetMethod ("IsNullOrEmpty"), Expression.Constant (""));
  128. Assert.AreEqual ("IsNullOrEmpty(\"\")", call.ToString ());
  129. }
  130. [Test]
  131. [Category ("NotDotNet")] // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=339351
  132. [ExpectedException (typeof (ArgumentException))]
  133. public void CallStaticMethodWithInstanceArgument ()
  134. {
  135. Expression.Call (
  136. Expression.Parameter (GetType (), "t"),
  137. GetType ().GetMethod ("Identity"),
  138. Expression.Constant (null));
  139. }
  140. public static object Identity (object o)
  141. {
  142. return o;
  143. }
  144. [Test]
  145. public void CompileSimpleStaticCall ()
  146. {
  147. var p = Expression.Parameter (typeof (object), "o");
  148. var lambda = Expression.Lambda<Func<object, object>> (Expression.Call (GetType ().GetMethod ("Identity"), p), p);
  149. var i = lambda.Compile ();
  150. Assert.AreEqual (2, i (2));
  151. Assert.AreEqual ("Foo", i ("Foo"));
  152. }
  153. [Test]
  154. public void CompileSimpleInstanceCall ()
  155. {
  156. var p = Expression.Parameter (typeof (string), "p");
  157. var lambda = Expression.Lambda<Func<string, string>> (
  158. Expression.Call (
  159. p, typeof (string).GetMethod ("ToString", Type.EmptyTypes)),
  160. p);
  161. var ts = lambda.Compile ();
  162. Assert.AreEqual ("foo", ts ("foo"));
  163. Assert.AreEqual ("bar", ts ("bar"));
  164. }
  165. [Test]
  166. [ExpectedException (typeof (InvalidOperationException))]
  167. public void CheckTypeArgsIsNotUsedForParameterLookup ()
  168. {
  169. Expression.Call (GetType (), "EineMethod", new [] { typeof (string), typeof (int) }, "foo".ToConstant (), 2.ToConstant ());
  170. }
  171. public static void EineGenericMethod<X, Y> (string foo, int bar)
  172. {
  173. }
  174. [Test]
  175. public void CheckTypeArgsIsUsedForGenericArguments ()
  176. {
  177. var m = Expression.Call (GetType (), "EineGenericMethod", new [] { typeof (string), typeof (int) }, "foo".ToConstant (), 2.ToConstant ());
  178. Assert.IsNotNull (m.Method);
  179. Assert.AreEqual ("Void EineGenericMethod[String,Int32](System.String, Int32)", m.Method.ToString ());
  180. }
  181. public struct EineStrukt {
  182. public string Foo;
  183. public EineStrukt (string foo)
  184. {
  185. Foo = foo;
  186. }
  187. public string GimmeFoo ()
  188. {
  189. return Foo;
  190. }
  191. }
  192. [Test]
  193. public void CallMethodOnStruct ()
  194. {
  195. var param = Expression.Parameter (typeof (EineStrukt), "s");
  196. var foo = Expression.Lambda<Func<EineStrukt, string>> (
  197. Expression.Call (param, typeof (EineStrukt).GetMethod ("GimmeFoo")), param).Compile ();
  198. var s = new EineStrukt ("foo");
  199. Assert.AreEqual ("foo", foo (s));
  200. }
  201. public static int OneStaticMethod ()
  202. {
  203. return 42;
  204. }
  205. [Test]
  206. public void CallMethodOnDateTime ()
  207. {
  208. var left = Expression.Call (Expression.Constant (DateTime.Now), typeof(DateTime).GetMethod ("AddDays"), Expression.Constant (-5.0));
  209. var right = Expression.Constant (DateTime.Today.AddDays (1));
  210. var expr = Expression.GreaterThan (left, right);
  211. var eq = Expression.Lambda<Func<bool>> (expr).Compile ();
  212. Assert.IsFalse (eq ());
  213. }
  214. [Test]
  215. [Category ("NotDotNet")] // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=339351
  216. [ExpectedException (typeof (ArgumentException))]
  217. public void CallStaticMethodOnNonSenseInstanceExpression ()
  218. {
  219. Expression.Call (
  220. Expression.Constant ("la la la"),
  221. this.GetType ().GetMethod ("OneStaticMethod"));
  222. }
  223. public static int DoSomethingWith (ref int a)
  224. {
  225. return a + 4;
  226. }
  227. public static string DoAnotherThing (ref int a, string s)
  228. {
  229. return s + a;
  230. }
  231. [Test]
  232. public void CallStaticMethodWithRefParameter ()
  233. {
  234. var p = Expression.Parameter (typeof (int), "i");
  235. var c = Expression.Lambda<Func<int, int>> (
  236. Expression.Call (GetType ().GetMethod ("DoSomethingWith"), p), p).Compile ();
  237. Assert.AreEqual (42, c (38));
  238. }
  239. [Test]
  240. public void CallStaticMethodWithRefParameterAndOtherParameter ()
  241. {
  242. var i = Expression.Parameter (typeof (int), "i");
  243. var s = Expression.Parameter (typeof (string), "s");
  244. var lamda = Expression.Lambda<Func<int, string, string>> (
  245. Expression.Call (GetType ().GetMethod ("DoAnotherThing"), i, s), i, s).Compile ();
  246. Assert.AreEqual ("foo42", lamda (42, "foo"));
  247. }
  248. [Test]
  249. public void CallDynamicMethod_ToString ()
  250. {
  251. // Regression test for #49686
  252. var m = new DynamicMethod ("intIntId", typeof (int), new Type [] { typeof (int) });
  253. var ilg = m.GetILGenerator ();
  254. ilg.Emit (OpCodes.Ldarg_0);
  255. ilg.Emit (OpCodes.Ret);
  256. var i = Expression.Parameter (typeof (int), "i");
  257. var e = Expression.Call (m, i);
  258. Assert.IsNotNull (e.ToString ());
  259. }
  260. [Test]
  261. public void CallDynamicMethod_CompileInvoke ()
  262. {
  263. var m = new DynamicMethod ("intIntId", typeof (int), new Type [] { typeof (int) });
  264. var ilg = m.GetILGenerator ();
  265. ilg.Emit (OpCodes.Ldarg_0);
  266. ilg.Emit (OpCodes.Ret);
  267. var i = Expression.Parameter (typeof (int), "i");
  268. var e = Expression.Call (m, i);
  269. var lambda = Expression.Lambda<Func<int, int>> (e, i).Compile ();
  270. Assert.AreEqual (42, lambda (42));
  271. }
  272. public static int Bang (Expression i)
  273. {
  274. return (int) (i as ConstantExpression).Value;
  275. }
  276. static bool fout_called = false;
  277. public static int FooOut (out int x)
  278. {
  279. fout_called = true;
  280. return x = 0;
  281. }
  282. [Test]
  283. public void Connect282729 ()
  284. {
  285. // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=282729
  286. var p = Expression.Parameter (typeof (int), "p");
  287. var lambda = Expression.Lambda<Func<int, int>> (
  288. Expression.Call (
  289. GetType ().GetMethod ("FooOut"),
  290. Expression.ArrayIndex(
  291. Expression.NewArrayBounds (
  292. typeof(int),
  293. 1.ToConstant ()),
  294. 0.ToConstant ())),
  295. p).Compile ();
  296. Assert.AreEqual (0, lambda (0));
  297. Assert.IsTrue (fout_called);
  298. }
  299. public static int FooOut2 (out int x)
  300. {
  301. x = 2;
  302. return 3;
  303. }
  304. [Test]
  305. public void Connect290278 ()
  306. {
  307. // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=290278
  308. var p = Expression.Parameter (typeof (int [,]), "p");
  309. var lambda = Expression.Lambda<Func<int [,], int>> (
  310. Expression.Call (
  311. GetType ().GetMethod ("FooOut2"),
  312. Expression.ArrayIndex (p, 0.ToConstant (), 0.ToConstant ())),
  313. p).Compile ();
  314. int [,] data = { { 1 } };
  315. Assert.AreEqual (3, lambda (data));
  316. Assert.AreEqual (2, data [0, 0]);
  317. }
  318. public static void FooRef (ref string s)
  319. {
  320. }
  321. [Test]
  322. public void Connect297597 ()
  323. {
  324. // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=297597
  325. var strings = new string [1];
  326. var lambda = Expression.Lambda<Action> (
  327. Expression.Call (
  328. GetType ().GetMethod ("FooRef"),
  329. Expression.ArrayIndex (
  330. Expression.Constant (strings), 0.ToConstant ()))).Compile ();
  331. lambda ();
  332. }
  333. [Test]
  334. [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=319190
  335. [Category ("NotWorkingInterpreter")]
  336. public void Connect319190 ()
  337. {
  338. var lambda = Expression.Lambda<Func<bool>> (
  339. Expression.TypeIs (
  340. Expression.New (typeof (TypedReference)),
  341. typeof (object))).Compile ();
  342. Assert.IsTrue (lambda ());
  343. }
  344. public static int Truc ()
  345. {
  346. return 42;
  347. }
  348. [Test]
  349. public void Connect282702 ()
  350. {
  351. var lambda = Expression.Lambda<Func<Func<int>>> (
  352. Expression.Convert (
  353. Expression.Call (
  354. typeof (Delegate).GetMethod ("CreateDelegate", new [] { typeof (Type), typeof (object), typeof (MethodInfo) }),
  355. Expression.Constant (typeof (Func<int>), typeof (Type)),
  356. Expression.Constant (null, typeof (object)),
  357. Expression.Constant (GetType ().GetMethod ("Truc"))),
  358. typeof (Func<int>))).Compile ();
  359. Assert.AreEqual (42, lambda ().Invoke ());
  360. }
  361. [Test]
  362. public void CallQueryableWhere ()
  363. {
  364. var queryable = new [] { 1, 2, 3 }.AsQueryable ();
  365. var parameter = Expression.Parameter (typeof (int), "i");
  366. var lambda = Expression.Lambda<Func<int, bool>> (
  367. Expression.LessThan (parameter, Expression.Constant (2)),
  368. parameter);
  369. var selector = Expression.Quote (lambda);
  370. var call = Expression.Call (
  371. typeof (Queryable),
  372. "Where",
  373. new [] { typeof (int) },
  374. queryable.Expression,
  375. selector);
  376. Assert.IsNotNull (call);
  377. Assert.IsNotNull (call.Method);
  378. }
  379. [Test]
  380. public void CallAsQueryable () // #537768
  381. {
  382. var constant = Expression.Constant (
  383. new List<string> (),
  384. typeof (IEnumerable<string>));
  385. var call = Expression.Call (
  386. typeof (Queryable),
  387. "AsQueryable",
  388. new [] { typeof (string) },
  389. constant);
  390. Assert.IsNotNull (call);
  391. Assert.AreEqual (1, call.Arguments.Count);
  392. Assert.AreEqual (constant, call.Arguments [0]);
  393. var method = call.Method;
  394. Assert.AreEqual ("AsQueryable", method.Name);
  395. Assert.IsTrue (method.IsGenericMethod);
  396. Assert.AreEqual (typeof (string), method.GetGenericArguments () [0]);
  397. }
  398. [Test]
  399. public void CallQueryableSelect () // #536637
  400. {
  401. var parameter = Expression.Parameter (typeof (string), "s");
  402. var string_length = Expression.Property (parameter, typeof (string).GetProperty ("Length"));
  403. var lambda = Expression.Lambda (string_length, parameter);
  404. var strings = new [] { "1", "22", "333" };
  405. var call = Expression.Call (
  406. typeof (Queryable),
  407. "Select",
  408. new [] { typeof (string), typeof (int) },
  409. Expression.Constant (strings.AsQueryable ()),
  410. lambda);
  411. Assert.IsNotNull (call);
  412. var method = call.Method;
  413. Assert.AreEqual ("Select", method.Name);
  414. Assert.IsTrue (method.IsGenericMethod);
  415. Assert.AreEqual (typeof (string), method.GetGenericArguments () [0]);
  416. Assert.AreEqual (typeof (int), method.GetGenericArguments () [1]);
  417. }
  418. [Test]
  419. public void CallNullableGetValueOrDefault () // #568989
  420. {
  421. var value = Expression.Parameter (typeof (int?), "value");
  422. var default_parameter = Expression.Parameter (typeof (int), "default");
  423. var getter = Expression.Lambda<Func<int?, int, int>> (
  424. Expression.Call (
  425. value,
  426. "GetValueOrDefault",
  427. Type.EmptyTypes,
  428. default_parameter),
  429. value,
  430. default_parameter).Compile ();
  431. Assert.AreEqual (2, getter (null, 2));
  432. Assert.AreEqual (4, getter (4, 2));
  433. }
  434. [Test]
  435. public void CallToStringOnEnum () // #625367
  436. {
  437. var lambda = Expression.Lambda<Func<string>> (
  438. Expression.Call (
  439. Expression.Constant (TypeCode.Boolean, typeof (TypeCode)),
  440. typeof (object).GetMethod ("ToString"))).Compile ();
  441. Assert.AreEqual ("Boolean", lambda ());
  442. }
  443. public static void AcceptsIEnumerable(IEnumerable<object> o)
  444. {
  445. }
  446. [Test]
  447. public void CallIQueryableMethodWithNewArrayBoundExpression () // #2304
  448. {
  449. Expression.Call (
  450. GetType ().GetMethod ("AcceptsIEnumerable", BindingFlags.Public | BindingFlags.Static),
  451. Expression.NewArrayBounds (typeof (object), Expression.Constant (0)));
  452. }
  453. }
  454. }