ExpressionTest_Call.cs 14 KB

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