ExpressionTest_Call.cs 15 KB

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