ExpressionTest.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #if !NET_4_0
  115. Assert.AreEqual ("<param>", p.ToString ());
  116. #endif
  117. }
  118. [Test]
  119. public void ParameterEmptyName ()
  120. {
  121. var p = Expression.Parameter (typeof (string), "");
  122. Assert.AreEqual ("", p.Name);
  123. Assert.AreEqual (typeof (string), p.Type);
  124. #if !NET_4_0
  125. Assert.AreEqual ("", p.ToString ());
  126. #endif
  127. }
  128. [Test]
  129. public void Parameter ()
  130. {
  131. var p = Expression.Parameter (typeof (string), "foo");
  132. Assert.AreEqual ("foo", p.Name);
  133. Assert.AreEqual (typeof (string), p.Type);
  134. Assert.AreEqual ("foo", p.ToString ());
  135. }
  136. [Test]
  137. [Category ("NotDotNet")]
  138. [ExpectedException (typeof (ArgumentException))]
  139. public void VoidParameter ()
  140. {
  141. Expression.Parameter (typeof (void), "hello");
  142. }
  143. static int buffer;
  144. public static int Identity (int i)
  145. {
  146. buffer = i;
  147. return i;
  148. }
  149. [Test]
  150. public void CompileActionDiscardingRetValue ()
  151. {
  152. var p = Expression.Parameter (typeof (int), "i");
  153. var identity = GetType ().GetMethod ("Identity", BindingFlags.Static | BindingFlags.Public );
  154. Assert.IsNotNull (identity);
  155. var lambda = Expression.Lambda<Action<int>> (Expression.Call (identity, p), p);
  156. var method = lambda.Compile ();
  157. buffer = 0;
  158. method (42);
  159. Assert.AreEqual (42, buffer);
  160. }
  161. [Test]
  162. [Category("TargetJvmNotSupported")]
  163. public void ExpressionDelegateTarget ()
  164. {
  165. var p = Expression.Parameter (typeof (string), "str");
  166. var identity = Expression.Lambda<Func<string, string>> (p, p).Compile ();
  167. Assert.AreEqual (typeof (Func<string, string>), identity.GetType ());
  168. Assert.IsNotNull (identity.Target);
  169. #if !NET_4_0
  170. Assert.AreEqual (typeof (ExecutionScope), identity.Target.GetType ());
  171. #endif
  172. }
  173. class Foo {
  174. public string gazonk;
  175. }
  176. struct Bar {
  177. public int baz;
  178. public override string ToString ()
  179. {
  180. return baz.ToString ();
  181. }
  182. }
  183. #if !NET_4_0
  184. [Test]
  185. [Category ("TargetJvmNotSupported")]
  186. public void GlobalsInScope ()
  187. {
  188. var foo = new Foo { gazonk = "gazonk" };
  189. var bar = new Bar { baz = 42 };
  190. var l = Expression.Lambda<Func<string>> (
  191. Expression.Call (
  192. typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }),
  193. Expression.Field (Expression.Constant (foo), typeof (Foo).GetField ("gazonk")),
  194. Expression.Call (Expression.Constant (bar), typeof (Bar).GetMethod ("ToString"))));
  195. var del = l.Compile ();
  196. var scope = del.Target as ExecutionScope;
  197. Assert.IsNotNull (scope);
  198. var globals = scope.Globals;
  199. Assert.IsNotNull (globals);
  200. Assert.AreEqual (2, globals.Length);
  201. Assert.AreEqual (typeof (StrongBox<Foo>), globals [0].GetType ());
  202. Assert.AreEqual (typeof (StrongBox<Bar>), globals [1].GetType ());
  203. Assert.AreEqual (foo, ((StrongBox<Foo>) globals [0]).Value);
  204. Assert.AreEqual (bar, ((StrongBox<Bar>) globals [1]).Value);
  205. Assert.AreEqual ("gazonk42", del ());
  206. }
  207. #endif
  208. [Test]
  209. public void SimpleHoistedParameter ()
  210. {
  211. var p = Expression.Parameter (typeof (string), "s");
  212. var f = Expression.Lambda<Func<string, Func<string>>> (
  213. Expression.Lambda<Func<string>> (
  214. p,
  215. new ParameterExpression [0]),
  216. p).Compile ();
  217. var f2 = f ("x");
  218. Assert.AreEqual ("x", f2 ());
  219. }
  220. [Test]
  221. public void TwoHoistingLevels ()
  222. {
  223. var p1 = Expression.Parameter (typeof (string), "x");
  224. var p2 = Expression.Parameter (typeof (string), "y");
  225. Expression<Func<string, Func<string, Func<string>>>> e =
  226. Expression.Lambda<Func<string, Func<string, Func<string>>>> (
  227. Expression.Lambda<Func<string, Func<string>>> (
  228. Expression.Lambda<Func<string>> (
  229. Expression.Call (
  230. typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }),
  231. new [] { p1, p2 }),
  232. new ParameterExpression [0]),
  233. new [] { p2 }),
  234. new [] { p1 });
  235. var f = e.Compile ();
  236. var f2 = f ("Hello ");
  237. var f3 = f2 ("World !");
  238. Assert.AreEqual ("Hello World !", f3 ());
  239. }
  240. [Test]
  241. public void HoistedParameter ()
  242. {
  243. var i = Expression.Parameter (typeof (int), "i");
  244. var l = Expression.Lambda<Func<int, string>> (
  245. Expression.Invoke (
  246. Expression.Lambda<Func<string>> (
  247. Expression.Call (i, typeof (int).GetMethod ("ToString", Type.EmptyTypes)))), i).Compile ();
  248. Assert.AreEqual ("42", l (42));
  249. }
  250. }
  251. }