ExpressionTest.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.Reflection;
  30. using System.Runtime.CompilerServices;
  31. using System.Linq;
  32. using System.Linq.Expressions;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Linq.Expressions {
  35. [TestFixture]
  36. public class ExpressionTest {
  37. [Test]
  38. [ExpectedException (typeof (ArgumentNullException))]
  39. public void GetFuncTypeArgNull ()
  40. {
  41. Expression.GetFuncType (null);
  42. }
  43. [Test]
  44. [ExpectedException (typeof (ArgumentException))]
  45. public void GetFuncTypeArgEmpty ()
  46. {
  47. Expression.GetFuncType (new Type [0]);
  48. }
  49. [Test]
  50. [ExpectedException (typeof (ArgumentException))]
  51. public void GetFuncTypeArgTooBig ()
  52. {
  53. Expression.GetFuncType (new Type [6]);
  54. }
  55. [Test]
  56. public void GetFuncTypeTest ()
  57. {
  58. var func = Expression.GetFuncType (new [] {typeof (int)});
  59. Assert.AreEqual (typeof (Func<int>), func);
  60. func = Expression.GetFuncType (new [] {typeof (int), typeof (int)});
  61. Assert.AreEqual (typeof (Func<int, int>), func);
  62. func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int)});
  63. Assert.AreEqual (typeof (Func<int, int, int>), func);
  64. func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int), typeof (int)});
  65. Assert.AreEqual (typeof (Func<int, int, int, int>), func);
  66. func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int), typeof (int), typeof (int)});
  67. Assert.AreEqual (typeof (Func<int, int, int, int, int>), func);
  68. }
  69. [Test]
  70. [ExpectedException (typeof (ArgumentNullException))]
  71. public void GetActionTypeArgNull ()
  72. {
  73. Expression.GetActionType (null);
  74. }
  75. [Test]
  76. [ExpectedException (typeof (ArgumentException))]
  77. public void GetActionTypeArgTooBig ()
  78. {
  79. Expression.GetActionType (new Type [5]);
  80. }
  81. [Test]
  82. public void GetActionTypeTest ()
  83. {
  84. var action = Expression.GetActionType (new Type [0]);
  85. Assert.AreEqual (typeof (Action), action);
  86. action = Expression.GetActionType (new [] {typeof (int)});
  87. Assert.AreEqual (typeof (Action<int>), action);
  88. action = Expression.GetActionType (new [] {typeof (int), typeof (int)});
  89. Assert.AreEqual (typeof (Action<int, int>), action);
  90. action = Expression.GetActionType (new [] {typeof (int), typeof (int), typeof (int)});
  91. Assert.AreEqual (typeof (Action<int, int, int>), action);
  92. action = Expression.GetActionType (new [] {typeof (int), typeof (int), typeof (int), typeof (int)});
  93. Assert.AreEqual (typeof (Action<int, int, int, int>), action);
  94. }
  95. [Test]
  96. [ExpectedException (typeof (ArgumentNullException))]
  97. public void ParameterNullType ()
  98. {
  99. Expression.Parameter (null, "foo");
  100. }
  101. [Test]
  102. public void ParameterNullName ()
  103. {
  104. var p = Expression.Parameter (typeof (string), null);
  105. Assert.AreEqual (null, p.Name);
  106. Assert.AreEqual (typeof (string), p.Type);
  107. Assert.AreEqual ("<param>", p.ToString ());
  108. }
  109. [Test]
  110. public void ParameterEmptyName ()
  111. {
  112. var p = Expression.Parameter (typeof (string), "");
  113. Assert.AreEqual ("", p.Name);
  114. Assert.AreEqual (typeof (string), p.Type);
  115. Assert.AreEqual ("", p.ToString ());
  116. }
  117. [Test]
  118. public void Parameter ()
  119. {
  120. var p = Expression.Parameter (typeof (string), "foo");
  121. Assert.AreEqual ("foo", p.Name);
  122. Assert.AreEqual (typeof (string), p.Type);
  123. Assert.AreEqual ("foo", p.ToString ());
  124. }
  125. static int buffer;
  126. public static int Identity (int i)
  127. {
  128. buffer = i;
  129. return i;
  130. }
  131. [Test]
  132. public void CompileActionDiscardingRetValue ()
  133. {
  134. var p = Expression.Parameter (typeof (int), "i");
  135. var identity = GetType ().GetMethod ("Identity", BindingFlags.Static | BindingFlags.Public );
  136. Assert.IsNotNull (identity);
  137. var lambda = Expression.Lambda<Action<int>> (Expression.Call (identity, p), p);
  138. var method = lambda.Compile ();
  139. buffer = 0;
  140. method (42);
  141. Assert.AreEqual (42, buffer);
  142. }
  143. [Test]
  144. [Category ("NotWorking")]
  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. }
  154. }