ExpressionTest.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. static int buffer;
  127. public static int Identity (int i)
  128. {
  129. buffer = i;
  130. return i;
  131. }
  132. [Test]
  133. public void CompileActionDiscardingRetValue ()
  134. {
  135. var p = Expression.Parameter (typeof (int), "i");
  136. var identity = GetType ().GetMethod ("Identity", BindingFlags.Static | BindingFlags.Public );
  137. Assert.IsNotNull (identity);
  138. var lambda = Expression.Lambda<Action<int>> (Expression.Call (identity, p), p);
  139. var method = lambda.Compile ();
  140. buffer = 0;
  141. method (42);
  142. Assert.AreEqual (42, buffer);
  143. }
  144. [Test]
  145. public void ExpressionDelegateTarget ()
  146. {
  147. var p = Expression.Parameter (typeof (string), "str");
  148. var identity = Expression.Lambda<Func<string, string>> (p, p).Compile ();
  149. Assert.AreEqual (typeof (Func<string, string>), identity.GetType ());
  150. Assert.IsNotNull (identity.Target);
  151. Assert.AreEqual (typeof (ExecutionScope), identity.Target.GetType ());
  152. }
  153. class Foo {
  154. public string gazonk;
  155. }
  156. struct Bar {
  157. public int baz;
  158. public override string ToString ()
  159. {
  160. return baz.ToString ();
  161. }
  162. }
  163. [Test]
  164. public void GlobalsInScope ()
  165. {
  166. var foo = new Foo { gazonk = "gazonk" };
  167. var bar = new Bar { baz = 42 };
  168. var l = Expression.Lambda<Func<string>> (
  169. Expression.Call (
  170. typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }),
  171. Expression.Field (Expression.Constant (foo), typeof (Foo).GetField ("gazonk")),
  172. Expression.Call (Expression.Constant (bar), typeof (Bar).GetMethod ("ToString"))));
  173. var del = l.Compile ();
  174. var scope = del.Target as ExecutionScope;
  175. Assert.IsNotNull (scope);
  176. var globals = scope.Globals;
  177. Assert.IsNotNull (globals);
  178. Assert.AreEqual (2, globals.Length);
  179. Assert.AreEqual (typeof (StrongBox<Foo>), globals [0].GetType ());
  180. Assert.AreEqual (typeof (StrongBox<Bar>), globals [1].GetType ());
  181. Assert.AreEqual (foo, ((StrongBox<Foo>) globals [0]).Value);
  182. Assert.AreEqual (bar, ((StrongBox<Bar>) globals [1]).Value);
  183. Assert.AreEqual ("gazonk42", del ());
  184. }
  185. }
  186. }