ExpressionTest.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.Linq;
  31. using System.Linq.Expressions;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Linq.Expressions {
  34. [TestFixture]
  35. public class ExpressionTest {
  36. [Test]
  37. [ExpectedException (typeof (ArgumentNullException))]
  38. public void GetFuncTypeArgNull ()
  39. {
  40. Expression.GetFuncType (null);
  41. }
  42. [Test]
  43. [ExpectedException (typeof (ArgumentException))]
  44. public void GetFuncTypeArgEmpty ()
  45. {
  46. Expression.GetFuncType (new Type [0]);
  47. }
  48. [Test]
  49. [ExpectedException (typeof (ArgumentException))]
  50. public void GetFuncTypeArgTooBig ()
  51. {
  52. Expression.GetFuncType (new Type [6]);
  53. }
  54. [Test]
  55. public void GetFuncTypeTest ()
  56. {
  57. var func = Expression.GetFuncType (new [] {typeof (int)});
  58. Assert.AreEqual (typeof (Func<int>), func);
  59. func = Expression.GetFuncType (new [] {typeof (int), typeof (int)});
  60. Assert.AreEqual (typeof (Func<int, int>), func);
  61. func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int)});
  62. Assert.AreEqual (typeof (Func<int, int, int>), func);
  63. func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int), typeof (int)});
  64. Assert.AreEqual (typeof (Func<int, int, int, int>), func);
  65. func = Expression.GetFuncType (new [] {typeof (int), typeof (int), typeof (int), typeof (int), typeof (int)});
  66. Assert.AreEqual (typeof (Func<int, int, int, int, int>), func);
  67. }
  68. [Test]
  69. [ExpectedException (typeof (ArgumentNullException))]
  70. public void GetActionTypeArgNull ()
  71. {
  72. Expression.GetActionType (null);
  73. }
  74. [Test]
  75. [ExpectedException (typeof (ArgumentException))]
  76. public void GetActionTypeArgTooBig ()
  77. {
  78. Expression.GetActionType (new Type [5]);
  79. }
  80. [Test]
  81. public void GetActionTypeTest ()
  82. {
  83. var action = Expression.GetActionType (new Type [0]);
  84. Assert.AreEqual (typeof (Action), action);
  85. action = Expression.GetActionType (new [] {typeof (int)});
  86. Assert.AreEqual (typeof (Action<int>), action);
  87. action = Expression.GetActionType (new [] {typeof (int), typeof (int)});
  88. Assert.AreEqual (typeof (Action<int, int>), action);
  89. action = Expression.GetActionType (new [] {typeof (int), typeof (int), typeof (int)});
  90. Assert.AreEqual (typeof (Action<int, int, int>), action);
  91. action = Expression.GetActionType (new [] {typeof (int), typeof (int), typeof (int), typeof (int)});
  92. Assert.AreEqual (typeof (Action<int, int, int, int>), action);
  93. }
  94. [Test]
  95. [ExpectedException (typeof (ArgumentNullException))]
  96. public void ParameterNullType ()
  97. {
  98. Expression.Parameter (null, "foo");
  99. }
  100. [Test]
  101. public void ParameterNullName ()
  102. {
  103. var p = Expression.Parameter (typeof (string), null);
  104. Assert.AreEqual (null, p.Name);
  105. Assert.AreEqual (typeof (string), p.Type);
  106. Assert.AreEqual ("<param>", p.ToString ());
  107. }
  108. [Test]
  109. public void ParameterEmptyName ()
  110. {
  111. var p = Expression.Parameter (typeof (string), "");
  112. Assert.AreEqual ("", p.Name);
  113. Assert.AreEqual (typeof (string), p.Type);
  114. Assert.AreEqual ("", p.ToString ());
  115. }
  116. [Test]
  117. public void Parameter ()
  118. {
  119. var p = Expression.Parameter (typeof (string), "foo");
  120. Assert.AreEqual ("foo", p.Name);
  121. Assert.AreEqual (typeof (string), p.Type);
  122. Assert.AreEqual ("foo", p.ToString ());
  123. }
  124. }
  125. }