ExpressionTest_Lift.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // ExpressionTest_Lift: this contains tests for the various lifting settings in binary expressions
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. //
  22. // Authors:
  23. // Miguel de Icaza ([email protected])
  24. //
  25. using System;
  26. using System.Reflection;
  27. using System.Linq;
  28. using System.Linq.Expressions;
  29. using NUnit.Framework;
  30. namespace MonoTests.System.Linq.Expressions
  31. {
  32. [TestFixture]
  33. [Category("SRE")]
  34. public class ExpressionTest_Lifting
  35. {
  36. [Test]
  37. public void TestLiftOnEqual ()
  38. {
  39. ConstantExpression a = Expression.Constant (1, typeof (int?));
  40. ConstantExpression b = Expression.Constant (1, typeof (int?));
  41. BinaryExpression cmp = Expression.Equal (a, b);
  42. Assert.AreEqual (true, cmp.IsLifted, "IsLifted");
  43. Assert.AreEqual (false, cmp.IsLiftedToNull, "IsLiftedToNull");
  44. Assert.AreEqual (typeof(bool), cmp.Type, "type");
  45. }
  46. [Test]
  47. public void TestLiftOnEqual_ForcedLifted ()
  48. {
  49. ConstantExpression a = Expression.Constant (1, typeof (int?));
  50. ConstantExpression b = Expression.Constant (1, typeof (int?));
  51. // Force the lift on equal
  52. BinaryExpression cmp = Expression.Equal (a, b, true, null);
  53. Assert.AreEqual (true, cmp.IsLifted, "IsLifted");
  54. Assert.AreEqual (true, cmp.IsLiftedToNull, "IsLiftedToNull");
  55. Assert.AreEqual (typeof(bool?), cmp.Type);
  56. }
  57. static MethodInfo GM(string n)
  58. {
  59. MethodInfo [] m = typeof(ExpressionTest_Lifting).GetMethods(BindingFlags.Static | BindingFlags.Public);
  60. foreach (MethodInfo mm in m)
  61. if (mm.Name == n)
  62. return mm;
  63. throw new Exception("No method found: " + n);
  64. }
  65. static public int MyCompare (OpStruct a, OpStruct b)
  66. {
  67. return 1;
  68. }
  69. [Test]
  70. public void TestLiftOnEqual_WithMethodInfo ()
  71. {
  72. ConstantExpression a = Expression.Constant (new OpStruct (), typeof (OpStruct?));
  73. ConstantExpression b = Expression.Constant (null, typeof (OpStruct?));
  74. // Force the lift on equal
  75. BinaryExpression cmp = Expression.Equal (a, b, true, GM("MyCompare"));
  76. Assert.AreEqual (true, cmp.IsLifted, "IsLifted");
  77. Assert.AreEqual (true, cmp.IsLiftedToNull, "IsLiftedToNull");
  78. BinaryExpression cmp2 = Expression.Equal (a, b, false, GM("MyCompare"));
  79. //
  80. // When we use a MethodInfo, that has a non-bool return type,
  81. // the result is always Nullable<returntype> regardless of the
  82. // setting of "liftToNull"
  83. //
  84. Assert.AreEqual (typeof(int?), cmp.Type);
  85. Assert.AreEqual (typeof(int?), cmp2.Type);
  86. }
  87. }
  88. }