ExpressionTest_Call.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 (ArgumentNullException))]
  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. Expression.Call (Expression.Constant (1), typeof (string).GetMethod ("Intern"));
  89. }
  90. [Test]
  91. [ExpectedException (typeof (ArgumentException))]
  92. public void MethodArgumentCountDoesnMatchParameterLength ()
  93. {
  94. Expression.Call (Expression.Constant (new object ()), typeof (object).GetMethod ("ToString"), Expression.Constant (new object ()));
  95. }
  96. public class Foo {
  97. public void Bar (string s)
  98. {
  99. }
  100. }
  101. [Test]
  102. [ExpectedException (typeof (ArgumentNullException))]
  103. public void MethodHasNullArgument ()
  104. {
  105. Expression.Call (Expression.New (typeof (Foo)), typeof (Foo).GetMethod ("Bar"), null as Expression);
  106. }
  107. [Test]
  108. [ExpectedException (typeof (ArgumentException))]
  109. public void MethodArgumentDoesntMatchParameterType ()
  110. {
  111. Expression.Call (Expression.New (typeof (Foo)), typeof (Foo).GetMethod ("Bar"), Expression.Constant (42));
  112. }
  113. [Test]
  114. public void CallToString ()
  115. {
  116. var call = Expression.Call (Expression.Constant (new object ()), typeof (object).GetMethod ("ToString"));
  117. Assert.AreEqual ("value(System.Object).ToString()", call.ToString ());
  118. }
  119. [Test]
  120. public void CallStringIsNullOrEmpty ()
  121. {
  122. var call = Expression.Call (null, typeof (string).GetMethod ("IsNullOrEmpty"), Expression.Constant (""));
  123. Assert.AreEqual ("IsNullOrEmpty(\"\")", call.ToString ());
  124. }
  125. public static object Identity (object o)
  126. {
  127. return o;
  128. }
  129. [Test]
  130. public void CompileSimpleStaticCall ()
  131. {
  132. var p = Expression.Parameter (typeof (object), "o");
  133. var lambda = Expression.Lambda<Func<object, object>> (Expression.Call (GetType ().GetMethod ("Identity"), p), p);
  134. var i = lambda.Compile ();
  135. Assert.AreEqual (2, i (2));
  136. Assert.AreEqual ("Foo", i ("Foo"));
  137. }
  138. [Test]
  139. public void CompileSimpleInstanceCall ()
  140. {
  141. var p = Expression.Parameter (typeof (string), "p");
  142. var lambda = Expression.Lambda<Func<string, string>> (
  143. Expression.Call (
  144. p, typeof (string).GetMethod ("ToString", Type.EmptyTypes)),
  145. p);
  146. var ts = lambda.Compile ();
  147. Assert.AreEqual ("foo", ts ("foo"));
  148. Assert.AreEqual ("bar", ts ("bar"));
  149. }
  150. [Test]
  151. [ExpectedException (typeof (InvalidOperationException))]
  152. public void CheckTypeArgsIsNotUsedForParameterLookup ()
  153. {
  154. Expression.Call (GetType (), "EineMethod", new [] { typeof (string), typeof (int) }, "foo".ToConstant (), 2.ToConstant ());
  155. }
  156. public static void EineGenericMethod<X, Y> (string foo, int bar)
  157. {
  158. }
  159. [Test]
  160. public void CheckTypeArgsIsUsedForGenericArguments ()
  161. {
  162. var m = Expression.Call (GetType (), "EineGenericMethod", new [] { typeof (string), typeof (int) }, "foo".ToConstant (), 2.ToConstant ());
  163. Assert.IsNotNull (m.Method);
  164. Assert.AreEqual ("Void EineGenericMethod[String,Int32](System.String, Int32)", m.Method.ToString ());
  165. }
  166. public struct EineStrukt {
  167. public string Foo;
  168. public EineStrukt (string foo)
  169. {
  170. Foo = foo;
  171. }
  172. public string GimmeFoo ()
  173. {
  174. return Foo;
  175. }
  176. }
  177. [Test]
  178. public void CallMethodOnStruct ()
  179. {
  180. var param = Expression.Parameter (typeof (EineStrukt), "s");
  181. var foo = Expression.Lambda<Func<EineStrukt, string>> (
  182. Expression.Call (param, typeof (EineStrukt).GetMethod ("GimmeFoo")), param).Compile ();
  183. var s = new EineStrukt ("foo");
  184. Assert.AreEqual ("foo", foo (s));
  185. }
  186. public static int OneStaticMethod ()
  187. {
  188. return 42;
  189. }
  190. [Test]
  191. public void CallStaticMethodOnNonSenseInstanceExpression ()
  192. {
  193. var call = Expression.Call (
  194. Expression.Constant ("la la la"),
  195. this.GetType ().GetMethod ("OneStaticMethod"));
  196. Assert.IsNotNull (call.Object);
  197. var callMethod = Expression.Lambda<Func<int>> (call).Compile ();
  198. Assert.AreEqual (42, callMethod ());
  199. }
  200. public static int DoSomethingWith (ref int a)
  201. {
  202. return a + 4;
  203. }
  204. public static string DoAnotherThing (ref int a, string s)
  205. {
  206. return s + a;
  207. }
  208. [Test]
  209. public void CallStaticMethodWithRefParameter ()
  210. {
  211. var p = Expression.Parameter (typeof (int), "i");
  212. var c = Expression.Lambda<Func<int, int>> (
  213. Expression.Call (GetType ().GetMethod ("DoSomethingWith"), p), p).Compile ();
  214. Assert.AreEqual (42, c (38));
  215. }
  216. [Test]
  217. public void CallStaticMethodWithRefParameterAndOtherParameter ()
  218. {
  219. var i = Expression.Parameter (typeof (int), "i");
  220. var s = Expression.Parameter (typeof (string), "s");
  221. var lamda = Expression.Lambda<Func<int, string, string>> (
  222. Expression.Call (GetType ().GetMethod ("DoAnotherThing"), i, s), i, s).Compile ();
  223. Assert.AreEqual ("foo42", lamda (42, "foo"));
  224. }
  225. public static int Bang (Expression i)
  226. {
  227. return (int) (i as ConstantExpression).Value;
  228. }
  229. [Test]
  230. public void CallMethodWithExpressionParameter ()
  231. {
  232. var call = Expression.Call (GetType ().GetMethod ("Bang"), Expression.Constant (42));
  233. Assert.AreEqual (ExpressionType.Quote, call.Arguments [0].NodeType);
  234. var l = Expression.Lambda<Func<int>> (call).Compile ();
  235. Assert.AreEqual (42, l ());
  236. }
  237. static bool fout_called = false;
  238. public static int FooOut (out int x)
  239. {
  240. fout_called = true;
  241. return x = 0;
  242. }
  243. [Test]
  244. public void Connect282729 ()
  245. {
  246. // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=282729
  247. var p = Expression.Parameter (typeof (int), "p");
  248. var lambda = Expression.Lambda<Func<int, int>> (
  249. Expression.Call (
  250. GetType ().GetMethod ("FooOut"),
  251. Expression.ArrayIndex(
  252. Expression.NewArrayBounds (
  253. typeof(int),
  254. 1.ToConstant ()),
  255. 0.ToConstant ())),
  256. p).Compile ();
  257. Assert.AreEqual (0, lambda (0));
  258. Assert.IsTrue (fout_called);
  259. }
  260. public static int FooOut2 (out int x)
  261. {
  262. x = 2;
  263. return 3;
  264. }
  265. [Test]
  266. [Category ("NotWorking")]
  267. public void Connect290278 ()
  268. {
  269. // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=290278
  270. var p = Expression.Parameter (typeof (int [,]), "p");
  271. var lambda = Expression.Lambda<Func<int [,], int>> (
  272. Expression.Call (
  273. GetType ().GetMethod ("FooOut2"),
  274. Expression.ArrayIndex (p, 0.ToConstant (), 0.ToConstant ())),
  275. p).Compile ();
  276. int [,] data = { { 1 } };
  277. Assert.AreEqual (3, lambda (data));
  278. Assert.AreEqual (2, data [0, 0]);
  279. }
  280. public static void FooRef (ref string s)
  281. {
  282. }
  283. [Test]
  284. public void Connect297597 ()
  285. {
  286. // test from https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=297597
  287. var strings = new string [1];
  288. var lambda = Expression.Lambda<Action> (
  289. Expression.Call (
  290. GetType ().GetMethod ("FooRef"),
  291. Expression.ArrayIndex (
  292. Expression.Constant (strings), 0.ToConstant ()))).Compile ();
  293. lambda ();
  294. }
  295. [Test]
  296. [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=319190
  297. public void Connect319190 ()
  298. {
  299. var lambda = Expression.Lambda<Func<bool>> (
  300. Expression.TypeIs (
  301. Expression.New (typeof (TypedReference)),
  302. typeof (object))).Compile ();
  303. Assert.IsTrue (lambda ());
  304. }
  305. public static int Truc ()
  306. {
  307. return 42;
  308. }
  309. [Test]
  310. public void Connect282702 ()
  311. {
  312. var lambda = Expression.Lambda<Func<Func<int>>> (
  313. Expression.Convert (
  314. Expression.Call (
  315. typeof (Delegate).GetMethod ("CreateDelegate", new [] { typeof (Type), typeof (object), typeof (MethodInfo) }),
  316. Expression.Constant (typeof (Func<int>), typeof (Type)),
  317. Expression.Constant (null, typeof (object)),
  318. Expression.Constant (GetType ().GetMethod ("Truc"))),
  319. typeof (Func<int>))).Compile ();
  320. Assert.AreEqual (42, lambda ().Invoke ());
  321. }
  322. }
  323. }