ExpressionTest_Call.cs 14 KB

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