ExpressionTest_Call.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. #if !MONOTOUCH
  249. [Test]
  250. public void CallDynamicMethod_ToString ()
  251. {
  252. // Regression test for #49686
  253. var m = new DynamicMethod ("intIntId", typeof (int), new Type [] { typeof (int) });
  254. var ilg = m.GetILGenerator ();
  255. ilg.Emit (OpCodes.Ldarg_0);
  256. ilg.Emit (OpCodes.Ret);
  257. var i = Expression.Parameter (typeof (int), "i");
  258. var e = Expression.Call (m, i);
  259. Assert.IsNotNull (e.ToString ());
  260. }
  261. [Test]
  262. public void CallDynamicMethod_CompileInvoke ()
  263. {
  264. var m = new DynamicMethod ("intIntId", typeof (int), new Type [] { typeof (int) });
  265. var ilg = m.GetILGenerator ();
  266. ilg.Emit (OpCodes.Ldarg_0);
  267. ilg.Emit (OpCodes.Ret);
  268. var i = Expression.Parameter (typeof (int), "i");
  269. var e = Expression.Call (m, i);
  270. var lambda = Expression.Lambda<Func<int, int>> (e, i).Compile ();
  271. Assert.AreEqual (42, lambda (42));
  272. }
  273. #endif
  274. public static int Bang (Expression i)
  275. {
  276. return (int) (i as ConstantExpression).Value;
  277. }
  278. static bool fout_called = false;
  279. public static int FooOut (out int x)
  280. {
  281. fout_called = true;
  282. return x = 0;
  283. }
  284. [Test]
  285. public void Connect282729 ()
  286. {
  287. // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=282729
  288. var p = Expression.Parameter (typeof (int), "p");
  289. var lambda = Expression.Lambda<Func<int, int>> (
  290. Expression.Call (
  291. GetType ().GetMethod ("FooOut"),
  292. Expression.ArrayIndex(
  293. Expression.NewArrayBounds (
  294. typeof(int),
  295. 1.ToConstant ()),
  296. 0.ToConstant ())),
  297. p).Compile ();
  298. Assert.AreEqual (0, lambda (0));
  299. Assert.IsTrue (fout_called);
  300. }
  301. public static int FooOut2 (out int x)
  302. {
  303. x = 2;
  304. return 3;
  305. }
  306. [Test]
  307. public void Connect290278 ()
  308. {
  309. // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=290278
  310. var p = Expression.Parameter (typeof (int [,]), "p");
  311. var lambda = Expression.Lambda<Func<int [,], int>> (
  312. Expression.Call (
  313. GetType ().GetMethod ("FooOut2"),
  314. Expression.ArrayIndex (p, 0.ToConstant (), 0.ToConstant ())),
  315. p).Compile ();
  316. int [,] data = { { 1 } };
  317. Assert.AreEqual (3, lambda (data));
  318. Assert.AreEqual (2, data [0, 0]);
  319. }
  320. public static void FooRef (ref string s)
  321. {
  322. }
  323. [Test]
  324. public void Connect297597 ()
  325. {
  326. // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=297597
  327. var strings = new string [1];
  328. var lambda = Expression.Lambda<Action> (
  329. Expression.Call (
  330. GetType ().GetMethod ("FooRef"),
  331. Expression.ArrayIndex (
  332. Expression.Constant (strings), 0.ToConstant ()))).Compile ();
  333. lambda ();
  334. }
  335. [Test]
  336. [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=319190
  337. [Category ("NotWorkingInterpreter")]
  338. public void Connect319190 ()
  339. {
  340. var lambda = Expression.Lambda<Func<bool>> (
  341. Expression.TypeIs (
  342. Expression.New (typeof (TypedReference)),
  343. typeof (object))).Compile ();
  344. Assert.IsTrue (lambda ());
  345. }
  346. public static int Truc ()
  347. {
  348. return 42;
  349. }
  350. [Test]
  351. public void Connect282702 ()
  352. {
  353. var lambda = Expression.Lambda<Func<Func<int>>> (
  354. Expression.Convert (
  355. Expression.Call (
  356. typeof (Delegate).GetMethod ("CreateDelegate", new [] { typeof (Type), typeof (object), typeof (MethodInfo) }),
  357. Expression.Constant (typeof (Func<int>), typeof (Type)),
  358. Expression.Constant (null, typeof (object)),
  359. Expression.Constant (GetType ().GetMethod ("Truc"))),
  360. typeof (Func<int>))).Compile ();
  361. Assert.AreEqual (42, lambda ().Invoke ());
  362. }
  363. [Test]
  364. public void CallQueryableWhere ()
  365. {
  366. var queryable = new [] { 1, 2, 3 }.AsQueryable ();
  367. var parameter = Expression.Parameter (typeof (int), "i");
  368. var lambda = Expression.Lambda<Func<int, bool>> (
  369. Expression.LessThan (parameter, Expression.Constant (2)),
  370. parameter);
  371. var selector = Expression.Quote (lambda);
  372. var call = Expression.Call (
  373. typeof (Queryable),
  374. "Where",
  375. new [] { typeof (int) },
  376. queryable.Expression,
  377. selector);
  378. Assert.IsNotNull (call);
  379. Assert.IsNotNull (call.Method);
  380. }
  381. [Test]
  382. public void CallAsQueryable () // #537768
  383. {
  384. var constant = Expression.Constant (
  385. new List<string> (),
  386. typeof (IEnumerable<string>));
  387. var call = Expression.Call (
  388. typeof (Queryable),
  389. "AsQueryable",
  390. new [] { typeof (string) },
  391. constant);
  392. Assert.IsNotNull (call);
  393. Assert.AreEqual (1, call.Arguments.Count);
  394. Assert.AreEqual (constant, call.Arguments [0]);
  395. var method = call.Method;
  396. Assert.AreEqual ("AsQueryable", method.Name);
  397. Assert.IsTrue (method.IsGenericMethod);
  398. Assert.AreEqual (typeof (string), method.GetGenericArguments () [0]);
  399. }
  400. [Test]
  401. public void CallQueryableSelect () // #536637
  402. {
  403. var parameter = Expression.Parameter (typeof (string), "s");
  404. var string_length = Expression.Property (parameter, typeof (string).GetProperty ("Length"));
  405. var lambda = Expression.Lambda (string_length, parameter);
  406. var strings = new [] { "1", "22", "333" };
  407. var call = Expression.Call (
  408. typeof (Queryable),
  409. "Select",
  410. new [] { typeof (string), typeof (int) },
  411. Expression.Constant (strings.AsQueryable ()),
  412. lambda);
  413. Assert.IsNotNull (call);
  414. var method = call.Method;
  415. Assert.AreEqual ("Select", method.Name);
  416. Assert.IsTrue (method.IsGenericMethod);
  417. Assert.AreEqual (typeof (string), method.GetGenericArguments () [0]);
  418. Assert.AreEqual (typeof (int), method.GetGenericArguments () [1]);
  419. }
  420. [Test]
  421. public void CallNullableGetValueOrDefault () // #568989
  422. {
  423. var value = Expression.Parameter (typeof (int?), "value");
  424. var default_parameter = Expression.Parameter (typeof (int), "default");
  425. var getter = Expression.Lambda<Func<int?, int, int>> (
  426. Expression.Call (
  427. value,
  428. "GetValueOrDefault",
  429. Type.EmptyTypes,
  430. default_parameter),
  431. value,
  432. default_parameter).Compile ();
  433. Assert.AreEqual (2, getter (null, 2));
  434. Assert.AreEqual (4, getter (4, 2));
  435. }
  436. [Test]
  437. public void CallToStringOnEnum () // #625367
  438. {
  439. var lambda = Expression.Lambda<Func<string>> (
  440. Expression.Call (
  441. Expression.Constant (TypeCode.Boolean, typeof (TypeCode)),
  442. typeof (object).GetMethod ("ToString"))).Compile ();
  443. Assert.AreEqual ("Boolean", lambda ());
  444. }
  445. public static void AcceptsIEnumerable(IEnumerable<object> o)
  446. {
  447. }
  448. [Test]
  449. public void CallIQueryableMethodWithNewArrayBoundExpression () // #2304
  450. {
  451. Expression.Call (
  452. GetType ().GetMethod ("AcceptsIEnumerable", BindingFlags.Public | BindingFlags.Static),
  453. Expression.NewArrayBounds (typeof (object), Expression.Constant (0)));
  454. }
  455. }
  456. }