ExpressionTest_Call.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. [Test]
  205. public void CallStaticMethodWithRefParameter ()
  206. {
  207. var p = Expression.Parameter (typeof (int), "i");
  208. var c = Expression.Lambda<Func<int, int>> (
  209. Expression.Call (GetType ().GetMethod ("DoSomethingWith"), p), p).Compile ();
  210. Assert.AreEqual (42, c (38));
  211. }
  212. }
  213. }