ExpressionTest_Lambda.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. using System;
  22. using System.Reflection;
  23. using System.Linq;
  24. using System.Linq.Expressions;
  25. using NUnit.Framework;
  26. namespace MonoTests.System.Linq.Expressions
  27. {
  28. [TestFixture]
  29. public class ExpressionTest_Lambda
  30. {
  31. [Test]
  32. [ExpectedException (typeof (ArgumentException))]
  33. public void NonDelegateTypeInCtor ()
  34. {
  35. // The first parameter must be a delegate type
  36. Expression.Lambda (typeof(string), Expression.Constant (1), new ParameterExpression [0]);
  37. }
  38. delegate object delegate_object_emtpy ();
  39. delegate object delegate_object_int (int a);
  40. delegate object delegate_object_string (string s);
  41. delegate object delegate_object_object (object s);
  42. [Test]
  43. [ExpectedException (typeof (ArgumentException))]
  44. public void InvalidConversion ()
  45. {
  46. // float to object, invalid
  47. Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant (1.0), new ParameterExpression [0]);
  48. }
  49. [Test]
  50. [ExpectedException (typeof (ArgumentException))]
  51. public void InvalidConversion2 ()
  52. {
  53. // float to object, invalid
  54. Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant (1), new ParameterExpression [0]);
  55. }
  56. [Test]
  57. [ExpectedException (typeof (ArgumentException))]
  58. public void InvalidArgCount ()
  59. {
  60. // missing a parameter
  61. Expression.Lambda (typeof (delegate_object_int), Expression.Constant ("foo"), new ParameterExpression [0]);
  62. }
  63. [Test]
  64. [ExpectedException (typeof (ArgumentException))]
  65. public void InvalidArgCount2 ()
  66. {
  67. // extra parameter
  68. ParameterExpression p = Expression.Parameter (typeof (int), "AAA");
  69. Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant ("foo"), new ParameterExpression [1] {p});
  70. }
  71. [Test]
  72. [ExpectedException (typeof (ArgumentException))]
  73. public void InvalidArgType ()
  74. {
  75. // invalid argument type
  76. ParameterExpression p = Expression.Parameter (typeof (string), "AAA");
  77. Expression.Lambda (typeof (delegate_object_int), Expression.Constant ("foo"), new ParameterExpression [1] {p});
  78. }
  79. [Test]
  80. [ExpectedException (typeof (ArgumentException))]
  81. public void InvalidArgType2 ()
  82. {
  83. // invalid argument type
  84. ParameterExpression p = Expression.Parameter (typeof (string), "AAA");
  85. Expression.Lambda (typeof (delegate_object_object), Expression.Constant ("foo"), new ParameterExpression [1] {p});
  86. }
  87. [Test]
  88. public void Assignability ()
  89. {
  90. // allowed: string to object
  91. Expression.Lambda (typeof (delegate_object_emtpy), Expression.Constant ("string"), new ParameterExpression [0]);
  92. // allowed delegate has string, delegate has base class (object)
  93. ParameterExpression p = Expression.Parameter (typeof (object), "ParObject");
  94. var l = Expression.Lambda (typeof (delegate_object_string), Expression.Constant (""), new ParameterExpression [1] {p});
  95. Assert.AreEqual ("ParObject => \"\"", l.ToString ());
  96. }
  97. [Test]
  98. [ExpectedException(typeof(InvalidOperationException))]
  99. public void ParameterOutOfScope ()
  100. {
  101. ParameterExpression a = Expression.Parameter(typeof (int), "a");
  102. ParameterExpression second_a = Expression.Parameter(typeof(int), "a");
  103. // Here we have the same name for the parameter expression, but
  104. // we pass a different object to the Lambda expression, so they are
  105. // different, this should throw
  106. Expression<Func<int,int>> l = Expression.Lambda<Func<int,int>>(a, new ParameterExpression [] { second_a });
  107. l.Compile ();
  108. }
  109. [Test]
  110. public void ParameterRefTest ()
  111. {
  112. ParameterExpression a = Expression.Parameter(typeof(int), "a");
  113. ParameterExpression b = Expression.Parameter(typeof(int), "b");
  114. Expression<Func<int,int,int>> l = Expression.Lambda<Func<int,int,int>>(
  115. Expression.Add (a, b), new ParameterExpression [] { a, b });
  116. Assert.AreEqual (typeof (Func<int, int, int>), l.Type);
  117. Assert.AreEqual ("(a, b) => (a + b)", l.ToString ());
  118. Func<int,int,int> xx = l.Compile ();
  119. int res = xx (10, 20);
  120. Assert.AreEqual (res, 30);
  121. }
  122. [Test]
  123. public void Compile ()
  124. {
  125. Expression<Func<int>> l = Expression.Lambda<Func<int>> (Expression.Constant (1), new ParameterExpression [0]);
  126. Assert.AreEqual (typeof (Func<int>), l.Type);
  127. Assert.AreEqual ("() => 1", l.ToString ());
  128. Func<int> fi = l.Compile ();
  129. fi ();
  130. }
  131. [Test]
  132. [ExpectedException(typeof(ArgumentException))]
  133. public void ReturnValueCheck ()
  134. {
  135. ParameterExpression p1 = Expression.Parameter(typeof(int?), "va");
  136. ParameterExpression p2 = Expression.Parameter(typeof(int?), "vb");
  137. Expression add = Expression.Add(p1, p2);
  138. // This should throw, since the add.Type is "int?" and the return
  139. // type we have here is int.
  140. Expression.Lambda<Func<int?,int?,int>> (add, p1, p2);
  141. }
  142. public static void Foo ()
  143. {
  144. }
  145. [Test]
  146. public void LambdaType ()
  147. {
  148. var l = Expression.Lambda (Expression.Constant (1), new [] { Expression.Parameter (typeof (int), "foo") });
  149. Assert.AreEqual (typeof (Func<int, int>), l.Type);
  150. l = Expression.Lambda (Expression.Call (null, GetType ().GetMethod ("Foo")), new [] { Expression.Parameter (typeof (string), "foofoo") });
  151. Assert.AreEqual (typeof (Action<string>), l.Type);
  152. }
  153. [Test]
  154. public void UnTypedLambdaReturnsExpressionOfDelegateType ()
  155. {
  156. var l = Expression.Lambda ("foo".ToConstant ());
  157. Assert.AreEqual (typeof (Expression<Func<string>>), l.GetType ());
  158. }
  159. public static int CallDelegate (Func<int, int> e)
  160. {
  161. return e (42);
  162. }
  163. [Test]
  164. public void LambdaPassedAsDelegate ()
  165. {
  166. var pi = Expression.Parameter (typeof (int), "i");
  167. var identity = Expression.Lambda<Func<int, int>> (pi, pi);
  168. var l = Expression.Lambda<Func<int>> (
  169. Expression.Call (
  170. GetType ().GetMethod ("CallDelegate"),
  171. identity)).Compile ();
  172. Assert.AreEqual (42, l ());
  173. }
  174. }
  175. }