ExpressionTest_Call.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. [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. public void CallMethodOnStruct ()
  197. {
  198. var param = Expression.Parameter (typeof (EineStrukt), "s");
  199. var foo = Expression.Lambda<Func<EineStrukt, string>> (
  200. Expression.Call (param, typeof (EineStrukt).GetMethod ("GimmeFoo")), param).Compile ();
  201. var s = new EineStrukt ("foo");
  202. Assert.AreEqual ("foo", foo (s));
  203. }
  204. public static int OneStaticMethod ()
  205. {
  206. return 42;
  207. }
  208. [Test]
  209. [Category ("NotDotNet")] // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=339351
  210. [ExpectedException (typeof (ArgumentException))]
  211. public void CallStaticMethodOnNonSenseInstanceExpression ()
  212. {
  213. Expression.Call (
  214. Expression.Constant ("la la la"),
  215. this.GetType ().GetMethod ("OneStaticMethod"));
  216. }
  217. public static int DoSomethingWith (ref int a)
  218. {
  219. return a + 4;
  220. }
  221. public static string DoAnotherThing (ref int a, string s)
  222. {
  223. return s + a;
  224. }
  225. [Test]
  226. public void CallStaticMethodWithRefParameter ()
  227. {
  228. var p = Expression.Parameter (typeof (int), "i");
  229. var c = Expression.Lambda<Func<int, int>> (
  230. Expression.Call (GetType ().GetMethod ("DoSomethingWith"), p), p).Compile ();
  231. Assert.AreEqual (42, c (38));
  232. }
  233. [Test]
  234. public void CallStaticMethodWithRefParameterAndOtherParameter ()
  235. {
  236. var i = Expression.Parameter (typeof (int), "i");
  237. var s = Expression.Parameter (typeof (string), "s");
  238. var lamda = Expression.Lambda<Func<int, string, string>> (
  239. Expression.Call (GetType ().GetMethod ("DoAnotherThing"), i, s), i, s).Compile ();
  240. Assert.AreEqual ("foo42", lamda (42, "foo"));
  241. }
  242. public static int Bang (Expression i)
  243. {
  244. return (int) (i as ConstantExpression).Value;
  245. }
  246. #if !NET_4_0 // dlr bug 5875
  247. [Test]
  248. public void CallMethodWithExpressionParameter ()
  249. {
  250. var call = Expression.Call (GetType ().GetMethod ("Bang"), Expression.Constant (42));
  251. Assert.AreEqual (ExpressionType.Quote, call.Arguments [0].NodeType);
  252. var l = Expression.Lambda<Func<int>> (call).Compile ();
  253. Assert.AreEqual (42, l ());
  254. }
  255. #endif
  256. static bool fout_called = false;
  257. public static int FooOut (out int x)
  258. {
  259. fout_called = true;
  260. return x = 0;
  261. }
  262. [Test]
  263. public void Connect282729 ()
  264. {
  265. // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=282729
  266. var p = Expression.Parameter (typeof (int), "p");
  267. var lambda = Expression.Lambda<Func<int, int>> (
  268. Expression.Call (
  269. GetType ().GetMethod ("FooOut"),
  270. Expression.ArrayIndex(
  271. Expression.NewArrayBounds (
  272. typeof(int),
  273. 1.ToConstant ()),
  274. 0.ToConstant ())),
  275. p).Compile ();
  276. Assert.AreEqual (0, lambda (0));
  277. Assert.IsTrue (fout_called);
  278. }
  279. public static int FooOut2 (out int x)
  280. {
  281. x = 2;
  282. return 3;
  283. }
  284. [Test]
  285. [Category ("NotWorking")]
  286. public void Connect290278 ()
  287. {
  288. // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=290278
  289. var p = Expression.Parameter (typeof (int [,]), "p");
  290. var lambda = Expression.Lambda<Func<int [,], int>> (
  291. Expression.Call (
  292. GetType ().GetMethod ("FooOut2"),
  293. Expression.ArrayIndex (p, 0.ToConstant (), 0.ToConstant ())),
  294. p).Compile ();
  295. int [,] data = { { 1 } };
  296. Assert.AreEqual (3, lambda (data));
  297. Assert.AreEqual (2, data [0, 0]);
  298. }
  299. public static void FooRef (ref string s)
  300. {
  301. }
  302. [Test]
  303. public void Connect297597 ()
  304. {
  305. // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=297597
  306. var strings = new string [1];
  307. var lambda = Expression.Lambda<Action> (
  308. Expression.Call (
  309. GetType ().GetMethod ("FooRef"),
  310. Expression.ArrayIndex (
  311. Expression.Constant (strings), 0.ToConstant ()))).Compile ();
  312. lambda ();
  313. }
  314. [Test]
  315. #if MONOTOUCH
  316. [Category ("NotWorking")]
  317. #endif
  318. [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=319190
  319. public void Connect319190 ()
  320. {
  321. var lambda = Expression.Lambda<Func<bool>> (
  322. Expression.TypeIs (
  323. Expression.New (typeof (TypedReference)),
  324. typeof (object))).Compile ();
  325. Assert.IsTrue (lambda ());
  326. }
  327. public static int Truc ()
  328. {
  329. return 42;
  330. }
  331. [Test]
  332. public void Connect282702 ()
  333. {
  334. var lambda = Expression.Lambda<Func<Func<int>>> (
  335. Expression.Convert (
  336. Expression.Call (
  337. typeof (Delegate).GetMethod ("CreateDelegate", new [] { typeof (Type), typeof (object), typeof (MethodInfo) }),
  338. Expression.Constant (typeof (Func<int>), typeof (Type)),
  339. Expression.Constant (null, typeof (object)),
  340. Expression.Constant (GetType ().GetMethod ("Truc"))),
  341. typeof (Func<int>))).Compile ();
  342. Assert.AreEqual (42, lambda ().Invoke ());
  343. }
  344. [Test]
  345. public void CallQueryableWhere ()
  346. {
  347. var queryable = new [] { 1, 2, 3 }.AsQueryable ();
  348. var parameter = Expression.Parameter (typeof (int), "i");
  349. var lambda = Expression.Lambda<Func<int, bool>> (
  350. Expression.LessThan (parameter, Expression.Constant (2)),
  351. parameter);
  352. var selector = Expression.Quote (lambda);
  353. var call = Expression.Call (
  354. typeof (Queryable),
  355. "Where",
  356. new [] { typeof (int) },
  357. queryable.Expression,
  358. selector);
  359. Assert.IsNotNull (call);
  360. Assert.IsNotNull (call.Method);
  361. }
  362. [Test]
  363. public void CallAsQueryable () // #537768
  364. {
  365. var constant = Expression.Constant (
  366. new List<string> (),
  367. typeof (IEnumerable<string>));
  368. var call = Expression.Call (
  369. typeof (Queryable),
  370. "AsQueryable",
  371. new [] { typeof (string) },
  372. constant);
  373. Assert.IsNotNull (call);
  374. Assert.AreEqual (1, call.Arguments.Count);
  375. Assert.AreEqual (constant, call.Arguments [0]);
  376. var method = call.Method;
  377. Assert.AreEqual ("AsQueryable", method.Name);
  378. Assert.IsTrue (method.IsGenericMethod);
  379. Assert.AreEqual (typeof (string), method.GetGenericArguments () [0]);
  380. }
  381. [Test]
  382. public void CallQueryableSelect () // #536637
  383. {
  384. var parameter = Expression.Parameter (typeof (string), "s");
  385. var string_length = Expression.Property (parameter, typeof (string).GetProperty ("Length"));
  386. var lambda = Expression.Lambda (string_length, parameter);
  387. var strings = new [] { "1", "22", "333" };
  388. var call = Expression.Call (
  389. typeof (Queryable),
  390. "Select",
  391. new [] { typeof (string), typeof (int) },
  392. Expression.Constant (strings.AsQueryable ()),
  393. lambda);
  394. Assert.IsNotNull (call);
  395. var method = call.Method;
  396. Assert.AreEqual ("Select", method.Name);
  397. Assert.IsTrue (method.IsGenericMethod);
  398. Assert.AreEqual (typeof (string), method.GetGenericArguments () [0]);
  399. Assert.AreEqual (typeof (int), method.GetGenericArguments () [1]);
  400. }
  401. [Test]
  402. #if MONOTOUCH
  403. [Category ("NotWorking")]
  404. #endif
  405. public void CallNullableGetValueOrDefault () // #568989
  406. {
  407. var value = Expression.Parameter (typeof (int?), "value");
  408. var default_parameter = Expression.Parameter (typeof (int), "default");
  409. var getter = Expression.Lambda<Func<int?, int, int>> (
  410. Expression.Call (
  411. value,
  412. "GetValueOrDefault",
  413. Type.EmptyTypes,
  414. default_parameter),
  415. value,
  416. default_parameter).Compile ();
  417. Assert.AreEqual (2, getter (null, 2));
  418. Assert.AreEqual (4, getter (4, 2));
  419. }
  420. [Test]
  421. public void CallToStringOnEnum () // #625367
  422. {
  423. var lambda = Expression.Lambda<Func<string>> (
  424. Expression.Call (
  425. Expression.Constant (TypeCode.Boolean, typeof (TypeCode)),
  426. typeof (object).GetMethod ("ToString"))).Compile ();
  427. Assert.AreEqual ("Boolean", lambda ());
  428. }
  429. public static void AcceptsIEnumerable(IEnumerable<object> o)
  430. {
  431. }
  432. [Test]
  433. public void CallIQueryableMethodWithNewArrayBoundExpression () // #2304
  434. {
  435. Expression.Call (
  436. GetType ().GetMethod ("AcceptsIEnumerable", BindingFlags.Public | BindingFlags.Static),
  437. Expression.NewArrayBounds (typeof (object), Expression.Constant (0)));
  438. }
  439. }
  440. }