ExpressionTest_Call.cs 14 KB

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