ExpressionTest_Lift.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. public class ExpressionTest_Lifting
  34. {
  35. [Test]
  36. public void TestLiftOnEqual ()
  37. {
  38. ConstantExpression a = Expression.Constant (1, typeof (int?));
  39. ConstantExpression b = Expression.Constant (1, typeof (int?));
  40. BinaryExpression cmp = Expression.Equal (a, b);
  41. Assert.AreEqual (true, cmp.IsLifted, "IsLifted");
  42. Assert.AreEqual (false, cmp.IsLiftedToNull, "IsLiftedToNull");
  43. Assert.AreEqual (typeof(bool), cmp.Type, "type");
  44. }
  45. [Test]
  46. public void TestLiftOnEqual_ForcedLifted ()
  47. {
  48. ConstantExpression a = Expression.Constant (1, typeof (int?));
  49. ConstantExpression b = Expression.Constant (1, typeof (int?));
  50. // Force the lift on equal
  51. BinaryExpression cmp = Expression.Equal (a, b, true, null);
  52. Assert.AreEqual (true, cmp.IsLifted, "IsLifted");
  53. Assert.AreEqual (true, cmp.IsLiftedToNull, "IsLiftedToNull");
  54. Assert.AreEqual (typeof(bool?), cmp.Type);
  55. }
  56. static MethodInfo GM(string n)
  57. {
  58. MethodInfo [] m = typeof(ExpressionTest_Lifting).GetMethods(BindingFlags.Static | BindingFlags.Public);
  59. foreach (MethodInfo mm in m)
  60. if (mm.Name == n)
  61. return mm;
  62. throw new Exception("No method found: " + n);
  63. }
  64. static public int MyCompare (OpStruct a, OpStruct b)
  65. {
  66. return 1;
  67. }
  68. [Test]
  69. public void TestLiftOnEqual_WithMethodInfo ()
  70. {
  71. ConstantExpression a = Expression.Constant (new OpStruct (), typeof (OpStruct?));
  72. ConstantExpression b = Expression.Constant (null, typeof (OpStruct?));
  73. // Force the lift on equal
  74. BinaryExpression cmp = Expression.Equal (a, b, true, GM("MyCompare"));
  75. Assert.AreEqual (true, cmp.IsLifted, "IsLifted");
  76. Assert.AreEqual (true, cmp.IsLiftedToNull, "IsLiftedToNull");
  77. BinaryExpression cmp2 = Expression.Equal (a, b, false, GM("MyCompare"));
  78. //
  79. // When we use a MethodInfo, that has a non-bool return type,
  80. // the result is always Nullable<returntype> regardless of the
  81. // setting of "liftToNull"
  82. //
  83. Assert.AreEqual (typeof(int?), cmp.Type);
  84. Assert.AreEqual (typeof(int?), cmp2.Type);
  85. }
  86. }
  87. }