ExpressionTest.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // ExpressionTest.cs
  3. //
  4. // Author:
  5. // Jb Evain ([email protected])
  6. //
  7. // (C) 2008 Novell, Inc. (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using System.Runtime.CompilerServices;
  32. using System.Linq;
  33. using System.Linq.Expressions;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Linq.Expressions {
  36. [TestFixture]
  37. [Category("SRE")]
  38. public class ExpressionTest {
  39. [Test]
  40. [ExpectedException (typeof (ArgumentNullException))]
  41. public void GetFuncTypeArgNull ()
  42. {
  43. Expression.GetFuncType (null);
  44. }
  45. static Type [] GetTestTypeArray (int length)
  46. {
  47. return Enumerable.Range (0, length - 1)
  48. .Select (i => typeof (int))
  49. .ToArray ();
  50. }
  51. [Test]
  52. [ExpectedException (typeof (ArgumentException))]
  53. public void GetFuncTypeArgEmpty ()
  54. {
  55. Expression.GetFuncType (Type.EmptyTypes);
  56. }
  57. [Test]
  58. [ExpectedException (typeof (ArgumentException))]
  59. public void GetFuncTypeArgTooBig ()
  60. {
  61. Expression.GetFuncType (GetTestTypeArray (64));
  62. }
  63. [Test]
  64. public void GetFuncTypeTest ()
  65. {
  66. var func = Expression.GetFuncType (new [] {typeof (int)});
  67. Assert.AreEqual (typeof (Func<int>), func);
  68. func = Expression.GetFuncType (new [] {typeof (int), typeof (int)});
  69. Assert.AreEqual (typeof (Func<int, int>), func);
  70. func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int)});
  71. Assert.AreEqual (typeof (Func<int, int, int>), func);
  72. func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int), typeof (int)});
  73. Assert.AreEqual (typeof (Func<int, int, int, int>), func);
  74. func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int), typeof (int), typeof (int)});
  75. Assert.AreEqual (typeof (Func<int, int, int, int, int>), func);
  76. }
  77. [Test]
  78. [ExpectedException (typeof (ArgumentNullException))]
  79. public void GetActionTypeArgNull ()
  80. {
  81. Expression.GetActionType (null);
  82. }
  83. [Test]
  84. [ExpectedException (typeof (ArgumentException))]
  85. public void GetActionTypeArgTooBig ()
  86. {
  87. Expression.GetActionType (GetTestTypeArray (45));
  88. }
  89. [Test]
  90. public void GetActionTypeTest ()
  91. {
  92. var action = Expression.GetActionType (new Type [0]);
  93. Assert.AreEqual (typeof (Action), action);
  94. action = Expression.GetActionType (new [] {typeof (int)});
  95. Assert.AreEqual (typeof (Action<int>), action);
  96. action = Expression.GetActionType (new [] {typeof (int), typeof (int)});
  97. Assert.AreEqual (typeof (Action<int, int>), action);
  98. action = Expression.GetActionType (new [] {typeof (int), typeof (int), typeof (int)});
  99. Assert.AreEqual (typeof (Action<int, int, int>), action);
  100. action = Expression.GetActionType (new [] {typeof (int), typeof (int), typeof (int), typeof (int)});
  101. Assert.AreEqual (typeof (Action<int, int, int, int>), action);
  102. }
  103. [Test]
  104. [ExpectedException (typeof (ArgumentNullException))]
  105. public void ParameterNullType ()
  106. {
  107. Expression.Parameter (null, "foo");
  108. }
  109. [Test]
  110. public void ParameterNullName ()
  111. {
  112. var p = Expression.Parameter (typeof (string), null);
  113. Assert.AreEqual (null, p.Name);
  114. Assert.AreEqual (typeof (string), p.Type);
  115. }
  116. [Test]
  117. public void ParameterEmptyName ()
  118. {
  119. var p = Expression.Parameter (typeof (string), "");
  120. Assert.AreEqual ("", p.Name);
  121. Assert.AreEqual (typeof (string), p.Type);
  122. }
  123. [Test]
  124. public void Parameter ()
  125. {
  126. var p = Expression.Parameter (typeof (string), "foo");
  127. Assert.AreEqual ("foo", p.Name);
  128. Assert.AreEqual (typeof (string), p.Type);
  129. Assert.AreEqual ("foo", p.ToString ());
  130. }
  131. [Test]
  132. [Category ("NotDotNet")]
  133. [ExpectedException (typeof (ArgumentException))]
  134. public void VoidParameter ()
  135. {
  136. Expression.Parameter (typeof (void), "hello");
  137. }
  138. static int buffer;
  139. public static int Identity (int i)
  140. {
  141. buffer = i;
  142. return i;
  143. }
  144. [Test]
  145. public void CompileActionDiscardingRetValue ()
  146. {
  147. var p = Expression.Parameter (typeof (int), "i");
  148. var identity = GetType ().GetMethod ("Identity", BindingFlags.Static | BindingFlags.Public );
  149. Assert.IsNotNull (identity);
  150. var lambda = Expression.Lambda<Action<int>> (Expression.Call (identity, p), p);
  151. var method = lambda.Compile ();
  152. buffer = 0;
  153. method (42);
  154. Assert.AreEqual (42, buffer);
  155. }
  156. [Test]
  157. public void ExpressionDelegateTarget ()
  158. {
  159. var p = Expression.Parameter (typeof (string), "str");
  160. var identity = Expression.Lambda<Func<string, string>> (p, p).Compile ();
  161. Assert.AreEqual (typeof (Func<string, string>), identity.GetType ());
  162. Assert.IsNotNull (identity.Target);
  163. }
  164. [Test]
  165. public void SimpleHoistedParameter ()
  166. {
  167. var p = Expression.Parameter (typeof (string), "s");
  168. var f = Expression.Lambda<Func<string, Func<string>>> (
  169. Expression.Lambda<Func<string>> (
  170. p,
  171. new ParameterExpression [0]),
  172. p).Compile ();
  173. var f2 = f ("x");
  174. Assert.AreEqual ("x", f2 ());
  175. }
  176. [Test]
  177. public void TwoHoistingLevels ()
  178. {
  179. var p1 = Expression.Parameter (typeof (string), "x");
  180. var p2 = Expression.Parameter (typeof (string), "y");
  181. Expression<Func<string, Func<string, Func<string>>>> e =
  182. Expression.Lambda<Func<string, Func<string, Func<string>>>> (
  183. Expression.Lambda<Func<string, Func<string>>> (
  184. Expression.Lambda<Func<string>> (
  185. Expression.Call (
  186. typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }),
  187. new [] { p1, p2 }),
  188. new ParameterExpression [0]),
  189. new [] { p2 }),
  190. new [] { p1 });
  191. var f = e.Compile ();
  192. var f2 = f ("Hello ");
  193. var f3 = f2 ("World !");
  194. Assert.AreEqual ("Hello World !", f3 ());
  195. }
  196. [Test]
  197. public void HoistedParameter ()
  198. {
  199. var i = Expression.Parameter (typeof (int), "i");
  200. var l = Expression.Lambda<Func<int, string>> (
  201. Expression.Invoke (
  202. Expression.Lambda<Func<string>> (
  203. Expression.Call (i, typeof (int).GetMethod ("ToString", Type.EmptyTypes)))), i).Compile ();
  204. Assert.AreEqual ("42", l (42));
  205. }
  206. }
  207. }