ExpressionTest_Lambda.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. //
  19. // Authors:
  20. // Miguel de Icaza ([email protected])
  21. // Jb Evain ([email protected])
  22. //
  23. using System;
  24. using System.Reflection;
  25. using System.Linq;
  26. using System.Linq.Expressions;
  27. using NUnit.Framework;
  28. namespace MonoTests.System.Linq.Expressions
  29. {
  30. [TestFixture]
  31. [Category("SRE")]
  32. public class ExpressionTest_Lambda
  33. {
  34. [Test]
  35. [ExpectedException (typeof (ArgumentException))]
  36. public void NonDelegateTypeInCtor ()
  37. {
  38. // The first parameter must be a delegate type
  39. Expression.Lambda (typeof(string), Expression.Constant (1), new ParameterExpression [0]);
  40. }
  41. delegate object delegate_object_emtpy ();
  42. delegate object delegate_object_int (int a);
  43. delegate object delegate_object_string (string s);
  44. delegate object delegate_object_object (object s);
  45. [Test]
  46. [ExpectedException (typeof (ArgumentException))]
  47. public void InvalidConversion ()
  48. {
  49. // float to object, invalid
  50. Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant (1.0), new ParameterExpression [0]);
  51. }
  52. [Test]
  53. [ExpectedException (typeof (ArgumentException))]
  54. public void InvalidConversion2 ()
  55. {
  56. // float to object, invalid
  57. Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant (1), new ParameterExpression [0]);
  58. }
  59. [Test]
  60. [ExpectedException (typeof (ArgumentException))]
  61. public void InvalidArgCount ()
  62. {
  63. // missing a parameter
  64. Expression.Lambda (typeof (delegate_object_int), Expression.Constant ("foo"), new ParameterExpression [0]);
  65. }
  66. [Test]
  67. [ExpectedException (typeof (ArgumentException))]
  68. public void InvalidArgCount2 ()
  69. {
  70. // extra parameter
  71. ParameterExpression p = Expression.Parameter (typeof (int), "AAA");
  72. Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant ("foo"), new ParameterExpression [1] {p});
  73. }
  74. [Test]
  75. [ExpectedException (typeof (ArgumentException))]
  76. public void InvalidArgType ()
  77. {
  78. // invalid argument type
  79. ParameterExpression p = Expression.Parameter (typeof (string), "AAA");
  80. Expression.Lambda (typeof (delegate_object_int), Expression.Constant ("foo"), new ParameterExpression [1] {p});
  81. }
  82. [Test]
  83. [ExpectedException (typeof (ArgumentException))]
  84. public void InvalidArgType2 ()
  85. {
  86. // invalid argument type
  87. ParameterExpression p = Expression.Parameter (typeof (string), "AAA");
  88. Expression.Lambda (typeof (delegate_object_object), Expression.Constant ("foo"), new ParameterExpression [1] {p});
  89. }
  90. [Test]
  91. [ExpectedException (typeof (ArgumentNullException))]
  92. public void NullParameter ()
  93. {
  94. Expression.Lambda<Func<int, int>> (Expression.Constant (1), new ParameterExpression [] { null });
  95. }
  96. [Test]
  97. public void Assignability ()
  98. {
  99. // allowed: string to object
  100. Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant ("string"), new ParameterExpression [0]);
  101. // allowed delegate has string, delegate has base class (object)
  102. ParameterExpression p = Expression.Parameter (typeof (object), "ParObject");
  103. var l = Expression.Lambda (typeof (delegate_object_string), Expression.Constant (""), new ParameterExpression [1] {p});
  104. Assert.AreEqual ("ParObject => \"\"", l.ToString ());
  105. }
  106. [Test]
  107. [ExpectedException(typeof(InvalidOperationException))]
  108. public void ParameterOutOfScope ()
  109. {
  110. ParameterExpression a = Expression.Parameter(typeof (int), "a");
  111. ParameterExpression second_a = Expression.Parameter(typeof(int), "a");
  112. // Here we have the same name for the parameter expression, but
  113. // we pass a different object to the Lambda expression, so they are
  114. // different, this should throw
  115. Expression<Func<int,int>> l = Expression.Lambda<Func<int,int>>(a, new ParameterExpression [] { second_a });
  116. l.Compile ();
  117. }
  118. [Test]
  119. public void ParameterRefTest ()
  120. {
  121. ParameterExpression a = Expression.Parameter(typeof(int), "a");
  122. ParameterExpression b = Expression.Parameter(typeof(int), "b");
  123. Expression<Func<int,int,int>> l = Expression.Lambda<Func<int,int,int>>(
  124. Expression.Add (a, b), new ParameterExpression [] { a, b });
  125. Assert.AreEqual (typeof (Func<int, int, int>), l.Type);
  126. Assert.AreEqual ("(a, b) => (a + b)", l.ToString ());
  127. Func<int,int,int> xx = l.Compile ();
  128. int res = xx (10, 20);
  129. Assert.AreEqual (res, 30);
  130. }
  131. [Test]
  132. public void Compile ()
  133. {
  134. Expression<Func<int>> l = Expression.Lambda<Func<int>> (Expression.Constant (1), new ParameterExpression [0]);
  135. Assert.AreEqual (typeof (Func<int>), l.Type);
  136. Assert.AreEqual ("() => 1", l.ToString ());
  137. Func<int> fi = l.Compile ();
  138. fi ();
  139. }
  140. [Test]
  141. [ExpectedException(typeof(ArgumentException))]
  142. public void ReturnValueCheck ()
  143. {
  144. ParameterExpression p1 = Expression.Parameter(typeof(int?), "va");
  145. ParameterExpression p2 = Expression.Parameter(typeof(int?), "vb");
  146. Expression add = Expression.Add(p1, p2);
  147. // This should throw, since the add.Type is "int?" and the return
  148. // type we have here is int.
  149. Expression.Lambda<Func<int?,int?,int>> (add, p1, p2);
  150. }
  151. public static void Foo ()
  152. {
  153. }
  154. [Test]
  155. public void LambdaType ()
  156. {
  157. var l = Expression.Lambda (Expression.Constant (1), new [] { Expression.Parameter (typeof (int), "foo") });
  158. Assert.AreEqual (typeof (Func<int, int>), l.Type);
  159. l = Expression.Lambda (Expression.Call (null, GetType ().GetMethod ("Foo")), new [] { Expression.Parameter (typeof (string), "foofoo") });
  160. Assert.AreEqual (typeof (Action<string>), l.Type);
  161. }
  162. [Test]
  163. public void UnTypedLambdaReturnsExpressionOfDelegateType ()
  164. {
  165. var l = Expression.Lambda ("foo".ToConstant ());
  166. Assert.AreEqual (typeof (Func<string>), l.Type);
  167. }
  168. public static int CallDelegate (Func<int, int> e)
  169. {
  170. return e (42);
  171. }
  172. [Test]
  173. public void LambdaPassedAsDelegate ()
  174. {
  175. var pi = Expression.Parameter (typeof (int), "i");
  176. var identity = Expression.Lambda<Func<int, int>> (pi, pi);
  177. var l = Expression.Lambda<Func<int>> (
  178. Expression.Call (
  179. GetType ().GetMethod ("CallDelegate"),
  180. identity)).Compile ();
  181. Assert.AreEqual (42, l ());
  182. }
  183. [Test]
  184. [Category ("NotWorking")]
  185. public void LambdaPassedAsDelegateUsingParentParameter ()
  186. {
  187. var a = Expression.Parameter (typeof (int), "a");
  188. var b = Expression.Parameter (typeof (int), "b");
  189. var l = Expression.Lambda<Func<int, int>> (
  190. Expression.Call (
  191. this.GetType ().GetMethod ("CallDelegate"),
  192. Expression.Lambda<Func<int, int>> (
  193. Expression.Multiply (a, b), b)),
  194. a).Compile ();
  195. Assert.AreEqual (84, l (2));
  196. }
  197. public static int CallFunc (Func<int, int> e, int i)
  198. {
  199. return e (i);
  200. }
  201. [Test]
  202. public void NestedParentParameterUse ()
  203. {
  204. var a = Expression.Parameter (typeof (int), null);
  205. var b = Expression.Parameter (typeof (int), null);
  206. var c = Expression.Parameter (typeof (int), null);
  207. var d = Expression.Parameter (typeof (int), null);
  208. var l = Expression.Lambda<Func<int, int>> (
  209. Expression.Call (
  210. this.GetType ().GetMethod ("CallFunc"),
  211. Expression.Lambda<Func<int, int>> (
  212. Expression.Call (
  213. this.GetType ().GetMethod ("CallFunc"),
  214. Expression.Lambda<Func<int, int>> (
  215. Expression.Call (
  216. this.GetType ().GetMethod ("CallFunc"),
  217. Expression.Lambda<Func<int, int>> (
  218. Expression.Add (c, d),
  219. d),
  220. Expression.Add (b, c)),
  221. c),
  222. Expression.Add (a, b)),
  223. b),
  224. a),
  225. a).Compile ();
  226. Assert.AreEqual (5, l (1));
  227. }
  228. }
  229. }