PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Core/Test/System.Linq.Expressions/ExpressionTest_Lambda.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 285 lines | 202 code | 47 blank | 36 comment | 0 complexity | cb488cc459a76d15e324a27b8fbb3455 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  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 (miguel@novell.com)
  21. // Jb Evain (jbevain@novell.com)
  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. [ExpectedException (typeof (ArgumentNullException))]
  91. public void NullParameter ()
  92. {
  93. Expression.Lambda<Func<int, int>> (Expression.Constant (1), new ParameterExpression [] { null });
  94. }
  95. [Test]
  96. public void Assignability ()
  97. {
  98. // allowed: string to object
  99. Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant ("string"), new ParameterExpression [0]);
  100. // allowed delegate has string, delegate has base class (object)
  101. ParameterExpression p = Expression.Parameter (typeof (object), "ParObject");
  102. var l = Expression.Lambda (typeof (delegate_object_string), Expression.Constant (""), new ParameterExpression [1] {p});
  103. Assert.AreEqual ("ParObject => \"\"", l.ToString ());
  104. }
  105. [Test]
  106. [ExpectedException(typeof(InvalidOperationException))]
  107. public void ParameterOutOfScope ()
  108. {
  109. ParameterExpression a = Expression.Parameter(typeof (int), "a");
  110. ParameterExpression second_a = Expression.Parameter(typeof(int), "a");
  111. // Here we have the same name for the parameter expression, but
  112. // we pass a different object to the Lambda expression, so they are
  113. // different, this should throw
  114. Expression<Func<int,int>> l = Expression.Lambda<Func<int,int>>(a, new ParameterExpression [] { second_a });
  115. l.Compile ();
  116. }
  117. [Test]
  118. public void ParameterRefTest ()
  119. {
  120. ParameterExpression a = Expression.Parameter(typeof(int), "a");
  121. ParameterExpression b = Expression.Parameter(typeof(int), "b");
  122. Expression<Func<int,int,int>> l = Expression.Lambda<Func<int,int,int>>(
  123. Expression.Add (a, b), new ParameterExpression [] { a, b });
  124. Assert.AreEqual (typeof (Func<int, int, int>), l.Type);
  125. Assert.AreEqual ("(a, b) => (a + b)", l.ToString ());
  126. Func<int,int,int> xx = l.Compile ();
  127. int res = xx (10, 20);
  128. Assert.AreEqual (res, 30);
  129. }
  130. [Test]
  131. public void Compile ()
  132. {
  133. Expression<Func<int>> l = Expression.Lambda<Func<int>> (Expression.Constant (1), new ParameterExpression [0]);
  134. Assert.AreEqual (typeof (Func<int>), l.Type);
  135. Assert.AreEqual ("() => 1", l.ToString ());
  136. Func<int> fi = l.Compile ();
  137. fi ();
  138. }
  139. [Test]
  140. [ExpectedException(typeof(ArgumentException))]
  141. public void ReturnValueCheck ()
  142. {
  143. ParameterExpression p1 = Expression.Parameter(typeof(int?), "va");
  144. ParameterExpression p2 = Expression.Parameter(typeof(int?), "vb");
  145. Expression add = Expression.Add(p1, p2);
  146. // This should throw, since the add.Type is "int?" and the return
  147. // type we have here is int.
  148. Expression.Lambda<Func<int?,int?,int>> (add, p1, p2);
  149. }
  150. public static void Foo ()
  151. {
  152. }
  153. [Test]
  154. public void LambdaType ()
  155. {
  156. var l = Expression.Lambda (Expression.Constant (1), new [] { Expression.Parameter (typeof (int), "foo") });
  157. Assert.AreEqual (typeof (Func<int, int>), l.Type);
  158. l = Expression.Lambda (Expression.Call (null, GetType ().GetMethod ("Foo")), new [] { Expression.Parameter (typeof (string), "foofoo") });
  159. Assert.AreEqual (typeof (Action<string>), l.Type);
  160. }
  161. [Test]
  162. public void UnTypedLambdaReturnsExpressionOfDelegateType ()
  163. {
  164. var l = Expression.Lambda ("foo".ToConstant ());
  165. Assert.AreEqual (typeof (Expression<Func<string>>), l.GetType ());
  166. }
  167. public static int CallDelegate (Func<int, int> e)
  168. {
  169. return e (42);
  170. }
  171. [Test]
  172. public void LambdaPassedAsDelegate ()
  173. {
  174. var pi = Expression.Parameter (typeof (int), "i");
  175. var identity = Expression.Lambda<Func<int, int>> (pi, pi);
  176. var l = Expression.Lambda<Func<int>> (
  177. Expression.Call (
  178. GetType ().GetMethod ("CallDelegate"),
  179. identity)).Compile ();
  180. Assert.AreEqual (42, l ());
  181. }
  182. [Test]
  183. [Category ("NotWorking")]
  184. public void LambdaPassedAsDelegateUsingParentParameter ()
  185. {
  186. var a = Expression.Parameter (typeof (int), "a");
  187. var b = Expression.Parameter (typeof (int), "b");
  188. var l = Expression.Lambda<Func<int, int>> (
  189. Expression.Call (
  190. this.GetType ().GetMethod ("CallDelegate"),
  191. Expression.Lambda<Func<int, int>> (
  192. Expression.Multiply (a, b), b)),
  193. a).Compile ();
  194. Assert.AreEqual (84, l (2));
  195. }
  196. public static int CallFunc (Func<int, int> e, int i)
  197. {
  198. return e (i);
  199. }
  200. [Test]
  201. [Category ("NotWorking")]
  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. [Test]
  229. public void LambdaReturningExpression ()
  230. {
  231. var l = Expression.Lambda<Func<Expression>> (Expression.Constant (42));
  232. Assert.AreEqual (ExpressionType.Quote, l.Body.NodeType);
  233. var quoter = l.Compile ();
  234. var q = quoter ();
  235. Assert.AreEqual (ExpressionType.Constant, q.NodeType);
  236. }
  237. }
  238. }