ExpressionTest.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. [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. Assert.AreEqual (typeof (ExecutionScope), identity.Target.GetType ());
  160. }
  161. class Foo {
  162. public string gazonk;
  163. }
  164. struct Bar {
  165. public int baz;
  166. public override string ToString ()
  167. {
  168. return baz.ToString ();
  169. }
  170. }
  171. [Test]
  172. [Category ("TargetJvmNotSupported")]
  173. public void GlobalsInScope ()
  174. {
  175. var foo = new Foo { gazonk = "gazonk" };
  176. var bar = new Bar { baz = 42 };
  177. var l = Expression.Lambda<Func<string>> (
  178. Expression.Call (
  179. typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }),
  180. Expression.Field (Expression.Constant (foo), typeof (Foo).GetField ("gazonk")),
  181. Expression.Call (Expression.Constant (bar), typeof (Bar).GetMethod ("ToString"))));
  182. var del = l.Compile ();
  183. var scope = del.Target as ExecutionScope;
  184. Assert.IsNotNull (scope);
  185. var globals = scope.Globals;
  186. Assert.IsNotNull (globals);
  187. Assert.AreEqual (2, globals.Length);
  188. Assert.AreEqual (typeof (StrongBox<Foo>), globals [0].GetType ());
  189. Assert.AreEqual (typeof (StrongBox<Bar>), globals [1].GetType ());
  190. Assert.AreEqual (foo, ((StrongBox<Foo>) globals [0]).Value);
  191. Assert.AreEqual (bar, ((StrongBox<Bar>) globals [1]).Value);
  192. Assert.AreEqual ("gazonk42", del ());
  193. }
  194. }
  195. }