ExpressionTest_Call.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. [Category ("NotWorking")] // need for a better method finder.
  64. public void StaticMethod ()
  65. {
  66. Expression.Call (typeof (MemberClass), "StaticMethod", null, Expression.Constant (1));
  67. }
  68. //[Test]
  69. public void StaticGenericMethod ()
  70. {
  71. MemberClass.StaticGenericMethod(1);
  72. Expression.Call (typeof (MemberClass), "StaticGenericMethod", new Type [1] { typeof (int) }, Expression.Constant (1));
  73. }
  74. [Test]
  75. [ExpectedException (typeof (ArgumentNullException))]
  76. public void ArgMethodNull ()
  77. {
  78. Expression.Call (Expression.Constant (new object ()), null);
  79. }
  80. [Test]
  81. [ExpectedException (typeof (ArgumentNullException))]
  82. public void ArgInstanceNullForNonStaticMethod ()
  83. {
  84. Expression.Call (null, typeof (object).GetMethod ("ToString"));
  85. }
  86. [Test]
  87. [ExpectedException (typeof (ArgumentException))]
  88. public void InstanceTypeDoesntMatchMethodDeclaringType ()
  89. {
  90. Expression.Call (Expression.Constant (1), typeof (string).GetMethod ("Intern"));
  91. }
  92. [Test]
  93. [ExpectedException (typeof (ArgumentException))]
  94. public void MethodArgumentCountDoesnMatchParameterLength ()
  95. {
  96. Expression.Call (Expression.Constant (new object ()), typeof (object).GetMethod ("ToString"), Expression.Constant (new object ()));
  97. }
  98. public class Foo {
  99. public void Bar (string s)
  100. {
  101. }
  102. }
  103. [Test]
  104. [ExpectedException (typeof (ArgumentNullException))]
  105. public void MethodHasNullArgument ()
  106. {
  107. Expression.Call (Expression.New (typeof (Foo)), typeof (Foo).GetMethod ("Bar"), null as Expression);
  108. }
  109. [Test]
  110. [ExpectedException (typeof (ArgumentException))]
  111. public void MethodArgumentDoesntMatchParameterType ()
  112. {
  113. Expression.Call (Expression.New (typeof (Foo)), typeof (Foo).GetMethod ("Bar"), Expression.Constant (42));
  114. }
  115. [Test]
  116. public void CallToString ()
  117. {
  118. var call = Expression.Call (Expression.Constant (new object ()), typeof (object).GetMethod ("ToString"));
  119. Assert.AreEqual ("value(System.Object).ToString()", call.ToString ());
  120. }
  121. [Test]
  122. public void CallStringIsNullOrEmpty ()
  123. {
  124. var call = Expression.Call (null, typeof (string).GetMethod ("IsNullOrEmpty"), Expression.Constant (""));
  125. Assert.AreEqual ("IsNullOrEmpty(\"\")", call.ToString ());
  126. }
  127. public static object Identity (object o)
  128. {
  129. return o;
  130. }
  131. [Test]
  132. public void CompileSimpleStaticCall ()
  133. {
  134. var p = Expression.Parameter (typeof (object), "o");
  135. var lambda = Expression.Lambda<Func<object, object>> (Expression.Call (GetType ().GetMethod ("Identity"), p), p);
  136. var i = lambda.Compile ();
  137. Assert.AreEqual (2, i (2));
  138. Assert.AreEqual ("Foo", i ("Foo"));
  139. }
  140. [Test]
  141. public void CompileSimpleInstanceCall ()
  142. {
  143. var p = Expression.Parameter (typeof (string), "p");
  144. var lambda = Expression.Lambda<Func<string, string>> (
  145. Expression.Call (
  146. p, typeof (string).GetMethod ("ToString", Type.EmptyTypes)),
  147. p);
  148. var ts = lambda.Compile ();
  149. Assert.AreEqual ("foo", ts ("foo"));
  150. Assert.AreEqual ("bar", ts ("bar"));
  151. }
  152. }
  153. }