ExpressionTest_Constant.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. //
  19. // Authors:
  20. // Federico Di Gregorio <[email protected]>
  21. using System;
  22. using System.Linq;
  23. using System.Linq.Expressions;
  24. using NUnit.Framework;
  25. namespace MonoTests.System.Linq.Expressions
  26. {
  27. [TestFixture]
  28. public class ExpressionTest_Constant
  29. {
  30. [Test]
  31. [ExpectedException (typeof (ArgumentException))]
  32. public void Arg2NotNullable ()
  33. {
  34. Expression.Constant(null, typeof(int));
  35. }
  36. [Test]
  37. [ExpectedException (typeof (ArgumentNullException))]
  38. public void Arg2Null ()
  39. {
  40. Expression.Constant (1, null);
  41. }
  42. [Test]
  43. public void NullValue ()
  44. {
  45. ConstantExpression expr = Expression.Constant (null);
  46. Assert.AreEqual (ExpressionType.Constant, expr.NodeType, "Constant#01");
  47. Assert.IsNull (expr.Value, "Constant#02");
  48. Assert.AreEqual (typeof (object), expr.Type, "Constant#03");
  49. Assert.AreEqual ("null", expr.ToString(), "Constant#04");
  50. }
  51. [Test]
  52. public void NullableValue1 ()
  53. {
  54. ConstantExpression expr = Expression.Constant (null, typeof(int?));
  55. Assert.AreEqual (ExpressionType.Constant, expr.NodeType, "Constant#05");
  56. Assert.IsNull (expr.Value, "Constant#06");
  57. Assert.AreEqual (typeof (int?), expr.Type, "Constant#07");
  58. Assert.AreEqual ("null", expr.ToString(), "Constant#08");
  59. }
  60. [Test]
  61. public void NullableValue2 ()
  62. {
  63. ConstantExpression expr = Expression.Constant (1, typeof (int?));
  64. Assert.AreEqual (ExpressionType.Constant, expr.NodeType, "Constant#09");
  65. Assert.AreEqual (1, expr.Value, "Constant#10");
  66. Assert.AreEqual (typeof (int?), expr.Type, "Constant#11");
  67. Assert.AreEqual ("1", expr.ToString(), "Constant#12");
  68. }
  69. [Test]
  70. public void NullableValue3 ()
  71. {
  72. ConstantExpression expr = Expression.Constant ((int?)1);
  73. Assert.AreEqual (ExpressionType.Constant, expr.NodeType, "Constant#13");
  74. Assert.AreEqual (1, expr.Value, "Constant#14");
  75. Assert.AreEqual (typeof (int), expr.Type, "Constant#15");
  76. Assert.AreEqual ("1", expr.ToString(), "Constant#16");
  77. }
  78. [Test]
  79. public void IntegerValue ()
  80. {
  81. ConstantExpression expr = Expression.Constant (0);
  82. Assert.AreEqual (ExpressionType.Constant, expr.NodeType, "Constant#17");
  83. Assert.AreEqual (0, expr.Value, "Constant#18");
  84. Assert.AreEqual (typeof (int), expr.Type, "Constant#19");
  85. Assert.AreEqual ("0", expr.ToString(), "Constant#20");
  86. }
  87. [Test]
  88. public void StringValue ()
  89. {
  90. ConstantExpression expr = Expression.Constant ("a string");
  91. Assert.AreEqual (ExpressionType.Constant, expr.NodeType, "Constant#21");
  92. Assert.AreEqual ("a string", expr.Value, "Constant#22");
  93. Assert.AreEqual (typeof (string), expr.Type, "Constant#23");
  94. Assert.AreEqual ("\"a string\"", expr.ToString(), "Constant#24");
  95. }
  96. [Test]
  97. public void DateTimeValue ()
  98. {
  99. ConstantExpression expr = Expression.Constant (new DateTime(1971, 10, 19));
  100. Assert.AreEqual (ExpressionType.Constant, expr.NodeType, "Constant#25");
  101. Assert.AreEqual (new DateTime(1971, 10, 19), expr.Value, "Constant#26");
  102. Assert.AreEqual (typeof (DateTime), expr.Type, "Constant#27");
  103. Assert.AreEqual (new DateTime(1971, 10, 19).ToString(), expr.ToString(), "Constant#28");
  104. }
  105. [Test]
  106. public void UserClassValue ()
  107. {
  108. OpClass oc = new OpClass ();
  109. ConstantExpression expr = Expression.Constant (oc);
  110. Assert.AreEqual (ExpressionType.Constant, expr.NodeType, "Constant#29");
  111. Assert.AreEqual (oc, expr.Value, "Constant#30");
  112. Assert.AreEqual (typeof (OpClass), expr.Type, "Constant#31");
  113. Assert.AreEqual ("value(MonoTests.System.Linq.Expressions.OpClass)", expr.ToString(), "Constant#32");
  114. }
  115. }
  116. }