PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 298 lines | 214 code | 57 blank | 27 comment | 0 complexity | 86bd301823e46aea4e19b9f9301ea210 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. //
  2. // ExpressionTest.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@novell.com)
  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. [Test]
  45. [ExpectedException (typeof (ArgumentException))]
  46. public void GetFuncTypeArgEmpty ()
  47. {
  48. Expression.GetFuncType (new Type [0]);
  49. }
  50. [Test]
  51. [ExpectedException (typeof (ArgumentException))]
  52. public void GetFuncTypeArgTooBig ()
  53. {
  54. Expression.GetFuncType (new Type [6]);
  55. }
  56. [Test]
  57. public void GetFuncTypeTest ()
  58. {
  59. var func = Expression.GetFuncType (new [] {typeof (int)});
  60. Assert.AreEqual (typeof (Func<int>), func);
  61. func = Expression.GetFuncType (new [] {typeof (int), typeof (int)});
  62. Assert.AreEqual (typeof (Func<int, int>), func);
  63. func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int)});
  64. Assert.AreEqual (typeof (Func<int, int, int>), func);
  65. func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int), typeof (int)});
  66. Assert.AreEqual (typeof (Func<int, int, int, int>), func);
  67. func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int), typeof (int), typeof (int)});
  68. Assert.AreEqual (typeof (Func<int, int, int, int, int>), func);
  69. }
  70. [Test]
  71. [ExpectedException (typeof (ArgumentNullException))]
  72. public void GetActionTypeArgNull ()
  73. {
  74. Expression.GetActionType (null);
  75. }
  76. [Test]
  77. [ExpectedException (typeof (ArgumentException))]
  78. public void GetActionTypeArgTooBig ()
  79. {
  80. Expression.GetActionType (new Type [5]);
  81. }
  82. [Test]
  83. public void GetActionTypeTest ()
  84. {
  85. var action = Expression.GetActionType (new Type [0]);
  86. Assert.AreEqual (typeof (Action), action);
  87. action = Expression.GetActionType (new [] {typeof (int)});
  88. Assert.AreEqual (typeof (Action<int>), action);
  89. action = Expression.GetActionType (new [] {typeof (int), typeof (int)});
  90. Assert.AreEqual (typeof (Action<int, int>), action);
  91. action = Expression.GetActionType (new [] {typeof (int), typeof (int), typeof (int)});
  92. Assert.AreEqual (typeof (Action<int, int, int>), action);
  93. action = Expression.GetActionType (new [] {typeof (int), typeof (int), typeof (int), typeof (int)});
  94. Assert.AreEqual (typeof (Action<int, int, int, int>), action);
  95. }
  96. [Test]
  97. [ExpectedException (typeof (ArgumentNullException))]
  98. public void ParameterNullType ()
  99. {
  100. Expression.Parameter (null, "foo");
  101. }
  102. [Test]
  103. public void ParameterNullName ()
  104. {
  105. var p = Expression.Parameter (typeof (string), null);
  106. Assert.AreEqual (null, p.Name);
  107. Assert.AreEqual (typeof (string), p.Type);
  108. Assert.AreEqual ("<param>", p.ToString ());
  109. }
  110. [Test]
  111. public void ParameterEmptyName ()
  112. {
  113. var p = Expression.Parameter (typeof (string), "");
  114. Assert.AreEqual ("", p.Name);
  115. Assert.AreEqual (typeof (string), p.Type);
  116. Assert.AreEqual ("", p.ToString ());
  117. }
  118. [Test]
  119. public void Parameter ()
  120. {
  121. var p = Expression.Parameter (typeof (string), "foo");
  122. Assert.AreEqual ("foo", p.Name);
  123. Assert.AreEqual (typeof (string), p.Type);
  124. Assert.AreEqual ("foo", p.ToString ());
  125. }
  126. [Test]
  127. [Category ("NotDotNet")]
  128. [ExpectedException (typeof (ArgumentException))]
  129. public void VoidParameter ()
  130. {
  131. Expression.Parameter (typeof (void), "hello");
  132. }
  133. static int buffer;
  134. public static int Identity (int i)
  135. {
  136. buffer = i;
  137. return i;
  138. }
  139. [Test]
  140. public void CompileActionDiscardingRetValue ()
  141. {
  142. var p = Expression.Parameter (typeof (int), "i");
  143. var identity = GetType ().GetMethod ("Identity", BindingFlags.Static | BindingFlags.Public );
  144. Assert.IsNotNull (identity);
  145. var lambda = Expression.Lambda<Action<int>> (Expression.Call (identity, p), p);
  146. var method = lambda.Compile ();
  147. buffer = 0;
  148. method (42);
  149. Assert.AreEqual (42, buffer);
  150. }
  151. [Test]
  152. [Category("TargetJvmNotSupported")]
  153. public void ExpressionDelegateTarget ()
  154. {
  155. var p = Expression.Parameter (typeof (string), "str");
  156. var identity = Expression.Lambda<Func<string, string>> (p, p).Compile ();
  157. Assert.AreEqual (typeof (Func<string, string>), identity.GetType ());
  158. Assert.IsNotNull (identity.Target);
  159. #if !NET_4_0
  160. Assert.AreEqual (typeof (ExecutionScope), identity.Target.GetType ());
  161. #endif
  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. #if !NET_4_0
  174. [Test]
  175. [Category ("TargetJvmNotSupported")]
  176. public void GlobalsInScope ()
  177. {
  178. var foo = new Foo { gazonk = "gazonk" };
  179. var bar = new Bar { baz = 42 };
  180. var l = Expression.Lambda<Func<string>> (
  181. Expression.Call (
  182. typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }),
  183. Expression.Field (Expression.Constant (foo), typeof (Foo).GetField ("gazonk")),
  184. Expression.Call (Expression.Constant (bar), typeof (Bar).GetMethod ("ToString"))));
  185. var del = l.Compile ();
  186. var scope = del.Target as ExecutionScope;
  187. Assert.IsNotNull (scope);
  188. var globals = scope.Globals;
  189. Assert.IsNotNull (globals);
  190. Assert.AreEqual (2, globals.Length);
  191. Assert.AreEqual (typeof (StrongBox<Foo>), globals [0].GetType ());
  192. Assert.AreEqual (typeof (StrongBox<Bar>), globals [1].GetType ());
  193. Assert.AreEqual (foo, ((StrongBox<Foo>) globals [0]).Value);
  194. Assert.AreEqual (bar, ((StrongBox<Bar>) globals [1]).Value);
  195. Assert.AreEqual ("gazonk42", del ());
  196. }
  197. #endif
  198. [Test]
  199. public void SimpleHoistedParameter ()
  200. {
  201. var p = Expression.Parameter (typeof (string), "s");
  202. var f = Expression.Lambda<Func<string, Func<string>>> (
  203. Expression.Lambda<Func<string>> (
  204. p,
  205. new ParameterExpression [0]),
  206. p).Compile ();
  207. var f2 = f ("x");
  208. Assert.AreEqual ("x", f2 ());
  209. }
  210. [Test]
  211. public void TwoHoistingLevels ()
  212. {
  213. var p1 = Expression.Parameter (typeof (string), "x");
  214. var p2 = Expression.Parameter (typeof (string), "y");
  215. Expression<Func<string, Func<string, Func<string>>>> e =
  216. Expression.Lambda<Func<string, Func<string, Func<string>>>> (
  217. Expression.Lambda<Func<string, Func<string>>> (
  218. Expression.Lambda<Func<string>> (
  219. Expression.Call (
  220. typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }),
  221. new [] { p1, p2 }),
  222. new ParameterExpression [0]),
  223. new [] { p2 }),
  224. new [] { p1 });
  225. var f = e.Compile ();
  226. var f2 = f ("Hello ");
  227. var f3 = f2 ("World !");
  228. Assert.AreEqual ("Hello World !", f3 ());
  229. }
  230. [Test]
  231. public void HoistedParameter ()
  232. {
  233. var i = Expression.Parameter (typeof (int), "i");
  234. var l = Expression.Lambda<Func<int, string>> (
  235. Expression.Invoke (
  236. Expression.Lambda<Func<string>> (
  237. Expression.Call (i, typeof (int).GetMethod ("ToString", Type.EmptyTypes)))), i).Compile ();
  238. Assert.AreEqual ("42", l (42));
  239. }
  240. }
  241. }