ExpressionTest_Call.cs 14 KB

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