ExpressionTest_Lambda.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. public class ExpressionTest_Lambda
  32. {
  33. [Test]
  34. [ExpectedException (typeof (ArgumentException))]
  35. public void NonDelegateTypeInCtor ()
  36. {
  37. // The first parameter must be a delegate type
  38. Expression.Lambda (typeof(string), Expression.Constant (1), new ParameterExpression [0]);
  39. }
  40. delegate object delegate_object_emtpy ();
  41. delegate object delegate_object_int (int a);
  42. delegate object delegate_object_string (string s);
  43. delegate object delegate_object_object (object s);
  44. [Test]
  45. [ExpectedException (typeof (ArgumentException))]
  46. public void InvalidConversion ()
  47. {
  48. // float to object, invalid
  49. Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant (1.0), new ParameterExpression [0]);
  50. }
  51. [Test]
  52. [ExpectedException (typeof (ArgumentException))]
  53. public void InvalidConversion2 ()
  54. {
  55. // float to object, invalid
  56. Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant (1), new ParameterExpression [0]);
  57. }
  58. [Test]
  59. [ExpectedException (typeof (ArgumentException))]
  60. public void InvalidArgCount ()
  61. {
  62. // missing a parameter
  63. Expression.Lambda (typeof (delegate_object_int), Expression.Constant ("foo"), new ParameterExpression [0]);
  64. }
  65. [Test]
  66. [ExpectedException (typeof (ArgumentException))]
  67. public void InvalidArgCount2 ()
  68. {
  69. // extra parameter
  70. ParameterExpression p = Expression.Parameter (typeof (int), "AAA");
  71. Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant ("foo"), new ParameterExpression [1] {p});
  72. }
  73. [Test]
  74. [ExpectedException (typeof (ArgumentException))]
  75. public void InvalidArgType ()
  76. {
  77. // invalid argument type
  78. ParameterExpression p = Expression.Parameter (typeof (string), "AAA");
  79. Expression.Lambda (typeof (delegate_object_int), Expression.Constant ("foo"), new ParameterExpression [1] {p});
  80. }
  81. [Test]
  82. [ExpectedException (typeof (ArgumentException))]
  83. public void InvalidArgType2 ()
  84. {
  85. // invalid argument type
  86. ParameterExpression p = Expression.Parameter (typeof (string), "AAA");
  87. Expression.Lambda (typeof (delegate_object_object), Expression.Constant ("foo"), new ParameterExpression [1] {p});
  88. }
  89. [Test]
  90. public void Assignability ()
  91. {
  92. // allowed: string to object
  93. Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant ("string"), new ParameterExpression [0]);
  94. // allowed delegate has string, delegate has base class (object)
  95. ParameterExpression p = Expression.Parameter (typeof (object), "ParObject");
  96. var l = Expression.Lambda (typeof (delegate_object_string), Expression.Constant (""), new ParameterExpression [1] {p});
  97. Assert.AreEqual ("ParObject => \"\"", l.ToString ());
  98. }
  99. [Test]
  100. [ExpectedException(typeof(InvalidOperationException))]
  101. public void ParameterOutOfScope ()
  102. {
  103. ParameterExpression a = Expression.Parameter(typeof (int), "a");
  104. ParameterExpression second_a = Expression.Parameter(typeof(int), "a");
  105. // Here we have the same name for the parameter expression, but
  106. // we pass a different object to the Lambda expression, so they are
  107. // different, this should throw
  108. Expression<Func<int,int>> l = Expression.Lambda<Func<int,int>>(a, new ParameterExpression [] { second_a });
  109. l.Compile ();
  110. }
  111. [Test]
  112. public void ParameterRefTest ()
  113. {
  114. ParameterExpression a = Expression.Parameter(typeof(int), "a");
  115. ParameterExpression b = Expression.Parameter(typeof(int), "b");
  116. Expression<Func<int,int,int>> l = Expression.Lambda<Func<int,int,int>>(
  117. Expression.Add (a, b), new ParameterExpression [] { a, b });
  118. Assert.AreEqual (typeof (Func<int, int, int>), l.Type);
  119. Assert.AreEqual ("(a, b) => (a + b)", l.ToString ());
  120. Func<int,int,int> xx = l.Compile ();
  121. int res = xx (10, 20);
  122. Assert.AreEqual (res, 30);
  123. }
  124. [Test]
  125. public void Compile ()
  126. {
  127. Expression<Func<int>> l = Expression.Lambda<Func<int>> (Expression.Constant (1), new ParameterExpression [0]);
  128. Assert.AreEqual (typeof (Func<int>), l.Type);
  129. Assert.AreEqual ("() => 1", l.ToString ());
  130. Func<int> fi = l.Compile ();
  131. fi ();
  132. }
  133. [Test]
  134. [ExpectedException(typeof(ArgumentException))]
  135. public void ReturnValueCheck ()
  136. {
  137. ParameterExpression p1 = Expression.Parameter(typeof(int?), "va");
  138. ParameterExpression p2 = Expression.Parameter(typeof(int?), "vb");
  139. Expression add = Expression.Add(p1, p2);
  140. // This should throw, since the add.Type is "int?" and the return
  141. // type we have here is int.
  142. Expression.Lambda<Func<int?,int?,int>> (add, p1, p2);
  143. }
  144. public static void Foo ()
  145. {
  146. }
  147. [Test]
  148. public void LambdaType ()
  149. {
  150. var l = Expression.Lambda (Expression.Constant (1), new [] { Expression.Parameter (typeof (int), "foo") });
  151. Assert.AreEqual (typeof (Func<int, int>), l.Type);
  152. l = Expression.Lambda (Expression.Call (null, GetType ().GetMethod ("Foo")), new [] { Expression.Parameter (typeof (string), "foofoo") });
  153. Assert.AreEqual (typeof (Action<string>), l.Type);
  154. }
  155. [Test]
  156. public void UnTypedLambdaReturnsExpressionOfDelegateType ()
  157. {
  158. var l = Expression.Lambda ("foo".ToConstant ());
  159. Assert.AreEqual (typeof (Expression<Func<string>>), l.GetType ());
  160. }
  161. public static int CallDelegate (Func<int, int> e)
  162. {
  163. return e (42);
  164. }
  165. [Test]
  166. public void LambdaPassedAsDelegate ()
  167. {
  168. var pi = Expression.Parameter (typeof (int), "i");
  169. var identity = Expression.Lambda<Func<int, int>> (pi, pi);
  170. var l = Expression.Lambda<Func<int>> (
  171. Expression.Call (
  172. GetType ().GetMethod ("CallDelegate"),
  173. identity)).Compile ();
  174. Assert.AreEqual (42, l ());
  175. }
  176. [Test]
  177. [Category ("NotWorking")]
  178. public void LambdaPassedAsDelegateUsingParentParameter ()
  179. {
  180. var a = Expression.Parameter (typeof (int), "a");
  181. var b = Expression.Parameter (typeof (int), "b");
  182. var l = Expression.Lambda<Func<int, int>> (
  183. Expression.Call (
  184. this.GetType ().GetMethod ("CallDelegate"),
  185. Expression.Lambda<Func<int, int>> (
  186. Expression.Multiply (a, b), b)),
  187. a).Compile ();
  188. Assert.AreEqual (84, l (2));
  189. }
  190. public static int CallFunc (Func<int, int> e, int i)
  191. {
  192. return e (i);
  193. }
  194. [Test]
  195. [Category ("NotWorking")]
  196. public void NestedParentParameterUse ()
  197. {
  198. var a = Expression.Parameter (typeof (int), null);
  199. var b = Expression.Parameter (typeof (int), null);
  200. var c = Expression.Parameter (typeof (int), null);
  201. var d = Expression.Parameter (typeof (int), null);
  202. var l = Expression.Lambda<Func<int, int>> (
  203. Expression.Call (
  204. this.GetType ().GetMethod ("CallFunc"),
  205. Expression.Lambda<Func<int, int>> (
  206. Expression.Call (
  207. this.GetType ().GetMethod ("CallFunc"),
  208. Expression.Lambda<Func<int, int>> (
  209. Expression.Call (
  210. this.GetType ().GetMethod ("CallFunc"),
  211. Expression.Lambda<Func<int, int>> (
  212. Expression.Add (c, d),
  213. d),
  214. Expression.Add (b, c)),
  215. c),
  216. Expression.Add (a, b)),
  217. b),
  218. a),
  219. a).Compile ();
  220. Assert.AreEqual (5, l (1));
  221. }
  222. [Test]
  223. public void LambdaReturningExpression ()
  224. {
  225. var l = Expression.Lambda<Func<Expression>> (Expression.Constant (42));
  226. Assert.AreEqual (ExpressionType.Quote, l.Body.NodeType);
  227. var quoter = l.Compile ();
  228. var q = quoter ();
  229. Assert.AreEqual (ExpressionType.Constant, q.NodeType);
  230. }
  231. }
  232. }