ExpressionTest.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. public class ExpressionTest {
  38. [Test]
  39. [ExpectedException (typeof (ArgumentNullException))]
  40. public void GetFuncTypeArgNull ()
  41. {
  42. Expression.GetFuncType (null);
  43. }
  44. static Type [] GetTestTypeArray (int length)
  45. {
  46. return Enumerable.Range (0, length - 1)
  47. .Select (i => typeof (int))
  48. .ToArray ();
  49. }
  50. [Test]
  51. [ExpectedException (typeof (ArgumentException))]
  52. public void GetFuncTypeArgEmpty ()
  53. {
  54. Expression.GetFuncType (Type.EmptyTypes);
  55. }
  56. [Test]
  57. [ExpectedException (typeof (ArgumentException))]
  58. public void GetFuncTypeArgTooBig ()
  59. {
  60. Expression.GetFuncType (GetTestTypeArray (64));
  61. }
  62. [Test]
  63. public void GetFuncTypeTest ()
  64. {
  65. var func = Expression.GetFuncType (new [] {typeof (int)});
  66. Assert.AreEqual (typeof (Func<int>), func);
  67. func = Expression.GetFuncType (new [] {typeof (int), typeof (int)});
  68. Assert.AreEqual (typeof (Func<int, int>), func);
  69. func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int)});
  70. Assert.AreEqual (typeof (Func<int, int, int>), func);
  71. func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int), typeof (int)});
  72. Assert.AreEqual (typeof (Func<int, int, int, int>), func);
  73. func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int), typeof (int), typeof (int)});
  74. Assert.AreEqual (typeof (Func<int, int, int, int, int>), func);
  75. }
  76. [Test]
  77. [ExpectedException (typeof (ArgumentNullException))]
  78. public void GetActionTypeArgNull ()
  79. {
  80. Expression.GetActionType (null);
  81. }
  82. [Test]
  83. [ExpectedException (typeof (ArgumentException))]
  84. public void GetActionTypeArgTooBig ()
  85. {
  86. Expression.GetActionType (GetTestTypeArray (45));
  87. }
  88. [Test]
  89. public void GetActionTypeTest ()
  90. {
  91. var action = Expression.GetActionType (new Type [0]);
  92. Assert.AreEqual (typeof (Action), action);
  93. action = Expression.GetActionType (new [] {typeof (int)});
  94. Assert.AreEqual (typeof (Action<int>), action);
  95. action = Expression.GetActionType (new [] {typeof (int), typeof (int)});
  96. Assert.AreEqual (typeof (Action<int, int>), action);
  97. action = Expression.GetActionType (new [] {typeof (int), typeof (int), typeof (int)});
  98. Assert.AreEqual (typeof (Action<int, int, int>), action);
  99. action = Expression.GetActionType (new [] {typeof (int), typeof (int), typeof (int), typeof (int)});
  100. Assert.AreEqual (typeof (Action<int, int, int, int>), action);
  101. }
  102. [Test]
  103. [ExpectedException (typeof (ArgumentNullException))]
  104. public void ParameterNullType ()
  105. {
  106. Expression.Parameter (null, "foo");
  107. }
  108. [Test]
  109. public void ParameterNullName ()
  110. {
  111. var p = Expression.Parameter (typeof (string), null);
  112. Assert.AreEqual (null, p.Name);
  113. Assert.AreEqual (typeof (string), p.Type);
  114. }
  115. [Test]
  116. public void ParameterEmptyName ()
  117. {
  118. var p = Expression.Parameter (typeof (string), "");
  119. Assert.AreEqual ("", p.Name);
  120. Assert.AreEqual (typeof (string), p.Type);
  121. }
  122. [Test]
  123. public void Parameter ()
  124. {
  125. var p = Expression.Parameter (typeof (string), "foo");
  126. Assert.AreEqual ("foo", p.Name);
  127. Assert.AreEqual (typeof (string), p.Type);
  128. Assert.AreEqual ("foo", p.ToString ());
  129. }
  130. [Test]
  131. [Category ("NotDotNet")]
  132. [ExpectedException (typeof (ArgumentException))]
  133. public void VoidParameter ()
  134. {
  135. Expression.Parameter (typeof (void), "hello");
  136. }
  137. static int buffer;
  138. public static int Identity (int i)
  139. {
  140. buffer = i;
  141. return i;
  142. }
  143. [Test]
  144. public void CompileActionDiscardingRetValue ()
  145. {
  146. var p = Expression.Parameter (typeof (int), "i");
  147. var identity = GetType ().GetMethod ("Identity", BindingFlags.Static | BindingFlags.Public );
  148. Assert.IsNotNull (identity);
  149. var lambda = Expression.Lambda<Action<int>> (Expression.Call (identity, p), p);
  150. var method = lambda.Compile ();
  151. buffer = 0;
  152. method (42);
  153. Assert.AreEqual (42, buffer);
  154. }
  155. [Test]
  156. public void ExpressionDelegateTarget ()
  157. {
  158. var p = Expression.Parameter (typeof (string), "str");
  159. var identity = Expression.Lambda<Func<string, string>> (p, p).Compile ();
  160. Assert.AreEqual (typeof (Func<string, string>), identity.GetType ());
  161. Assert.IsNotNull (identity.Target);
  162. }
  163. class Foo {
  164. public string gazonk;
  165. }
  166. struct Bar {
  167. public int baz;
  168. public override string ToString ()
  169. {
  170. return baz.ToString ();
  171. }
  172. }
  173. [Test]
  174. public void SimpleHoistedParameter ()
  175. {
  176. var p = Expression.Parameter (typeof (string), "s");
  177. var f = Expression.Lambda<Func<string, Func<string>>> (
  178. Expression.Lambda<Func<string>> (
  179. p,
  180. new ParameterExpression [0]),
  181. p).Compile ();
  182. var f2 = f ("x");
  183. Assert.AreEqual ("x", f2 ());
  184. }
  185. [Test]
  186. public void TwoHoistingLevels ()
  187. {
  188. var p1 = Expression.Parameter (typeof (string), "x");
  189. var p2 = Expression.Parameter (typeof (string), "y");
  190. Expression<Func<string, Func<string, Func<string>>>> e =
  191. Expression.Lambda<Func<string, Func<string, Func<string>>>> (
  192. Expression.Lambda<Func<string, Func<string>>> (
  193. Expression.Lambda<Func<string>> (
  194. Expression.Call (
  195. typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }),
  196. new [] { p1, p2 }),
  197. new ParameterExpression [0]),
  198. new [] { p2 }),
  199. new [] { p1 });
  200. var f = e.Compile ();
  201. var f2 = f ("Hello ");
  202. var f3 = f2 ("World !");
  203. Assert.AreEqual ("Hello World !", f3 ());
  204. }
  205. [Test]
  206. public void HoistedParameter ()
  207. {
  208. var i = Expression.Parameter (typeof (int), "i");
  209. var l = Expression.Lambda<Func<int, string>> (
  210. Expression.Invoke (
  211. Expression.Lambda<Func<string>> (
  212. Expression.Call (i, typeof (int).GetMethod ("ToString", Type.EmptyTypes)))), i).Compile ();
  213. Assert.AreEqual ("42", l (42));
  214. }
  215. }
  216. }